diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2018-04-09 12:24:09 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2018-04-09 12:24:09 +0200 |
commit | 92fcc3cd4fc50bba1b2b2d95f8bed4e0a5a03136 (patch) | |
tree | 52563407085f5f48b0be659b29d82a9474e0da02 /custom/lvm-snapshots.nix | |
parent | e52e5ea5494319f158e9d9c1425b16b9986e929a (diff) | |
download | nixos-92fcc3cd4fc50bba1b2b2d95f8bed4e0a5a03136.tar nixos-92fcc3cd4fc50bba1b2b2d95f8bed4e0a5a03136.tar.gz nixos-92fcc3cd4fc50bba1b2b2d95f8bed4e0a5a03136.tar.bz2 nixos-92fcc3cd4fc50bba1b2b2d95f8bed4e0a5a03136.tar.xz nixos-92fcc3cd4fc50bba1b2b2d95f8bed4e0a5a03136.zip |
lvm snapshots
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 | } | ||