Udev (userspace /dev ) 是一个用于动态设备检测和管理的 Linux 子系统,从内核版本2.6开始。它是devfs和hotplug的替代品。
它在启动时或者在您添加设备或从中删除设备时动态创建或删除设备节点(设备驱动程序的接口,出现在文件系统中,就好像它是普通文件,存储在 / dev目录下)系统。然后,它将有关设备的信息或对其状态的更改传播到用户空间。
它的功能是 1) 向系统应用程序提供设备事件,2) 管理设备节点的权限,3) 可以在/dev目录中创建有用的符号链接以访问设备,甚至重命名网络接口。
udev的优点之一是它可以使用持久设备名称来保证重新启动后设备命名的一致性,无论其发现顺序如何。此功能非常有用,因为内核只是根据发现顺序分配不可预测的设备名称。
在本文中,我们将学习如何使用Udev在 Linux 系统上进行设备检测和管理。请注意,大多数(如果不是全部)主流现代 Linux 发行版都将Udev作为默认安装的一部分。
了解 Linux 中 Udev 的基础知识
udev守护进程systemd -udevd(或systemd-udevd.service)与内核通信,并在每次从系统中添加或删除设备或设备更改其状态时直接从内核接收设备uevent 。
Udev基于规则——它的规则灵活且非常强大。每个收到的设备事件都会与从/lib/udev/rules.d和/run/udev/rules.d中的文件读取的规则集进行匹配。
您可以在/etc/udev/rules.d/目录中编写自定义规则文件(文件应以扩展名结尾.rules
)来处理设备。请注意,该目录中的规则文件具有最高优先级。
要创建设备节点文件,udev需要使用某些属性来标识设备,例如标签、序列号、使用的主设备号和次设备号、总线设备号等等。此信息由sysfs文件系统导出。
Whenever you connect a device to the system, the kernel detects and initializes it, and a directory with the device name is created under /sys/ directory which stores the device attributes.
The main configuration file for udev is /etc/udev/udev.conf, and to control the runtime behavior the udev daemon, you can use the udevadm utility.
To display received kernel events (uevents) and udev events (which udev sends out after rule processing), run udevadm with the monitor command. Then connect a device to your system and watch, from the terminal, how the device event is handled.
The following screenshot shows an excerpt of an ADD event after connecting a USB flash disk to the test system:
$ udevadm monitor
![](https://www.alaica.com/wp-content/uploads/linux-403.png)
To find the name assigned to your USB disk, use the lsblk utility which reads the sysfs filesystem and udev db to gather information about processed devices.
$ lsblk
![](https://www.alaica.com/wp-content/uploads/linux-404.png)
From the output of the previous command, the USB disk is named sdb1
(absolute path should be /dev/sdb1
). To query the device attributes from the udev database, use the info command.
$ udevadm info /dev/sdb1
![](https://www.alaica.com/wp-content/uploads/linux-405.png)
How to Work with Udev Rules in Linux
In this section, we will briefly discuss how to write udev rules. A rule comprises of a comma-separated list of one or more key-value pairs. Rules allow you to rename a device node from the default name, modify permissions and ownership of a device node, trigger execution of a program or script when a device node is created or deleted, among others.
We will write a simple rule to launch a script when a USB device is added and when it is removed from the running system.
Let’s start by creating the two scripts:
$ sudo vim /bin/device_added.sh
Add the following lines in the device_added.sh
script.
#!/bin/bash echo "USB device added at $(date)" >>/tmp/scripts.log
Open the second script.
$ sudo vim /bin/device_removed.sh
Then add the following lines to device_removed.sh
script.
#!/bin/bash echo "USB device removed at $(date)" >>/tmp/scripts.log
Save the files, close and make both scripts executable.
$ sudo chmod +x /bin/device_added.sh $ sudo chmod +x /bin/device_removed.sh
Next, let’s create a rule to trigger execution of the above scripts, called /etc/udev/rules.d/80-test.rules.
$ vim /etc/udev/rules.d/80-test.rules
Add these two following rules in it.
SUBSYSTEM=="usb", ACTION=="add", ENV{DEVTYPE}=="usb_device", RUN+="/bin/device_added.sh" SUBSYSTEM==“usb”,ACTION==“删除”,ENV {DEVTYPE}==“usb_device”,RUN + =“/bin/device_removed.sh”
在哪里:
"=="
: 是比较相等性的运算符。"+="
: 是一个运算符,用于将值添加到保存条目列表的键中。- SUBSYSTEM:匹配事件设备的子系统。
- ACTION:匹配事件操作的名称。
- ENV{DEVTYPE}:与设备属性值(在本例中为设备类型)匹配。
- RUN:指定作为事件处理的一部分执行的程序或脚本。
保存文件并关闭它。然后以 root 身份,通过运行告诉systemd-udevd重新加载规则文件(这也会重新加载其他数据库,例如内核模块索引)。
$ sudo udevadm 控制 --reload
现在将 USB 驱动器连接到您的计算机并检查device_added.sh
脚本是否已执行。首先,应在/tmp下创建文件script.log。
$ ls -l /tmp/scripts.log
![](https://www.alaica.com/wp-content/uploads/linux-406.png)
然后该文件应该有一个条目,例如“ USB device returned at date_time ”,如屏幕截图所示。
$猫/tmp/scripts.log
![](https://www.alaica.com/wp-content/uploads/linux-407.png)
有关如何编写 udev 规则和管理 udev 的更多信息,请分别查阅udev和udevadm手册条目,方法是运行:
$ 人 udev $ 人 udevadm
概括
Udev是一个出色的设备管理器,它提供了在目录中设置设备节点的动态方法/dev
。它确保设备在插入并发现后立即进行配置。它将有关已处理设备的信息或其状态更改传播到用户空间。
如果您对此主题有任何问题或想法要分享,请使用反馈表。