improve ensureZfsMounted script

This commit is contained in:
2025-08-12 02:25:44 -07:00
parent ff305c8c4c
commit a2d622613d
2 changed files with 25 additions and 16 deletions

View File

@@ -3,31 +3,31 @@ final: prev: {
name = "zfsEnsureMounted";
runtimeInputs = with prev; [
zfs
gnugrep
gawk
coreutils
];
text = ''
#!/bin/sh -x
#!/bin/sh
if [[ "$#" -eq "0" ]]; then
echo "no arguments passed"
exit 1
fi
if [[ "$#" -eq "0" ]]; then
echo "no arguments passed"
exit 1
fi
TARGETS=$(echo "$@" | sort | uniq)
MOUNTED=$(zfs list -o mountpoint,mounted -H | awk '$2 == "yes" {print $1}' | sort | uniq)
NUM_MATCHED=$(echo "$MOUNTED" | grep -Ec "$(echo "$@" | tr ' ' '\|')") # does not properly handle paths with strings
if [[ "$NUM_MATCHED" -eq "$#" ]]; then
exit 0
fi
MOUNTED=$(zfs list -o mountpoint,mounted -H | awk '$NF == "yes" {$NF=""; print $0}' | sed 's/[[:space:]]*$//')
FOUND=$(printf "%s\n%s" "$TARGETS" "$MOUNTED" | sort | uniq -c | awk '$1 == "2" {print $2}' | sort)
MISSING=$(printf "%s\n%s" "$FOUND" "$TARGETS" | sort | uniq -u | sort)
MISSING=""
for target in "$@"; do
if ! echo "$MOUNTED" | grep -Fxq "$target"; then
MISSING="$MISSING $target"
fi
done
echo "FAILURE, missing: $MISSING" 1>&2
exit 1
if [[ -n "$MISSING" ]]; then
echo "FAILURE, missing:$MISSING" 1>&2
exit 1
fi
'';
};
}