From 89b9c8a416ed1f98c14961ca5e4884a99e1fde6c Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Sun, 12 Jun 2022 15:05:07 -0400 Subject: [PATCH] validate datasets exist before running - this allows for early exit as well as better error messaging --- CHANGES.md | 9 ++++++++- zfs-prune-snapshots | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index b3a1d5f..169fed5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,11 +6,18 @@ Not Yet Released (nothing yet) +`v1.5.0` +-------- + +- Sanity check datasets existence before running + - `-q` will hide warnings for datesets not existing + - Based on ([#9](https://github.com/bahamas10/zfs-prune-snapshots/pull/9)) + `v1.4.1` -------- - Allow snapshot datasets to contain spaces - ([#18](https://github.com/bahamas10/zfs-prune-snapshots/pull/18) + ([#18](https://github.com/bahamas10/zfs-prune-snapshots/pull/18)) `v1.4.0` -------- diff --git a/zfs-prune-snapshots b/zfs-prune-snapshots index 0d2e6aa..20a4d67 100755 --- a/zfs-prune-snapshots +++ b/zfs-prune-snapshots @@ -69,7 +69,7 @@ EOF } debug() { - ((verbosity >= 1)) && echo "$@" >&2 + ((verbosity >= 1)) && echo '>' "$@" >&2 return 0 } @@ -174,6 +174,7 @@ verbosity=0 prefix= suffix= invert=false +quiet=false while getopts 'hniqlRp:s:vV' option; do case "$option" in h) usage; exit 0;; @@ -182,7 +183,7 @@ while getopts 'hniqlRp:s:vV' option; do l) listonly=true;; p) prefix=$OPTARG;; s) suffix=$OPTARG;; - q) exec 1>/dev/null;; + q) quiet=true; exec 1>/dev/null;; R) recursive=true;; v) ((verbosity++));; V) echo "$VERSION"; exit 0;; @@ -221,13 +222,10 @@ fi shift -pools=("$@") destroyargs=() code=0 totalused=0 numsnapshots=0 -humanpools=${pools[*]} -humanpools=${humanpools:-} if $recursive; then destroyargs+=('-R') @@ -243,6 +241,36 @@ if ! command -v zfs &>/dev/null; then exit 1 fi +# validate pools given, print warnings if not in quiet mode +pools=() +for arg in "$@"; do + debug "checking '$arg' exists" + + error=$(zfs list "$arg" 2>&1 1>/dev/null) + code=$? + debug "zfs list '$arg' -> exited $code" + + if ((code == 0)) && [[ -z $error ]]; then + debug "adding dataset '$arg'" + pools+=("$arg") + else + msg="dataset '$arg' invalid: $error" + debug "$msg" + if ! $quiet; then + echo "$msg" >&2 + fi + fi +done + +# it is an error if arguments were given but all datasets were invalidated +if [[ -n $1 && -z ${pools[0]} ]]; then + echo 'no valid dataset names provided' >&2 + exit 1 +fi + +humanpools=${pools[*]} +humanpools=${humanpools:-} + # first pass of the pools (to calculate totals and filter unwanted datasets lines=() while read -r line; do