1 { config, lib, pkgs, ... }:
5 cfg = config.services.lvm;
7 options.services.lvm = {
8 enable = mkEnableOption "lvm2" // {
16 defaultText = literalExpression "pkgs.lvm2";
18 This option allows you to override the LVM package that's used on the system
19 (udev rules, tmpfiles, systemd services).
20 Defaults to pkgs.lvm2, pkgs.lvm2_dmeventd if dmeventd or pkgs.lvm2_vdo if vdo is enabled.
23 dmeventd.enable = mkEnableOption "the LVM dmevent daemon";
24 boot.thin.enable = mkEnableOption "support for booting from ThinLVs";
25 boot.vdo.enable = mkEnableOption "support for booting from VDOLVs";
28 options.boot.initrd.services.lvm.enable = mkEnableOption "booting from LVM2 in the initrd" // {
30 *This will only be used when systemd is used in stage 1.*
32 Whether to enable booting from LVM2 in the initrd.
38 # minimal configuration file to make lvmconfig/lvm2-activation-generator happy
39 environment.etc."lvm/lvm.conf".text = "config {}";
42 systemd.tmpfiles.packages = [ cfg.package.out ];
43 environment.systemPackages = [ cfg.package ];
44 systemd.packages = [ cfg.package ];
46 services.udev.packages = [ cfg.package.out ];
48 (mkIf config.boot.initrd.services.lvm.enable {
49 # We need lvm2 for the device-mapper rules
50 boot.initrd.services.udev.packages = [ cfg.package ];
51 # The device-mapper rules want to call tools from lvm2
52 boot.initrd.systemd.initrdBin = [ cfg.package ];
53 boot.initrd.services.udev.binPackages = [ cfg.package ];
55 (mkIf cfg.dmeventd.enable {
56 systemd.sockets."dm-event".wantedBy = [ "sockets.target" ];
57 systemd.services."lvm2-monitor".wantedBy = [ "sysinit.target" ];
59 environment.etc."lvm/lvm.conf".text = ''
60 dmeventd/executable = "${cfg.package}/bin/dmeventd"
62 services.lvm.package = mkDefault pkgs.lvm2_dmeventd;
64 (mkIf cfg.boot.thin.enable {
66 kernelModules = [ "dm-snapshot" "dm-thin-pool" ];
68 systemd.initrdBin = lib.mkIf config.boot.initrd.services.lvm.enable [ pkgs.thin-provisioning-tools ];
70 extraUtilsCommands = mkIf (!config.boot.initrd.systemd.enable) ''
71 for BIN in ${pkgs.thin-provisioning-tools}/bin/*; do
72 copy_bin_and_libs $BIN
76 extraUtilsCommandsTest = mkIf (!config.boot.initrd.systemd.enable) ''
77 ls ${pkgs.thin-provisioning-tools}/bin/ | grep -v pdata_tools | while read BIN; do
78 $out/bin/$(basename $BIN) --help > /dev/null
83 environment.etc."lvm/lvm.conf".text = concatMapStringsSep "\n"
84 (bin: "global/${bin}_executable = ${pkgs.thin-provisioning-tools}/bin/${bin}")
85 [ "thin_check" "thin_dump" "thin_repair" "cache_check" "cache_dump" "cache_repair" ];
87 environment.systemPackages = [ pkgs.thin-provisioning-tools ];
89 (mkIf cfg.boot.vdo.enable {
91 assertion = lib.versionAtLeast config.boot.kernelPackages.kernel.version "6.9";
92 message = "boot.vdo.enable requires at least kernel version 6.9";
97 kernelModules = [ "dm-vdo" ];
99 systemd.initrdBin = lib.mkIf config.boot.initrd.services.lvm.enable [ pkgs.vdo ];
101 extraUtilsCommands = mkIf (!config.boot.initrd.systemd.enable)''
102 ls ${pkgs.vdo}/bin/ | while read BIN; do
103 copy_bin_and_libs ${pkgs.vdo}/bin/$BIN
105 substituteInPlace $out/bin/vdorecover --replace "${pkgs.bash}/bin/bash" "/bin/sh"
106 substituteInPlace $out/bin/adaptlvm --replace "${pkgs.bash}/bin/bash" "/bin/sh"
109 extraUtilsCommandsTest = mkIf (!config.boot.initrd.systemd.enable)''
110 ls ${pkgs.vdo}/bin/ | grep -vE '(adaptlvm|vdorecover)' | while read BIN; do
111 $out/bin/$(basename $BIN) --help > /dev/null
117 services.lvm.package = mkOverride 999 pkgs.lvm2_vdo; # this overrides mkDefault
119 environment.systemPackages = [ pkgs.vdo ];
121 (mkIf (cfg.dmeventd.enable || cfg.boot.thin.enable) {
122 boot.initrd.systemd.contents."/etc/lvm/lvm.conf".text = optionalString (config.boot.initrd.services.lvm.enable && cfg.boot.thin.enable) (concatMapStringsSep "\n"
123 (bin: "global/${bin}_executable = /bin/${bin}")
124 [ "thin_check" "thin_dump" "thin_repair" "cache_check" "cache_dump" "cache_repair" ]
125 ) + "\n" + optionalString cfg.dmeventd.enable ''
126 dmeventd/executable = /bin/false
127 activation/monitoring = 0
130 boot.initrd.preLVMCommands = mkIf (!config.boot.initrd.systemd.enable) ''
132 cat << EOF >> /etc/lvm/lvm.conf
133 ${optionalString cfg.boot.thin.enable (
134 concatMapStringsSep "\n"
135 (bin: "global/${bin}_executable = $(command -v ${bin})")
136 [ "thin_check" "thin_dump" "thin_repair" "cache_check" "cache_dump" "cache_repair" ]
139 ${optionalString cfg.dmeventd.enable ''
140 dmeventd/executable = "$(command -v false)"
141 activation/monitoring = 0