Table of Contents
Abstract
Linux kernel 2.6 introduces a new user space solution
for a dynamic device directory /dev with persistent
device designations: udev. It provides only the files for
devices that are actually present. It creates or removes device node files
usually located in the /dev directory and is able to
rename network interfaces.
The previous implementation of a dynamic
/dev with devfs has been replaced by
udev.
Traditionally, device nodes were stored in the /dev
directory on Linux systems. There was a node for every possible type of
device, regardless of whether it actually existed in the system. As a result,
this directory contained thousands of unused files. Before a newly added
subsystem or kernel device was usable, the corresponding nodes needed to be
created with an special application. The devfs file system
brought a significant improvement, because only devices that actually existed
and were known to the kernel were given a device node in
/dev.
udev introduces a new way of creating device nodes. The kernel exports its internal state in sysfs and, every time a device is recognized by the kernel, it updates the information in sysfs and sends an event to user space. With the information made available by sysfs udev matches a simple rule syntax with the provided device attributes and creates or removes the corresponding device nodes.
The user is not required to create any udev rule for new devices. If a device is connected, the appropriate device node is created automatically. However, the rules introduce the possibility of defining a policy for device naming. This also offers the convenience of replacing a cryptic device name with a name that is easy to remember and also of having persistent device names where two devices of the same type have been connected at the same time.
Assume you have two printers, a high-quality color laser printer and
a black-and-white ink jet printer, both connected via USB. They
appear as /dev/usb/lpX, where X is a number depending
on the order in which they have been connected.
Using udev, create custom udev rules naming one printer
/dev/colorlaser and the other
/dev/inkprinter.
Because these device nodes are created by udev based on the
characteristics of the device, they always point to the
correct device, regardless of the connection order or status.
Before udev creates device nodes under
/dev, it reads all files in
/etc/udev/rules.d with the suffix
.rules in alphabetical order. The first rule that fits
a device is used, even if other rules would also apply. Comments are
introduced with a hash sign (#). Rules take the following
form:
key, [key,...] NAME [, SYMLINK]
At least one key must be specified, because rules are assigned to devices on
the basis of these keys. It is also essential to specify a name. The
device node that is created in /dev bears this name.
The optional symlink parameter allows nodes to be created in other places. A
rule for a printer could take the following form:
BUS=="usb", SYSFS{serial}=="12345", NAME="lp_hp", SYMLINK+="printers/hp"
In this example, there are two keys, BUS and
SYSFS{serial}. udev compares the
serial number to the serial number of the device that is connected to the
USB bus. To assign the name lp_hp to the device in the
/dev directory, all the keys must be
identical. In addition, a
symbolic link /dev/printers/hp, which refers
to the device node, is created. At the same time, the
printers
directory is automatically created. Print jobs can then be sent to
/dev/printers/hp or /dev/lp_hp.