various fixes, a few new features
[distro-setup] / btrfsmaint
1 #!/bin/bash
2
3
4 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
5
6 set -e; . /usr/local/lib/err; set +e
7
8 # inspired from
9 # https://github.com/kdave/btrfsmaintenance
10
11 if [[ $INVOCATION_ID ]]; then
12 err-cleanup() {
13 exim -odf -i root <<EOF
14 From: root@$(hostname -f)
15 To: root@$(hostname -f)
16 Subject: btrfsmaint automatically exited on command error
17
18 journalctl -u btrfsmaint -n 50:
19 $(journalctl -u btrfsmaint -n 50)
20 EOF
21 }
22 fi
23
24 dusage="5 10"
25 musage="5"
26
27 e() {
28 echo "btrfsmaint: $*"
29 if ! $dryrun; then
30 "$@"
31 fi
32 }
33
34 check-idle() {
35 type -p xscreensaver-command &>/dev/null || return 0
36 export DISPLAY=:0
37 locked=false
38 if lock_info=$(xscreensaver-command -timer); then
39 if [[ $lock_info != *non-blanked* ]]; then
40 locked=true
41 fi
42 else
43 locked=true
44 fi
45 }
46
47 usage() {
48 cat <<EOF
49 Usage: ${0##*/} [OPTIONS]
50 Do btrfs maintence or stop if we have X and xprintidle shows a user
51
52 Normally, no options are needed.
53
54 --check Only check if an existing maintence should be cancelled due to
55 nonidle user and run in a loop every 20 seconds for 10
56 minutes.
57
58 --dryrun Just print out what we would do.
59
60 --force Run regardless of user idle status on all disks and do scrub
61 regardless of when it was last run.
62 --no-stats Avoid checking error statistics. Use this to avoid a rare race
63 condition when running --check concurrently with normal run.
64
65
66 -h|--help Show help
67
68 Note: Uses util-linux getopt option parsing: spaces between args and
69 options, short options can be combined, options before args.
70 EOF
71 exit $1
72 }
73
74 ##### begin command line parsing ########
75
76 # ensure we can handle args with spaces or empty.
77 ret=0; getopt -T || ret=$?
78 [[ $ret == 4 ]] || { echo "Install util-linux for enhanced getopt" >&2; exit 1; }
79
80 check=false
81 dryrun=false
82 force=false
83 stats=true
84
85 temp=$(getopt -l help,check,dryrun,force,no-stats h "$@") || usage 1
86 eval set -- "$temp"
87 while true; do
88 case $1 in
89 --check) check=true ;;
90 --dryrun) dryrun=true ;;
91 --force) force=true ;;
92 --no-stats) stats=false ;;
93 -h|--help) usage ;;
94 --) shift; break ;;
95 *) echo "$0: unexpected args: $*" >&2 ; usage 1 ;;
96 esac
97 shift
98 done
99 readonly check dryrun force stats
100 ##### end command line parsing ########
101
102
103 main() {
104 if ! $force; then
105 check-idle
106 if ! $check; then
107 min=0
108 max_min=300
109 # When the cron kicks in, we may not be idle (physically sleeping) yet, so
110 # wait.
111 while ! $locked && (( min < max_min )); do
112 min=$(( min + 1 ))
113 sleep 60
114 check-idle
115 done
116 # If we've waited a really long time for idle, just give up.
117 if (( min == max_min )); then
118 return
119 fi
120 fi
121 fi
122
123
124 fnd="findmnt --types btrfs --noheading"
125 for x in $($fnd --output "SOURCE" --nofsroot | sort -u); do
126 mnt=$($fnd --output "TARGET" --first-only --source $x)
127 [[ $mnt ]] || continue
128
129 #### begin look for diff in stats, eg: increasing error count ####
130 if $stats; then
131 tmp=$(mktemp)
132 # ${mnt%/} so that if mnt is / we avoid making a buggy looking path
133 stats_path=${mnt%/}/btrfs-dev-stats
134 if [[ ! -e $stats_path ]]; then
135 btrfs dev stats -c $mnt >$stats_path ||: # populate initial reading
136 elif ! btrfs dev stats -c $mnt >$tmp; then
137 if ! diff -q $stats_path $tmp; then
138 mv $stats_path $stats_path.1
139 cat $tmp >$stats_path
140 diff=$(diff -u $stats_path $tmp 2>&1 ||:)
141 printf "diff of: btrfs dev stats -c %s\n%s\n" "$mnt" "$diff"
142 exim -odf -i root <<EOF
143 From: root@$(hostname -f)
144 To: root@$(hostname -f)
145 Subject: btrfsmaint: device stats changed for $mnt
146
147 diff of: btrfs dev stats -c $mnt
148 $diff
149 EOF
150 fi
151 fi
152 rm -f $tmp
153 fi
154 #### end look for diff in stats, eg: increasing error count ####
155
156 if $check; then
157 if ! $locked; then
158 if $dryrun; then
159 echo "$0: not idle. if this wasnt a dry run, btrfs scrub cancel $mnt"
160 else
161 btrfs scrub cancel $mnt &>/dev/null ||:
162 fi
163 fi
164 continue
165 fi
166
167 # for comparing before and after balance.
168 # the log is already fairly verbose, so commented.
169 # e btrfs filesystem df $mnt
170 # e df -H $mnt
171 if btrfs filesystem df $mnt | grep -q "Data+Metadata"; then
172 for usage in $dusage; do
173 e ionice -c 3 btrfs balance start -dusage=$usage -musage=$usage $mnt
174 done
175 else
176 e ionice -c 3 btrfs balance start -dusage=0 $mnt
177 for usage in $dusage; do
178 e ionice -c 3 btrfs balance start -dusage=$usage $mnt
179 done
180 e ionice -c 3 btrfs balance start -musage=0 $mnt
181 for usage in $musage; do
182 e ionice -c 3 btrfs balance start -musage=$usage $mnt
183 done
184 fi
185 date=
186 scrub_status=$(btrfs scrub status $mnt)
187 if printf "%s\n" "$scrub_status" | grep -i '^status:[[:space:]]*finished$' &>/dev/null; then
188 date=$(printf "%s\n" "$scrub_status" | sed -rn 's/^Scrub started:[[:space:]]*(.*)/\1/p')
189 fi
190 if [[ ! $date ]]; then
191 # output from older versions, at least btrfs v4.15.1
192 date=$(
193 printf "%s\n" "$scrub_status" | \
194 sed -rn 's/^\s*scrub started at (.*) and finished.*/\1/p'
195 )
196 fi
197 if ! $force && [[ $date ]]; then
198 if $dryrun; then
199 echo "$0: last scrub finish for $mnt: $date"
200 fi
201 date=$(date --date="$date" +%s)
202 # if date is sooner than 60 days ago
203 # the wiki recommends 30 days or so, but
204 # I'm going with 60 days.
205 if (( date > EPOCHSECONDS - 60*60*24*60 )); then
206 if $dryrun; then
207 echo "$0: skiping scrub of $mnt, last was $(( (EPOCHSECONDS - date) / 60/60/24 )) days ago, < 30 days"
208 fi
209 continue
210 fi
211 fi
212 # btrfsmaintenance does -c 2 -n 4, but I want lowest pri.
213 e btrfs scrub start -Bd -c 3 $mnt
214
215 # We normally only do one disk since this is meant to be run in
216 # downtime and if we try to do all disks, we invariably end up doing
217 # a scrub after downtime. So, just do one disk per day.
218 if ! $force; then
219 return 0
220 fi
221 done
222 }
223
224 loop-main() {
225 while true; do
226 main
227 sleep 60
228 done
229 }
230
231 if $check; then
232 loop-main
233 else
234 main
235 fi