reference whole disk (/dev/sda) using UUID

On my system (Debian), I can see the UUID identifier for all of my disks partitions (i.e. /dev/sda1, dev/sda2, ..)

ls /dev/disk/by-uuid/

However, I don’t see the UUID identifier for /dev/sda itself. Is it possible to reference whole disk with UUID?

I need this because I want to reference a particular disk, and I cannot rely it will be called /dev/sda.


The solution suggested by @don_crissti is great. However, I would like the UUID to be the same for all hard disks of the same Model/Manufacturer, not unique by serial number.

Using udevadm, I can see the disk attributes:

udevadm info -n /dev/sda -a
ATTRS{model}=="Samsung SSD 840 "

How can I generate a UUID from these attributes, so that same Model/Manufacturer disk will have the same UUID ?

Asked By: Michael Boies


The symlinks under /dev/disk/by-uuid/ are created by udev rules based on filesystems UUIDs. If you look at /usr/lib/udev/rules.d/60-persistent-storage.rules you will find entries like:

...... ENV{ID_FS_UUID_ENC}=="?*", SYMLINK+="disk/by-uuid/$env{ID_FS_UUID_ENC}"

To reference a disk you could use the disk serial number and the ENV{ID_SERIAL_SHORT} key.
The following udev rule matches the drive with serial no. 0000000013100925DB96 and creates a symlink with the same name under /dev/disk/by-uuid/:

KERNEL=="sd*", SUBSYSTEM=="block", ENV{DEVTYPE}=="disk", ENV{ID_SERIAL_SHORT}=="0000000013100925DB96", SYMLINK+="disk/by-uuid/$env{ID_SERIAL_SHORT}"

As to your other question… sure, you could always use ENV{ID_MODEL} instead of ENV{ID_SERIAL_SHORT} and use a custom string for your symlink name.
The following rule matches any drive with ID_MODEL = M4-CT128M4SSD2 and creates a symlink M4-SSD-1234567890 under /dev/disk/by-uuid/:

KERNEL=="sd*", SUBSYSTEM=="block", ENV{DEVTYPE}=="disk", ENV{ID_MODEL}=="M4-CT128M4SSD2", SYMLINK+="disk/by-uuid/M4-SSD-1234567890"

Note that this works fine as long as there’s only one drive matching the ID_MODEL. If there are multiple drives of the same model, the rule is applied again for each of them and the symlink will point to the last detected/added drive.

Answered By: don_crissti
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.