33.4. Key Selection

To identify a device uniquely and distinguish multiple devices from each other, a unique property is essential for a working udev rule. Here are some examples of standard keys:

SUBSYSTEM

Subsystem of which the device is part

BUS

Device bus type

KERNEL

Device name the kernel uses

ID

Device number on the bus (for example, PCI bus ID)

SYSFS{...}

sysfs device attributes, like label, vendor, or serial number

The keys SUBSYSTEM and ID can be useful, but usually the keys BUS, KERNEL, and SYSFS{...} are used. The udev configuration also provides keys that call external scripts and evaluate their results. Find details about this in the udev man page.

The file system sysfs exposes information about the hardware in a directory tree. Each file generally only contains one item of information, such as the device name, the vendor, or the serial number. Each of these files can be used to match with a key. To use several SYSFS keys in one rule, however, you can only use files in the same directory as key values. The tool udevinfo can help finding useful and unique key values.

You must find one subdirectory of /sys that refers to the relevant device and contains a file dev. These directories are all located under /sys/block or /sys/class. If a device node already exists for the device, udevinfo can find the right subdirectory for you. The command udevinfo -q path -n /dev/sda outputs /block/sda. This means that the desired directory is /sys/block/sda. Now call udevinfo with the command udevinfo -a -p /sys/block/sda. The two commands can also be combined, as in udevinfo -a -p `udevinfo -q path -n /dev/sda`. The following is an extract from the output:

BUS=="scsi"
ID=="0:0:0:0"
SYSFS{detach_state}=="0"
SYSFS{type}=="0"
SYSFS{max_sectors}=="240"
SYSFS{device_blocked}=="0"
SYSFS{queue_depth}=="1"
SYSFS{scsi_level}=="3"
SYSFS{vendor}==" "
SYSFS{model}=="USB 2.0M DSC"
SYSFS{rev}=="1.00"
SYSFS{online}=="1"

From the output information, look for suitable keys that do not change. Remember that you cannot use keys from different directories in one rule.