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";
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
'';
};
}

View File

@ -86,6 +86,15 @@ testPkgs.testers.runNixOSTest {
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("systemctl start foobar")