This repository was archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdparmify.in
executable file
·193 lines (161 loc) · 4 KB
/
hdparmify.in
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
# This project is licensed under the MIT License (see LICENSE).
set -eu
shopt -s nullglob
readonly VERSION=@VERSION
readonly rundir="${HDPARMIFY_RUNDIR:-/run/hdparmify}"
config='/etc/hdparmify.conf'
device=''
try=false
now=false
usage() {
echo 'usage: hdparmify [options] [command]
options:
-h Show help message
-c CONFIG Path to config
-d DEVICE Device to affect
-t Exit successfully if device not in config
-n Ignore delay options
-v Show version
commands:
apply Affect devices
reset Reset state
reapply Reset state and apply
restore Restore devices to default state
status Print status'
}
status() {
local dev
local bold
for f in "$rundir"/*; do
dev="$(iniq "$f")"
[[ -t 1 ]] && bold="\033[1m$dev\033[0m"
echo -e "${bold-$dev} - $(date -r "$f")"
iniq -p "$dev" -f ' %k=%v' "$f"
done
exit
}
get_state() {
for f in "$rundir"/*; do
if [[ "$(iniq "$f")" == "$1" ]]; then
echo "$f"
return
fi
done
return 1
}
apply() {
local dev="$1"
local args=''
local opts
local delay
local state
get_state "$dev" > /dev/null && return 1
if ! opts="$(iniq -q -p "$dev" -f '%k=%v' "$config")"; then
$try && exit
echo "$dev not in $config" >&2
exit 1
fi
while IFS='=' read -r k v; do
case "$k" in
power_management) args+=" -B $v" ;;
acoustic_management) args+=" -M $v" ;;
standby_timeout) args+=" -S $v" ;;
power_mode)
case "$v" in
standby) args+=" -y" ;;
sleep) args+=" -Y" ;;
*)
echo "Invalid power_mode value: $v" >&2
exit 1
esac
;;
delay) delay="$v" ;;
*)
echo "Unknown option: $k" >&2
exit 1
esac
done <<< "$opts"
(
! $now && [[ -n "${delay-}" ]] && sleep "$delay"
if hdparm $args "$dev"; then
# replace all / with - in device path
state="${dev//\//-}"
[[ ! -d "$rundir" ]] && mkdir -p "$rundir"
# exclude the delay option from saved state
echo -e "[$dev]\n$opts" | sed '/delay=/d' > "$rundir/${state:1}"
fi
) &
}
restore() {
local dev="$1"
local state="$2"
local restored=false
while IFS='=' read -r k v; do
case "$k" in
standby_timeout)
hdparm -S 0 "$dev"
restored=true
;;
esac
done < <(iniq -p "$dev" "$state")
$restored && rm "$state"
}
while getopts ':hc:d:tnv' opt; do
case "$opt" in
h) usage; exit ;;
c) config="$OPTARG" ;;
d) device="$OPTARG" ;;
t) try=true ;;
n) now=true ;;
v) echo "$VERSION"; exit ;;
*) usage >&2; exit 2
esac
done
shift $((OPTIND - 1))
[[ $# -eq 0 ]] && status
cmd_apply() {
local pids=''
local r=0
if [[ -n "$device" ]]; then
apply "$device" && pids+=" $!"
else
# re devs: failed subshell in for loop did not trigger `set -e`
devs="$(iniq "$config")"
for d in $devs; do
apply "$d" && pids+=" $!"
done
fi
for pid in $pids; do
wait "$pid" || (( r+=1 ))
done
exit $r
}
cmd_reset() {
local r=1
if [[ -n "$device" ]]; then
rm "$(get_state "$device")" && r=0
else
for f in "$rundir"/*; do
rm "$f" && r=0
done
fi
return $r
}
cmd_restore() {
if [[ -n "$device" ]]; then
restore "$dev" "$(get_state "$device")"
else
for f in "$rundir"/*; do
restore "$(iniq "$f")" "$f"
done
fi
}
case "$1" in
apply) cmd_apply ;;
reset) cmd_reset || exit $? ;;
reapply) cmd_reset; cmd_apply ;;
restore) cmd_restore ;;
status) status ;;
*) usage >&2; exit 2
esac