Chapter 15. Dynamic Device Nodes with udev

Table of Contents

15.1. Creating Rules
15.2. Automization with NAME and SYMLINK
15.3. Regular Expressions in Keys
15.4. Key Selection
15.5. Consistent Names for Mass Storage Devices


Linux kernel 2.6 introduces a new user space solution for a dynamic device directory /dev with consistent device designations: udev. The previous implementation of /dev with devfs no longer works and 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. The result was that this directory took up a lot of space. The command devfs has brought a significant improvement, because now only devices that really exist are given a device node in /dev.

udev introduces a new way of creating device nodes. It compares the information made available by sysfs with data provided by the user in the form of rules. sysfs is a new file system in kernel 2.6. It provides basic information about devices connected to the system. sysfs is mounted under /sys.

It is not absolutely necessary for the user to create rules. If a device is connected, the appropriate device node is created. However, the rules introduce the possibility of changing the names for the nodes. This offers the convenience of replacing a cryptic device name with a name that is easy to remember and also of having consistent device names where two devices of the same type have been connected.

Unless otherwise specified, two printers are given the designations /dev/lp0 and /dev/lp1. Which device is given which device node depends on the order in which they are switched on. Another example is external mass storage devices, such as USB hard disks. The udev command allows exact device paths to be entered in /etc/fstab.

15.1. Creating Rules

Before udev creates device nodes under /dev, it reads the file /etc/udev/udev.rules. 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, as rules are assigned to devices on the basis of these keys. It is also essential to specify a name, as 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 thus 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 agree. In addition, a symbolic /dev/printers/hp, which refers to the device node, is created. During this operation, the printers directory is automatically created. Print jobs can then be sent to /dev/printers/hp or /dev/lp_hp.