Devfs, sysfs, and udev in Linux
In Linux, there is a dedicated File System for device management. devfs and sysfs are two of them. Before the 2.6 kernel, devfs was used, and devfs was mounted to the/dev directory, providing a file-like method to manage all devices in the/dev directory, we know that each file in the/dev directory corresponds to a device, and these special files are located on the root file system, we have created these device files when creating a file system. Therefore, by operating these special files, we can interact with the kernel.
However, the devfs file system has some disadvantages: (1) for uncertain device ing, sometimes the device files mapped by a device may be different. For example, our USB flash drive may correspond to sda or sdb. (2) For example, if there are not enough primary/secondary device numbers, this will become a problem when there are too many devices. (3) Too many files in the/dev directory cannot represent the actual devices on the current system. (4) Naming is not flexible enough and cannot be specified at will.
After Linux2.6, a new file system sysfs was introduced, which is mounted to the/sys directory. Like devfs, it is also a virtual file system, it is also used to manage system devices. It organizes the devices and bus actually linked to the system into a hierarchical file, user space programs can also use this information to interact with the kernel. This file system is an intuitive response to the actual device tree on the current system, this information is created through the kobject subsystem. When a kobject is created, the corresponding file and directory card are created, and the corresponding directory is located in/sys, since each device has a unique directory in sysfs, it can be read and written by user space.
The user space tool udev uses the information provided by sysfs to implement all devfs functions. However, the difference is that udev runs in the user space, while devfs runs in the kernel space, and udev does not have the inherent defects of devfs. Therefore, sysfs is the future development direction.
Udev is a tool that updates device files, including creating and deleting device files, based on the status of hardware devices in the system. Device Files are usually stored in the/dev directory. After udev is used, only the real devices in the system are contained in the/dev directory. It is independent of the hardware platform and is located in the user space. It requires support from the kernel sysfs and tmpfs. sysfs-bit udev provides the device portal and uevent channel, while tmpfs provides storage space for udev device files.
Udev works completely in user mode, and uses the hotplug event sent by the kernel when the device is added or removed. The device details are output from the kernel to the sysfs File System in/sys. All device naming policies, permission control, and event processing are completed in user mode. However, devfs is part of the kernel.
Udev is used to manage/dev, not to load the kernel driver. Therefore, udev does not automatically load the driver when a nonexistent node is opened. All devices in the system should generate hotplug events and load appropriate drivers. udev will pay attention to this and create corresponding device nodes for it, if you don't want all the device drivers to stay in the memory, we should use other things to manage our modules, which is not the work of udev. The methods used by devfs have resulted in a large number of useless modprobe attempts to detect the existence of devices. Every test detection creates a process that runs modprobe, and almost all of these processes are useless.
Devfs naming is not recommended and not officially supported, this is because the simple device enumeration method is really stupid when the device may be added or deleted at any time. These numbers are troublesome and cannot be used to identify devices.
When the kernel detects a new device in the system, the kernel will generate a new record for the device in the sysfs file system, generally, the sysfs file system is mounted to the/sys directory. A new record is represented by one or more files or directories. Each file contains specific information. Udev is running in the system as a daemon. udevd detects the appearance of new devices in a certain way and obtains information about the devices by searching the records in sysfs corresponding to the devices. Udev will be based on/etc/udev. check the files in the directory specified by udev_rules In the conf file one by one. The files in this directory are all rule files for certain classes or devices. Udev reads files in alphabetical order of file names. If udev finds a rule that matches the new device, udev configures the new device according to the measures defined by the rule. At the same time, the subsequent rule files are no longer read.
Udev maintains a unified device name regardless of the device connection sequence. udev adds an alias to the device name generated by the kernel to achieve the above purpose. Udev is a user-Mode Program and does not change the kernel behavior. Therefore, the kernel can still generate device names such as sda and sdb. However, udev can distinguish different devices based on other information of the device, such as bus information, manufacturer, and vendor, and generate device files. Udev can solve this problem by obtaining a fixed file name for the device file. In subsequent operations on a device, you only need to reference a new device name. However, to ensure maximum compatibility, the new device is used as a symbolic link to the device name automatically generated by the kernel.