summaryrefslogtreecommitdiffstats
path: root/disk-deactivate/disk-deactivate.jq
blob: 54d98f75e441269a37b5bd1bceb9aa5dbb40597f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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")