The links under /dev/disk/by-id is created by udev. In /etc/udev/rules.d/50-udev.rules such lines can be found:
KERNEL=="sd*[!0-9]|sr*", SYSFS{ieee1394_id}=="*", ENV{ID_SERIAL}="$sysfs{ieee1394_id}", ENV{ID_BUS}="ieee1394"
KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}=="", IMPORT{program}="/lib/udev/usb_id -x"
KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}=="", IMPORT{program}="/lib/udev/scsi_id -g -x -s %p -d $tempnode"
KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}=="", IMPORT{program}="/lib/udev/scsi_id -g -x -a -s %p -d $tempnode"
KERNEL=="nst[0-9]*|st*|sd*[!0-9]|sr*|dasd*[!0-9]|cciss?c", ENV{ID_SERIAL}=="?*", SYMLINK+="disk/by-id/$env{ID_BUS}-$env{ID_SERIAL}"
Unfortunately it is not trivial to find out this by-id
symlink from the name of the device. But there is at least one direct way:
$ udevinfo -q path -n /dev/sda
/block/sda
$ udevinfo -q symlink -p /block/sda
disk/by-id/scsi-SATA_KINGSTON_SVP100_31JY100MY5SK disk/by-path/pci-0000:00:11.0-scsi-0:0:0:0
A space separated list is returned. So the by-id
path can be found.
Or You can directly get the "composite" serial number:
$ udevinfo -q env -p /block/sda
ID_VENDOR=ATA
ID_MODEL=KINGSTON_SVP100S
ID_REVISION=CJR1
ID_SERIAL=SATA_KINGSTON_SVP100_31JY100MY5SK
ID_TYPE=disk
ID_BUS=scsi
ID_PATH=pci-0000:00:11.0-scsi-0:0:0:0
In the ID_SERIAL
after the last underscore You can get the serial number.
As I see a serial number can contain alphanumerical characters and '-' (I have such SCSI HDDs). Does anybody know any limitation about the serial number? It if can contain '_' then this method is not bullet proof.
The ID_MODEL
key could be used to localise more precisely the beginning of the serial number, as (it seems to me) its first 15 characters are added to the ID_SERIAL
(spaces replaced by '_').