Compute Engine — identifying your disks
I stymied myself and confused my colleagues when I explained the troubles I was having reconciling Compute Engine Persistent Disk (“disk”) names to Linux devices.
The solution is trivial but the behavior is not what I expected.
Here’s my recommendation: when you use gcloud compute instances attach-disk, or you call the attachDisk method on the Compute API directly or via one of the SDKs, I recommend you specify the device-name too and set it to the disk’s name, e.g:
DISK=mydiskgcloud compute instances attach-disk ${INSTANCE} \
--disk=${DISK} \
--device-name=${DISK} \
--project=${PROJECT} \
--zone=${ZONE}
If you specify the device name, once you’re on the VM, you will be readily able to identify the Linux device that’s mapped to the disk. See how “mydisk” is reflected in the device names below:
ls -l /dev/disk/by-idgoogle-mydisk -> ../../sdb
google-anotherdisk1 -> ../../sdc
google-anotherdisk2 -> ../../sdd
google-instance-1 -> ../../sda
google-instance-1-part1 -> ../../sda1
scsi-0Google_PersistentDisk_mydisk -> ../../sdb
scsi-0Google_PersistentDisk_anotherdisk1 -> ../../sdc
scsi-0Google_PersistentDisk_anotherdisk2 -> ../../sdd
scsi-0Google_PersistentDisk_instance-1 -> ../../sda
scsi-0Google_PersistentDisk_instance-1-part1 -> ../../sda1
If you do *not* specify the device name when you attach the disk to the instance, the disks will be numbered incrementally and it’s not obvious which device now corresponds to ‘mydisk’ particularly if you attach multiple disks:
ls -l /dev/disk/by-idgoogle-persistent-disk-1 -> ../../sdb
google-persistent-disk-2 -> ../../sdc
google-persistent-disk-3 -> ../../sdd
google-instance-1 -> ../../sda
google-instance-1-part1 -> ../../sda1
scsi-0Google_PersistentDisk_persistent-disk-1 -> ../../sdb
scsi-0Google_PersistentDisk_persistent-disk-2 -> ../../sdc
scsi-0Google_PersistentDisk_persistent-disk-3 -> ../../sdd
scsi-0Google_PersistentDisk_instance-1 -> ../../sda
scsi-0Google_PersistentDisk_instance-1-part1 -> ../../sda1
It actually is possible to identify the correct device from the order in which the disks are attached to the instance: the first will be ‘sdb’ and called ‘persistent-disk-1’, the second ‘sdc’ and ‘persistent-disk-2’, etc. But you should not depend on this mechanism as it may change and could cause you problems.
I’ve been unable to find another way to correlate disks to devices without specifying the device name.
With the above scenario, lsblk doesn’t help:
lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdd 8:48 0 10G 0 disk
sdb 8:16 0 10G 0 disk
sdc 8:32 0 10G 0 disk
sda 8:0 0 10G 0 disk
└─sda1 8:1 0 10G 0 part /
Nor does UUID:
ls -l /dev/disk/by-uuid2bcc54f8-02d4-4f91-baf1-0ad372a1ac18 -> ../../sdc
567ab888-a3b5-43d4-a92a-f594e8653924 -> ../../sda1
604ff07f-8aeb-49f8-894a-4283f5c16eda -> ../../sdd
be51c746-a168-4f1d-b486-02815c822e77 -> ../../sdb
Curiously, if you customarily using Cloud Console to attach disks to instances, this transparently sets the device name property for you, i.e. if you’re using Cloud Console the devices’ names will include the disks’ names per the recommendation above.