16.5. Tips On The Selection Of Suitable Keys

It is essential to choose a good key for every functioning udev rule. Here are some examples of standard keys:

BUS

device bus type

KERNEL

device name which the kernel uses

ID

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

PLACE

physical point where the device is connected (for example, on USB)

The keys ID and PLACE can be useful, but usually the keys BUS, KERNEL and SYSFS{...} are used. The udev command also provides keys which call external scripts and evaluate their results. Further details on this can be found on man page man udev.

The command sysfs stores small files with hardware information in a directory tree. Each file generally is only given one item of information, such as the device name, the vendor or the serial number. Each of these files can be used as the value of a key. If you want to use several SYSFS{...} keys in one rule, however, you can only use files in the same directory.

udevinfo can be a useful tool here. You just have to find one subdirectory of /sys which refers to the relevant device and is given a file dev. These directories are all located under /sys/block or /sys/class.

If a device node already exists for the device, once again udevinfo can reduce the amount of work you have to do. The command udevinfo -q path -n /dev/sda outputs /block/sda. This means that the directory you are looking for is /sys/block/sda. Now call udevinfo with the following command: udevinfo -a -p /sys/block/sda. The two commands can also be combined, for example: 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 all the output wealth of information, you should now look for suitable keys that will not change. Remember that you cannot normally use keys from different directories.