Kernel – How udev and uevent Work

deviceskerneludev

So i was just wondering if my explanations of udev and how it works seem correct and my understanding is correct please let me know.

SO my understanding of udev is that it is a dynamic device manager on Linux which runs as a daemon.

  • when a change to a device occurs such as if a device is plugged in the kernel sends a uevent to udev, udev then can go to sysfs to find details on the device such as the vendor, device name and model etc.
  • once it has the details of the device, Udev then matches these attributes to the rules set for a specific kind of device in this case it may be a usb device. if a rule like "create symlink for all usb devices" exists then udev will do this. udev checks the rules and matches the attributes to verify the device and then can start adding changes to the device as well as do whatever the rules say to the device.

That is basically my current understanding please correct me if im wrong and tell me extra info.

Best Answer

UDEV

Udev stand for "userspace /dev" it is a device manager for the Linux kernel. It is part of systemd (an init system used to bootstrap user space and manage user processes).

Originally udev was independent from systemd, it was merged with systemd in 2012, this lead to some complication for distribution running without systemd like explained here for the gentoo distribution.

This application (udev) is meant to replace devfsd and hotplug, udev primarily manages device nodes in the /dev directory. At the same time, udev also handles all user space events raised when hardware devices are added into the system or removed from it, including firmware loading as required by certain devices (via kernel modules). Concretely udev is run as systemd service (systemd-udevd.service) to achieve its tasks, it listens to kernel uevents. For every event, systemd-udevd executes matching instructions specified in udev rules (/etc/udev/rules.d/), details about rules writing are available on this article.

At the Linux kernel level 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 behavior of the udev daemon (service) can be configured using udev.conf(5) (/etc/udev/udev.conf), its command line options, environment variables, and on the kernel command line, or changed dynamically with udevadm control.

The udev, as a whole, is divided into three parts:

  • Library libudev that allows access to device information.
  • User space daemon (sytemd) udevd that manages the virtual /dev.
  • Administrative command-line utility udevadm for diagnostics.

Udev itself is divided on those three parts but it completely rely on the kernel device management and it's uevents calls, the system gets calls from the kernel via netlink socket. Earlier versions used hotplug, adding a link to themselves in /etc/hotplug.d/default with this purpose.

Note that other application/daemon may listen to uevents calls over libudev, gudev or directly from the kernel with GUdevClient

Further infos on udev are available on the sources of this answer: debian wiki, arch linux wiki, opensource.com, the geek diary, freedesktop.org, wikipedia, pks.mpg.de and other linked sites.

Details about operation layers of udev are explained here and illustrated with this diagram:

enter image description here

Related Question