1{ lib, pkgs, ... }:
2{
3 name = "systemd-initrd-btrfs-raid";
4
5 nodes.machine =
6 { pkgs, ... }:
7 {
8 # Use systemd-boot
9 virtualisation = {
10 emptyDiskImages = [
11 512
12 512
13 ];
14 useBootLoader = true;
15 # Booting off the BTRFS RAID requires an available init script from the Nix store
16 mountHostNixStore = true;
17 useEFIBoot = true;
18 };
19 boot.loader.systemd-boot.enable = true;
20 boot.loader.efi.canTouchEfiVariables = true;
21
22 environment.systemPackages = with pkgs; [ btrfs-progs ];
23 boot.initrd.systemd = {
24 enable = true;
25 emergencyAccess = true;
26 };
27
28 specialisation.boot-btrfs-raid.configuration = {
29 fileSystems = lib.mkVMOverride {
30 "/".fsType = lib.mkForce "btrfs";
31 };
32 virtualisation.rootDevice = "/dev/vdb";
33 };
34 };
35
36 testScript = ''
37 # Create RAID
38 machine.succeed("mkfs.btrfs -d raid0 /dev/vdb /dev/vdc")
39 machine.succeed("mkdir -p /mnt && mount /dev/vdb /mnt && echo hello > /mnt/test && umount /mnt")
40
41 # Boot from the RAID
42 machine.succeed("bootctl set-default nixos-generation-1-specialisation-boot-btrfs-raid.conf")
43 machine.succeed("sync")
44 machine.crash()
45 machine.wait_for_unit("multi-user.target")
46
47 # Ensure we have successfully booted from the RAID
48 assert "(initrd)" in machine.succeed("systemd-analyze") # booted with systemd in stage 1
49 assert "/dev/vdb on / type btrfs" in machine.succeed("mount")
50 assert "hello" in machine.succeed("cat /test")
51 assert "Total devices 2" in machine.succeed("btrfs filesystem show")
52 '';
53}