====== UDEV 规则配置 ====== - 官方文档:https://www.reactivated.net/writing_udev_rules.html - `KERNEL / SUBSYSTEM / DRIVER / ATTR` 只匹配本设备不匹配父设备. 加个`S`, 就可以在本设备以及父设备整条链上搜索匹配。 `KERNELS / SUBSYSTEMS / DRIVERS / ATTRS` - 字符匹配: ```bash * - match any character, zero or more times ? - match any character exactly once [] - match any single character specified in the brackets, ranges are also permitted ``` - 替换符 ```bash $kernel, %k The kernel name for this device. $number, %n The kernel number for this device. For example, "sda3" has kernel number 3. $devpath, %p The devpath of the device. $id, %b The name of the device matched while searching the devpath upwards for SUBSYSTEMS, KERNELS, DRIVERS, and ATTRS. $driver The driver name of the device matched while searching the devpath upwards for SUBSYSTEMS, KERNELS, DRIVERS, and ATTRS. $attr{file}, %s{file} The value of a sysfs attribute found at the device where all keys of the rule have matched. If the matching device does not have such an attribute, and a previous KERNELS, SUBSYSTEMS, DRIVERS, or ATTRS test selected a parent device, then the attribute from that parent device is used. If the attribute is a symlink, the last element of the symlink target is returned as the value. %%: 符号 % 本身。 $$: 符号 $ 本身。 ``` - 获取设备信息: - `udevadm info` 指令 取代 `udevinfo` - `udevadm info -a -p /sys/block/sda` 获取设备ATTRS各项属性 - `last_rule` - 到本条规则为止,后续规则不起作用 - `OPTIONS+="last_rule"` - `udevadm test /sys/block/sda` 测试规则 - `udevadm control --reload-rules` 修改规则后重新加载生效 - 或者 `udevadm control -R` - `NAME=` 只对网路设备起作用 ```bash NAME The name to use for a network interface. See systemd.link(5) for a higher-level mechanism for setting the interface name. The name of a device node cannot be changed by udev, only additional symlinks can be created. ``` ## 示例 - ubuntu默认的usb storage mount 规则 `/etc/udev/rules.d/10-usb-storage.rules` : ```bash KERNEL!="sd[a-z][0-9]",GOTO="automount_exit" ACTION=="add", SUBSYSTEM=="block", RUN+="/bin/mkdir -p /media/%k", RUN+="/usr/bin/mount $devnode /media/%k" ACTION=="remove", RUN+="/usr/bin/umount /media/%k",RUN+="/bin/rmdir /media/%k" LABEL="automount_exit" ``` - 指定类型 ```bash ACTION=="add", KERNEL=="sd*[0-9]", SUBSYSTEMS=="block", SYMLINK+="sd-myself", ATTRS{model}=="Your_Harddisk_Model"` ```