这是我的示例规则:
cat /etc/udev/rules.d/55-battery-charge.rules
ACTION=="add|change", KERNEL=="BAT*", SUBSYSTEM=="power_supply", ATTR{charge_control_end_threshold}="60"
udevadm test /sys/class/power_supply/BAT0
工作正常:
BAT0: /etc/udev/rules.d/55-battery-charge.rules:1 ATTR '/sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:20/PNP0C09:01/PNP0C0A:03/power_supply/BAT0/charge_control_end_threshold' writing '60'
BAT0: /etc/udev/rules.d/55-battery-charge.rules:1 Failed to write ATTR{/sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:20/PNP0C09:01/PNP0C0A:03/power_supply/BAT0/charge_control_end_threshold}, ignoring: Permission denied
DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:20/PNP0C09:01/PNP0C0A:03/power_supply/BAT0
...
ACTION=add
SUBSYSTEM=power_supply
USEC_INITIALIZED=2126021481
...
真正启动时,udev调试日志没有显示BAT0的任何触发规则:
journalctl -b 0 -u systemd-udevd | grep BAT0
systemd-udevd[591]: BAT0: Device (SEQNUM=4977, ACTION=add) is queued
systemd-udevd[591]: BAT0: sd-device-monitor: Passed 734 byte to netlink monitor
systemd-udevd[668]: BAT0: Processing device (SEQNUM=4977, ACTION=add)
systemd-udevd[668]: BAT0: Device (SEQNUM=4977, ACTION=add) processed
systemd-udevd[668]: BAT0: sd-device-monitor: Passed 734 byte to netlink monitor
我已经做了 initramfs 并且规则在启动时加载:
systemd-udevd[591]: Reading rules file: /etc/udev/rules.d/55-battery-charge.rules
奇怪的是,如果我删除SUBSYSTEM=="power_supply"
它,它会在中途起作用:
systemd-udevd[589]: BAT0: Device (SEQNUM=4976, ACTION=add) is queued
systemd-udevd[589]: BAT0: sd-device-monitor: Passed 735 byte to netlink monitor
systemd-udevd[667]: BAT0: Processing device (SEQNUM=4976, ACTION=add)
systemd-udevd[667]: BAT0: /etc/udev/rules.d/55-battery-charge.rules:1 ATTR '/sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:20/PNP0C09:01/PNP0C0A:03/power_supply/BAT0/charge_control_end_threshold' writing '60'
systemd-udevd[667]: BAT0: /etc/udev/rules.d/55-battery-charge.rules:1 Failed to write ATTR{/sys/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:20/PNP0C09:01/PNP0C0A:03/power_supply/BAT0/charge_control_end_threshold}, ignoring: No such file or directory
systemd-udevd[667]: BAT0: Device (SEQNUM=4976, ACTION=add) processed
systemd-udevd[667]: BAT0: sd-device-monitor: Passed 735 byte to netlink monitor
看来当添加BAT0时,charge_control_end_threshold sysfs还不存在。我如何确保它仅在阈值文件已公开时发生?
嗯...现在我明白了,另一个内核模块实际上在 BAT0 之后很久才公开该参数。
答案1
在启动过程中,像 BAT0 这样的设备很早就被添加了。然后一些其他内核模块可以将其他文件/属性公开到 BAT0 的 sysfs 中。因此,如果规则基于以下规则,则设置这些附加属性的规则可能会失败ACTION==add