Why does udevadm not provide me a disk serial number when hdparm can?

Compare:

root@home:~# hdparm -I /dev/sdb | grep Serial
    Serial Number:      WCJ025C8
    Transport:          Serial, ATA8-AST, SATA 1.0a, SATA II Extensions, SATA Rev 2.5, SATA Rev 2.6, SATA Rev 3.0
root@home:~#

to:

root@home:~# udevadm info -a -p $(udevadm info -q path -n /dev/sdb) | grep -i serial
    ATTRS{serial}=="0123456789ABCDEF"
    ATTRS{serial}=="0000:00:14.0"
root@home:~#

I see this 0123456789ABCDEF a lot with udev, but it’s bogus, whereas hdparm‘s output exactly matches the physical number on the drive.

As a result, I can’t create udev rules that’ll match my 4 USB drives by serial number.

I’m running ubuntu 16.04 LTE.

Suggestions greatly appreciated.

Answer

In the end I went with this, which is a bit of a hack:

KERNEL=="sd?", SUBSYSTEM=="block", PROGRAM="/root/get_disk_serial.sh %k", SYMLINK+="disk/by-serial/%c"

and then a quick script like this:

#!/bin/bash

/sbin/hdparm -I /dev/$1 | grep 'Serial Number' | awk '{print $3}'

It ain’t pretty, but it works…

Attribution
Source : Link , Question Author : Brad , Answer Author : Brad

Leave a Comment