Udev rule to change storage device symlink

Some time ago, when testing a script, I - by accident - targeted my projects storage device in an incredibly destructive manner - using /dev/sda1 :man_facepalming: .

Although I lost a lot of data - it was not catastrophic - but as one always try to learn from my mistakes, I began to investigate the options of renaming device links.

udev documentation indicates supporting this but I cannot make this particular rule work.

An udev rule has three parts

|<--- ACTIONs ---->|  |<-------------------- FILTERs ------------------->|    |<--------- CHANGEs ---------------->|
ACTION=="add|change", KERNEL=="sd*", ENV{ID_WWN}==<world wide name>",         SYMLINK+="<my-disk-name>%n"

First I find the values which uniquely identifies the device

udevadm info --query=all /dev/sda | grep -i -E 'serial|wwn'
S: disk/by-id/wwn-0x5002538844584d30
E: ID_SERIAL=Samsung_SSD_840_EVO_500GB_mSATA_S1KMNSAFB02237V
E: ID_SERIAL_SHORT=S1KMNSAFB02237V
E: ID_WWN=0x5002538844584d30
E: ID_WWN_WITH_EXTENSION=0x5002538844584d30
E: DEVLINKS=/dev/disk/by-path/pci-0000:05:00.0-ata-4.0 /dev/disk/by-id/ata-Samsung_SSD_840_EVO_500GB_mSATA_S1KMNSAFB02237V /dev/disk/by-id/wwn-0x5002538844584d30 /dev/disk/by-diskseq/1 /dev/disk/by-path/pci-0000:05:00.0-ata-4

Create the rule using the chosen identifier - in this case I use the world wide name.
The symlink value must be unique and not anything which is reserved by the kernel.
Save the rule as /etc/udev/rules.d/10-safe-name.rules

ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_WWN}=="0x5002538844584d30", SYMLINK+="my_vbox%n"

The rule verification is solid

udevadm verify

Reload all rules

udevadm control --reload

Trigger the device rules

udevadm trigger --type=devices --action=change

Based on the rule I expect /dev/sda changing to /dev/my_vbox and the only partition to be /dev/my_vbox1.

Not even a reboot changes this.

I am having a hard time figuring out what I am missing.

EDIT:

It turns out there is a rule file named /usr/lib/udev/rules.d/60-persistent-storage.rules which appears to override my rule.

From the content of that file it seems I need lessons in writing udev rules.

1 Like

udevadm verify - o.K

sudo udevadm control --reload
sudo udevadm trigger --type=devices --action=change

That would be correct if one is in user context.

For these tests I am in root context - hence no need for sudo.