如何使 sysfs 参数值在每次重新启动时保持不变?

如何使 sysfs 参数值在每次重新启动时保持不变?

我必须编写一些 sysfs 参数文件并每次重新启动。我想知道是否有一种方法可以尽早自动设置这些设置,最好是在 initramfs 期间。有规范的方法可以做到这一点吗?

我使用 lsinitramfs 检查负责该 sysfs 的模块。它们确实存在。例如:asus-wmi.ko,如果我想更改/sys/class/power_supply/BAT0/charge_control_end_threshold.

笔记

  • 这一切都应该在 / 挂载之前发生,因此我更有可能需要直接将文件/配置添加到 initramfs 而不是 / 磁盘。
  • 最好应该发生在scripts/local-top/cryptroot

答案1

请注意,由于您提到的特定类是由内核模块添加的,因此在加载模块之前不会发生任何修改。


您可以选择一些 udev 规则要添加到/etc/udev/rules.d目录中:

ACTION=="add", KERNEL=="asus-nb-wmi", RUN+="/bin/bash -c 'echo [TheValueOfYourchoice] > /sys/class/power_supply/BAT?/charge_control_end_threshold'"

护理:你提到的是华硕wmi模块。上述规则适用于华硕-NB-WMI模块。你可能应该适应。


或者,如果在 systemd 下,创建一个 systemd 服务在目录下/etc/systemd/system

[Unit]
Description=Set the battery charge end threshold
After=multi-user.target
StartLimitBurst=0

[Service]
Type=oneshot
Restart=on-failure
ExecStart=/bin/bash -c 'echo [TheValueOfYourChoice] > /sys/class/power_supply/BAT0/charge_control_end_threshold'

[Install]
WantedBy=multi-user.target

如果您已经熟悉 systemd 服务,您会注意到Restart=on-failure and StartLimitBurst=0解决在加载 asus 模块之前启动服务的情况所需的技巧 ( )

当然,在这两种情况下,改变[您选择的价值]对于任何有效号码。

答案2

就是这样sysfsutils是为了.

它应该针对大多数(如果不是全部)Linux 发行版进行打包。 Debian 软件包描述如下:

Package: sysfsutils
Version: 2.1.1-3
Installed-Size: 62
Maintainer: Guillem Jover <[email protected]>
Architecture: amd64
Depends: libc6 (>= 2.34), libsysfs2 (>= 2.1.1), lsb-base, pci.ids
Pre-Depends: init-system-helpers (>= 1.54~)
Description-en: sysfs query tool and boot-time setup
 The sysfs is a virtual file system found in Linux kernels 2.5+ that provides
 a tree of system devices. This package provides the program 'systool' to
 query it, which can be used to list devices by bus, class, and topology.
 .
 In addition this package ships a configuration file /etc/sysfs.conf which
 allows one to conveniently set sysfs attributes at system bootup (via an
 init script).
Description-md5: 07811d91c926da426d94db98052434b1
Multi-Arch: foreign
Homepage: https://github.com/linux-ras/sysfsutils

顺便说一句,另请参阅sysctlsysctl.conf过程,它为 中的内核参数配置提供类似的功能/proc/sys/。您几乎肯定已经安装了这个,因为它是在 Linux 上提供pspgreppkill、等的软件包。top


最后,如果您需要在加载内核模块时设置特定模块选项,可以使用/etc/modules和 .conf 文件/etc/modprobe.d/。例如我有以下内容/etc/modprobe.d/zfs.conf

# use minimum 8GB and maxmum of 16GB RAM for ZFS ARC
options zfs zfs_arc_min=8589934592 zfs_arc_max=17179869184

相关内容