summaryrefslogtreecommitdiffstats
path: root/nixos/tests/lvm2/systemd-stage-1.nix
blob: c6aed70675e29a42a0240e8708c376622979b356 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{
  lib,
  kernelPackages ? null,
  flavour,
  mkXfsFlags ? "",
  ...
}:
let
  preparationCode =
    {
      raid = ''
        machine.succeed("vgcreate test_vg /dev/vdb /dev/vdc")
        machine.succeed("lvcreate -L 512M --type raid0 test_vg -n test_lv")
      '';

      thinpool = ''
        machine.succeed("vgcreate test_vg /dev/vdb")
        machine.succeed("lvcreate -L 512M -T test_vg/test_thin_pool")
        machine.succeed("lvcreate -n test_lv -V 16G --thinpool test_thin_pool test_vg")
      '';

      vdo = ''
        machine.succeed("vgcreate test_vg /dev/vdb")
        machine.succeed("lvcreate --type vdo -n test_lv -L 6G -V 12G test_vg/vdo_pool_lv")
      '';
    }
    .${flavour};

  extraConfig =
    {
      raid = {
        boot.initrd.kernelModules = [
          "dm-raid"
          "raid0"
        ];
      };

      thinpool = {
        services.lvm = {
          boot.thin.enable = true;
          dmeventd.enable = true;
        };
      };

      vdo = {
        services.lvm = {
          boot.vdo.enable = true;
          dmeventd.enable = true;
        };
      };
    }
    .${flavour};

  extraCheck =
    {
      raid = ''
        "test_lv" in machine.succeed("lvs --select segtype=raid0")
      '';

      thinpool = ''
        "test_lv" in machine.succeed("lvs --select segtype=thin-pool")
      '';

      vdo = ''
        "test_lv" in machine.succeed("lvs --select segtype=vdo")
      '';
    }
    .${flavour};

in
{
  name = "lvm2-${flavour}-systemd-stage-1";
  meta.maintainers = with lib.maintainers; [
    das_j
    helsinki-Jo
  ];

  nodes.machine =
    { pkgs, lib, ... }:
    {
      imports = [ extraConfig ];
      # Use systemd-boot
      virtualisation = {
        emptyDiskImages = [
          8192
          8192
        ];
        useBootLoader = true;
        useEFIBoot = true;
        # To boot off the LVM disk, we need to have a init script which comes from the Nix store.
        mountHostNixStore = true;
      };
      boot.loader.systemd-boot.enable = true;
      boot.loader.efi.canTouchEfiVariables = true;

      environment.systemPackages = with pkgs; [ xfsprogs ];
      boot = {
        initrd.systemd = {
          enable = true;
          emergencyAccess = true;
        };
        initrd.services.lvm.enable = true;
        kernelPackages = lib.mkIf (kernelPackages != null) kernelPackages;
      };

      specialisation.boot-lvm.configuration.virtualisation = {
        useDefaultFilesystems = false;
        fileSystems = {
          "/" = {
            device = "/dev/test_vg/test_lv";
            fsType = "xfs";
          };
        };

        rootDevice = "/dev/test_vg/test_lv";
      };
    };

  testScript =
    { nodes, ... }:
    let
      boot-lvm = nodes.machine.specialisation.boot-lvm.configuration.system.build.toplevel;
    in
    # python
    ''
      machine.wait_for_unit("multi-user.target")
      # Create a VG for the root
      ${preparationCode}
      machine.succeed("mkfs.xfs ${mkXfsFlags} /dev/test_vg/test_lv")
      machine.succeed("mkdir -p /mnt && mount /dev/test_vg/test_lv /mnt && echo hello > /mnt/test && umount /mnt")

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

      # Ensure we have successfully booted from LVM
      assert "(initrd)" in machine.succeed("systemd-analyze")  # booted with systemd in stage 1
      assert "/dev/mapper/test_vg-test_lv on / type xfs" in machine.succeed("mount")
      assert "hello" in machine.succeed("cat /test")
      ${extraCheck}
    '';
}