4 [[ $EUID == 0 ]] ||
exec sudo
-E "${BASH_SOURCE[0]}" "$@"
6 set -e; .
/usr
/local
/lib
/bash-bear
; set +e
9 # https://github.com/kdave/btrfsmaintenance
11 if [[ $INVOCATION_ID ]]; then
13 exim
-odf -i root
<<EOF
14 From: root@$(hostname -f)
15 To: root@$(hostname -f)
16 Subject: btrfsmaint automatically exited on command error
18 journalctl -u btrfsmaint -n 50:
19 $(journalctl -u btrfsmaint -n 50)
35 type -p xscreensaver-command
&>/dev
/null ||
return 0
36 export XAUTHORITY
=/home
/iank
/.Xauthority
39 if lock_info
=$
(xscreensaver-command
-time); then
40 if [[ $lock_info != *non-blanked
* ]]; then
50 Usage: ${0##*/} [OPTIONS]
51 Do btrfs maintence or stop if we have X and xprintidle shows a user
53 Normally, no options are needed.
55 --check Only check if an existing maintence should be cancelled due to
56 nonidle user and run in a loop every 20 seconds for 10
59 --dryrun Just print out what we would do.
61 --force Run regardless of user idle status on all disks and do scrub
62 regardless of when it was last run.
63 --no-stats Avoid checking error statistics. Use this to avoid a rare race
64 condition when running --check concurrently with normal run.
69 Note: Uses util-linux getopt option parsing: spaces between args and
70 options, short options can be combined, options before args.
75 ##### begin command line parsing ########
77 # ensure we can handle args with spaces or empty.
78 ret
=0; getopt
-T || ret
=$?
79 [[ $ret == 4 ]] ||
{ echo "Install util-linux for enhanced getopt" >&2; exit 1; }
86 temp
=$
(getopt
-l help,check
,dryrun
,force
,no-stats h
"$@") || usage
1
90 --check) check
=true
;;
91 --dryrun) dryrun
=true
;;
92 --force) force
=true
;;
93 --no-stats) stats
=false
;;
96 *) echo "$0: unexpected args: $*" >&2 ; usage
1 ;;
100 readonly check dryrun force stats
101 ##### end command line parsing ########
110 # When the cron kicks in, we may not be idle (physically sleeping) yet, so
112 while ! $locked && (( min
< max_min
)); do
117 # If we've waited a really long time for idle, just give up.
118 if (( min
== max_min
)); then
125 fnd
="findmnt --types btrfs --noheading"
126 for x
in $
($fnd --output "SOURCE" --nofsroot |
sort -u); do
127 mnt
=$
($fnd --output "TARGET" --first-only --source $x)
128 [[ $mnt ]] ||
continue
130 #### begin look for diff in stats, eg: increasing error count ####
133 # ${mnt%/} so that if mnt is / we avoid making a buggy looking path
134 stats_path
=${mnt%/}/btrfs-dev-stats
135 if [[ ! -e $stats_path ]]; then
136 btrfs dev stats
-c $mnt >$stats_path ||
: # populate initial reading
137 elif ! btrfs dev stats
-c $mnt >$tmp; then
138 if ! diff -q $stats_path $tmp; then
139 mv $stats_path $stats_path.1
140 cat $tmp >$stats_path
141 diff=$
(diff -u $stats_path $tmp 2>&1 ||
:)
142 printf "diff of: btrfs dev stats -c %s\n%s\n" "$mnt" "$diff"
143 exim
-odf -i root
<<EOF
144 From: root@$(hostname -f)
145 To: root@$(hostname -f)
146 Subject: btrfsmaint: device stats changed for $mnt
148 diff of: btrfs dev stats -c $mnt
155 #### end look for diff in stats, eg: increasing error count ####
160 echo "$0: not idle. if this wasnt a dry run, btrfs scrub cancel $mnt"
162 btrfs scrub cancel
$mnt &>/dev
/null ||
:
168 # for comparing before and after balance.
169 # the log is already fairly verbose, so commented.
170 # e btrfs filesystem df $mnt
172 if btrfs filesystem df
$mnt |
grep -q "Data+Metadata"; then
173 for usage
in $dusage; do
174 e ionice
-c 3 btrfs balance start
-dusage=$usage -musage=$usage $mnt
177 e ionice
-c 3 btrfs balance start
-dusage=0 $mnt
178 for usage
in $dusage; do
179 e ionice
-c 3 btrfs balance start
-dusage=$usage $mnt
181 e ionice
-c 3 btrfs balance start
-musage=0 $mnt
182 for usage
in $musage; do
183 e ionice
-c 3 btrfs balance start
-musage=$usage $mnt
187 scrub_status
=$
(btrfs scrub status
$mnt)
188 if printf "%s\n" "$scrub_status" |
grep -i '^status:[[:space:]]*finished$' &>/dev
/null
; then
189 date=$
(printf "%s\n" "$scrub_status" |
sed -rn 's/^Scrub started:[[:space:]]*(.*)/\1/p')
191 if [[ ! $date ]]; then
192 # output from older versions, at least btrfs v4.15.1
194 printf "%s\n" "$scrub_status" | \
195 sed -rn 's/^\s*scrub started at (.*) and finished.*/\1/p'
198 if ! $force && [[ $date ]]; then
200 echo "$0: last scrub finish for $mnt: $date"
202 date=$
(date --date="$date" +%s
)
203 # if date is sooner than 60 days ago
204 # the wiki recommends 30 days or so, but
205 # I'm going with 60 days.
206 if (( date > EPOCHSECONDS
- 60*60*24*60 )); then
208 echo "$0: skiping scrub of $mnt, last was $(( (EPOCHSECONDS - date) / 60/60/24 )) days ago, < 30 days"
213 # btrfsmaintenance does -c 2 -n 4, but I want lowest pri.
214 e btrfs scrub start
-Bd -c 3 $mnt
216 # We normally only do one disk since this is meant to be run in
217 # downtime and if we try to do all disks, we invariably end up doing
218 # a scrub after downtime. So, just do one disk per day.