Blkid

24 Mar 2022

Bad Hacks to Find Linux Drive Device from UUID

On linux, you can’t count on the devices listed under /dev/sd* or /dev/xvd*, to have the same names or order. With the UUID (which is how /etc/fstab usually specifies how to mount the root system device), the /dev device name can be determined using the blkid command and some hacky bash.

1
2
# For a system that uses /dev/sda, etc. use 0:8 string slice for blkid
MYDEVICE=$(blkid | grep ${MYUUID}) ; echo ${MYDEVICE:0:8}  # /dev/sdc

However, you might need to do something like start a Docker container with your main system device connected using docker run --device=..., and so the following also works in this specific case looking for the device mounted at the local machine’s /.