diff options
-rwxr-xr-x | disk-deactivate/disk-deactivate | 6 | ||||
-rw-r--r-- | disk-deactivate/disk-deactivate.jq | 77 | ||||
-rw-r--r-- | tests/lib.nix | 11 | ||||
-rw-r--r-- | types.nix | 23 |
4 files changed, 97 insertions, 20 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") + diff --git a/tests/lib.nix b/tests/lib.nix index 4b81da7..80ad0e3 100644 --- a/tests/lib.nix +++ b/tests/lib.nix @@ -26,6 +26,7 @@ tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. { }).create (import disko-config { disks = builtins.tail disks; inherit lib; })); tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. { }).mount (import disko-config { disks = builtins.tail disks; inherit lib; })); tsp-config = (pkgs.callPackage ../. { }).config (import disko-config { inherit disks; inherit lib; }); + tsp-disko = pkgs.writeScript "disko" ((pkgs.callPackage ../. { }).zapCreateMount (import disko-config { disks = builtins.tail disks; inherit lib; })); num-disks = builtins.length (lib.attrNames (import disko-config { inherit lib; }).disk); installed-system = { modulesPath, ... }: { imports = [ @@ -60,9 +61,6 @@ efiSupport = efi; efiInstallAsRemovable = efi; }; - environment.systemPackages = [ - pkgs.jq - ]; }; installedTopLevel = (eval-config { modules = [ installed-system ]; @@ -93,6 +91,9 @@ (modulesPath + "/profiles/minimal.nix") extraConfig ]; + environment.systemPackages = [ + pkgs.jq + ]; # speed-up eval documentation.enable = false; @@ -127,20 +128,24 @@ machine.succeed("${tsp-create}") machine.succeed("${tsp-mount}") machine.succeed("${tsp-mount}") # verify that the command is idempotent + machine.succeed("${tsp-disko}") # verify that we can destroy and recreate ''} ${lib.optionalString (testMode == "module") '' machine.succeed("${nodes.machine.system.build.formatScript}") machine.succeed("${nodes.machine.system.build.mountScript}") machine.succeed("${nodes.machine.system.build.mountScript}") # verify that the command is idempotent + machine.succeed("${nodes.machine.system.build.disko}") # verify that we can destroy and recreate again ''} ${lib.optionalString (testMode == "cli") '' # TODO use the disko cli here # machine.succeed("${../.}/disko --no-pkgs --mode create ${disko-config}") # machine.succeed("${../.}/disko --no-pkgs --mode mount ${disko-config}") # machine.succeed("${../.}/disko --no-pkgs --mode mount ${disko-config}") # verify that the command is idempotent + # machine.succeed("${../.}/disko --no-pkgs --mode zap_create_mount ${disko-config}") # verify that we can destroy and recreate again machine.succeed("${tsp-create}") machine.succeed("${tsp-mount}") machine.succeed("${tsp-mount}") # verify that the command is idempotent + machine.succeed("${tsp-disko}") # verify that we can destroy and recreate ''} ${lib.optionalString testBoot '' @@ -154,23 +154,11 @@ rec { */ zapCreateMount = devices: '' set -efux - shopt -s nullglob - # print existing disks - lsblk + umount -Rv /mnt || : - # TODO get zap the same way we get create - # make partitioning idempotent by dismounting already mounted filesystems - if findmnt /mnt; then - umount -Rlv /mnt - fi - - # stop all existing raids - if command -v mdadm; then - for r in /dev/md/* /dev/md[0-9]*; do - # might fail if the device was already closed in the loop - mdadm --stop "$r" || true - done - fi + for dev in ${toString (lib.catAttrs "device" (lib.attrValues devices.disk))}; do + ${./disk-deactivate}/disk-deactivate "$dev" | bash -x + done echo 'creating partitions...' ${diskoLib.create devices} @@ -820,6 +808,7 @@ rec { type = types.functionTo types.str; default = vg: '' lvcreate \ + --yes \ ${if hasInfix "%" config.size then "-l" else "-L"} ${config.size} \ -n ${config.name} \ ${optionalString (!isNull config.lvm_type) "--type=${config.lvm_type}"} \ @@ -1326,7 +1315,7 @@ rec { internal = true; readOnly = true; type = types.functionTo (types.listOf types.package); - default = pkgs: lib.optionals (!isNull config.content) (config.content._pkgs pkgs); + default = pkgs: [ pkgs.jq ] ++ lib.optionals (!isNull config.content) (config.content._pkgs pkgs); }; }; }); |