0

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.

Brad
  • 289
  • 4
  • 10
  • BTW, I tried the solution at http://www.linuxquestions.org/questions/linux-hardware-18/udev-rules-fail-on-identical-sata-hard-disk-drives-584595/ to no avail. – Brad Apr 12 '17 at 12:28
  • 1
    You should get the serial number with `udevadm info --query=all --name=/dev/sdb | grep -i serial`. With that information you can create a udev rule with `ENV{ID_SERIAL_SHORT}==`. – Thomas Apr 12 '17 at 16:46
  • @Thomas -- did that -- no joy `root@home:/sys/block/sdb# udevadm info --query=all --name=/dev/sdb | grep -i serial E: ID_SERIAL=JMicron_Generic_DISK00_0123456789ABCDEF-0:0 E: ID_SERIAL_SHORT=0123456789ABCDEF root@home:/sys/block/sdb#` – Brad Apr 12 '17 at 16:51

2 Answers2

2

According to this SUSE knowledge base article, some (or possibly all) of these device properties are set by 60-persistent-storage.rules. Since udev delays execution of udev rules until all .rules files have been evaluated, these properties are available in, e.g., a RUN assignment key–value pair, but not during evaluation of matching key–value pairs.

In short, to use these properties in your matching key–value pairs, your udev rule file must have a name "greater than" "60-persistent-storage.rules", e.g., "80-set-storage-permissions.rules". This will ensure it's only evaluated after 60-persistent-storage.rules.

The following worked for me to match a drive by partition table UUID or serial number:

# Match by partition table UUID.
SUBSYSTEM="block", \
ACTION="add", \
ENV{ID_PART_TABLE_UUID}="00000000-0000-0000-0000-000000000000", \
RUN+="<command>"

# Match by serial number.
SUBSYSTEM="block", \
ACTION="add", \
ENV{ID_SERIAL}="WD00000000_0000000000", \
RUN+="<command>"

For a full list of the properties you can match by, run udevadm info /sys/block/<kernel>, where <kernel> is the name of the block device, e.g., sda, nvme0n1.

swilton
  • 21
  • 2
2

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...

Brad
  • 289
  • 4
  • 10