From dcabccaad6b821e766329e8c72b5ce43370a2394 Mon Sep 17 00:00:00 2001 From: Felix Uhl Date: Mon, 14 Oct 2024 18:34:44 +0200 Subject: [PATCH] swap: fix partition type Fixes #391 --- lib/types/gpt.nix | 3 ++- tests/swap.nix | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/types/gpt.nix b/lib/types/gpt.nix index 2042a20..442ad7b 100644 --- a/lib/types/gpt.nix +++ b/lib/types/gpt.nix @@ -26,7 +26,8 @@ in lib.types.either (lib.types.strMatching (hexPattern 4)) (lib.types.strMatching (lib.concatMapStringsSep "-" hexPattern [ 8 4 4 4 12 ])); - default = "8300"; + default = if partition.config.content.type == "swap" then "8200" else "8300"; + defaultText = ''8300 (Linux filesystem) normally, 8200 (Linux swap) if content.type is "swap"''; description = '' Filesystem type to use. This can either be an sgdisk-specific short code (run sgdisk -L to see what is available), diff --git a/tests/swap.nix b/tests/swap.nix index 57e7eb2..7d1678b 100644 --- a/tests/swap.nix +++ b/tests/swap.nix @@ -9,9 +9,21 @@ diskoLib.testLib.makeDiskoTest { import json machine.succeed("mountpoint /"); machine.succeed("swapon --show >&2"); - out = json.loads(machine.succeed("lsblk --json /dev/vda")) - mnt_point = out["blockdevices"][0]["children"][1]["children"][0]["mountpoints"][0] - assert mnt_point == "[SWAP]" + machine.succeed("lsblk -o +PARTTYPENAME --json /dev/vda >&2"); + out = json.loads(machine.succeed("lsblk -o +PARTTYPENAME --json /dev/vda")) + + encrypted_swap_crypt = out["blockdevices"][0]["children"][1] + mnt_point = encrypted_swap_crypt["children"][0]["mountpoints"][0] + assert mnt_point == "[SWAP]", f"Expected encrypted swap partition to be mounted as [SWAP], got {mnt_point}" + part_type = encrypted_swap_crypt["parttypename"] + # The dm-crypt partition should be labelled as swap, not dm-crypt, see https://github.com/util-linux/util-linux/issues/3238 + assert part_type == "Linux swap", f"Expected encrypted swap container to be of type Linux swap, got {part_type}" + + plain_swap_part = out["blockdevices"][0]["children"][3] + mnt_point = plain_swap_part["mountpoints"][0] + assert mnt_point == "[SWAP]", f"Expected swap partition to be mounted as [SWAP], got {mnt_point}" + part_type = plain_swap_part["parttypename"] + assert part_type == "Linux swap", f"Expected plain swap partition to be of type Linux swap, got {part_type}" ''; extraSystemConfig = { environment.systemPackages = [ pkgs.jq ];