Mapping Device to Persistent Name Using udev

Tags: April 12, 2014 11:38 PM

To prevent device name changes from time to time i.e: /dev/sdc to /dev/sdd and so on a corresponding udev rules need to be created. The first step to dive into udev configuration is getting information from the device.

Getting Device Information

In this example I have Seagate 250GB hard drive which connected through USB. The device is currently attached to /dev/sdd. Here's how to get information from the device. All commands are run by root.
$ udevadm info --query=property --name=/dev/sdd
DEVLINKS=/dev/disk/by-id/usb-ST325031_0AS_222262984E80-0:1 /dev/disk/by-path/pci-0000:00:1d.0-usb-0:1.2:1.0-scsi-0:0:0:1 /dev/seagate-sata
DEVNAME=/dev/sdd
DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host17/target17:0:0/17:0:0:1/block/sdd
DEVTYPE=disk
ID_BUS=usb
ID_INSTANCE=0:1
ID_MODEL=0AS
ID_MODEL_ENC=0AS\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
ID_MODEL_ID=2338
ID_PART_TABLE_TYPE=dos
ID_PATH=pci-0000:00:1d.0-usb-0:1.2:1.0-scsi-0:0:0:1
ID_PATH_TAG=pci-0000_00_1d_0-usb-0_1_2_1_0-scsi-0_0_0_1
ID_REVISION=F
ID_SERIAL=ST325031_0AS_222262984E80-0:1
ID_SERIAL_SHORT=222262984E80
ID_TYPE=disk
ID_USB_DRIVER=usb-storage
ID_USB_INTERFACES=:080650:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=ST325031
ID_VENDOR_ENC=ST325031
ID_VENDOR_ID=152d
MAJOR=8
MINOR=48
SUBSYSTEM=block
UDEV_LOG=3
UDISKS_PARTITION_TABLE=1
UDISKS_PARTITION_TABLE_COUNT=4
UDISKS_PARTITION_TABLE_SCHEME=mbr
UDISKS_PRESENTATION_NOPOLICY=0
USEC_INITIALIZED=4454013463
One of value which uniquely identify the device is ID_SERIAL. I will using it inside the udev rules which I want to create.

UDEV Rules Configuration

I will save the configuration on /etc/udev/rules.d/99-custom.rules. Instead of /dev/sdX I will name the device to be more human friendly which is: /dev/seagate-250gb. Bear in mind, you may still find that kernel still creating your old /dev/sdX, it's OK since our udev rules is number 99 and does not override the default rules.
# SEAGATE 250GB Internal HD, connected from sata-to-usb
SUBSYSTEM=="block", KERNEL=="sd*", ENV{ID_SERIAL}=="ST325031_0AS_222262984E80-0:1", NAME="seagate-250gb", SYMLINK+="seagate-sata%n"
Now try to unplug and plug the device. My hard drive has 4 partition so the output is as follow:
$ ls -l /dev/seagate-*
brw-rw---- 1 root disk 8, 49 Apr 12 22:54 /dev/seagate-250gb
lrwxrwxrwx 1 root root    13 Apr 12 22:54 /dev/seagate-sata -> seagate-250gb
lrwxrwxrwx 1 root root    13 Apr 12 22:54 /dev/seagate-sata1 -> seagate-250gb
lrwxrwxrwx 1 root root    13 Apr 12 22:54 /dev/seagate-sata2 -> seagate-250gb
lrwxrwxrwx 1 root root    13 Apr 12 22:54 /dev/seagate-sata3 -> seagate-250gb
lrwxrwxrwx 1 root root    13 Apr 12 22:54 /dev/seagate-sata5 -> seagate-250gb

References

Share on Facebook Twitter

0 comments:

Post a Comment