linux udev

  • 了解udev (Linux dynamic device management)

1.概述

  Udev is the device manager for the Linux kernel. Udev dynamically creates or removes device node files at boot time in the /dev directory for all types of devices.

  udev is a replacement for the Device File System (DevFS) starting with the Linux 2.6 kernel series. It allows you to identify devices based on their properties, like vendor ID and device ID, dynamically. udev runs in userspace (as opposed to devfs which was executed in kernel space).

  Udev is the Linux subsystem that supplies your computer with device events. In plain English, that means it’s the code that detects when you have things plugged into your computer, like a network card, external hard drives (including USB thumb drives), mouses, keyboards, joysticks and gamepads, DVD-ROM drives, and so on. That makes it a potentially useful utility, and it’s well-enough exposed that a standard user can manually script it to do things like performing certain tasks when a certain hard drive is plugged in.

  udev 是 Linux2.6 内核里的一个功能,它替代了原来的 devfs,成为当前 Linux 默认的设备管理工具。udev 以守护进程的形式运行,通过侦听内核发出来的 uevent 来管理 /dev目录下的设备文件。不像之前的设备管理工具,udev 在用户空间 (user space) 运行,而不在内核空间 (kernel space) 运行。
  udev负责在用户空间,根据用户设置的规则,在监测到设备被插入后,在/dev/下自动创建并命名设备文件节点(实际上只能做一个已存在节点的链接文件),也可以自动设置设备属性。

1.1.Overview

  The udev system is composed of some kernel services and the udevd daemon. The kernel informs the udevd daemon when certain events happen. The udevd daemon is configured to respond to events with corresponding actions. The event information comes from the kernel - the actions happen in userspace. The responses to the events are configurable in “rules”.

  The userspace udev functionality is implemented by the systemd-udevd.service Its config file is in /etc/udev/udev.conf(Configuration for device event managing daemon). The rules files (which amount to more configuration for udevd) are taken from /run/udev/rules.d, /etc/udev/rules.d or /lib/udev/rules.d. Packages install rules in /lib/udev/rules.d), while the /etc and /run locations provide a facility for the administrator to override the behavior of a package-provided rule. If a file with the same name is present in more than one of these directories then the latter(s) file will be ignored. Files in there are parsed in alpha order, as long as the name ends with “.rules”. When the config file or rules files are changed, the udevadm program should be used to instruct systemd-udevd to reload the rules (see below).

  udev was created to respond to hotplug type of events. Much documentation refers to creating devices in response to new devices that have appeared. But, udev is more general; it can run arbitrary userspace commands in response to a new device appearing - or to whatever events it receives from the kernel.

  The times when udevd is active are:

  • at startup, it parses all the config files and rule files and builds a rules database in memory.
  • When an event happens, it checks its rule database and performs the appropriate actions.

1.2.systemd-udevd.service

  The udev daemon, systemd-udevd.service, receives device uevents directly from the kernel whenever a device is added or removed from the system, or it changes its state. When udev receives a device event, it matches its configured set of rules against various device attributes to identify the device. Rules that match may provide additional device information to be stored in the udev database or to be used to create meaningful symlink names.

  All device information udev processes is stored in the udev database and sent out to possible event subscribers. Access to all stored data and the event sources is provided by the library libudev.

  Synopsis:

  • systemd-udevd.service

  • systemd-udevd-control.socket

  • systemd-udevd-kernel.socket

  • /usr/lib/systemd/systemd-udevd ( udev 的守护进程)

    systemd-udevd listens to kernel uevents. For every event, systemd-udevd executes matching instructions specified in udev rules.

Debug:
  In Grub menu, press E for edit. Remove quiet splash to get boot in text mode. Add udev.log-priority=debug. Then Ctrl+X to boot. You can see the udev messages while booting or after using: journalctl -u systemd-udevd.service -f
  For permanent settings, you may set udev.log-priority=debug in GRUB_CMDLINE_LINUX

/usr/share/initramfs-tools/scripts/init-top/udev

1.3.udev在linux的那个位置

  udev的守护进程在linux的位置在systemd中的位置如下所示,举个例子:如果向pc中插入一个usb设备,kernel在总线上发现这个设备,使用dirver初始化,在sysfs创建device目录等操作之后,将通知用户空间的udev,然后上层的显示层才能看到这个usb设备,并最终将它显示在desktop上:
在这里插入图片描述
2.工作原理

  udev是一个通用的内核设备管理器。它以守护进程的方式运行于Linux系统,并监听在新设备初始化或设备从系统中移除时,内核(通过netlink socket)所发出的uevent。

  系统提供了一套规则用于匹配可发现的设备事件和属性的导出值。匹配规则可能命名并创建设备节点,并运行配置程序来对设备进行设置。udev规则可以匹配像内核子系统、内核设备名称、设备的物理等属性,或设备序列号的属性。规则也可以请求外部程序提供信息来命名设备,或指定一个永远一样的自定义名称来命名设备,而不管设备什么时候被系统发现.

在这里插入图片描述

2.1.工作方式

  当设备连接或者移除的时候,内核会发出热拔插事件(hotplug event),而热拔插的时候,设备的详细的信息会由内核通过 netlink 套接字发送出来,发送出来的事情,我们称之为 uevent。udev 基址,就根据这些内核发出来的信息,去创建设备文件节点工作,而设备的命名策略、权限控制、事件处理都是由 udev 规则文件去执行。

  • (1)内核检测设备的连接状况,通过 netlink 发出 uevent

  • (2) udev 获取内核发出的信息,进行规则的匹配工作。

  udev 工作流程图:
在这里插入图片描述
2.2.udev系统架构:

udev系统可以分为三个部分:

  • libudev函数库,可以用来获取设备的信息。
  • udevd守护进程,处于用户空间,用于管理虚拟/dev
  • 管理命令udevadm,用来诊断出错情况。

2.2.1.udevadm (udev management tool)

  udevadm可以用来监视和控制udev运行时的行为,请求内核事件,管理事件队列,以及提供简单的调试机制。

选项:

  • -d, --debug
    Print debug messages to standard error. This option is implied in udevadm test and udevadm test-builtin commands.

  • info options:查询udev数据库中的设备信息。也可以从sysfs文件系统中查询到设备的属性以辅助创建udev规则。

    • –query=type     从数据库中查询指定类型的设备。需要–path和–name来指定设备。合法的查询文件是:设备名,链接,路径,属性
    • –path=devpath    设备的路径
    • –name=file      设备节点或者链接
    • –attribute-walk    打印指定设备的所有sysfs记录的属性,以用来udev规则匹配特殊的设备。该选项打印链上的所有设备信息,最大可能到sys目录。
    • –device-id-of-file=file 打印主/从设备号
    • –export-db      输出udev数据库中的内容
  • trigger [options]:接收内核发送来的设备事件。主要用于重放coldplug事件信息。
    内核在启动时已经检测到了系统的硬件设备,并把硬件设备信息通过sysfs内核虚拟文件系统导出。udev扫描sysfs文件系统,根据硬件设备信息生成热插拔(hotplug)事件,udev再读取这些事件,生成对应的硬件设备文件。由于没有实际的硬件插拔动作,所以这一过程被称为coldplug。

  • test [options] devpath : 模拟一个udev事件,打印出debug信息

  • control : 修改运行的udev守护进程的内部状态。

    • udevadm control --log-priority=debug
    • udevadm control reload-rules && udevadm trigger
  • monitor [options]:监听内核事件和udev发送的events事件。打印事件发出的设备。可以通过比较内核或者udev事件的时间戳来分析事件时序。

    • –kernel  输出内核事件
    • –udev  输出udev规则执行时的udev事件
    • –property  输出事件的属性
    • –subsystem-match=string  通过子系统或者设备类型过滤事件。只有匹配了子系统值的udev设备事件通过。
    • –tag-match=string  通过属性过滤事件,只有匹配了标签的udev事件通过。

Note: 一般systemd-udev-trigger.service中应用udevadm trigger回放coldplug:
ExecStart=/bin/udevadm trigger --type=subsystems --action=add ; /bin/udevadm trigger --type=devices --action=add

Example:

//當前系統識別了多少個磁盤
root@uos-PC:/home/uos# fdisk -l  | grep -o '^Disk /dev/[sh]d[a-z]\>'
Disk /dev/sda

//顯示/dev/sda磁盤路徑硬件點
root@uos-PC:/home/uos# udevadm info -q path -n /dev/sda
/devices/pci0000:00/0000:00:08.0/ata1/host0/target0:0:0/0:0:0:0/block/sda

//列出設備的具體屬性
 udevadm info -a -p  [device name]    #這裏的device name表示目前識別的規則NAME

3.udev rules

  udev rules written by the administrator go in /etc/udev/rules.d/, their file name has to end with .rules. The udev rules shipped with various packages are found in /usr/lib/udev/rules.d/. If there are two files by the same name under /usr/lib and /etc, the ones in /etc take precedence.

  udev 规则是定义在一个以 .rules 为扩展名的文件中。那些文件主要放在两个位置:

  • /usr/lib/udev/rules.d,这个目录用于存放系统安装的规则;

  • /etc/udev/rules.d/ 这个目录是保留给自定义规则的。

  • /etc/udev/udev.conf

uos@uos-PC:/usr/lib/udev$ cat /etc/udev/udev.conf
# see udev.conf(5) for details
#
# udevd is also started in the initrd.  When this file is modified you might
# also want to rebuild the initrd, so that it will include the modified configuration.

#udev_log=info
#children_max=
#exec_delay=
#event_timeout=180
#resolve_names=early

Note: udev_log:syslog记录日志的级别,默认值是 err。如果改为 info 或者 debug 的话,会有冗长的 udev 日志被记录下来。
refer to https://man7.org/linux/man-pages/man5/udev.conf.5.html

  • /usr/lib/udev/rules.d
    这些文件都是一些 udev 的规则文件,当一个设备连接的时候,就根据这些文件的规则,进行设备节点的创建。
uos@uos-PC:/usr/lib/udev/rules.d/$ ls
39-usbmuxd.rules         60-libgphoto2-6.rules             65-libwacom.rules           75-net-description.rules             77-mm-simtech-port-types.rules            80-mm-candidate.rules           90-pulseaudio.rules
40-usb_modeswitch.rules  60-libsane.rules                  69-libmtp.rules             75-probe_mtd.rules                   77-mm-telit-port-types.rules              80-net-setup-link.rules         92-libccid.rules
50-firmware.rules        60-libtss2-udev.rules             69-lvm-metad.rules          77-mm-cinterion-port-types.rules     77-mm-ublox-port-types.rules   

The difference between the two folders is that:

  • /usr/lib/udev/rules.d
    Contains default rules files. Do not edit these files.

  • /etc/udev/rules.d/*.rules
    Contains customized rules files. You can modify these files or create them.

  However, files with identical file names replace each other. Files in /etc have the highest priority and take precedence over files with the same name in /lib. This can be used to override the default rules.

  Another thing, to disable the the rules file entirely, you can create a symlink in /etc with the same name as a rules file in /lib, pointing to /dev/null.

refer to

  • https://wiki.debian.org/udev
  • https://wiki.archlinux.org/index.php/Udev
  • https://wiki.debian.org/DeviceManagement
  • https://man7.org/linux/man-pages/man7/udev.7.html
  • http://www.reactivated.net/writing_udev_rules.html
  • https://www.freedesktop.org/software/systemd/man/udev.conf.html#
  • https://mirrors.edge.kernel.org/pub/linux/utils/kernel/hotplug/
  • https://blog.csdn.net/li_wen01/article/details/89435306
  • https://www.cnblogs.com/carriezhangyan/p/9564900.html

猜你喜欢

转载自blog.csdn.net/weixin_41028621/article/details/114369020
今日推荐