What is linux 6.5 udev

藏色散人
Release: 2023-04-10 11:01:41
Original
1046 people have browsed it

linux 6.5 udev is the device manager of the Linux kernel; its main function is to manage the device nodes under the "/dev" directory. It is also used to replace devfs (device file system) and hotplug (hot plug) Insert) function; in traditional Linux systems, the device nodes in the "/dev" directory are a series of statically existing files, while "udev" dynamically provides device nodes that actually exist in the system.

What is linux 6.5 udev

#The operating environment of this tutorial: Linux 6.5 system, Dell G3 computer.

What is linux 6.5 udev?

Introduction to udev udevadm and analysis of Linux device renaming and automatic mounting application examples

(1) Introduction to UDEV

The definition of udev in Wikipedia is: udev (userspace /dev) is a device manager for the Linux kernel. As the successor of 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. In Chinese: udev is the device manager of the Linux kernel. Its main function is to manage device nodes under the /dev directory. It is also used to take over the functions of devfs (device file system) and hotplug (hot plug), which means that it has to handle the /dev directory and all user space behaviors when adding/removing hardware, including when loading firmware.

(1) udev summary:

In a traditional Linux system, the device nodes in the /dev directory are a series of static files, while udev is dynamic Provides the device nodes that actually exist in the system. Although devfs provides similar functions, udev has advantages over devfs:

  • udev supports fixed naming of devices and does not depend on the order in which the devices are inserted into the system. The default udev settings provide fixed naming of storage devices. A device can be confirmed using its vid (vendor), pid (device), device name (model) and other attributes or the corresponding attributes of its parent device.
  • udev is executed entirely in user space, rather than in kernel space like devfs. The result is that udev removes the naming policy from the kernel and allows any program to name the device in the device properties before the node is created.

(2) udev running mode:

udev is a universal kernel device manager. It runs as a daemon on Linux systems and listens for uevents issued by the kernel (through the netlink socket) when a new device is initialized or the device is removed from the system.
The system provides a set of rules for matching the exported values ​​of discoverable device events and attributes. Matching rules may name and create device nodes and run configuration programs to configure the device. udev rules can match attributes like the kernel subsystem, kernel device name, physical properties of the device, or device serial number. Rules can also request information from an external program to name the device, or specify a custom name that will always be the same, regardless of when the device is discovered by the system.

(3) udev system architecture:

udev system can be divided into three parts:

  • libudev function library, which can be used to obtain device information.
  • udevd daemon, located in user space, is used to manage virtual/dev
  • The management command udevadm is used to diagnose errors.

The system obtains the information sent by the kernel through the netlink socket. Earlier versions used hotplug and added a link to itself in /etc/hotplug.d/default to achieve this purpose.

(2) Introduction to udevadm command

This is how it is described in the Linux man page. udevadm - udev management tool. In other words, the udevadm command is a tool for managing udev. In fact, if we want to rename the device or automatically mount the device, we also use udevadm to view and track udev information.
udevadm can be used to monitor and control udev runtime behavior, request kernel events, manage event queues, and provide a simple debugging mechanism.

(1) udevadm main command:

  • info Query the sysfs or udev database
  • trigger Request events from the kernel
  • settle View the udev event queue, if Exit when all events have been processed
  • control Modify the internal status information of udev background
  • monitor Monitor the kernel's uevents
  • hwdb Process the hardware database index
  • test Debugging

(2) Command application:

(a) View device information:

udevadm info --query=all --name=sda 查询sda的所有信息
udevadm info --query=path --name=sda 查看sda的path
udevadm info --attribute-walk --name=/dev/nvme0n1  查看nvme0n1的所有父设备一直到sysfs的根节点
Copy after login

--query=type    Query the specified type of device from the database. --path and --name are required to specify the device. Legal query files are: device name, link, path, attribute
--path=devpath  Device path
--name=file   Device node or link
--attribute-walk  Print all the information about the specified device Properties recorded by sysfs to match specific devices with udev rules. This option prints all device information on the chain, up to the sys directory possible.
--device-id-of-file=file Print the master/slave device number
--export-db Output the contents of the udev database

(b) Monitor device events:

udevadm monitor [options] Monitor kernel events and events sent by udev. The device from which the print event was emitted. Event timing can be analyzed by comparing the timestamps of kernel or udev events.

udevadm monitor --property   输出事件的属性
udevadm monitor --kernel --property --subsystem-match=usb    过滤监听符合条件的时间
Copy after login

--kernel Output kernel events
--udev Output udev events when udev rules are executed
--property Output event properties
--subsystem-match=string Through subsystem Or device type filter events. Only udev device events that match the subsystem value are passed.
--tag-match=string Filter events by attributes, and only udev events that match the tag will pass.

(c) Simulate a udev event

udevadm test [options] devpath Simulate a udev event and print out debug information.

(d) Receive device events sent from the kernel

udevadm trigger [options] Receive device events sent from the kernel. Mainly used to replay coldplug event information
(The kernel has detected the system's hardware devices at startup and exported the hardware device information through the sysfs kernel virtual file system. udev scans the sysfs file system and generates hot plugs based on the hardware device information. Hotplug events, udev then reads these events and generates corresponding hardware device files. Since there is no actual hardware plugging and unplugging action, this process is called coldplug.)
--verbose  Output will be triggered Device List.
--dry-run    Does not actually trigger the event
--type=type    Triggers a special device. Legal types: devices, subsystem, failed. The default is devices
--action=action. The triggered event. The default is change
--subsystem-match=subsystem. The device event that triggers the matching subsystem. This option can be specified multiple times and supports shell pattern matching.
--attr-match=attribute=value   Trigger device events that match sysfs attributes. If an attribute value is specified together with an attribute, the attribute's value can be matched using shell patterns. If no value is specified, existing properties are revalidated. This option can be specified multiple times.
--attr-nomatch=attribute=value  Do not trigger device events that match attributes. If possible use pattern matching. You can also specify
--property-match=property=value multiple times to match devices with matching properties. You can specify multiple times to support pattern matching
--tag-match=property     Match devices with matching tags. Can be specified multiple times.
--sysname-match=name    ​​Match devices with the same sys device name. Support pattern matching can be specified multiple times.

(e) View the udev event queue

udevadm settle [options] View the udev event queue and exit if all events are processed.
--timeout=seconds  The maximum time to wait for the event queue to be empty. The default is 180 seconds. If it is 0, exit immediately.
--seq-start=seqnum  Only wait until the given sequence number.
--seq-end=seqnum  Only wait until the given sequence number.
--exit-if-exists=file    Exit if the file exists
--quiet  Do not output any information

(2) Application example:

Use udev to implement USB, Renaming, automatic mounting, and automatic unmounting of SD card devices.

For a simple application of this function, you can refer to another blog "udev renames the device node name and automatically mounts and unmounts the storage device partition"

This article mainly records the problems encountered during use. and precautions.

(三)注意事项:

(1)设备冲突问题:

在海思平台,对于有些SD卡或是USB自动挂载有些时候会出现冲突的问题,问题现象就是自动挂载的时候有时候有时候U盘和SD卡自动挂载相互干扰。主要原因点是因为海思的HI35XX的很多设备不具备SDIO总线,所以如果要使用SD的设备,一般都是将SD卡通过读卡器转换为USB总线信号。对于对于这类问题,可以通过SD卡的读卡器ID来区分是USB还是SD卡设备。

在海思平台可以使用下面命令查看USB设备信息:

lsusb:

~ # lsusb
Bus 001 Device 002: ID 1c9e:9b3c
Bus 001 Device 003: ID 05e3:0610
Bus 001 Device 001: ID 1d6b:0002
Bus 002 Device 001: ID 1d6b:0001
Bus 001 Device 008: ID 0951:1642
Bus 001 Device 009: ID 05e3:0716
Bus 001 Device 006: ID 0bda:8176
~ #
Copy after login

cat device

~ # cat /sys/kernel/debug/usb/devices 
T:  Bus=02 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#=  1 Spd=12   MxCh= 2
B:  Alloc=  0/900 us ( 0%), #Int=  0, #Iso=  0
D:  Ver= 1.10 Cls=09(hub  ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=1d6b ProdID=0001 Rev= 3.10
S:  Manufacturer=Linux 3.10.0 ohci_hcd
S:  Product=HIUSB OHCI
S:  SerialNumber=hiusb-ohci
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=  0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub  ) Sub=00 Prot=00 Driver=hub
E:  Ad=81(I) Atr=03(Int.) MxPS=   2 Ivl=255ms

T:  Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#=  1 Spd=480  MxCh= 2
B:  Alloc=  0/800 us ( 0%), #Int=  5, #Iso=  0
D:  Ver= 2.00 Cls=09(hub  ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=1d6b ProdID=0002 Rev= 3.10
S:  Manufacturer=Linux 3.10.0 ehci_hcd
S:  Product=HIUSB EHCI
S:  SerialNumber=hiusb-ehci
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=  0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub  ) Sub=00 Prot=00 Driver=hub
E:  Ad=81(I) Atr=03(Int.) MxPS=   4 Ivl=256ms

T:  Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#=  2 Spd=480  MxCh= 0
D:  Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=1c9e ProdID=9b3c Rev= 3.18
S:  Manufacturer=LONGSUNG
S:  Product=USB Modem
C:* #Ifs= 5 Cfg#= 1 Atr=80 MxPwr=500mA
I:* If#= 0 Alt= 0 #EPs= 2 Cls=ff(vend.) Sub=42 Prot=01 Driver=option
E:  Ad=01(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E:  Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
I:* If#= 1 Alt= 0 #EPs= 3 Cls=ff(vend.) Sub=00 Prot=00 Driver=option
E:  Ad=83(I) Atr=03(Int.) MxPS=  10 Ivl=32ms
E:  Ad=82(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E:  Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
I:* If#= 2 Alt= 0 #EPs= 3 Cls=ff(vend.) Sub=00 Prot=00 Driver=option
E:  Ad=85(I) Atr=03(Int.) MxPS=  10 Ivl=32ms
E:  Ad=84(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E:  Ad=03(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
I:* If#= 3 Alt= 0 #EPs= 3 Cls=ff(vend.) Sub=00 Prot=00 Driver=option
E:  Ad=87(I) Atr=03(Int.) MxPS=  10 Ivl=32ms
E:  Ad=86(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E:  Ad=04(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
I:* If#= 4 Alt= 0 #EPs= 3 Cls=ff(vend.) Sub=ff Prot=ff Driver=GobiNet
E:  Ad=89(I) Atr=03(Int.) MxPS=   8 Ivl=32ms
E:  Ad=88(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E:  Ad=05(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms

T:  Bus=01 Lev=01 Prnt=01 Port=01 Cnt=02 Dev#=  3 Spd=480  MxCh= 4
D:  Ver= 2.00 Cls=09(hub  ) Sub=00 Prot=02 MxPS=64 #Cfgs=  1
P:  Vendor=05e3 ProdID=0610 Rev=32.98
S:  Product=USB2.0 Hub
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=100mA
I:  If#= 0 Alt= 0 #EPs= 1 Cls=09(hub  ) Sub=00 Prot=01 Driver=hub
E:  Ad=81(I) Atr=03(Int.) MxPS=   1 Ivl=256ms
I:* If#= 0 Alt= 1 #EPs= 1 Cls=09(hub  ) Sub=00 Prot=02 Driver=hub
E:  Ad=81(I) Atr=03(Int.) MxPS=   1 Ivl=256ms

T:  Bus=01 Lev=02 Prnt=03 Port=00 Cnt=01 Dev#=  8 Spd=480  MxCh= 0
D:  Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=0951 ProdID=1642 Rev= 1.00
S:  Manufacturer=Kingston
S:  Product=DT 101 G2
S:  SerialNumber=001CC0EC32F7BB40F71300BF
C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=200mA
I:* If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
E:  Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E:  Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms

T:  Bus=01 Lev=02 Prnt=03 Port=02 Cnt=02 Dev#=  9 Spd=480  MxCh= 0
D:  Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=05e3 ProdID=0716 Rev=97.27
S:  Manufacturer=Genesys
S:  Product=USB Reader
S:  SerialNumber=000000000013
C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=500mA
I:* If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
E:  Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E:  Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms

T:  Bus=01 Lev=02 Prnt=03 Port=03 Cnt=03 Dev#=  6 Spd=480  MxCh= 0
D:  Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=0bda ProdID=8176 Rev= 2.00
S:  Manufacturer=Realtek
S:  Product=802.11n WLAN Adapter
S:  SerialNumber=00e04c000001
C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=500mA
I:* If#= 0 Alt= 0 #EPs= 4 Cls=ff(vend.) Sub=ff Prot=ff Driver=rtl8192cu
E:  Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E:  Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E:  Ad=03(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E:  Ad=84(I) Atr=03(Int.) MxPS=  64 Ivl=125us
~ #
Copy after login

查看上面信息可以知道USB相关的设备有:
Product=HIUSB EHCI #海思USB总线
Product=USB Modem #上网模块
Product=USB2.0 Hub #USB HUB
Product=DT 101 G2 # U盘
Product=USB Reader # usb 读卡器
Product=802.11n WLAN Adapter #USB网卡

(2)多属性匹配问题

对于一个设备如果要匹配他的多个属性,或者是同一个属性,它在不同的层级中有不同的值,那么这个时候需要使用GOTO功能。比如在一个设备中要重名名一个USB设备,它的命令如下:

KERNEL=="sd*",KERNELS=="*:0:0:1",ATTRS{scsi_level}=="0" ,ATTRS{product}=="USB Reader",ATTRS{idVendor}=="05e3",ATTRS{idProduct}=="0716",SYMLINK+="usbsda%n",OPTIONS="ignore_remove"
Copy after login

这里涉及到多个属性ATTRS 值的匹配,另外该USB因为有些经过了usbHUB,所以他的idVendor 和 idProduct 有多个,就有总线的,也有HUB的,还有USB设备的,这样的情况下是匹配不上设备的。具体是什么原因我没有找到一个合理的解释,但是我又一个可用的解决方案,那就是使用GOTO。将上面命令改成如下就可以了。

KERNEL=="sd*",KERNELS=="*:0:0:1",ATTRS{scsi_level}=="0" GOTO="hisi_end"
ATTRS{product}=="USB Reader",ATTRS{idVendor}=="05e3",ATTRS{idProduct}=="0716",SYMLINK+="usbsda%n",OPTIONS="ignore_remove"
LABEL="hisi_end"
Copy after login

(3)监控设备事件

udev的一些匹配规则有些时候比较的莫名其妙,我也没有找到哪里有比较详细的说明,网上的介绍都它过于简单,实际应用的时候还是很多的问题,比如针对上面介绍的设备,如果要写一条卸载设备的命令,可以使用下面语句:

ACTION=="remove",KERNELS=="*:0:0:1",ATTRS{idVendor}=="05e3",ATTRS{idProduct}=="0716",RUN+="/bin/umount -l /opt/usb_sd1_1"
Copy after login

但是在实际使用的时候,他们匹配不上,ACTION=="remove",KERNELS=="*:0:0:1"与ATTRS{idVendor}=="05e3",ATTRS{idProduct}=="0716" 不能同时使用,如果直接改成:

ACTION=="remove",KERNELS=="*:0:0:1",RUN+="/bin/umount -l /opt/usb_sd1_1"
Copy after login

命令功能可以实现,但是这样会出现于设备冲突的情况,在拔出该设备的时候,会把KERNELS=="*:0:0:1" 的其他设备也卸载掉。

在这个时候,我们可以使用 devadm monitor --property 去监控设备拔出的时候它有哪些事件,有哪些属性可以被捕捉到并且与其他的设备属性不同以便区分不同的设备。下面是截取到的一部分数据:

[10:00:33]KERNEL[1555639338.737818] remove   /devices/platform/hiusb-ehci.0/usb1/1-2/1-2.3 (usb)
[10:00:33]UDEV_LOG=3
[10:00:33]ACTION=remove
[10:00:33]DEVPATH=/devices/platform/hiusb-ehci.0/usb1/1-2/1-2.3
[10:00:33]SUBSYSTEM=usb
[10:00:33]DEVNAME=bus/usb/001/007
[10:00:33]DEVTYPE=usb_device
[10:00:33]PRODUCT=5e3/716/9727
[10:00:33]TYPE=0/0/0
[10:00:33]BUSNUM=001
[10:00:33]DEVNUM=007
[10:00:33]SEQNUM=1024
[10:00:33]MAJOR=189
[10:00:33]MINOR=6
Copy after login

从上面可以看出,拔出的时候有个PRODUCT ,它是 idVendor 和 idProduct 值的一个组合,可以使用它来区分不同的设备。最终可以正常卸载设备的命令如下:

ACTION=="remove",SUBSYSTEM=="usb",ENV{PRODUCT}=="5e3/716/9727",RUN+="/bin/umount -l /opt/usb_sd1_1"
Copy after login

相关推荐:《Linux视频教程

The above is the detailed content of What is linux 6.5 udev. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template