From c789d113c623e2d162fbb19a87c29ba4518396c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 3 Sep 2024 13:45:10 +0200 Subject: [PATCH 1/3] disk-deactivate: only run zfs command if they are present --- disk-deactivate/disk-deactivate.jq | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/disk-deactivate/disk-deactivate.jq b/disk-deactivate/disk-deactivate.jq index 6e444039..b7c502dc 100644 --- a/disk-deactivate/disk-deactivate.jq +++ b/disk-deactivate/disk-deactivate.jq @@ -1,7 +1,7 @@ # since lsblk lacks zfs support, we have to do it this way def remove: if .fstype == "zfs_member" then - "zpool destroy -f \(.label)" + "if type zpool >/dev/null; then zpool destroy -f \(.label); fi" elif .fstype == "LVM2_member" then [ "vg=$(pvs \(.path) --noheadings --options vg_name | grep -o '[a-zA-Z0-9-]*')", @@ -14,7 +14,7 @@ def remove: # maybe its zfs [ # the next line has some horrible escaping - "zpool=$(zdb -l \(.path) | sed -nr $'s/ +name: \\'(.*)\\'/\\\\1/p')", + "zpool=$(if type zdb >/dev/null; then zdb -l \(.path) | sed -nr $'s/ +name: \\'(.*)\\'/\\\\1/p') else echo ""; fi", "if [[ -n \"${zpool}\" ]]; then zpool destroy -f \"$zpool\"; fi", "unset zpool" ] From 736c8150b93f413ff976b80e3e4d7970f408a4f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 3 Sep 2024 15:44:26 +0200 Subject: [PATCH 2/3] disk-deactivate: also clear zpool labels this helps to avoid ghost pools. --- disk-deactivate/disk-deactivate.jq | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/disk-deactivate/disk-deactivate.jq b/disk-deactivate/disk-deactivate.jq index b7c502dc..cf6e9430 100644 --- a/disk-deactivate/disk-deactivate.jq +++ b/disk-deactivate/disk-deactivate.jq @@ -1,7 +1,7 @@ # since lsblk lacks zfs support, we have to do it this way def remove: if .fstype == "zfs_member" then - "if type zpool >/dev/null; then zpool destroy -f \(.label); fi" + "if type zpool >/dev/null; then zpool destroy -f \(.label); zpool labelclear -f \(.label); fi" elif .fstype == "LVM2_member" then [ "vg=$(pvs \(.path) --noheadings --options vg_name | grep -o '[a-zA-Z0-9-]*')", @@ -14,8 +14,8 @@ def remove: # maybe its zfs [ # the next line has some horrible escaping - "zpool=$(if type zdb >/dev/null; then zdb -l \(.path) | sed -nr $'s/ +name: \\'(.*)\\'/\\\\1/p') else echo ""; fi", - "if [[ -n \"${zpool}\" ]]; then zpool destroy -f \"$zpool\"; fi", + "zpool=$(if type zdb >/dev/null; then zdb -l \(.path) | sed -nr $'s/ +name: \\'(.*)\\'/\\\\1/p'; fi)", + "if [[ -n \"${zpool}\" ]]; then zpool destroy -f \"$zpool\"; zpool labelclear -f \"$zpool\"; fi", "unset zpool" ] else From 8f0f75f1a8a6764d52be25d2d2e89866b25502b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 3 Sep 2024 16:52:04 +0200 Subject: [PATCH 3/3] zpool: better error message if zpool has no assigned devices --- lib/types/zpool.nix | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/types/zpool.nix b/lib/types/zpool.nix index 0510b940..df391f2b 100644 --- a/lib/types/zpool.nix +++ b/lib/types/zpool.nix @@ -148,6 +148,10 @@ in in '' readarray -t zfs_devices < <(cat "$disko_devices_dir"/zfs_${config.name}) + if [ ''${#zfs_devices[@]} -eq 0 ]; then + echo "no devices found for zpool ${config.name}. Did you misspell the pool name?" >&2 + exit 1 + fi # Try importing the pool without mounting anything if it exists. # This allows us to set mounpoints. if zpool import -N -f '${config.name}' || zpool list '${config.name}'; then