nixos/tests/swap: Use systemd stage 1

This commit is contained in:
Will Fancher
2025-08-15 15:43:01 -04:00
parent 61305d1414
commit 1288734d09
2 changed files with 51 additions and 45 deletions

View File

@@ -1,33 +1,35 @@
{ lib, pkgs, ... }:
{ ... }:
{
name = "swap-partition";
nodes.machine =
{
config,
pkgs,
lib,
...
}:
{ config, ... }:
{
virtualisation.useDefaultFilesystems = false;
virtualisation.rootDevice = "/dev/vda1";
boot.initrd.postDeviceCommands = ''
if ! test -b /dev/vda1; then
${pkgs.parted}/bin/parted --script /dev/vda -- mklabel msdos
${pkgs.parted}/bin/parted --script /dev/vda -- mkpart primary 1MiB -250MiB
${pkgs.parted}/bin/parted --script /dev/vda -- mkpart primary -250MiB 100%
sync
fi
FSTYPE=$(blkid -o value -s TYPE /dev/vda1 || true)
if test -z "$FSTYPE"; then
${pkgs.e2fsprogs}/bin/mke2fs -t ext4 -L root /dev/vda1
${pkgs.util-linux}/bin/mkswap --label swap /dev/vda2
fi
'';
boot.initrd.systemd = {
enable = true;
repart = {
enable = true;
device = "/dev/vda";
empty = "allow";
};
};
systemd.repart.partitions = {
"00-root" = {
Type = "linux-generic";
Format = "ext4";
Label = "root";
};
"10-swap" = {
Type = "linux-generic";
Label = "swap";
SizeMinBytes = "250M";
SizeMaxBytes = "250M";
};
};
virtualisation.fileSystems = {
"/" = {
@@ -38,7 +40,8 @@
swapDevices = [
{
device = "/dev/disk/by-label/swap";
device = "/dev/disk/by-partlabel/swap";
options = [ "x-systemd.makefs" ];
}
];
};

View File

@@ -1,14 +1,9 @@
{ lib, pkgs, ... }:
{ ... }:
{
name = "swap-random-encryption";
nodes.machine =
{
config,
pkgs,
lib,
...
}:
{ pkgs, ... }:
{
environment.systemPackages = [ pkgs.cryptsetup ];
@@ -16,19 +11,27 @@
virtualisation.rootDevice = "/dev/vda1";
boot.initrd.postDeviceCommands = ''
if ! test -b /dev/vda1; then
${pkgs.parted}/bin/parted --script /dev/vda -- mklabel msdos
${pkgs.parted}/bin/parted --script /dev/vda -- mkpart primary 1MiB -250MiB
${pkgs.parted}/bin/parted --script /dev/vda -- mkpart primary -250MiB 100%
sync
fi
FSTYPE=$(blkid -o value -s TYPE /dev/vda1 || true)
if test -z "$FSTYPE"; then
${pkgs.e2fsprogs}/bin/mke2fs -t ext4 -L root /dev/vda1
fi
'';
boot.initrd.systemd = {
enable = true;
repart = {
enable = true;
device = "/dev/vda";
empty = "allow";
};
};
systemd.repart.partitions = {
"00-root" = {
Type = "linux-generic";
Format = "ext4";
Label = "root";
};
"10-swap" = {
Type = "linux-generic";
Label = "swap";
SizeMinBytes = "250M";
SizeMaxBytes = "250M";
};
};
virtualisation.fileSystems = {
"/" = {
@@ -39,7 +42,7 @@
swapDevices = [
{
device = "/dev/vda2";
device = "/dev/disk/by-partlabel/swap";
randomEncryption = {
enable = true;
@@ -60,7 +63,7 @@
with subtest("Swap device has 4k sector size"):
import json
result = json.loads(machine.succeed("lsblk -Jo PHY-SEC,LOG-SEC /dev/mapper/dev-vda2"))
result = json.loads(machine.succeed("lsblk -Jo PHY-SEC,LOG-SEC /dev/mapper/dev-disk-by\\x2dpartlabel-swap"))
block_devices = result["blockdevices"]
if len(block_devices) != 1:
raise Exception ("lsblk output did not report exactly one block device")
@@ -72,7 +75,7 @@
with subtest("Swap encrypt has assigned cipher and keysize"):
import re
results = machine.succeed("cryptsetup status dev-vda2").splitlines()
results = machine.succeed("cryptsetup status dev-disk-by\\x2dpartlabel-swap").splitlines()
cipher_pattern = re.compile(r"\s*cipher:\s+aes-xts-plain64\s*")
if not any(cipher_pattern.fullmatch(line) for line in results):