summaryrefslogtreecommitdiffstats
path: root/nixos/tests/systemd-initrd-swraid.nix
blob: 01fc9141997eb005e6736adc499725958d2d687c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{ lib, ... }:
{
  name = "systemd-initrd-swraid";

  nodes.machine =
    { pkgs, ... }:
    {
      # Use systemd-boot
      virtualisation = {
        emptyDiskImages = [
          512
          512
        ];
        useBootLoader = true;
        # Booting off the RAID requires an available init script
        mountHostNixStore = true;
        useEFIBoot = true;
      };
      boot.loader.systemd-boot.enable = true;
      boot.loader.efi.canTouchEfiVariables = true;

      environment.systemPackages = with pkgs; [
        mdadm
        e2fsprogs
      ]; # for mdadm and mkfs.ext4
      boot.swraid.enable = true;
      environment.etc."mdadm.conf".text = ''
        MAILADDR test@example.com
      '';
      boot.initrd = {
        systemd = {
          enable = true;
          emergencyAccess = true;
        };
        kernelModules = [ "raid0" ];
      };

      specialisation.boot-swraid.configuration.virtualisation.rootDevice = "/dev/disk/by-label/testraid";
      # This protects against a regression. We do not have to switch to it.
      # It's sufficient to trigger its evaluation.
      specialisation.build-old-initrd.configuration.boot.initrd.systemd.enable = lib.mkForce false;
    };

  testScript =
    { nodes, ... }:
    let
      boot-swraid = nodes.machine.specialisation.boot-swraid.configuration.system.build.toplevel;
    in
    # python
    ''
      # Create RAID
      machine.succeed("mdadm --create --force /dev/md0 -n 2 --level=raid1 /dev/vdb /dev/vdc --metadata=0.90 --bitmap=internal")
      machine.succeed("mkfs.ext4 -L testraid /dev/md0")
      machine.succeed("mkdir -p /mnt && mount /dev/md0 /mnt && echo hello > /mnt/test && umount /mnt")

      # Boot from the RAID
      machine.succeed("${boot-swraid}/bin/switch-to-configuration boot")
      machine.succeed("sync")
      machine.crash()
      machine.wait_for_unit("multi-user.target")

      # Ensure we have successfully booted from the RAID
      assert "(initrd)" in machine.succeed("systemd-analyze")  # booted with systemd in stage 1
      assert "/dev/md0 on / type ext4" in machine.succeed("mount")
      assert "hello" in machine.succeed("cat /test")
      assert "md0" in machine.succeed("cat /proc/mdstat")

      # Verify the RAID array was properly auto-detected and assembled
      detail = machine.succeed("mdadm --detail /dev/md0")
      assert "raid1" in detail, f"Expected raid1 in mdadm detail output: {detail}"
      assert "/dev/vdb" in detail, f"Expected /dev/vdb in array: {detail}"
      assert "/dev/vdc" in detail, f"Expected /dev/vdc in array: {detail}"

      machine.wait_for_unit("mdmonitor.service")
    '';
}