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 Today: September Issue of Linux Journal (Retail value: $5.99)||Sep 27, 2016|
|nginx||Sep 27, 2016|
|Epiq Solutions' Sidekiq M.2||Sep 26, 2016|
|Nativ Disc||Sep 23, 2016|
|Android Browser Security--What You Haven't Been Told||Sep 22, 2016|
|The Many Paths to a Solution||Sep 21, 2016|
- Free Today: September Issue of Linux Journal (Retail value: $5.99)
- Readers' Choice Awards 2013
- Android Browser Security--What You Haven't Been Told
- Epiq Solutions' Sidekiq M.2
- The Many Paths to a Solution
- Downloading an Entire Web Site with wget
- Securing the Programmer
- Nativ Disc
- Tech Tip: Really Simple HTTP Server with Python
Pick up any e-commerce web or mobile app today, and you’ll be holding a mashup of interconnected applications and services from a variety of different providers. For instance, when you connect to Amazon’s e-commerce app, cookies, tags and pixels that are monitored by solutions like Exact Target, BazaarVoice, Bing, Shopzilla, Liveramp and Google Tag Manager track every action you take. You’re presented with special offers and coupons based on your viewing and buying patterns. If you find something you want for your birthday, a third party manages your wish list, which you can share through multiple social- media outlets or email to a friend. When you select something to buy, you find yourself presented with similar items as kind suggestions. And when you finally check out, you’re offered the ability to pay with promo codes, gifts cards, PayPal or a variety of credit cards.Get the Guide