From 32282ae39d352428988891207fb4f276a311846a Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 6 Feb 2022 21:20:24 +0100 Subject: vidhar: borg --- modules/borgbackup/lvm-snapshots.nix | 133 ----------------------------------- 1 file changed, 133 deletions(-) delete mode 100644 modules/borgbackup/lvm-snapshots.nix (limited to 'modules/borgbackup/lvm-snapshots.nix') diff --git a/modules/borgbackup/lvm-snapshots.nix b/modules/borgbackup/lvm-snapshots.nix deleted file mode 100644 index 9b2a6562..00000000 --- a/modules/borgbackup/lvm-snapshots.nix +++ /dev/null @@ -1,133 +0,0 @@ -{ config, lib, utils, pkgs, ... }: - -with utils; -with lib; - -let - cfg = config.services.lvm-snapshots; - - snapshotMount = name: "${cfg.mountPoint}/${if isNull cfg.snapshots."${name}".mountName then name else cfg.snapshots."${name}".mountName}"; - snapshotName = name: "${name}-${cfg.mountSuffix}"; - - snapshotConfig = { - options = { - LV = mkOption { - type = types.str; - }; - - VG = mkOption { - type = types.str; - }; - - mountName = mkOption { - type = types.nullOr types.str; - default = null; - }; - - cowSize = mkOption { - type = types.str; - default = "-l20%ORIGIN"; - }; - - readOnly = mkOption { - type = types.bool; - default = true; - }; - - persist = mkOption { - type = types.bool; - default = false; - }; - }; - }; -in { - options = { - - services.lvm-snapshots = { - snapshots = mkOption { - type = types.attrsOf (types.submodule snapshotConfig); - default = {}; - }; - - mountPoint = mkOption { - type = types.path; - default = "/mnt"; - }; - - mountSuffix = mkOption { - type = types.str; - default = "-snapshot"; - }; - }; - }; - - - config = mkIf (cfg != {}) { - - boot.kernelModules = [ "dm_snapshot" ]; - - # system.activationScripts = mapAttrs' (name: scfg: nameValuePair ("lvm-mountpoint" + name) '' - # mkdir -p ${snapshotMount name} - # '') cfg.snapshots; - - systemd.services = mapAttrs' (name: scfg: nameValuePair ("lvm-snapshot@" + escapeSystemdPath name) { - enable = true; - - description = "LVM-snapshot of ${scfg.VG}/${scfg.LV}"; - - bindsTo = ["${escapeSystemdPath "/dev/${scfg.VG}/${scfg.LV}"}.device"]; - after = ["${escapeSystemdPath "/dev/${scfg.VG}/${scfg.LV}"}.device"]; - - unitConfig = { - StopWhenUnneeded = !scfg.persist; - AssertPathIsDirectory = "/var/lock"; - }; - - path = with pkgs; [ devicemapper utillinux ]; - - script = '' - ( - flock -xn -E 4 9 - if [[ "$?" -ne 0 ]]; then - exit $? - fi - - lvcreate -s ${scfg.cowSize} --name ${snapshotName name} ${scfg.VG}/${scfg.LV} - - sleep infinity & - ) 9>/var/lock/lvm-snapshot.${scfg.VG} - ''; - - preStart = '' - lvremove -f ${scfg.VG}/${snapshotName name} - ''; - - preStop = '' - lvremove -f ${scfg.VG}/${snapshotName name} - ''; - - serviceConfig = with pkgs; { - Type = "forking"; - RestartForceExitStatus = [ "4" ]; - RestartSec = "5min"; - }; - }) cfg.snapshots; - - systemd.mounts = mapAttrsToList (name: scfg: { - enable = true; - - unitConfig = { - # AssertPathIsDirectory = snapshotMount name; - StopWhenUnneeded = !scfg.persist; - }; - - bindsTo = [ ("lvm-snapshot@" + escapeSystemdPath name + ".service") ]; - after = [ ("lvm-snapshot@" + escapeSystemdPath name + ".service") ]; - - options = concatStringsSep "," ([ "noauto" ] ++ optional scfg.readOnly "ro"); - - where = snapshotMount name; - what = "/dev/" + scfg.VG + "/" + snapshotName name; - }) cfg.snapshots; - }; -} -- cgit v1.2.3