DEV Community

Cover image for Part-25: 🚀Google Cloud – Create, Attach Persistent Disk to VM and Resize Boot and Data Disks in GCP
Latchu@DevOps
Latchu@DevOps

Posted on

Part-25: 🚀Google Cloud – Create, Attach Persistent Disk to VM and Resize Boot and Data Disks in GCP

In this guide, we’ll learn how to create a non-boot persistent disk, attach it to a VM, and prepare it for use. This is a very common use case when you want to store application data separately from your VM’s boot disk.


🔹 Step 01: Introduction

We will perform the following tasks:

  • Create a non-boot persistent disk using Compute Engine → Storage → Disks.
  • Attach this disk to a VM instance.
  • Mount the disk inside the VM.
  • Create some test files and verify the disk.

👉 Before starting, make sure you have your startup script file (webserver-install.sh) uploaded into Cloud Shell.

Here’s the script we’ll use to install Nginx and prepare a demo web page:

#!/bin/bash sudo apt install -y telnet sudo apt install -y nginx sudo systemctl enable nginx sudo chmod -R 755 /var/www/html HOSTNAME=$(hostname) sudo echo "<!DOCTYPE html> <html> <body style='background-color:rgb(250, 210, 210);'> <h1>Welcome to Latchu@DevOps - WebVM App1 </h1> <p><strong>VM Hostname:</strong> $HOSTNAME</p> <p><strong>VM IP Address:</strong> $(hostname -I)</p> <p><strong>Application Version:</strong> V1</p> <p>Google Cloud Platform - Demos</p> </body></html>" | sudo tee /var/www/html/index.html 
Enter fullscreen mode Exit fullscreen mode

🔹 Step 02: Create a New VM Instance

Now let’s create a VM instance that will act as our application server.

gcloud compute instances create demo7-vm \ --zone=us-central1-a \ --machine-type=e2-micro \ --network-interface=subnet=default \ --tags=http-server \ --metadata-from-file=startup-script=webserver-install.sh # List all compute instances gcloud compute instances list 
Enter fullscreen mode Exit fullscreen mode

pd1

This will provision a new VM (demo7-vm) with our webserver startup script pre-installed.

pd2


🔹 Step 03: Create a Persistent Disk (Non-Boot Disk)

You can create a disk from the Google Cloud Console:

  • Go to: Compute Engine → Storage → Disks → Create Disk
  • Name: mydisk1
  • Description: mydisk1
  • Location: Single Zone
  • Region: us-central1
  • Zone: us-central1-a
  • Disk type: Balanced Persistent Disk (pd-balanced)
  • Size: 15GB
  • Encryption: Use default Google-managed encryption OR your own CMEK (KMS Key)
  • Add Label: environment=dev
  • Click Create

pd3

pd4

pd5

Now disk is available to attach to the VM

pd6

👉 Alternatively, you can also create the disk using gcloud CLI:

# Create a Blank Disk gcloud compute disks create mydisk1 \ --project=gcplearn9 \ --type=pd-balanced \ --description=mydisk1 \ --size=15GB \ --zone=us-central1-a 
Enter fullscreen mode Exit fullscreen mode

If you want to use a Customer Managed Encryption Key (CMEK) from Cloud KMS:

gcloud compute disks create mydisk1 \ --project=gcplearn9 \ --type=pd-balanced \ --description=mydisk1 \ --size=15GB \ --zone=us-central1-a \ --kms-key=projects/gcp-zero-to-hero-468909/locations/us-central1/keyRings/my-keyring1/cryptoKeys/my-symkey-1/cryptoKeyVersions/1 
Enter fullscreen mode Exit fullscreen mode

🔹 Step 04: Attach the Disk to the VM

Once the disk is created, let’s attach it to our VM.

Using Google Cloud Console:

  • Navigate to Compute Engine → VM Instances → demo7-vm → Edit.
  • Scroll down to Additional Disks → Click Attach existing disk.
  • Select mydisk1.
  • Mode: Read/Write
  • Deletion Rule: Keep disk
  • Device Name: Auto-filled based on disk name.
  • Click Save.

Now, the disk is attached to your VM but not yet mounted.

pd7


🔹 Step 05: List and Mount Disks on the VM

Now that we have attached the persistent disk to our VM, let’s verify and mount it.

SSH into the VM:

# Connect to VM instance using Cloud Shell gcloud compute ssh --zone "us-central1-a" "demo7-vm" --project "gcpdemos" 
Enter fullscreen mode Exit fullscreen mode

List attached disks:

ls -l /dev/disk/by-id/google-* 
Enter fullscreen mode Exit fullscreen mode

👉 You’ll notice a new device, for example /dev/sdb. This is our blank persistent disk.

Sample Output:

devops_samira@demo7-vm:~$ ls -lh /dev/disk/by-id/google-* lrwxrwxrwx 1 root root 9 Aug 26 08:44 /dev/disk/by-id/google-mydisk1 -> ../../sda lrwxrwxrwx 1 root root 9 Aug 26 08:44 /dev/disk/by-id/google-persistent-disk-0 -> ../../sdb lrwxrwxrwx 1 root root 10 Aug 26 08:44 /dev/disk/by-id/google-persistent-disk-0-part1 -> ../../sdb1 lrwxrwxrwx 1 root root 11 Aug 26 08:44 /dev/disk/by-id/google-persistent-disk-0-part14 -> ../../sdb14 lrwxrwxrwx 1 root root 11 Aug 26 08:44 /dev/disk/by-id/google-persistent-disk-0-part15 -> ../../sdb15 
Enter fullscreen mode Exit fullscreen mode

Format the disk:

sudo mkfs.ext4 -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/sda 
Enter fullscreen mode Exit fullscreen mode

Create a mount directory and mount the disk:

sudo mkdir -p /mnt/disks/myapp1 sudo mount -o discard,defaults /dev/sda /mnt/disks/myapp1 
Enter fullscreen mode Exit fullscreen mode

Set permissions:

sudo chmod a+w /mnt/disks/myapp1 
Enter fullscreen mode Exit fullscreen mode

Verify the mount point:

df -h 
Enter fullscreen mode Exit fullscreen mode

Sample Output:

devops_samira@demo7-vm:~$ df -h Filesystem Size Used Avail Use% Mounted on udev 474M 0 474M 0% /dev tmpfs 97M 460K 97M 1% /run /dev/sdb1 9.7G 2.2G 7.0G 24% / tmpfs 485M 0 485M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock /dev/sdb15 124M 12M 113M 10% /boot/efi tmpfs 97M 0 97M 0% /run/user/1000 /dev/sda 15G 24K 15G 1% /mnt/disks/myapp1 
Enter fullscreen mode Exit fullscreen mode

Test by creating a file:

echo "New disk attached" >> /mnt/disks/myapp1/newdisk.txt cat /mnt/disks/myapp1/newdisk.txt 
Enter fullscreen mode Exit fullscreen mode

🔹 Step 06: Configure Automatic Mounting on VM Restart

By default, the new disk won’t automatically mount after a reboot. To fix this, we’ll configure /etc/fstab.

Backup fstab:

sudo cp /etc/fstab /etc/fstab.backup 
Enter fullscreen mode Exit fullscreen mode

Get the UUID of the new disk:

sudo blkid /dev/sdb 
Enter fullscreen mode Exit fullscreen mode

Sample Output:

devops_samira@demo7-vm:~$ sudo blkid /dev/sdb /dev/sdb: PTUUID="eb1122e4-9c88-1e40-a53a-a48aad3822fa" PTTYPE="gpt" 
Enter fullscreen mode Exit fullscreen mode

Update /etc/fstab with the UUID:

echo UUID=eb1122e4-9c88-1e40-a53a-a48aad3822fa /mnt/disks/myapp1 ext4 discard,defaults,nofail 0 2 | sudo tee -a /etc/fstab 
Enter fullscreen mode Exit fullscreen mode

Verify the fstab entry:

cat /etc/fstab 
Enter fullscreen mode Exit fullscreen mode

Run mount check:

sudo mount -a sudo systemctl daemon-reload 
Enter fullscreen mode Exit fullscreen mode

If there are no errors, the disk will now auto-mount after every reboot.


🔹 Step 07: Stop and Start VM to Verify Persistence

Let’s test our setup by stopping and starting the VM.

# Stop VM gcloud compute instances stop demo7-vm --zone=us-central1-a # Start VM gcloud compute instances start demo7-vm --zone=us-central1-a 
Enter fullscreen mode Exit fullscreen mode

Now SSH back into the VM and verify:

gcloud compute ssh --zone "us-central1-a" "demo7-vm" --project "gcpdemos" # Check disk mount df -h # Verify file persists cat /mnt/disks/myapp1/newdisk.txt # Create new file to confirm echo "my new file 101" >> /mnt/disks/myapp1/mynewfile.txt ls /mnt/disks/myapp1/ cat /mnt/disks/myapp1/mynewfile.txt 
Enter fullscreen mode Exit fullscreen mode

✅ If everything looks good, your persistent disk is successfully attached and auto-mounted even after VM restarts.


🔹 Resizing Boot & Non-Boot Disks in Google Compute Engine

In this section, we’ll learn how to resize both boot and non-boot persistent disks attached to a VM instance in GCP.

👉 Best Practice: Always take snapshots of your disks before resizing in production. Snapshots can be used to restore your VM or data if anything goes wrong. In this demo, we’ll skip snapshots for simplicity.


🔹 Step 01: Introduction

Boot Disk Resize

  • If you are using GCP-provided public images (like Debian, Ubuntu, CentOS), the root partition and file system will auto-resize after increasing the boot disk size and restarting the VM.
  • If using custom images, you may need to manually resize the root partition.

Non-Boot Disk Resize

  • After resizing the non-boot disk, you must extend the file system manually inside the VM.

🔹 Step 02: Stop the VM

Before resizing, stop the VM.

# Stop VM Instance gcloud compute instances stop demo7-vm --zone=us-central1-a # Verify status gcloud compute instances list --filter='name:demo7-vm' 
Enter fullscreen mode Exit fullscreen mode

🔹 Step 03: Resize Boot Disk

  • Navigate to Compute Engine → Storage → Disks → demo7-vm → Edit
  • Current size: 10GB
  • New size: 20GB
  • Click Save

rd-1


🔹 Step 04: Resize Non-Boot Disk

  • Navigate to Compute Engine → Storage → Disks → mydisk1 → Edit
  • Current size: 15GB
  • New size: 25GB
  • Click Save

rd-2


🔹 Step 05: Start VM and Verify Root Disk

# Start VM Instance gcloud compute instances start demo7-vm --zone=us-central1-a # Connect to VM gcloud compute ssh --zone "us-central1-a" "demo7-vm" --project "gcpdemos" # Verify Root Disk Size sudo df -Th 
Enter fullscreen mode Exit fullscreen mode

Sample Output:

devops_samira@demo7-vm:~$ df -h Filesystem Size Used Avail Use% Mounted on udev 474M 0 474M 0% /dev tmpfs 97M 464K 97M 1% /run /dev/sda1 20G 2.2G 17G 12% / tmpfs 485M 0 485M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock /dev/sda15 124M 12M 113M 10% /boot/efi /dev/sdb 15G 32K 15G 1% /mnt/disks/myapp1 tmpfs 97M 0 97M 0% /run/user/1000 
Enter fullscreen mode Exit fullscreen mode

👉 As expected, the boot disk auto-resized to 20GB because Debian supports it. Non-boot disk still shows 15GB.


🔹 Step 06: Resize Non-Boot Disk Inside VM

Check attached devices

sudo lsblk 
Enter fullscreen mode Exit fullscreen mode

Sample Output:

devops_samira@demo7-vm:~$ sudo lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8:0 0 20G 0 disk ├─sda1 8:1 0 19.9G 0 part / ├─sda14 8:14 0 3M 0 part └─sda15 8:15 0 124M 0 part /boot/efi sdb 8:16 0 25G 0 disk /mnt/disks/myapp1 
Enter fullscreen mode Exit fullscreen mode

Extend the filesystem

sudo resize2fs /dev/sdb 
Enter fullscreen mode Exit fullscreen mode

Sample Output:

devops_samira@demo7-vm:~$ sudo resize2fs /dev/sdb resize2fs 1.47.0 (5-Feb-2023) Filesystem at /dev/sdb is mounted on /mnt/disks/myapp1; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 4 The filesystem on /dev/sdb is now 6553600 (4k) blocks long. 
Enter fullscreen mode Exit fullscreen mode

Verify new size

sudo df -Th 
Enter fullscreen mode Exit fullscreen mode

Sample Output:

devops_samira@demo7-vm:~$ sudo df -Th Filesystem Type Size Used Avail Use% Mounted on udev devtmpfs 474M 0 474M 0% /dev tmpfs tmpfs 97M 464K 97M 1% /run /dev/sda1 ext4 20G 2.2G 17G 12% / tmpfs tmpfs 485M 0 485M 0% /dev/shm tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock /dev/sda15 vfat 124M 12M 113M 10% /boot/efi /dev/sdb ext4 25G 32K 25G 1% /mnt/disks/myapp1 tmpfs tmpfs 97M 0 97M 0% /run/user/1000 
Enter fullscreen mode Exit fullscreen mode

✅ Now both boot disk (20GB) and non-boot disk (25GB) are resized successfully.


🔹 Step 07: Cleanup (Stop/Delete VM and Disk)

To avoid unnecessary charges in your demo environment:

rd

# Stop VM gcloud compute instances stop demo7-vm --zone=us-central1-a # Delete VM gcloud compute instances delete demo7-vm --zone=us-central1-a # Delete Disk gcloud compute disks delete mydisk1 --zone=us-central1-a 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)