diff options
Diffstat (limited to 'custom/lvm-snapshots.nix')
| -rw-r--r-- | custom/lvm-snapshots.nix | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/custom/lvm-snapshots.nix b/custom/lvm-snapshots.nix new file mode 100644 index 00000000..67ee2cbd --- /dev/null +++ b/custom/lvm-snapshots.nix | |||
| @@ -0,0 +1,95 @@ | |||
| 1 | { config, lib, pkgs, ... }: | ||
| 2 | |||
| 3 | with lib; | ||
| 4 | |||
| 5 | let | ||
| 6 | cfg = config.services.lvm-snapshots; | ||
| 7 | |||
| 8 | snapshotMount = name: "${cfg.mountPoint}/${if isNull cfg.snapshots."${name}".mountName then name else cfg.snapshots."${name}".mountName}"; | ||
| 9 | |||
| 10 | snapshotConfig = { | ||
| 11 | options = { | ||
| 12 | LV = mkOption { | ||
| 13 | type = types.str; | ||
| 14 | }; | ||
| 15 | |||
| 16 | VG = mkOption { | ||
| 17 | type = types.str; | ||
| 18 | }; | ||
| 19 | |||
| 20 | mountName = mkOption { | ||
| 21 | type = types.nullOr types.str; | ||
| 22 | default = null; | ||
| 23 | }; | ||
| 24 | |||
| 25 | cowSize = mkOption { | ||
| 26 | type = types.str; | ||
| 27 | default = "-l20%ORIGIN"; | ||
| 28 | }; | ||
| 29 | |||
| 30 | readOnly = mkOption { | ||
| 31 | type = types.bool; | ||
| 32 | default = true; | ||
| 33 | }; | ||
| 34 | |||
| 35 | persist = mkOption { | ||
| 36 | type = types.bool; | ||
| 37 | default = false; | ||
| 38 | }; | ||
| 39 | }; | ||
| 40 | }; | ||
| 41 | in { | ||
| 42 | options = { | ||
| 43 | |||
| 44 | services.lvm-snapshots = { | ||
| 45 | snapshots = mkOption { | ||
| 46 | type = types.attrsOf (types.submodule snapshotConfig); | ||
| 47 | default = {}; | ||
| 48 | }; | ||
| 49 | |||
| 50 | mountPoint = mkOption { | ||
| 51 | type = types.path; | ||
| 52 | default = "/mnt"; | ||
| 53 | }; | ||
| 54 | }; | ||
| 55 | }; | ||
| 56 | |||
| 57 | |||
| 58 | config = mkIf (cfg != {}) { | ||
| 59 | |||
| 60 | system.activationScripts = mapAttrs' (name: scfg: nameValuePair (lvm-mountpoint + name) '' | ||
| 61 | mkdir -p ${snapshotMount name} | ||
| 62 | '') cfg.snapshots; | ||
| 63 | |||
| 64 | systemd.services = mapAttrs' (name: scfg: nameValuePair ("lvm-snapshot@" + name) { | ||
| 65 | enable = true; | ||
| 66 | |||
| 67 | unitConfig = { | ||
| 68 | StopWhenUnneeded = true; | ||
| 69 | }; | ||
| 70 | |||
| 71 | serviceConfig = with pkgs; { | ||
| 72 | Type = "oneshot"; | ||
| 73 | ExecStart = "${devicemapper}/bin/lvcreate -s ${cfg.cowSize} --name ${name} ${scfg.VG}/${scfg.LV}"; | ||
| 74 | ExecStop = "${devicemapper}/bin/lvremove ${scfg.VG}/${name}"; | ||
| 75 | RemainAfterExit = true; | ||
| 76 | }; | ||
| 77 | }) cfg.snapshots; | ||
| 78 | |||
| 79 | systemd.mounts = mapAttrsToList (name: scfg: { | ||
| 80 | enable = true; | ||
| 81 | |||
| 82 | unitConfig = { | ||
| 83 | AssertPathIsDirectory = snapshotMount name; | ||
| 84 | StopWhenUnneeded = !scfg.persist; | ||
| 85 | }; | ||
| 86 | |||
| 87 | bindsTo = "lvm-snapshot@" + name; | ||
| 88 | |||
| 89 | options = mkIf scfg.readOnly "ro"; | ||
| 90 | |||
| 91 | where = snapshotMount name; | ||
| 92 | what = "/dev/" + scfg.VG + "/" + name; | ||
| 93 | }) cfg.snapshots; | ||
| 94 | }; | ||
| 95 | } | ||
