0

Here is my example rule:

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 works fine:

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
...

During real boot, the udev debug log did not show any triggered rule for 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

I did initramfs already and the rule did loaded at boot time:

systemd-udevd[591]: Reading rules file: /etc/udev/rules.d/55-battery-charge.rules

weird, if I remove SUBSYSTEM=="power_supply" it works in halfway:

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

It seems when the BAT0 added, the charge_control_end_threshold sysfs does not exist yet. How can I make sure it only happened when the threshold file is exposed already?

hmm... now I see, another kernel module actually expose the parameter much later after BAT0.

1 Answer 1

0

During boot some device like BAT0 is added very early. Then some other kernel modules can expose additional files/attributes into BAT0's sysfs. Thus the rules set those additional attributes can fail if the rule is based on ACTION==add

4
  • so.. what's the solution? How to do it properly, then? Apr 25, 2023 at 19:49
  • this depends on which module actually expose that attributes. You need to dig into kernel source code to figure it out. Unfortunately the entire sysfs ecosystem is still messy. There is no generic guide for this, it is always case by case. Even worse, module name/which module actual expose what can change at any time. So even you find a solution for some version, it can be very fragile @FrederickNord
    – Wang
    Apr 27, 2023 at 8:45
  • since there is no clear api for these stuff. Anything related to those is better to be part of the kernel source code, otherwise it is hard to test/maintain. So the correct way to do this is to contribute to kernel modules and expose a modprobe parameter charge_control_end_threshold, then people can set it as kernel parameter at boot time.
    – Wang
    Apr 27, 2023 at 8:52
  • have you tried matching on ACTION=="!remove"? And do you know which driver is dynamically adding sysfs attributes without throwing a uvent? That's probably considered a bug. Apr 28, 2023 at 8:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .