1{ lib, pkgs, ... }:
2{
3 name = "swap-partition";
4
5 nodes.machine =
6 {
7 config,
8 pkgs,
9 lib,
10 ...
11 }:
12 {
13 virtualisation.useDefaultFilesystems = false;
14
15 virtualisation.rootDevice = "/dev/vda1";
16
17 boot.initrd.postDeviceCommands = ''
18 if ! test -b /dev/vda1; then
19 ${pkgs.parted}/bin/parted --script /dev/vda -- mklabel msdos
20 ${pkgs.parted}/bin/parted --script /dev/vda -- mkpart primary 1MiB -250MiB
21 ${pkgs.parted}/bin/parted --script /dev/vda -- mkpart primary -250MiB 100%
22 sync
23 fi
24
25 FSTYPE=$(blkid -o value -s TYPE /dev/vda1 || true)
26 if test -z "$FSTYPE"; then
27 ${pkgs.e2fsprogs}/bin/mke2fs -t ext4 -L root /dev/vda1
28 ${pkgs.util-linux}/bin/mkswap --label swap /dev/vda2
29 fi
30 '';
31
32 virtualisation.fileSystems = {
33 "/" = {
34 device = "/dev/disk/by-label/root";
35 fsType = "ext4";
36 };
37 };
38
39 swapDevices = [
40 {
41 device = "/dev/disk/by-label/swap";
42 }
43 ];
44 };
45
46 testScript = ''
47 machine.wait_for_unit("multi-user.target")
48
49 with subtest("Swap is active"):
50 # Doesn't matter if the numbers reported by `free` are slightly off due to unit conversions.
51 machine.succeed("free -h | grep -E 'Swap:\s+2[45][0-9]Mi'")
52 '';
53}