improve ensureZfsMounted script

This commit is contained in:
Simon Gardling 2025-08-12 02:25:44 -07:00
parent ff305c8c4c
commit a2d622613d
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 25 additions and 16 deletions

View File

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

View File

@ -86,6 +86,15 @@ testPkgs.testers.runNixOSTest {
machine.succeed("zfsEnsureMounted /mnt/test_mountpoint /mnt/test_mountpoint_dos") machine.succeed("zfsEnsureMounted /mnt/test_mountpoint /mnt/test_mountpoint_dos")
machine.succeed("zfs create -o mountpoint='/mnt/test path with spaces' rpool/test3")
machine.succeed("zfsEnsureMounted '/mnt/test path with spaces'")
machine.succeed("echo 'ZFS output for escaped spaces:'; zfs list -o mountpoint,mounted -H | grep escaped")
machine.succeed("zfsEnsureMounted /mnt/test\\ escaped\\ spaces")
machine.succeed("zfsEnsureMounted /mnt/test_mountpoint '/mnt/test path with spaces' /mnt/test_mountpoint_dos")
machine.succeed("zfs create -o mountpoint=/mnt/foobar_data rpool/foobar") machine.succeed("zfs create -o mountpoint=/mnt/foobar_data rpool/foobar")
machine.succeed("systemctl start foobar") machine.succeed("systemctl start foobar")