nixos/swraid: fix monitor service

This commit is contained in:
Christian Theune 2023-09-09 21:38:16 +02:00 committed by Martin Weinelt
parent 74bb255760
commit 7f341bb450
2 changed files with 22 additions and 5 deletions

View File

@ -2,6 +2,8 @@
cfg = config.boot.swraid;
mdadm_conf = config.environment.etc."mdadm.conf";
in {
imports = [
(lib.mkRenamedOptionModule [ "boot" "initrd" "services" "swraid" "enable" ] [ "boot" "swraid" "enable" ])
@ -36,8 +38,14 @@ in {
};
config = lib.mkIf cfg.enable {
warnings = lib.mkIf
((builtins.match ".*(MAILADDR|PROGRAM).*" mdadm_conf.text) == null)
[ "mdadm: Neither MAILADDR nor PROGRAM has been set. This will cause the `mdmon` service to crash." ];
environment.systemPackages = [ pkgs.mdadm ];
environment.etc."mdadm.conf".text = lib.mkAfter cfg.mdadmConf;
services.udev.packages = [ pkgs.mdadm ];
systemd.packages = [ pkgs.mdadm ];
@ -59,12 +67,10 @@ in {
$out/bin/mdadm --version
'';
extraFiles."/etc/mdadm.conf".source = pkgs.writeText "mdadm.conf" config.boot.swraid.mdadmConf;
extraFiles."/etc/mdadm.conf" = mdadm_conf;
systemd = {
contents."/etc/mdadm.conf" = lib.mkIf (cfg.mdadmConf != "") {
text = cfg.mdadmConf;
};
contents."/etc/mdadm.conf".text = mdadm_conf.text;
packages = [ pkgs.mdadm ];
initrdBin = [ pkgs.mdadm ];

View File

@ -20,6 +20,9 @@ import ./make-test-python.nix ({ lib, pkgs, ... }: {
ARRAY /dev/md0 devices=/dev/vdb,/dev/vdc
'';
};
environment.etc."mdadm.conf".text = ''
MAILADDR test@example.com
'';
boot.initrd = {
systemd = {
enable = true;
@ -33,7 +36,7 @@ import ./make-test-python.nix ({ lib, pkgs, ... }: {
testScript = ''
# Create RAID
machine.succeed("mdadm --create --force /dev/md0 -n 2 --level=raid0 /dev/vdb /dev/vdc")
machine.succeed("mdadm --create --force /dev/md0 -n 2 --level=raid1 /dev/vdb /dev/vdc --metadata=0.90")
machine.succeed("mkfs.ext4 -L testraid /dev/md0")
machine.succeed("mkdir -p /mnt && mount /dev/md0 /mnt && echo hello > /mnt/test && umount /mnt")
@ -48,5 +51,13 @@ import ./make-test-python.nix ({ lib, pkgs, ... }: {
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")
expected_config = """MAILADDR test@example.com
ARRAY /dev/md0 devices=/dev/vdb,/dev/vdc
"""
got_config = machine.execute("cat /etc/mdadm.conf")[1]
assert expected_config == got_config, repr((expected_config, got_config))
machine.wait_for_unit("mdmonitor.service")
'';
})