X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=btrfsmaint;h=0204ed5aa78a4e9043e0aae3a624066252a77955;hb=72c18f3a6a7f1ed0ca16af654a1f804ab96e1ff9;hp=33cae4a36280351c170155f11fc83ba8ca3b2102;hpb=ea108a03dfa2d7f73447c0b14210d766e5ee5d9b;p=distro-setup diff --git a/btrfsmaint b/btrfsmaint index 33cae4a..0204ed5 100755 --- a/btrfsmaint +++ b/btrfsmaint @@ -3,20 +3,29 @@ [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@" -source /a/bin/errhandle/err +set -e; . /usr/local/lib/err; set +e # inspired from # https://github.com/kdave/btrfsmaintenance +if [[ $INVOCATION_ID ]]; then + err-cleanup() { + exim -odf -i root <&2; exit 1; } -force=false check=false dryrun=false -if [[ $1 ]]; then +force=false +stats=true + +temp=$(getopt -l help,check,dryrun,force,no-stats h "$@") || usage 1 +eval set -- "$temp" +while true; do case $1 in - check) - check=true - ;; - force) - force=true - ;; - dryrun) - dryrun=true - ;; - *) - echo "$0: error: unexpected arg" >&2 - usage 1 - ;; + --check) check=true ;; + --dryrun) dryrun=true ;; + --force) force=true ;; + --no-stats) stats=false ;; + -h|--help) usage ;; + --) shift; break ;; + *) echo "$0: unexpected args: $*" >&2 ; usage 1 ;; esac -fi + shift +done +readonly check dryrun force stats +##### end command line parsing ######## main() { @@ -102,21 +128,32 @@ main() { fi - tmp=$(mktemp) fnd="findmnt --types btrfs --noheading" for x in $($fnd --output "SOURCE" --nofsroot | sort -u); do mnt=$($fnd --output "TARGET" --first-only --source $x) [[ $mnt ]] || continue #### begin look for diff in stats, eg: increasing error count #### - - # Only run for $check, since it runs in parallel to non-check, avoid - # race condition. - if $check; then - if ! btrfs dev stats -c $mnt >$tmp; then - if diff -q $mnt/btrfs-dev-stats $tmp; then - diff -u $mnt/btrfs-dev-stats $tmp | mail -s "$HOSTNAME: error: btrfs dev stats -c $mnt" root@localhost - cat $tmp >$mnt/btrfs-dev-stats + if $stats; then + tmp=$(mktemp) + # ${mnt%/} so that if mnt is / we avoid making a buggy looking path + stats_path=${mnt%/}/btrfs-dev-stats + if [[ ! -e $stats_path ]]; then + btrfs dev stats -c $mnt >$stats_path ||: # populate initial reading + elif ! btrfs dev stats -c $mnt >$tmp; then + if ! diff -q $stats_path $tmp; then + mv $stats_path $stats_path.1 + cat $tmp >$stats_path + diff=$(diff -u $stats_path $tmp 2>&1 ||:) + printf "diff of: btrfs dev stats -c %s\n%s\n" "$mnt" "$diff" + exim -odf -i root <