1. udev规则文件路径一般在:
/etc/udev/rules
2. udev规则文件命名方式为 [优先级]-[描述].rules
3. udev规则文件中可以有很多条规则(毕竟文件扩展名是rules),一行为一条规则
4. 一条规则的编写方式为 :
ACTION==[规则触发条件],[匹配属性]==[属性值],[匹配属性]==[属性值],......,[设置属性]=[设置值]
# eg.
ACTION=="add",SUBSYSTEM=="net",DRIVER=="r8152",NAME="eth1"
5. ACTION支持的条件:
add:当设备被添加到系统时触发规则。
remove:当设备被从系统中移除时触发规则。
change:当设备的某些属性发生变化时触发规则。
move:当设备被移动到另一个位置时触发规则。
online:当设备从离线状态变为在线状态时触发规则。
offline:当设备从在线状态变为离线状态时触发规则。
6. 常见属性:
ATTRS:设备的属性,例如ATTRS{idVendor}
、ATTRS{idProduct}
等。
KERNEL:设备的内核名称,例如KERNEL=="sd*"
匹配所有块设备。
SUBSYSTEM:设备所属的子系统,例如SUBSYSTEM=="block"
匹配所有块设备。
ACTION:设备的操作,例如ACTION=="add"
匹配设备插入事件。
DRIVER:设备的驱动程序,例如DRIVER=="usb"
匹配所有USB设备。
ENV:设置环境变量,例如ENV{ID_BUS}="usb"
。
SYMLINK:创建符号链接,例如SYMLINK+="mydevice"
。
PROGRAM:执行外部程序,例如PROGRAM="/path/to/script.sh"
。
RUN:执行内联命令,例如RUN+="/bin/command"
。
NAME: 重命名设备
7. 具体的attr查看方式为:
udevadm info -a -n [设备路径]
# eg.
udevadm info -a -n /dev/urandom
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
looking at device '/devices/virtual/mem/urandom':
KERNEL=="urandom"
SUBSYSTEM=="mem"
DRIVER==""
ATTR{power/control}=="auto"
ATTR{power/runtime_active_time}=="0"
ATTR{power/runtime_status}=="unsupported"
ATTR{power/runtime_suspended_time}=="0"
8. 查看USB设备用
lsusb -t
udevadm info -a -n /dev/bus/usb/*