2022-12-01 17:39:11 +03:00
# 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:
2024-01-26 23:59:23 +03:00
if .type == "disk" or .type == "loop" then
2022-12-01 17:39:11 +03:00
[
2024-01-09 16:09:08 +03:00
# If this disk is a member of raid, stop that raid
2024-01-10 15:24:53 +03:00
"md_dev=$(lsblk \(.path) -l -p -o type,name | awk 'match($1,\"raid.*\") {print $2}')",
"if [[ -n \"${md_dev}\" ]]; then umount \"$md_dev\"; mdadm --stop \"$md_dev\"; fi",
2024-01-09 16:09:08 +03:00
# Remove all file-systems and other magic strings
2023-10-18 23:52:42 +03:00
"wipefs --all -f \(.path)",
# Remove the MBR bootstrap code
"dd if=/dev/zero of=\(.path) bs=440 count=1"
2022-12-01 17:39:11 +03:00
]
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)"
]
2023-09-12 11:36:12 +03:00
elif (.type | contains("raid")) then
2022-12-01 17:39:11 +03:00
[
"mdadm --stop \(.name)"
]
else
2024-01-26 23:59:23 +03:00
["echo Warning: unknown type '\(.type)'. Consider handling this in https://github.com/nix-community/disko/blob/master/disk-deactivate/disk-deactivate.jq"]
2022-12-01 17:39:11 +03:00
end
;
def walk:
[
2023-02-03 16:57:55 +03:00
(.mountpoints[] | select(. != null) | "umount -R \(.)"),
2022-12-01 17:39:11 +03:00
((.children // []) | map(walk)),
remove,
deactivate
]
;
def init:
"/dev/\(.name)" as $disk |
2024-04-20 22:54:20 +03:00
"/dev/disk/by-id/\(."id-link")" as $disk_by_id |
"/dev/disk/by-id/\(.tran)-\(.id)" as $disk_by_id2 |
"/dev/disk/by-id/\(.tran)-\(.wwn)" as $disk_by_wwn |
if $disk == $disk_to_clear or $disk_by_id == $disk_to_clear or $disk_by_id2 == $disk_to_clear or $disk_by_wwn == $disk_to_clear then
2022-12-01 17:39:11 +03:00
[
"set -fu",
walk
]
else
[]
end
;
.blockdevices | map(init) | flatten | join("\n")