Chapter 19. Dynamic Device Nodes with udev

Table of Contents

19.1. Creating Rules
19.2. Automation with NAME and SYMLINK
19.3. Regular Expressions in Keys
19.4. Key Selection
19.5. Persistent Names for Mass Storage Devices


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 it renames network interfaces. 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. As a result, this directory took up a lot of space. The command devfs brought a significant improvement, because only devices that really existed were 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.

The user is not required 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 persistent 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.

19.1. Creating Rules

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.

SUSE LINUX Administration Guide 9.3