Getting started with Udev Rules — Part A
By: Somesh Mohan
Tags: Linux, Udev, Ubuntu, Autoruns, Kernel

Udev is the device manager for linux 2.6 which dynamically creates/modifies/removes device nodes in the /dev directory. Udev plays a major role in hotplugging. As soon as a device is connected to the system, udev listens to it, and based on defined udev rules, triggers an action/device driver.

Without further ado, lets get straight into understanding how the udev rules work and how to modify them.

Location

Udev rules are located in two directories:

/lib/udev/rules.d => Default rules /etc/udev/rules.d => Custom rules. Have precedence over default rules

Nomenclature

Rules files should end with .rules extension. Rules files should have unique names. Rule files in custom directory takes precedence over the file with same name in default directory. Rules files are sorted and processed in lexical order. Additionally, a number can be added to specify the priority of execution. Higher the number, higher is the priority.

E.g: 90-test.rules

Syntax

Every line in the rule files contain key-value pair separated by comma(,). Each line should contain at least one key-value pair. Lines beginning with # are ignored. Can be used for commenting There are two kind of keys => (A) Match Keys (B) Assignment Keys. If all match keys match against their values, the rule gets applied and the assignment keys get the specified values.

Operators

The relation between key and value is specified by a set of specific operators.

“==” : Compare for equality “!=” : Compare for inequality “=” : Assign a value to a key “+=” : Add the value to a key that holds a list of entries “-=” : Remove the value from a key that holds a list of entries “:=” : Assign a final value to key, no changes are allowed any further..

Match Keys

ACTION: Match the name of the event action, viz., ADD,REMOVE,CHANGE DEVPATH: Match the devpath of the event device. eg: /dev/sda1, /dev/tty0 KERNEL: Match the name of the event device NAME: Match the name of a interface. The NAME key needs to be set in one of the preceding rules before being used. SYMLINK: Matches the name of a symbolic link targeting the node.The SYMLINK key needs to be set in one of the preceding rules before it can be used. SUBSYSTEM: Matches the subsystem of the event device. e.g.: block, tty, usb, input, rtc, loop DRIVER: Matches the driver name of the event device. Works only for devices which are bound to a driver at the time of event processing. ATTR{attrName}: Matches the given sysfs attribute values of the event device. e.g.: ATTR{idVendor}, ATTR{idProduct}. TAG: Matched the event device against a device tag KERNELS, SUBSYSTEMS, DRIVERS, ATTRS, TAGS: These work the same as their singular counterparts, just that these search for matches in the entire upward devpath.

Wildcard Characters

The above matching keys also support pattern matching and use wildcard characters similar to any other language:

“*” : Matches zero or more characters “?” : Matches any single character “[]” : Matches any single character specified with the brackets. Also supports range (-) “|” : Separates alternative patterns.

Assignment Keys

The following keys can be assigned on a match

NAME SYMLINK OWNER, GROUP, MODE SECLABEL ATTR{key} SYSCTL ENV{key} TAG

Please visit the Linux man page for detailed explanation of above assignment keys

man udev

Conclusion

In the next part, I will explain using udev, how to show a message when an usb device is connected or removed.