summaryrefslogtreecommitdiffstats
path: root/disk-deactivate
diff options
context:
space:
mode:
authorJörg Thalheim <Mic92@users.noreply.github.com>2022-12-25 18:25:37 +0000
committerGitHub <noreply@github.com>2022-12-25 18:25:37 +0000
commite3527b826d9ce94855cd0f2716c46cef76a4219a (patch)
treed63f70120437bb4fdcc4d0b656c674e533c6abea /disk-deactivate
parent11d5c74c30cec9783d8281a3af51d269ac7f3eab (diff)
parent164ea97c01ae57ecb76153ef43be4a184aa5d33a (diff)
Merge pull request #75 from nix-community/zap2HEADmaster
zap only devices we care about
Diffstat (limited to 'disk-deactivate')
-rwxr-xr-xdisk-deactivate/disk-deactivate6
-rw-r--r--disk-deactivate/disk-deactivate.jq77
2 files changed, 83 insertions, 0 deletions
diff --git a/disk-deactivate/disk-deactivate b/disk-deactivate/disk-deactivate
new file mode 100755
index 0000000..7dcd753
--- /dev/null
+++ b/disk-deactivate/disk-deactivate
@@ -0,0 +1,6 @@
+#!/bin/sh
+set -efux
+# dependencies: jq util-linux lvm2 mdadm zfs
+disk=$1
+
+lsblk --output-all --json | jq -r --arg disk_to_clear "$disk" -f "$(dirname $0)/disk-deactivate.jq"
diff --git a/disk-deactivate/disk-deactivate.jq b/disk-deactivate/disk-deactivate.jq
new file mode 100644
index 0000000..54d98f7
--- /dev/null
+++ b/disk-deactivate/disk-deactivate.jq
@@ -0,0 +1,77 @@
+# since lsblk lacks zfs support, we have to do it this way
+def remove:
+ if .fstype == "zfs_member" then
+ "zpool destroy -f \(.label)"
+ elif .fstype == "LVM2_member" then
+ [
+ "vg=$(pvs \(.path) --noheadings --options vg_name | grep -o '[a-zA-Z0-9-]*')",
+ "vgchange -a n \"$vg\"",
+ "vgremove -f \"$vg\""
+ ]
+ elif .fstype == "swap" then
+ "swapoff \(.path)"
+ elif .fstype == null then
+ # maybe its zfs
+ [
+ # the next line has some horrible escaping
+ "zpool=$(zdb -l \(.path) | sed -nr $'s/ +name: \\'(.*)\\'/\\\\1/p')",
+ "if [[ -n \"${zpool}\" ]]; then zpool destroy -f \"$zpool\"; fi",
+ "unset zpool"
+ ]
+ else
+ []
+ end
+;
+
+def deactivate:
+ if .type == "disk" then
+ [
+ "wipefs --all -f \(.path)"
+ ]
+ elif .type == "part" then
+ [
+ "wipefs --all -f \(.path)"
+ ]
+ elif .type == "crypt" then
+ [
+ "cryptsetup luksClose \(.path)",
+ "wipefs --all -f \(.path)"
+ ]
+ elif .type == "lvm" then
+ (.name | split("-")[0]) as $vgname |
+ (.name | split("-")[1]) as $lvname |
+ [
+ "lvremove -fy \($vgname)/\($lvname)"
+ ]
+ elif .type == "raid1" then
+ [
+ "mdadm --stop \(.name)"
+ ]
+ else
+ []
+ end
+;
+
+def walk:
+ [
+ (.mountpoints[] | "umount -R \(.)"),
+ ((.children // []) | map(walk)),
+ remove,
+ deactivate
+ ]
+;
+
+def init:
+ "/dev/\(.name)" as $disk |
+ if $disk == $disk_to_clear then
+ [
+ "set -fu",
+ walk
+ ]
+ else
+ []
+ end
+;
+
+.blockdevices | map(init) | flatten | join("\n")
+