Contents
/dev Directory
Since version 2.6, the kernel is capable of adding or removing almost any
device in the running system. Changes in device state (whether a device is
plugged in or removed) need to be propagated to userspace. Devices need to be
configured as soon as they are plugged in and discovered. Users of a certain
device need to be informed about any state changes of this device. udev
provides the needed infrastructure to dynamically maintain the device node
files and symbolic links in the /dev directory. udev
rules provide a way to plug external tools into the kernel device event
processing. This enables you to customize udev device handling, for example,
by adding certain scripts to execute as part of kernel device handling, or
request and import additional data to evaluate during device handling.
/dev Directory¶
The device nodes in the /dev directory provide access
to the corresponding kernel devices. With udev, the
/dev directory reflects the current state of the
kernel. Every kernel device has one corresponding device file. If a device
is disconnected from the system, the device node is removed.
The content of the /dev directory is kept on a
temporary file system and all files are created from scratch at every
system start-up. Manually created or changed files intentionally do not
survive a reboot. Static files and directories that should always be
present in the /dev directory regardless of the
state of the corresponding kernel device can be placed in the
/lib/udev/devices directory. At system start-up, the
contents of that directory is copied to the /dev
directory with the same ownership and permissions as the files in
/lib/udev/devices.
The required device information is exported by the sysfs file system. For every device the kernel has detected and initialized, a directory with the device name is created. It contains attribute files with device-specific properties. Every time a device is added or removed, the kernel sends a uevent to notify udev of the change.
The udev daemon reads and parses all provided rules from the
/etc/udev/rules.d/*.rules files once at start-up and
keeps them in memory. If rules files are changed, added, or removed, the
daemon receives an event and updates the in-memory representation of the
rules.
Every received event is matched against the set of provides rules. The rules can add or change event environment keys, request a specific name for the device node to create, add symlinks pointing to the node, or add programs to run after the device node is created. The driver core uevents are received from a kernel netlink socket.
The kernel bus drivers probe for devices. For every detected device, the
kernel creates an internal device structure and the driver core sends a
uevent to the udev daemon. Bus devices identify themselves by a
specially-formatted ID, which tells what kind of device it is. Usually these
IDs
consist of vendor and product ID and other subsystem-specific values. Every
bus has its own scheme for these IDs, called
MODALIAS. The kernel takes the device information, composes a
MODALIAS ID string from it, and sends that string along
with the event. For a USB mouse, it looks like this:
MODALIAS=usb:v046DpC03Ed2000dc00dsc00dp00ic03isc01ip02
Every device driver carries a list of known aliases for devices it can
handle. The list is contained in the kernel module file itself. The program
depmod reads the ID lists and creates the file
modules.alias in the kernel's
/lib/modules directory for all currently available
modules. With this infrastructure, module loading is as easy as calling
modprobe for every event that carries a
MODALIAS key. If modprobe $MODALIAS is
called, it matches the device alias composed for the device with the aliases
provided by the modules. If a matching entry is found, that module is
loaded. All this is triggered by udev and happens automatically.
All device events happening during the boot process before the udev daemon is
running are lost, because the infrastructure to handle these events lives on
the root file system and is not available at that time. To cover that loss,
the kernel provides a uevent file for every device in
the sysfs file system. By writing add to that file, the
kernel resends the same event as the one lost during
boot. A simple loop over all uevent files in
/sys triggers all events again to create the device
nodes and perform device setup.
As an example, a USB mouse present during boot may not be initialized by the early boot logic, because the driver is not available that time. The event for the device discovery was lost and failed to find a kernel module for the device. Instead of manually searching for possibly connected devices, udev just requests all device events from the kernel after the root file system is available, so the event for the USB mouse device just runs again. Now it finds the kernel module on the mounted root file system and the USB mouse can be initialized.
From userspace, there is no visible difference between a device coldplug sequence and a device discovery during runtime. In both cases, the same rules are used to match and the same configured programs are run.
The program udevmonitor can be used to visualize the driver core events and the timing of the udev event processes.
UEVENT[1132632714.285362] add@/devices/pci0000:00/0000:00:1d.1/usb2/2-2 UEVENT[1132632714.288166] add@/devices/pci0000:00/0000:00:1d.1/usb2/2-2/2-2:1.0 UEVENT[1132632714.309485] add@/class/input/input6 UEVENT[1132632714.309511] add@/class/input/input6/mouse2 UEVENT[1132632714.309524] add@/class/usb_device/usbdev2.12 UDEV [1132632714.348966] add@/devices/pci0000:00/0000:00:1d.1/usb2/2-2 UDEV [1132632714.420947] add@/devices/pci0000:00/0000:00:1d.1/usb2/2-2/2-2:1.0 UDEV [1132632714.427298] add@/class/input/input6 UDEV [1132632714.434223] add@/class/usb_device/usbdev2.12 UDEV [1132632714.439934] add@/class/input/input6/mouse2
The UEVENT lines show the events the kernel has sent over
netlink. The UDEV lines show the finished udev event
handlers. The timing is printed in microseconds. The time between
UEVENT and UDEV is the time udev took
to process this event or the udev daemon has delayed its execution to
synchronize this event with related and already running events. For example,
events for hard disk partitions always wait for the main disk device event
to finish, because the partition events may rely on the data the main disk
event has queried from the hardware.
udevmonitor --env shows the complete event environment:
UDEV [1132633002.937243] add@/class/input/input7 UDEV_LOG=3 ACTION=add DEVPATH=/class/input/input7 SUBSYSTEM=input SEQNUM=1043 PHYSDEVPATH=/devices/pci0000:00/0000:00:1d.1/usb2/2-2/2-2:1.0 PHYSDEVBUS=usb PHYSDEVDRIVER=usbhid PRODUCT=3/46d/c03e/2000 NAME="Logitech USB-PS/2 Optical Mouse" PHYS="usb-0000:00:1d.1-2/input0" UNIQ="" EV=7 KEY=70000 0 0 0 0 0 0 0 0 REL=103
udev also sends messages to syslog. The default syslog priority that
controls which messages are sent to syslog is specified in the udev
configuration file /etc/udev/udev.conf. The log
priority of the running daemon can be changed with udevcontrol
log_priority=level/number.
A udev rule can match any property the kernel adds to the event itself
or any information that the kernel exports to sysfs.
The rule can also request additional information from external programs.
Every event is matched against all provided rules. All rules are located in
the /etc/udev/rules.d directory.
Every line in the rules file contains at least one key value pair. There are two kinds of keys, match and assignment keys. If all match keys match their values, the rule is applied and the assignment keys are assigned the specified value. A matching rule may specify the name of the device node, add symlinks pointing to the node, or run a specified program as part of the event handling. If no matching rule is found, the default device node name is used to create the device node. The rule syntax and the provided keys to match or import data are described in the udev man page.
The dynamic device directory and the udev rules infrastructure make it possible to provide stable names for all disk devices—regardless of their order of recognition or the connection used for the device. Every appropriate block device the kernel creates is examined by tools with special knowledge about certain buses, drive types, or file systems. Along with the dynamic kernel-provided device node name, udev maintains classes of persistent symbolic links pointing to the device:
/dev/disk
|-- by-id
| |-- scsi-SATA_HTS726060M9AT00_MRH453M4HWHG7B -> ../../sda
| |-- scsi-SATA_HTS726060M9AT00_MRH453M4HWHG7B-part1 -> ../../sda1
| |-- scsi-SATA_HTS726060M9AT00_MRH453M4HWHG7B-part6 -> ../../sda6
| |-- scsi-SATA_HTS726060M9AT00_MRH453M4HWHG7B-part7 -> ../../sda7
| |-- usb-Generic_STORAGE_DEVICE_02773 -> ../../sdd
| `-- usb-Generic_STORAGE_DEVICE_02773-part1 -> ../../sdd1
|-- by-label
| |-- Photos -> ../../sdd1
| |-- SUSE10 -> ../../sda7
| `-- devel -> ../../sda6
|-- by-path
| |-- pci-0000:00:1f.2-scsi-0:0:0:0 -> ../../sda
| |-- pci-0000:00:1f.2-scsi-0:0:0:0-part1 -> ../../sda1
| |-- pci-0000:00:1f.2-scsi-0:0:0:0-part6 -> ../../sda6
| |-- pci-0000:00:1f.2-scsi-0:0:0:0-part7 -> ../../sda7
| |-- pci-0000:00:1f.2-scsi-1:0:0:0 -> ../../sr0
| |-- usb-02773:0:0:2 -> ../../sdd
| |-- usb-02773:0:0:2-part1 -> ../../sdd1
`-- by-uuid
|-- 159a47a4-e6e6-40be-a757-a629991479ae -> ../../sda7
|-- 3e999973-00c9-4917-9442-b7633bd95b9e -> ../../sda6
`-- 4210-8F8C -> ../../sdd1
The formerly used hotplug package is entirely replaced by udev and the udev-related kernel infrastructure. The following parts of the former hotplug infrastructure have been made obsolete or had their functionality taken over by udev:
/etc/hotplug/*.agentNo longer needed or moved to /lib/udev
/etc/hotplug/*.rcReplaced by the /sys/*/uevent trigger
/etc/hotplug/blacklistReplaced by the blacklist option in
modprobe.conf
/etc/dev.d/*Replaced by the udev rule RUN key
/etc/hotplug.d/*Replaced by the udev rule RUN key
/sbin/hotplugReplaced by udevd listening to netlink; only used in the initial RAM file system until the root file system can be mounted, then it is disabled
/dev/*Replaced by dynamic udev and static content in /lib/udev/devices/*
The following files and directories contain the crucial elements of the udev infrastructure:
/etc/udev/udev.confMain udev configuration file
/etc/udev/rules.d/*udev event matching rules
/lib/udev/devices/*
Static /dev content
/lib/udev/*Helper programs called from udev rules
For more information about the udev infrastructure, refer to the following man pages:
General information about udev, keys, rules, and other important configuration issues.
udevinfo can be used to query device information from the udev database.
Information about the udev event managing daemon.
udevmonitor prints the kernel and udev event sequence to the console. This tool is mainly used for debugging purposes.