1 import ./make-test-python.nix ({ lib, pkgs, ... }:
3 name = "swap-random-encryption";
6 { config, pkgs, lib, ... }:
8 environment.systemPackages = [ pkgs.cryptsetup ];
10 virtualisation.useDefaultFilesystems = false;
12 virtualisation.rootDevice = "/dev/vda1";
14 boot.initrd.postDeviceCommands = ''
15 if ! test -b /dev/vda1; then
16 ${pkgs.parted}/bin/parted --script /dev/vda -- mklabel msdos
17 ${pkgs.parted}/bin/parted --script /dev/vda -- mkpart primary 1MiB -250MiB
18 ${pkgs.parted}/bin/parted --script /dev/vda -- mkpart primary -250MiB 100%
22 FSTYPE=$(blkid -o value -s TYPE /dev/vda1 || true)
23 if test -z "$FSTYPE"; then
24 ${pkgs.e2fsprogs}/bin/mke2fs -t ext4 -L root /dev/vda1
28 virtualisation.fileSystems = {
30 device = "/dev/disk/by-label/root";
41 cipher = "aes-xts-plain64";
50 machine.wait_for_unit("multi-user.target")
52 with subtest("Swap is active"):
53 # Doesn't matter if the numbers reported by `free` are slightly off due to unit conversions.
54 machine.succeed("free -h | grep -E 'Swap:\s+2[45][0-9]Mi'")
56 with subtest("Swap device has 4k sector size"):
58 result = json.loads(machine.succeed("lsblk -Jo PHY-SEC,LOG-SEC /dev/mapper/dev-vda2"))
59 block_devices = result["blockdevices"]
60 if len(block_devices) != 1:
61 raise Exception ("lsblk output did not report exactly one block device")
63 swapDevice = block_devices[0];
64 if not (swapDevice["phy-sec"] == 4096 and swapDevice["log-sec"] == 4096):
65 raise Exception ("swap device does not have the sector size specified in the configuration")
67 with subtest("Swap encrypt has assigned cipher and keysize"):
70 results = machine.succeed("cryptsetup status dev-vda2").splitlines()
72 cipher_pattern = re.compile(r"\s*cipher:\s+aes-xts-plain64\s*")
73 if not any(cipher_pattern.fullmatch(line) for line in results):
74 raise Exception ("swap device encryption does not use the cipher specified in the configuration")
76 key_size_pattern = re.compile(r"\s*keysize:\s+512\s+bits\s*")
77 if not any(key_size_pattern.fullmatch(line) for line in results):
78 raise Exception ("swap device encryption does not use the key size specified in the configuration")