Kernel Korner - udev—Persistent Device Naming in User Space
So, back to our two-printer example. To name both of these devices in a consistent manner, the following two udev rules might be used:
BUS="usb", SYSFS_serial="W09090207101241330", \ NAME="lp_color" BUS="usb", SYSFS_serial="HXOLL0012202323480", \ NAME="lp_plain"
These rules cause udev to look at the sysfs file serial for both printers and, depending on the value in the file, name the printer either lp_color or lp_plain. This ensures that no matter which device was plugged in first or detected first or whether another USB printer is added to the system, both of the printers have the same persistent name.
udev allows a number of printf-like string substitutions to be used in the NAME, SYMLINK and PROGRAM fields in the udev.rules file. These fields are:
%n: kernel number of the device; for example, the device sda3 has a kernel number of 3.
%k: kernel name for the device.
%M: kernel major number for the device.
%m: kernel minor number for the device.
%b: bus ID for the device.
%c: PROGRAM returned string. A number can be added to this modifier to pick up only a specific word within the string. This field does not work within the PROGRAM field for the obvious reason.
%%: % character itself.
Also, a number of the different keys support a simple form of shell-style pattern matching. These patterns are:
*: matches zero, one or more characters.
?: matches any single character but does not match zero characters.
[ ]: matches any single character specified within the brackets; for example, the pattern string tty[SR] would match either ttyS or ttyR. Ranges also are supported within this match with the - character. For example, to match on the range of all digits, the pattern [0-9] would be used. If the first character following the [ is !, any character not enclosed is matched.
Because of the ability to do these simple string substitutions and string pattern matching, combined with the ability to have udev run any other program and use its result, udev has become an extremely flexible tool for naming devices. As an example of this power, take a look at the following rule:
KERNEL="[hs]d[a-z]", PROGRAM="name_cdrom.pl %M %m", \ NAME="%1c", SYMLINK="cdrom"
This rule matches any block device and calls the Perl script name_cdrom.pl with the major and minor number of the device. If this program is successful, udev uses the first word of the program's output to name the device and creates a symlink called cdrom. The name_cdrom.pl script can be found in the udev release.
What this script does is determine whether the device is a CD-ROM device. If it is, it queries the Free CDDB database to see if the CD-ROM present in the device is known in the database. If it is, it then names the device based on the CD. For example, my /dev looks like the following when using this rule:
$ ls -l /dev/S* /dev/cdrom brw------- 1 root root 22, 64 Feb 15 08:26 /dev/Samiam-Astray lrwxrwxrwx 1 root root 8 Feb 15 08:26 /dev/cdrom -> /dev/Samiam-Astray
This shows how udev can query a database across the Internet to determine how to name a device. Yes, it's a crazy naming scheme to try to use, but it shows how powerful and flexible udev can be.
The author would like to thank Daniel Stekloff of IBM, who has helped shape the design of udev in many ways. Without his perseverance, udev would not be available at all. Also, Kay Sievers has been instrumental in implementing the majority of the advanced features in udev, most notably the ability to have string modifiers and pattern matching, both of which are essential for using udev in the real world. Without his help, udev would not be anywhere near as powerful and useful as it is.
Also, without Pat Mochel's sysfs and driver model core, udev would not have been possible to implement. The author is indebted to him for undertaking what most thought of as an impossible task and for allowing others to build easily on his common framework, allowing all users to see the “Web woven by a spider on drugs” that the kernel keeps track of.
This article is based on the Ottawa Linux Symposium 2002 paper on udev (see Resources).
Resources for this article: /article/7496.
Greg Kroah-Hartman currently is the Linux kernel maintainer for a variety of different driver subsystems. He works for IBM, doing Linux kernel-related things, and can be reached at firstname.lastname@example.org.
Free DevOps eBooks, Videos, and more!
Regardless of where you are in your DevOps process, Linux Journal can help!
We offer here the DEFINITIVE DevOps for Dummies, a mobile Application Development Primer, and advice & help from the expert sources like:
- Linux Journal
- New Products
- Readers' Choice Awards 2014
- Handling the workloads of the Future
- Android Candy: Google Keep
- How Can We Get Business to Care about Freedom, Openness and Interoperability?
- Days Between Dates?
- diff -u: What's New in Kernel Development
- December 2014 Issue of Linux Journal: Readers' Choice
- Computing without a Computer