Getting started with Udev Rules — Part A

Somesh Mohan
3 min readNov 20, 2019

--

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:

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

Nomenclature

  1. Rules files should end with .rules extension.
  2. Rules files should have unique names. Rule files in custom directory takes precedence over the file with same name in default directory.
  3. Rules files are sorted and processed in lexical order.
  4. 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

  1. Every line in the rule files contain key-value pair separated by comma(,). Each line should contain at least one key-value pair.
  2. Lines beginning with # are ignored. Can be used for commenting
  3. There are two kind of keys => (A) Match Keys (B) Assignment Keys.
  4. 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.

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

Match Keys

  1. ACTION: Match the name of the event action, viz., ADD,REMOVE,CHANGE
  2. DEVPATH: Match the devpath of the event device. eg: /dev/sda1, /dev/tty0
  3. KERNEL: Match the name of the event device
  4. NAME: Match the name of a interface. The NAME key needs to be set in one of the preceding rules before being used.
  5. 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.
  6. SUBSYSTEM: Matches the subsystem of the event device. e.g.: block, tty, usb, input, rtc, loop
  7. 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.
  8. ATTR{attrName}: Matches the given sysfs attribute values of the event device. e.g.: ATTR{idVendor}, ATTR{idProduct}.
  9. TAG: Matched the event device against a device tag
  10. 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:

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

Assignment Keys

The following keys can be assigned on a match

  1. NAME
  2. SYMLINK
  3. OWNER, GROUP, MODE
  4. SECLABEL
  5. ATTR{key}
  6. SYSCTL
  7. ENV{key}
  8. 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.

--

--

Somesh Mohan

Mean stack developer, Love playing around with linux, Raspberry is life. Talk to me about anything hardware.