diff options
-rw-r--r-- | custom/btrfs-snapshots.nix | 4 | ||||
-rw-r--r-- | custom/lvm-snapshots.nix | 95 | ||||
-rw-r--r-- | odin.nix | 10 |
3 files changed, 109 insertions, 0 deletions
diff --git a/custom/btrfs-snapshots.nix b/custom/btrfs-snapshots.nix index 44b08740..9dc3f1f8 100644 --- a/custom/btrfs-snapshots.nix +++ b/custom/btrfs-snapshots.nix | |||
@@ -40,6 +40,10 @@ in { | |||
40 | 40 | ||
41 | config = mkIf cfg.enable { | 41 | config = mkIf cfg.enable { |
42 | 42 | ||
43 | system.activationScripts."btrfs-snapshots" = '' | ||
44 | mkdir -p ${mountPoint} | ||
45 | ''; | ||
46 | |||
43 | systemd.services."btrfs-snapshot@" = { | 47 | systemd.services."btrfs-snapshot@" = { |
44 | enable = true; | 48 | enable = true; |
45 | 49 | ||
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 | } | ||
@@ -11,6 +11,7 @@ | |||
11 | ./users.nix | 11 | ./users.nix |
12 | ./custom/uucp.nix | 12 | ./custom/uucp.nix |
13 | ./custom/uucp-mediaserver.nix | 13 | ./custom/uucp-mediaserver.nix |
14 | ./custom/lvm-snapshots.nix | ||
14 | ]; | 15 | ]; |
15 | 16 | ||
16 | # Use the GRUB 2 boot loader. | 17 | # Use the GRUB 2 boot loader. |
@@ -147,6 +148,15 @@ | |||
147 | networks = ["127.0.0.0/8" "[::ffff:127.0.0.0]/104" "[::1]/128" "10.141.0.0/16"]; | 148 | networks = ["127.0.0.0/8" "[::ffff:127.0.0.0]/104" "[::1]/128" "10.141.0.0/16"]; |
148 | }; | 149 | }; |
149 | 150 | ||
151 | services.lvm-snapshots = { | ||
152 | snapshots = { | ||
153 | snapshot-mail = { | ||
154 | LV = "mail"; | ||
155 | VG = "raid6"; | ||
156 | }; | ||
157 | }; | ||
158 | }; | ||
159 | |||
150 | system.autoUpgrade.enable = true; | 160 | system.autoUpgrade.enable = true; |
151 | system.stateVersion = "18.09"; | 161 | system.stateVersion = "18.09"; |
152 | 162 | ||