4 [[ $EUID == 0 ]] ||
exec sudo
-E "${BASH_SOURCE[0]}" "$@"
6 source /a
/bin
/errhandle
/err
9 # https://github.com/kdave/btrfsmaintenance
12 # Man page says we could also use a range, i suppose it would be
13 # logical to use a pattern like 5..10 10..20,
14 # but I don't know if this would help us at all.
18 e
() { echo "cron: $*"; "$@"; }
21 type -p xprintidle
&>/dev
/null ||
return 0
23 # a hours, a movie could run that long.
24 idle_limit
=$
((1000 * 60 * 60 * 2))
26 while read -r user
; do
27 new_idle_time
=$
(sudo
-u $user xprintidle
2>/dev
/null
) ||
:
28 if [[ $new_idle_time && $new_idle_time -lt $idle_time ]]; then
29 idle_time
=$new_idle_time
31 done < <(users |
tr " " "\n" |
sort -u)
32 if (( idle_time
< idle_limit
)); then
43 Do btrfs maintence or stop if xprintidle shows a user
45 force Run regardless of user idle status
46 check Only check if an existing maintence should be cancelled due to
47 nonidle user. Also, runs in a loop every 20 seconds for 10
50 Note: Uses util-linux getopt option parsing: spaces between args and
51 options, short options can be combined, options before args.
68 echo "$0: error: unexpected arg" >&2
83 fnd
="findmnt --types btrfs --noheading"
84 for x
in $
($fnd --output "SOURCE" --nofsroot |
sort -u); do
85 mnt
=$
($fnd --output "TARGET" --first-only --source $x)
86 [[ $mnt ]] ||
continue
88 if ! btrfs dev stats
-c $mnt >$tmp; then
89 if diff -q $mnt/btrfs-dev-stats
$tmp; then
90 diff -u $mnt/btrfs-dev-stats
$tmp |
mail -s "$HOSTNAME: error: btrfs dev stats -c $mnt" root@localhost
91 cat $tmp >$mnt/btrfs-dev-stats
96 btrfs scrub cancel
$mnt &>/dev
/null ||
:
103 # for comparing before and after balance.
104 # the log is already fairly verbose, so commented.
105 # e btrfs filesystem df $mnt
107 if btrfs filesystem df
$mnt |
grep -q "Data+Metadata"; then
108 for usage
in $dusage; do
109 e ionice
-c 3 btrfs balance start
-dusage=$usage -musage=$usage $mnt
112 e ionice
-c 3 btrfs balance start
-dusage=0 $mnt
113 for usage
in $dusage; do
114 e ionice
-c 3 btrfs balance start
-dusage=$usage $mnt
116 e ionice
-c 3 btrfs balance start
-musage=0 $mnt
117 for usage
in $musage; do
118 e ionice
-c 3 btrfs balance start
-musage=$usage $mnt
121 # e btrfs filesystem df $mnt
124 btrfs scrub status
$mnt | \
125 sed -rn 's/^\s*scrub started at (.*) and finished.*/\1/p'
128 date=$
(date --date="$date" +%s
)
129 # if date is sooner than 90 days ago
130 # the wiki recommends 30 days or so, but
131 # it makes the comp lag like shit for a day,
132 # so I'm going with 90 days.
133 if (( date > $
(date +%s
) - 60*60*24*30 )); then
134 echo "cron: skiping scrub of $mnt"
138 # -c 2 -n 4 is from btrfsmaintenance, does ionice
139 e btrfs scrub start
-Bd -c 2 -n 4 $mnt