types lvm_vg: inline lvm_lv type

This commit is contained in:
lassulus 2023-04-07 15:08:20 +02:00
parent d6f062ea11
commit 654ecb386e
6 changed files with 49 additions and 93 deletions

View file

@ -105,7 +105,6 @@
type = "lvm_vg"; type = "lvm_vg";
lvs = { lvs = {
root = { root = {
type = "lvm_lv";
size = "10M"; size = "10M";
lvm_type = "mirror"; lvm_type = "mirror";
content = { content = {
@ -118,7 +117,6 @@
}; };
}; };
raid1 = { raid1 = {
type = "lvm_lv";
size = "30M"; size = "30M";
lvm_type = "raid0"; lvm_type = "raid0";
content = { content = {
@ -127,7 +125,6 @@
}; };
}; };
raid2 = { raid2 = {
type = "lvm_lv";
size = "30M"; size = "30M";
lvm_type = "raid0"; lvm_type = "raid0";
content = { content = {
@ -136,7 +133,6 @@
}; };
}; };
zfs1 = { zfs1 = {
type = "lvm_lv";
size = "128M"; size = "128M";
lvm_type = "raid0"; lvm_type = "raid0";
content = { content = {
@ -145,7 +141,6 @@
}; };
}; };
zfs2 = { zfs2 = {
type = "lvm_lv";
size = "128M"; size = "128M";
lvm_type = "raid0"; lvm_type = "raid0";
content = { content = {

View file

@ -46,7 +46,6 @@
type = "lvm_vg"; type = "lvm_vg";
lvs = { lvs = {
root = { root = {
type = "lvm_lv";
size = "100M"; size = "100M";
content = { content = {
type = "filesystem"; type = "filesystem";
@ -58,7 +57,6 @@
}; };
}; };
home = { home = {
type = "lvm_lv";
size = "10M"; size = "10M";
content = { content = {
type = "filesystem"; type = "filesystem";
@ -67,7 +65,6 @@
}; };
}; };
raw = { raw = {
type = "lvm_lv";
size = "10M"; size = "10M";
}; };
}; };

View file

@ -79,7 +79,6 @@
type = "lvm_vg"; type = "lvm_vg";
lvs = { lvs = {
root = { root = {
type = "lvm_lv";
size = "100M"; size = "100M";
lvm_type = "mirror"; lvm_type = "mirror";
content = { content = {
@ -92,7 +91,6 @@
}; };
}; };
home = { home = {
type = "lvm_lv";
size = "10M"; size = "10M";
lvm_type = "raid0"; lvm_type = "raid0";
content = { content = {

View file

@ -345,7 +345,6 @@ rec {
swap = ./swap.nix; swap = ./swap.nix;
lvm_pv = ./lvm_pv.nix; lvm_pv = ./lvm_pv.nix;
lvm_vg = ./lvm_vg.nix; lvm_vg = ./lvm_vg.nix;
lvm_lv = ./lvm_lv.nix;
zfs = ./zfs.nix; zfs = ./zfs.nix;
zpool = ./zpool.nix; zpool = ./zpool.nix;
zfs_dataset = ./zfs_dataset.nix; zfs_dataset = ./zfs_dataset.nix;

View file

@ -1,76 +0,0 @@
{ config, options, lib, diskoLib, ... }:
{
options = {
name = lib.mkOption {
type = lib.types.str;
default = config._module.args.name;
description = "Name of the logical volume";
};
type = lib.mkOption {
type = lib.types.enum [ "lvm_lv" ];
default = "lvm_lv";
internal = true;
description = "Type";
};
size = lib.mkOption {
type = lib.types.str; # TODO lvm size type
description = "Size of the logical volume";
};
lvm_type = lib.mkOption {
type = lib.types.nullOr (lib.types.enum [ "mirror" "raid0" "raid1" ]); # TODO add all lib.types
default = null; # maybe there is always a default type?
description = "LVM type";
};
extraArgs = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "Extra arguments";
};
content = diskoLib.partitionType;
_meta = lib.mkOption {
internal = true;
readOnly = true;
type = lib.types.functionTo diskoLib.jsonType;
default = dev:
lib.optionalAttrs (config.content != null) (config.content._meta dev);
description = "Metadata";
};
_create = diskoLib.mkCreateOption {
inherit config options;
default = { vg }: ''
lvcreate \
--yes \
${if lib.hasInfix "%" config.size then "-l" else "-L"} ${config.size} \
-n ${config.name} \
${lib.optionalString (config.lvm_type != null) "--type=${config.lvm_type}"} \
${toString config.extraArgs} \
${vg}
${lib.optionalString (config.content != null) (config.content._create {dev = "/dev/${vg}/${config.name}";})}
'';
};
_mount = diskoLib.mkMountOption {
inherit config options;
default = { vg }:
lib.optionalAttrs (config.content != null) (config.content._mount { dev = "/dev/${vg}/${config.name}"; });
};
_config = lib.mkOption {
internal = true;
readOnly = true;
default = vg:
[
(lib.optional (config.content != null) (config.content._config "/dev/${vg}/${config.name}"))
(lib.optional (config.lvm_type != null) {
boot.initrd.kernelModules = [ "dm-${config.lvm_type}" ];
})
];
description = "NixOS configuration";
};
_pkgs = lib.mkOption {
internal = true;
readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: lib.optionals (config.content != null) (config.content._pkgs pkgs);
description = "Packages";
};
};
}

View file

@ -12,7 +12,30 @@
description = "Type"; description = "Type";
}; };
lvs = lib.mkOption { lvs = lib.mkOption {
type = lib.types.attrsOf subTypes.lvm_lv; type = lib.types.attrsOf (lib.types.submodule ({ config, ... }: {
options = {
name = lib.mkOption {
type = lib.types.str;
default = config._module.args.name;
description = "Name of the logical volume";
};
size = lib.mkOption {
type = lib.types.str; # TODO lvm size type
description = "Size of the logical volume";
};
lvm_type = lib.mkOption {
type = lib.types.nullOr (lib.types.enum [ "mirror" "raid0" "raid1" ]); # TODO add all lib.types
default = null; # maybe there is always a default type?
description = "LVM type";
};
extraArgs = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = "Extra arguments";
};
content = diskoLib.partitionType;
};
}));
default = { }; default = { };
description = "LVS for the volume group"; description = "LVS for the volume group";
}; };
@ -21,7 +44,9 @@
readOnly = true; readOnly = true;
type = diskoLib.jsonType; type = diskoLib.jsonType;
default = default =
diskoLib.deepMergeMap (lv: lv._meta [ "lvm_vg" config.name ]) (lib.attrValues config.lvs); diskoLib.deepMergeMap (lv:
lib.optionalAttrs (lv.content != null) (lv.content._meta [ "lvm_vg" config.name ])
) (lib.attrValues config.lvs);
description = "Metadata"; description = "Metadata";
}; };
_create = diskoLib.mkCreateOption { _create = diskoLib.mkCreateOption {
@ -30,14 +55,25 @@
readarray -t lvm_devices < <(cat "$disko_devices_dir"/lvm_${config.name}) readarray -t lvm_devices < <(cat "$disko_devices_dir"/lvm_${config.name})
vgcreate ${config.name} \ vgcreate ${config.name} \
"''${lvm_devices[@]}" "''${lvm_devices[@]}"
${lib.concatMapStrings (lv: lv._create {vg = config.name; }) (lib.attrValues config.lvs)} ${lib.concatMapStrings (lv: ''
lvcreate \
--yes \
${if lib.hasInfix "%" lv.size then "-l" else "-L"} ${lv.size} \
-n ${lv.name} \
${lib.optionalString (lv.lvm_type != null) "--type=${lv.lvm_type}"} \
${toString lv.extraArgs} \
${config.name}
${lib.optionalString (lv.content != null) (lv.content._create {dev = "/dev/${config.name}/${lv.name}";})}
'') (lib.attrValues config.lvs)}
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {
inherit config options; inherit config options;
default = _: default = _:
let let
lvMounts = diskoLib.deepMergeMap (lv: lv._mount { vg = config.name; }) (lib.attrValues config.lvs); lvMounts = diskoLib.deepMergeMap (lv:
lib.optionalAttrs (lv.content != null) (lv.content._mount { dev = "/dev/${config.name}/${lv.name}"; })
) (lib.attrValues config.lvs);
in in
{ {
dev = '' dev = ''
@ -51,14 +87,21 @@
internal = true; internal = true;
readOnly = true; readOnly = true;
default = default =
map (lv: lv._config config.name) (lib.attrValues config.lvs); map (lv: [
(lib.optional (lv.content != null) (lv.content._config "/dev/${config.name}/${lv.name}"))
(lib.optional (lv.lvm_type != null) {
boot.initrd.kernelModules = [ "dm-${lv.lvm_type}" ];
})
]) (lib.attrValues config.lvs);
description = "NixOS configuration"; description = "NixOS configuration";
}; };
_pkgs = lib.mkOption { _pkgs = lib.mkOption {
internal = true; internal = true;
readOnly = true; readOnly = true;
type = lib.types.functionTo (lib.types.listOf lib.types.package); type = lib.types.functionTo (lib.types.listOf lib.types.package);
default = pkgs: lib.flatten (map (lv: lv._pkgs pkgs) (lib.attrValues config.lvs)); default = pkgs: lib.flatten (map (lv:
lib.optional (lv.content != null) (lv.content._pkgs pkgs)
) (lib.attrValues config.lvs));
description = "Packages"; description = "Packages";
}; };
}; };