add lvm raid

This commit is contained in:
lassulus 2022-08-25 18:36:56 +02:00
parent 0ffaac7913
commit dadc491330
4 changed files with 168 additions and 49 deletions

View file

@ -52,10 +52,10 @@ let
boot.initrd.luks.devices.${x.name}.device = q.device; boot.initrd.luks.devices.${x.name}.device = q.device;
} // config-f { device = "/dev/mapper/${x.name}"; } x.content; } // config-f { device = "/dev/mapper/${x.name}"; } x.content;
config.lv = q: x: config.lvm_lv = q: x:
config-f { device = "/dev/mapper/${q.vgname}-${q.name}"; } x.content; config-f { device = "/dev/${q.vgname}/${q.name}"; } x.content;
config.lvm = q: x: config.lvm_vg = q: x:
foldl' recursiveUpdate {} (mapAttrsToList (name: config-f { inherit name; vgname = x.name; }) x.lvs); foldl' recursiveUpdate {} (mapAttrsToList (name: config-f { inherit name; vgname = x.name; }) x.lvs);
config.noop = q: x: {}; config.noop = q: x: {};
@ -74,8 +74,8 @@ let
''; '';
create.devices = q: x: let create.devices = q: x: let
raid-devices = lib.filterAttrs (_: dev: dev.type == "mdadm" || dev.type == "zpool") x.content; raid-devices = lib.filterAttrs (_: dev: dev.type == "mdadm" || dev.type == "zpool" || dev.type == "lvm_vg") x.content;
other-devices = lib.filterAttrs (_: dev: dev.type != "mdadm" && dev.type != "zpool") x.content; other-devices = lib.filterAttrs (_: dev: dev.type != "mdadm" && dev.type != "zpool" && dev.type != "lvm_vg") x.content;
in '' in ''
${concatStrings (mapAttrsToList (name: create-f { device = "/dev/${name}"; }) other-devices)} ${concatStrings (mapAttrsToList (name: create-f { device = "/dev/${name}"; }) other-devices)}
${concatStrings (mapAttrsToList (name: create-f { device = "/dev/${name}"; name = name; }) raid-devices)} ${concatStrings (mapAttrsToList (name: create-f { device = "/dev/${name}"; name = name; }) raid-devices)}
@ -98,15 +98,24 @@ let
${create-f { device = "/dev/mapper/${x.name}"; } x.content} ${create-f { device = "/dev/mapper/${x.name}"; } x.content}
''; '';
create.lv = q: x: '' create.lvm_pv = q: x: ''
lvcreate ${if hasInfix "%" x.size then "-l" else "-L"} ${x.size} -n ${q.name} ${q.vgname} pvcreate ${q.device}
${create-f { device = "/dev/mapper/${q.vgname}-${q.name}"; } x.content} LVMDEVICES_${x.vg}="''${LVMDEVICES_${x.vg}:-}${q.device} "
''; '';
create.lvm = q: x: '' create.lvm_lv = q: x: ''
pvcreate ${q.device} lvcreate \
vgcreate ${x.name} ${q.device} ${if hasInfix "%" x.size then "-l" else "-L"} ${x.size} \
${concatStrings (mapAttrsToList (name: create-f { inherit name; vgname = x.name; }) x.lvs)} -n ${q.name} \
${lib.optionalString (!isNull x.lvm_type or null) "--type=${x.lvm_type}"} \
${lib.optionalString (!isNull x.extraArgs or null) x.extraArgs} \
${q.vgname}
${create-f { device = "/dev/${q.vgname}/${q.name}"; } x.content}
'';
create.lvm_vg = q: x: ''
vgcreate ${q.name} $LVMDEVICES_${q.name}
${concatStrings (mapAttrsToList (name: create-f { inherit name; vgname = q.name; }) x.lvs)}
''; '';
create.noop = q: x: ""; create.noop = q: x: "";
@ -190,17 +199,19 @@ let
'';} '';}
); );
mount.lv = q: x: mount.lvm_lv = q: x:
mount-f { device = "/dev/mapper/${q.vgname}-${q.name}"; } x.content; mount-f { device = "/dev/${q.vgname}/${q.name}"; } x.content;
mount.lvm = q: x: ( mount.lvm_vg = q: x: (
recursiveUpdate recursiveUpdate
(foldl' recursiveUpdate {} (mapAttrsToList (name: mount-f { inherit name; vgname = x.name; }) x.lvs)) (foldl' recursiveUpdate {} (mapAttrsToList (name: mount-f { inherit name; vgname = q.name; }) x.lvs))
{lvm.${q.device} = '' {lvm.${q.device} = ''
vgchange -a y vgchange -a y
'';} '';}
); );
mount.lvm_pv = mount.noop;
mount.noop = q: x: {}; mount.noop = q: x: {};
mount.mdadm = q: x: mount.mdadm = q: x:

View file

@ -36,11 +36,18 @@
"--iter-time 5000" "--iter-time 5000"
]; ];
content = { content = {
type = "lvm"; type = "lvm_pv";
name = "pool"; vg = "pool";
};
};
}
];
};
pool = {
type = "lvm_vg";
lvs = { lvs = {
root = { root = {
type = "lv"; type = "lvm_lv";
size = "100M"; size = "100M";
mountpoint = "/"; mountpoint = "/";
content = { content = {
@ -53,7 +60,7 @@
}; };
}; };
home = { home = {
type = "lv"; type = "lvm_lv";
size = "10M"; size = "10M";
content = { content = {
type = "filesystem"; type = "filesystem";
@ -62,7 +69,7 @@
}; };
}; };
raw = { raw = {
type = "lv"; type = "lvm_lv";
size = "10M"; size = "10M";
content = { content = {
type = "noop"; type = "noop";
@ -71,8 +78,4 @@
}; };
}; };
}; };
}
];
};
};
} }

66
example/lvm-raid.nix Normal file
View file

@ -0,0 +1,66 @@
{
type = "devices";
content = {
vdb = {
type = "table";
format = "gpt";
partitions = [
{
type = "partition";
part-type = "primary";
start = "0%";
end = "100%";
content = {
type = "lvm_pv";
vg = "pool";
};
}
];
};
vdc = {
type = "table";
format = "gpt";
partitions = [
{
type = "partition";
part-type = "primary";
start = "0%";
end = "100%";
content = {
type = "lvm_pv";
vg = "pool";
};
}
];
};
pool = {
type = "lvm_vg";
lvs = {
root = {
type = "lvm_lv";
size = "100M";
mountpoint = "/";
lvm_type = "mirror";
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/";
options = [
"defaults"
];
};
};
home = {
type = "lvm_lv";
size = "10M";
lvm_type = "raid0";
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/home";
};
};
};
};
};
}

39
tests/lvm-raid.nix Normal file
View file

@ -0,0 +1,39 @@
{ makeTest ? import <nixpkgs/nixos/tests/make-test-python.nix>
, pkgs ? (import <nixpkgs> {})
}:
let
makeTest' = args:
makeTest args {
inherit pkgs;
inherit (pkgs) system;
};
disko-config = import ../example/lvm-raid.nix;
tsp-create = pkgs.writeScript "create" ((pkgs.callPackage ../. {}).create disko-config);
tsp-mount = pkgs.writeScript "mount" ((pkgs.callPackage ../. {}).mount disko-config);
in makeTest' {
name = "disko";
nodes.machine =
{ config, pkgs, modulesPath, ... }:
{
imports = [
(modulesPath + "/profiles/installation-device.nix")
(modulesPath + "/profiles/base.nix")
];
# speed-up eval
documentation.enable = false;
virtualisation.emptyDiskImages = [ 512 512 ];
boot.kernelModules = [ "dm-raid" "dm-mirror" ];
};
testScript = ''
machine.succeed("echo 'secret' > /tmp/secret.key");
machine.succeed("${tsp-create}");
machine.succeed("${tsp-mount}");
machine.succeed("${tsp-mount}"); # verify that the command is idempotent
machine.succeed("grep -qs '/mnt/home' /proc/mounts");
'';
}