Compute Engine Google Cloud

Attach and Mount Disks to VM Instance in Google Cloud

Attach Disks to VM Instance in Google Cloud Platform. This guide, demonstrates how to attach new additional disks to your VM Instance and mount them.

You can attach a blank disk or an image disk. If you create a blank or empty disk your new zonal persistent disks start with no data or file systems. You must format those disks yourself and mount them after you attach them to your instances.

You can check your instance before adding the new disk with this command. Connect your instance by clicking the SSH button and execute the following command.

You can also resize the disk without any downtime.

Choose Best Hosting for your Business

PlatformReviewsPricing
Siteground★★★★★$3.95
Kinsta – Google Cloud★★★★★$30

Check Partitions

 sudo lsblk

You will get an output similar to the one below. This output is for 10Gb disk created by default.

Output
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 91.1M 1 loop /snap/core/6531
loop1 7:1 0 56.7M 1 loop /snap/google-cloud-sdk/75
sda 8:0 0 10G 0 disk
├─sda1 8:1 0 9.9G 0 part /
├─sda14 8:14 0 4M 0 part
└─sda15 8:15 0 106M 0 part /boot/efi

Attach New Disk

Go to your Google Cloud Console and navigate to Compute >> VM Instances.

Click the name of the instance where you want to add a disk.

Click Edit, at the top of the instance details page.

Scroll down to Additional disks and click Add item.

In the Name drop-down menu, click Create disk.

Configure the disk’s properties and click Create.

Scroll down to the bottom of the page and click Save to apply your changes to the instance and attach the new disk.

Create new disk

Format the new disk

Click the SSH button next to the instance that has the new attached disk.

A new terminal window will be opened and a connection to the instance will be established.

Now, if the run the command again you can see the attached disk.

 sudo lsblk
Output
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 91.1M 1 loop /snap/core/6531
loop1 7:1 0 56.7M 1 loop /snap/google-cloud-sdk/75
sda 8:0 0 10G 0 disk
├─sda1 8:1 0 9.9G 0 part /
├─sda14 8:14 0 4M 0 part
└─sda15 8:15 0 106M 0 part /boot/efi
sdb 8:16 0 10G 0 disk

Here you can see, sdb is the DEVICE_ID for the new persistent disk.

Execute the following command to format the disk.

sudo mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/DEVICE_ID

Replace DEVICE_ID with your device ID.

Mount the new disk

Once the format is complete, you can mount the disk to an existing directory or to a new directory.

Now, you can create new directory and mount the disk in the directory.

sudo mkdir -p /folder/directory

Now mount the disk to the directory.

sudo mount -o discard,defaults /dev/sdb /folder/directory

Configure read and write permissions.

sudo chmod a+w /folder/directory

Verify the Mounted Disk

Now, you can virify the mounted disk by running the lsblk command again.

 sudo lsblk
Output
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 91.1M 1 loop /snap/core/6531
loop1 7:1 0 56.7M 1 loop /snap/google-cloud-sdk/75
sda 8:0 0 10G 0 disk
├─sda1 8:1 0 9.9G 0 part /
├─sda14 8:14 0 4M 0 part
└─sda15 8:15 0 106M 0 part /boot/efi
sdb 8:16 0 10G 0 disk /folder/directory

Now you have successfully formated and mounted the disk on a custom directory.

Set Up Auto Mount on Restart

With the current setting your disk cannot mount on it’s own if the instance get’s restarted.

So, you need to add the persistent disk to the /etc/fstab file so that the device automatically mounts again when the instance restarts.

Create a backup of your current /etc/fstab file.

sudo cp /etc/fstab /etc/fstab.backup

A UUID is generated for your disk when you format the disk. This UUID does not change when you move the disks to instances. So, you can use the UUID to mount the disk. To get the UUID you can use the blkid command.

sudo blkid /dev/DEVICE_ID
Output
/dev/sdb: UUID="f8a5c95c-f0f4-48d9-afab-d94dd8d57f7e" TYPE="ext4"

Create an entry in /etc/fstab to mount the /dev/sdb persistent disk at /folder/directory using its UUID.

echo UUID=`sudo blkid -s UUID -o value /dev/sdb` /folder/directory ext4 discard,defaults,nofail 0 2 | sudo tee -a /etc/fstab

This command will create an entry in the fstab file.

To verify the changes run the following command.

cat /etc/fstab
Output
LABEL=cloudimg-rootfs / ext4 defaults 0 0
LABEL=UEFI /boot/efi vfat defaults 0 0
UUID=f8a5c95c-f0f4-48d9-afab-d94dd8d57f7e /folder/directory ext4 discard,defaults,nofail 0 2

Get your Professional Google Cloud Architect certificate with this easy to learn course now.

Conclusion

Now you have learned how to attach and mount additional disks to your VM instance on Google Cloud Platform.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

4 Comments

  1. Oluwadamilola Reply

    I have a problem with my vm instance on gcloud.

    I saw that it has 100% use for /dev/sda1 which is 10GB
    I have invreased the disk size to 20Gb, yet my instance is not accessible, please help.

  2. How do i copy all my files from the newly added disk to my instance? that is to say i don’t want the old disk again

  3. Kieran M Sweeney Reply

    Where / why is ‘/dev’ introduced? Is that the name of the ‘new-disk’ created?

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

If you find this helpful? Support me!
Buy me a coffee Donation Please buy me a coffee