This repository was archived by the owner on Nov 8, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmseries-power-tune
More file actions
executable file
·173 lines (156 loc) · 6.37 KB
/
Copy pathmseries-power-tune
File metadata and controls
executable file
·173 lines (156 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
# Opt-in, reversible userspace power trims for the j314s (elementary OS 9, Aurora kernel).
#
# mseries-power-tune list items, what they do, whether they are applied
# mseries-power-tune status same as list, plus the live unit/sysfs state
# mseries-power-tune apply ITEM... apply items (root); prior state is saved first
# mseries-power-tune revert ITEM... restore exactly the saved prior state (root)
# mseries-power-tune revert-all revert every applied item
# add --dry-run before the command to print what would change without touching anything
#
# Scope rules (see mseries/power/OPTIMIZE-2026-10-18.md):
# * nothing here touches display (brightness, refresh, VRR, DPMS), audio (PipeWire, WirePlumber,
# speakersafetyd), the kernel, kernel parameters or the bootloader; a unit guard refuses those names;
# * every item records the previous state under $STATE before changing it, and `revert` restores it;
# * none of these is a measured watt saving; measure with power_ab.py before claiming one.
set -euo pipefail
shopt -s nullglob
SYS=${MSERIES_SYS:-/sys}
PROC=${MSERIES_PROC:-/proc}
STATE=${MSERIES_TUNE_STATE:-/var/lib/mseries/power-tune}
SYSTEMCTL=${MSERIES_SYSTEMCTL:-systemctl}
DRY=0
ITEMS=(cups-browsed rpcbind smartd dock-lan-wake)
describe() {
case $1 in
cups-browsed) echo "stop cups-browsed (network printer auto-discovery; no queues are configured)";;
rpcbind) echo "stop rpcbind service+socket (NFS/RPC portmapper; refused while an NFS mount exists)";;
smartd) echo "stop smartmontools (SMART polling every 30 min; loses disk-health warnings) [proposed only]";;
dock-lan-wake) echo "disable wake-from-sleep on USB Ethernet adapters (dock LAN), runtime only [sleep-only effect]";;
*) return 1;;
esac
}
die() { echo "mseries-power-tune: $*" >&2; exit 1; }
# Refuse anything that belongs to the display, audio or session stack, whatever an item says.
guard_unit() {
case $1 in
*pipewire*|*wireplumber*|*speakersafety*|*pulse*|*alsa*|*gala*|*gdm*|*lightdm*|*display*|*colord*|*geoclue*|*backlight*|*brightness*)
die "refusing to touch $1: display/audio/session units are out of scope";;
esac
}
run() {
if (( DRY )); then echo "would run: $*"; else "$@"; fi
}
unit_enabled() { "$SYSTEMCTL" is-enabled "$1" 2>/dev/null || true; }
unit_active() { "$SYSTEMCTL" is-active "$1" 2>/dev/null || true; }
save_units() { # item unit...
local item=$1 u f
shift
for u in "$@"; do guard_unit "$u"; done
f=$STATE/$item.prev
if (( DRY )); then echo "would save prior state of $* to $f"; return; fi
mkdir -p "$STATE"
: > "$f.tmp"
for u in "$@"; do printf 'unit %s %s %s\n' "$u" "$(unit_enabled "$u")" "$(unit_active "$u")" >> "$f.tmp"; done
mv "$f.tmp" "$f"
}
restore_units() { # item
local f=$STATE/$1.prev kind u en ac
[[ -f $f ]] || die "$1 is not applied (no $f)"
while read -r kind u en ac; do
[[ $kind == unit ]] || continue
guard_unit "$u"
[[ $en == enabled ]] && run "$SYSTEMCTL" enable "$u"
[[ $ac == active ]] && run "$SYSTEMCTL" start "$u"
done < "$f"
run rm -f "$f"
}
nfs_mounted() { grep -qE '[[:space:]]nfs4?[[:space:]]' "$PROC/mounts"; }
usb_lan_wakeups() {
local d
for d in "$SYS"/bus/usb/devices/*; do
[[ -f $d/power/wakeup ]] || continue
# USB interfaces bound to an Ethernet driver (r8152, ax88179_178a, cdc_ether) mark their parent device.
if compgen -G "$d/*:*/net/*" >/dev/null; then echo "$d"; fi
done
}
apply_item() {
local u
describe "$1" >/dev/null || die "unknown item $1 (try: list)"
# Idempotent: applying an applied item keeps the first saved prior state (A/B arms re-apply).
if [[ -f $STATE/$1.prev ]]; then echo "$1 already applied"; return 0; fi
case $1 in
cups-browsed)
save_units "$1" cups-browsed.service
run "$SYSTEMCTL" disable --now cups-browsed.service;;
rpcbind)
nfs_mounted && die "an NFS filesystem is mounted; rpcbind stays"
save_units "$1" rpcbind.socket rpcbind.service
run "$SYSTEMCTL" disable --now rpcbind.socket rpcbind.service;;
smartd)
save_units "$1" smartmontools.service
run "$SYSTEMCTL" disable --now smartmontools.service;;
dock-lan-wake)
local f=$STATE/$1.prev
if (( DRY )); then
for u in $(usb_lan_wakeups); do echo "would write disabled to $u/power/wakeup (was $(cat "$u/power/wakeup"))"; done
return
fi
mkdir -p "$STATE"; : > "$f.tmp"
for u in $(usb_lan_wakeups); do
printf 'wakeup %s %s\n' "$u" "$(cat "$u/power/wakeup")" >> "$f.tmp"
printf disabled > "$u/power/wakeup"
done
mv "$f.tmp" "$f";;
*) die "unknown item $1 (try: list)";;
esac
(( DRY )) || echo "applied $1"
}
revert_item() {
local f=$STATE/$1.prev kind path val
describe "$1" >/dev/null || die "unknown item $1"
[[ -f $f ]] || die "$1 is not applied (no $f)"
if grep -q '^wakeup ' "$f"; then
while read -r kind path val; do
[[ $kind == wakeup && -f $path/power/wakeup ]] || continue
if (( DRY )); then echo "would write $val to $path/power/wakeup"; else printf '%s' "$val" > "$path/power/wakeup"; fi
done < "$f"
run rm -f "$f"
else
restore_units "$1"
fi
(( DRY )) || echo "reverted $1"
}
show() {
local i mark
for i in "${ITEMS[@]}"; do
if [[ -f $STATE/$i.prev ]]; then mark=applied; else mark=-; fi
printf '%-14s %-8s %s\n' "$i" "$mark" "$(describe "$i")"
done
}
status() {
local u d
show
echo "--- live state"
for u in cups.service cups-browsed.service rpcbind.service rpcbind.socket smartmontools.service; do
printf '%-22s %-10s %s\n' "$u" "$(unit_enabled "$u")" "$(unit_active "$u")"
done
for d in $(usb_lan_wakeups); do printf 'usb-lan %s wakeup=%s\n' "${d##*/}" "$(cat "$d/power/wakeup")"; done
}
need_root() {
(( DRY )) && return 0
[[ ${MSERIES_TUNE_ALLOW_NONROOT:-0} == 1 ]] && return 0
(( EUID == 0 )) || die "needs root (try sudo), or use --dry-run"
}
[[ ${1:-} == --dry-run ]] && { DRY=1; shift; }
cmd=${1:-list}
shift || true
case $cmd in
list) show;;
status) status;;
apply) need_root; (( $# )) || die "apply which item?"; for i in "$@"; do apply_item "$i"; done;;
revert) need_root; (( $# )) || die "revert which item?"; for i in "$@"; do revert_item "$i"; done;;
revert-all) need_root; for i in "${ITEMS[@]}"; do [[ -f $STATE/$i.prev ]] && revert_item "$i"; done; true;;
-h|--help|help) sed -n '2,16p' "$0";;
*) die "unknown command $cmd";;
esac