udev 学习

udev is a device manager for the Linux kernel. Primarily, it manages device nodes in /dev. It is the successor of devfs and hotplug, which means that it handles the /dev directory and all user space actions when adding/removing devices, including firmware load.

与传统的顺序加载不同,udev 可以并行加载内核模块,具有潜在的性能优势。异步加载模块的方式也有一个天生的缺点:无法保证每次加载模块的顺序,如果机器具有多个块设备,那么它们的设备节点可能随机变化。例如如果有两个硬盘,/dev/sda 可能会随机变成/dev/sdb

/dev/ttyUSB0 /dev/ttyUSB1

规则:

/etc/udev/rules.d/*.rules

udevadm

udevadm expects a command and command specific options. It controls the
       runtime behavior of udev, requests kernel events, manages the event
       queue, and provides simple debugging mechanisms.

why ?

udev rules are flexible and very powerful. Here are some of the things you can use rules to achieve:

  • Rename a device node from the default name to something else
  • Provide an alternative/persistent name for a device node by creating a symbolic link to the default device node
  • Name a device node based on the output of a program
  • Change permissions and ownership of a device node
  • Launch a script when a device node is created or deleted (typically when a device is attached or unplugged)
  • Rename network interfaces

默认规则

Default udev rules are stored in /etc/udev/rules.d/50-udev.rulesIn general, you want your own rules to be parsed before the defaults

In a rules file, lines starting with "#" are treated as comments. Every other non-blank line is a rule. Rules cannot span multiple lines.

注释,

不能跨行

1对多(个规则)

It is important to understand that udev will not stop processing when it finds a matching rule, it will continue searching and attempt to apply every rule that it knows about.

KERNEL=="hdb", NAME="my_spare_disk"

匹配键== , 赋值键=

The above rule includes one match key (KERNEL) and one assignment key (NAME). The semantics of these keys and their properties will be detailed later. It is important to note that the match key is related to its value through the equality operator (==), whereas the assignment key is related to its value through the assignment operator (=).

  • NAME - the name that shall be used for the device node
  • SYMLINK - a list of symbolic links which a
  • ct as alternative names for the device node

As hinted above, udev only creates one true device node for one device.

KERNEL=="hdb", DRIVER=="ide-disk", SYMLINK+="sparedisk"
KERNEL=="hdc", SYMLINK+="cdrom cdrom0"


Matching sysfs attributes

SUBSYSTEM=="block", ATTR{size}=="234441648", SYMLINK+="my_disk"


String matching

As well as matching strings exactly, udev allows you to use shell-style pattern matching. There are 3 patterns supported:

  • * - match any character, zero or more times
  • ? - match any character exactly once
  • [] - match any single character specified in the brackets, ranges are also permitted

Here are some examples which incorporate the above patterns. Note the use of the string substitution operators.

KERNEL=="fd[0-9]*", NAME="floppy/%n", SYMLINK+="%k"
KERNEL=="hiddev*", NAME="usb/%k"

http://www.reactivated.net/writing_udev_rules.html
原文地址:https://www.cnblogs.com/kwingmei/p/3273685.html