[Linux Basics] - Detailed Explanation of Linux Device Manager udev

1. What is udev?

udev is the device manager of Linux Kernel 2.6 series. Its main function is to manage the device nodes under the /dev directory. It is also used to replace the functions of devfs and hotplug, which means that it has to deal with the behavior of the /dev directory and all user space when adding/removing hardware, including when loading firmware.

The latest version of udev depends on the latest version of the uevent interface of the upgraded Linux Kernel 2.6.13. The system using the new version of udev cannot be started under version 2.6.13, unless the noudev parameter is used to disable udev and the traditional /dev is used for device reading.

Second, the benefits of using udev

  1. When a device is added or deleted, udev's daemon frame listens to the uevent from the kernel to add or delete device files under /dev, so udev only generates device files for connected devices, not under /dev A large number of virtual device files.
  2. Linux users can flexibly generate device file names with strong identification through custom rule files, and does not depend on the order in which the devices are inserted into the system.
  3. udev can set the permissions of the device file and the owner and group of the device file according to certain conditions.

Three, udev configuration file

[inbi@debian ~]#cat /etc/udev/udev.conf
# The initial syslog(3) priority: "err", "info", "debug" or its
# numerical equivalent. For runtime debugging, the daemons internal
# state can be changed with: "udevcontrol log_priority=".
udev_log = "err"
  • udev_log: syslog logging level, the default value is err. If it is changed to info or debug, a lengthy udev log will be recorded.
  • udev_root: The directory where the device generated by udev is stored. The default value is /dev/. It is recommended not to modify the parameters, so this option is not displayed by default.

Fourth, udev's rules and rules files

The rules file is the most important part of udev and is stored in /etc/udev/rules.d/ by default. All rule files must be suffixed with ".rules", and the rule files are executed in the order of the first letter or number.

In the rule file, except for the line (comment) beginning with "#", all non-blank lines are regarded as a rule, but a rule cannot be extended to multiple lines. Rules are composed of multiple key-value pairs, separated by commas. Key-value pairs can be divided into conditional matching key-value pairs (hereinafter referred to as "matching keys") and assignment key-value pairs (hereinafter referred to as "Assignment key"), a rule can have multiple matching keys and multiple assignment keys. The matching key is to match all the conditions of a device attribute. When the attribute of a device matches all the matching keys in the rule, the rule is considered effective, and then the assignment of the rule is executed according to the content of the assignment key. Here is a simple rule:

KERNEL=="sdb", NAME="root disk", MODE="0660"

KERNEL is the matching key, and NAME and MODE are the assignment keys. The meaning of this rule is: if there is a device whose kernel device name is sdb, the condition will take effect, and the following assignment will be executed: a device file named root_disk is generated under /dev, and the device file permissions are set to 0660 .

All operators of udev rules
== Compare the key and value, if they are equal, the condition is satisfied
!= Compare keys and values, if they are not equal, the condition is satisfied
= Assign a value to a key
+= Assign a value to a key that represents multiple entries
:= Assign a value to a key and reject all subsequent changes to the key
Match key for udev rules
ACTION Event behavior: add (add device), remove (remove device)
KERNEL Kernel device name, for example: sda, cdrom
DEVPATH Devpath path of the device
SUBSYSTEM The name of the subsystem of the device, for example, the subsystem of sda is block
BUS The bus name of the device in devpath, for example: usb
DRIVER The device driver name of the device in devpath, for example: ide-cdrom
ID The identification number of the device in devpath
SYSFS{filename} The content in the property file "filename" of the device under the devpath path of the device. For example: SYSFS{mode}=="ST936701SS" means: if the model of the device is ST936701SS, the device matches the matching key. In a rule, you can set up to five SYSFS matching keys
ENV(key) Environment variables. In a rule, you can set up to five matching keys for environment variables
PROGRAM Invoke external commands
RESULT

The return result of the external command PROGRAM.

例:PROGRAM==”/lib/udev/scsi_id -g -s $devpath”, RESULT==”35000c50000a7ef67″

Assignment key of udev rules
NAME The device file name generated under /dev. Only the first assignment to the NAME of a device takes effect, and then the assignment to the NAME of the device by the matching rules will be ignored. If there is no rule to assign a value to the device NAME, udev will use the kernel device name to generate the device file.
SYMLINK Create symbolic links for device files under /dev/. Since udev can only generate one device file for a certain device, it is recommended to use symbolic links in order not to overwrite the files generated by the system's default udev rules.
OWNER Default user
GROUP Default user group
MODE Device permissions
ENV{key} Import an environment variable
udev callable substitution operator
$kernel,%k The kernel device name of the device, for example: sda, cdrom
$number,%n The kernel number of the device, for example: the kernel number of sda3 is 3
$devpath,%p Devpath path of the device
$id,%b Device ID number in devpath
$sysfs{file},%s{file}

The content of file in the sysfs of the device. In fact, it is the attribute value of the device.

For example: For example: $sysfs{size} indicates the size of the device (disk).

$env{key},%E{key} The value of an environment variable
$major,%M Major number of the device
$minor,%m

Minor number of the device

$result,%c PROGRAM 返回的结果。
$parent,%P 父设备的设备文件名
$root,%r

udev_root 的值,默认是 /dev/。

$tempnode,%N 临时设备名
%% 符号 % 本身
$$ 符号 $ 本身

devpath:是指一个设备在 sysfs 文件系统(/sys)下的相对路径,该路径包含了设备的属性文件。udev 里的多数命令都是针对 devpath 操作的。例如:sda 的 devpath 是 /block/sda,sda2 的 devpath 是 /block/sda/sda2。

五、udev 规则文件实例

KERNEL=="sd*", PROGRAM="/lib/udev/scsi_id -g -s %p",

RESULT=="35000C50000A7EF67", SYMLINK="%k_%c"

该规则的执行:如果有一个内核设备名称以 sd 开头,且 SCSI ID 为 35000c50000a7ef67,则为设备文件产生一个符号链接 “sda_35000c50000a7ef67”。

SUBSYSTEM=="net", SYSFS{address}=="00:1E:6E:00:36:F1", NAME="public_NIC"

如果存在设备的子系统为 net,并且地址(MAC address)为 “00:1E:6E:00:36:F1”,为该设备产生一个名为 public_NIC 的设备文件。

SUBSYSTEM=="block", SYSFS{size}=="71096640", SYMLINK="my_disk"

如果存在设备的子系统为 block,并且大小为 71096640(block),则为该设备的设备文件名产生一个名为 my_disk 为符号链接。

六、查询设备信息

查询 sysfs 文件系统

设备 sda 的 SYSFS{size} 可以通过 cat /sys/block/sda/size 得到;
SYSFS{model} 信息可以通过 cat /sys/block/sda/device/model 得到。

查询磁盘的 SCSI_ID

scsi_id -g -s /block/sda

参考文献:

http://zh.wikipedia.org/wiki/Udev

http://www.reactivated.net/writing_udev_rules.html

http://www.ibm.com/developerworks/cn/linux/l-cn-udev/index.html?ca=drs-cn-0304

Guess you like

Origin blog.csdn.net/u014674293/article/details/114934035