Skip to content
Search

Udev

lastmod 2020-09-05created 2020-09-056 edits

udev is a device manager for the Linux kernel. As the successor of devfsd and hotplug, udev primarily manages device nodes in the /dev directory. At the same time, udev handles all user space events raised while hardware devices are added to or removed from the system, including loading firmware required by certain devices.

Overview

Unlike traditional Unix systems, which keep static files for device nodes in the /dev directory, the Linux udev device manager dynamically provides nodes only for the devices actually present on the system. devfs was used to provide similar functionality, but Greg Kroah-Hartman cited a number of reasons for preferring udev over devfs.

  • udev supports persistent device naming; for example, it does not depend on the order in which devices are attached to the system. The default udev configuration provides persistent names for devices. Any hard disk is recognized by its unique file system ID, the name of the disk, and the physical location where the hardware is attached.
  • Unlike the kernel space of devfs, udev runs entirely in user space. It moves the naming policy out of the kernel and lets arbitrary programs run before a node is created, so that the device name can be composed from the device's attributes. In other words, the whole process is interruptible and runs at a lower priority.

Broadly, udev is divided into three parts:

  • The library libudev allows access to device information. It has been integrated into the systemd software bundle.
  • The user space daemon udevd, which manages the virtual /dev.
  • The administrative command-line utility udevadm, for diagnostics.

The system receives calls from the kernel through a netlink socket. Early versions used hotplug for this purpose, adding a link to themselves in /etc/hotplug.d/default.

Operation

After the Linux kernel boots, a daemon called udevd is started via init. The moment hardware is plugged into the PC, the kernel refers to the information under /sys written by sysfs and notifies udevd that hardware has been added. udevd then performs actions on the device based on this notification and on the rules it already has. The rules are located under /etc/udev/rules.d.

udev configuration file

The global udev configuration file can specify two parameters. The following are the parameters in udev.conf.

udev_root
Specifies where on the file system device files are placed. The default is /dev.
udev_log
Specifies the logging priority. Takes a syslog priority number or a string such as err or info.

udev rule files

The rule files udev consults when it receives an event from the kernel are located in the following 3 directories.

  • /lib/udev/rules.d: default rules directory
  • /etc/udev/rules.d: rules directory for customization
  • /run/dev/rules.d: temporary rules directory

Step example

udev rule files are watched for creation and updates, so they take effect as soon as they are saved. It is easy to understand with a USB memory stick as an example.

  1. When a USB memory stick is plugged into a PC socket, /dev/sdg1 is assigned automatically.
  2. Run udevadm here to get the device information.
  3. Using the /dev/sdg1 assignment as a hint, get the device path in sysfs.
  4. Use it to find out the device information.
  5. If you unplug the USB memory stick and plug it back in, you can see a message in syslog saying the kernel recognized the device.
  6. You can then see that a symbolic link to the device file has been created under /dev according to the rule.

Categories

Favorite site

author Zer0 sitemap