X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=btrfsmaint;h=0204ed5aa78a4e9043e0aae3a624066252a77955;hb=72c18f3a6a7f1ed0ca16af654a1f804ab96e1ff9;hp=d2263257d412b4da2d9e37e5c1678641e301e8f6;hpb=103a2e153f844343c359882079936b2521c82f15;p=distro-setup diff --git a/btrfsmaint b/btrfsmaint index d226325..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() { @@ -109,26 +134,26 @@ main() { [[ $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 $stats; then tmp=$(mktemp) - # if mnt is /, avoid making a buggy looking path + # ${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 - exim -t <$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 <