Friday, October 29, 2021

[SOLVED] Programmatically Create+Mount Disk From Within Google Compute VM

Issue

I'd like to write a script that can be run from a Google Compute instance, which creates a disk and mounts it. The disks I've created and mounted so far have been done through the web console. The problem I'm having is in figuring out the paramaters for safe_format_and_mount (and possibly in some step before).

From within the instance, here is my attempt so far:

ami@snowflake:~$ gcloud compute disks create foo --zone europe-west1-c
Created [https://www.googleapis.com/compute/v1/projects/snowflake-    1056/zones/europe-west1-c/disks/foo].
NAME ZONE           SIZE_GB TYPE        STATUS
foo  europe-west1-c 500     pd-standard READY
ami@snowflake:~$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0    10G  0 disk 
`-sda1   8:1    0    10G  0 part /
sdb      8:16   0   500G  0 disk /mnt/work
sdc      8:32   0     2T  0 disk /mnt/data1

The docs for safe_format_and_mount give now this sequence:

$ sudo mkdir MOUNT_POINT
$ sudo /usr/share/google/safe_format_and_mount -m "mkfs.ext4 -F" DISK_LOCATION MOUNT_POINT

However, I have no idea what DISK_LOCATION is, nor does lsblock's output give anything that seems pertinent.


Solution

Don't forget that you need to attach the disk to your instance before you can use it:

gcloud compute instances attach-disk myinstance --disk foo \
    --zone europe-west1-c --device-name foo

The --device-name option allows you to specify the device name the guest operating system will see. If you use the same name as the disk name, the disk location will be /dev/disk/by-id/google-foo.



Answered By - Andreas Veithen