diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix
index 2901df81bf26..7fa7924f4c13 100644
--- a/nixos/modules/module-list.nix
+++ b/nixos/modules/module-list.nix
@@ -1186,6 +1186,7 @@
./system/boot/systemd/tmpfiles.nix
./system/boot/systemd/user.nix
./system/boot/systemd/initrd.nix
+ ./system/boot/systemd/initrd-mdraid.nix
./system/boot/timesyncd.nix
./system/boot/tmp.nix
./system/etc/etc-activation.nix
diff --git a/nixos/modules/system/boot/stage-1.nix b/nixos/modules/system/boot/stage-1.nix
index 04753a6767d9..3ab873604d3a 100644
--- a/nixos/modules/system/boot/stage-1.nix
+++ b/nixos/modules/system/boot/stage-1.nix
@@ -355,7 +355,7 @@ let
[ { object = bootStage1;
symlink = "/init";
}
- { object = pkgs.writeText "mdadm.conf" config.boot.initrd.mdadmConf;
+ { object = pkgs.writeText "mdadm.conf" config.boot.initrd.services.mdraid.mdadmConf;
symlink = "/etc/mdadm.conf";
}
{ object = pkgs.runCommand "initrd-kmod-blacklist-ubuntu" {
@@ -505,14 +505,6 @@ in
'';
};
- boot.initrd.mdadmConf = mkOption {
- default = "";
- type = types.lines;
- description = ''
- Contents of /etc/mdadm.conf in stage 1.
- '';
- };
-
boot.initrd.preLVMCommands = mkOption {
default = "";
type = types.lines;
@@ -736,6 +728,9 @@ in
];
boot.initrd.supportedFilesystems = map (fs: fs.fsType) fileSystems;
-
};
+
+ imports = [
+ (mkRenamedOptionModule [ "boot" "initrd" "mdadmConf" ] [ "boot" "initrd" "services" "mdraid" "mdadmConf" ])
+ ];
}
diff --git a/nixos/modules/system/boot/systemd/initrd-mdraid.nix b/nixos/modules/system/boot/systemd/initrd-mdraid.nix
new file mode 100644
index 000000000000..b30f2e083fd0
--- /dev/null
+++ b/nixos/modules/system/boot/systemd/initrd-mdraid.nix
@@ -0,0 +1,32 @@
+{ config, pkgs, lib, ... }: let
+
+ cfg = config.boot.initrd.services.mdraid;
+
+in {
+ options.boot.initrd.services.mdraid = {
+ enable = (lib.mkEnableOption "mdraid support in initrd") // {
+ visible = false;
+ };
+
+ mdadmConf = lib.mkOption {
+ description = "Contents of /etc/mdadm.conf in initrd.";
+ type = lib.types.lines;
+ default = "";
+ };
+ };
+
+ config = lib.mkIf (config.boot.initrd.systemd.enable && cfg.enable) {
+ boot.initrd.systemd = {
+ contents."/etc/mdadm.conf" = lib.mkIf (cfg.mdadmConf != "") {
+ text = cfg.mdadmConf;
+ };
+
+ initrdBin = [ pkgs.mdadm ];
+ };
+
+ boot.initrd.services.udev.packages = [ pkgs.mdadm ];
+ boot.initrd.systemd.packages = [ pkgs.mdadm ];
+
+ boot.kernelModules = [ "dm-raid" ];
+ };
+}
diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix
index 44eeceda99eb..9e1fb16af0f7 100644
--- a/nixos/tests/all-tests.nix
+++ b/nixos/tests/all-tests.nix
@@ -518,6 +518,7 @@ in
systemd-confinement = handleTest ./systemd-confinement.nix {};
systemd-cryptenroll = handleTest ./systemd-cryptenroll.nix {};
systemd-escaping = handleTest ./systemd-escaping.nix {};
+ systemd-initrd-mdraid = handleTest ./systemd-initrd-mdraid.nix {};
systemd-initrd-simple = handleTest ./systemd-initrd-simple.nix {};
systemd-journal = handleTest ./systemd-journal.nix {};
systemd-machinectl = handleTest ./systemd-machinectl.nix {};
diff --git a/nixos/tests/systemd-initrd-mdraid.nix b/nixos/tests/systemd-initrd-mdraid.nix
new file mode 100644
index 000000000000..0d8827558fbb
--- /dev/null
+++ b/nixos/tests/systemd-initrd-mdraid.nix
@@ -0,0 +1,50 @@
+import ./make-test-python.nix ({ lib, pkgs, ... }: {
+ name = "systemd-initrd-mdraid";
+
+ nodes.machine = { pkgs, ... }: {
+ # Use systemd-boot
+ virtualisation = {
+ emptyDiskImages = [ 512 512 ];
+ useBootLoader = 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.initrd = {
+ systemd = {
+ enable = true;
+ emergencyAccess = true;
+ };
+ services.mdraid = {
+ enable = true;
+ mdadmConf = ''
+ ARRAY /dev/md0 devices=/dev/vdc,/dev/vdd
+ '';
+ };
+ kernelModules = [ "raid0" ];
+ };
+
+ specialisation.boot-mdraid.configuration.virtualisation.bootDevice = "/dev/disk/by-label/testraid";
+ };
+
+ testScript = ''
+ # Create RAID
+ machine.succeed("mdadm --create --force /dev/md0 -n 2 --level=raid0 /dev/vdc /dev/vdd")
+ 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("bootctl set-default nixos-generation-1-specialisation-boot-mdraid.conf")
+ 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")
+ '';
+})