7b2dabe78f1b3915fff3310820b0bb96b7f1609f
4 [[ $EUID == 0 ]] ||
exec sudo
-E "${BASH_SOURCE[0]}" "$@"
6 set -e; .
/usr
/local
/lib
/err
; 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
38 if lock_info
=$
(xscreensaver-command
-timer); then
39 if [[ $lock_info != *non-blanked
* ]]; then
49 Usage: ${0##*/} [OPTIONS]
50 Do btrfs maintence or stop if we have X and xprintidle shows a user
52 Normally, no options are needed.
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
58 --dryrun Just print out what we would do.
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.
68 Note: Uses util-linux getopt option parsing: spaces between args and
69 options, short options can be combined, options before args.
74 ##### begin command line parsing ########
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; }
85 temp
=$
(getopt
-l help,check
,dryrun
,force
,no-stats h
"$@") || usage
1
89 --check) check
=true
;;
90 --dryrun) dryrun
=true
;;
91 --force) force
=true
;;
92 --no-stats) stats
=false
;;
95 *) echo "$0: unexpected args: $*" >&2 ; usage
1 ;;
99 readonly check dryrun force stats
100 ##### end command line parsing ########
109 # When the cron kicks in, we may not be idle (physically sleeping) yet, so
111 while ! $locked && (( min
< max_min
)); do
116 # If we've waited a really long time for idle, just give up.
117 if (( min
== max_min
)); then
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
129 #### begin look for diff in stats, eg: increasing error count ####
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
147 diff of: btrfs dev stats -c $mnt
154 #### end look for diff in stats, eg: increasing error count ####
159 echo "$0: not idle. if this wasnt a dry run, btrfs scrub cancel $mnt"
161 btrfs scrub cancel
$mnt &>/dev
/null ||
:
167 # for comparing before and after balance.
168 # the log is already fairly verbose, so commented.
169 # e btrfs filesystem df $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
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
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
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')
190 if [[ ! $date ]]; then
191 # output from older versions, at least btrfs v4.15.1
193 printf "%s\n" "$scrub_status" | \
194 sed -rn 's/^\s*scrub started at (.*) and finished.*/\1/p'
197 if ! $force && [[ $date ]]; then
199 echo "$0: last scrub finish for $mnt: $date"
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
207 echo "$0: skiping scrub of $mnt, last was $(( (EPOCHSECONDS - date) / 60/60/24 )) days ago, < 30 days"
212 # btrfsmaintenance does -c 2 -n 4, but I want lowest pri.
213 e btrfs scrub start
-Bd -c 3 $mnt
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.