diff mbox series

[bug#46063] WIP snapper fixes and snapperd service

Message ID 20210124031337.72dfb12c@riseup.net
State New
Headers show
Series [bug#46063] WIP snapper fixes and snapperd service | expand

Checks

Context Check Description
cbaines/applying patch fail View Laminar job
cbaines/issue success View issue

Commit Message

Csepp Jan. 24, 2021, 2:13 a.m. UTC
Hi!

I have a technically working but still only lightly tested and not very
user friendly patch set for making Snapper work!

Included is Pierre Neidhardt's original patch, followed by my patches.

How to try it:
Everything needs sudo for now, sorry about that. This could probably be
changed so that users could administrate configs pertaining only to
their home dirs, but it looks like ading new configs will require sudo
no matter what.

0. Have a BTRFS file system mounted somewhere. You may have heard that
other setups (like ext4 or thin LVM) also works, but based on issue
tracker activity, it looks very much like they don't, or aren't
maintained and can break at any time.

```
# Create new subvolume
btrfs subvolume create
# Attempt to create a new config without understanding what you're
getting into:
sudo snapper -c my-config-name create-config path/to/subvolume
# This will show informative messages like `Failure (error.something).`
# Obviously this is because you haven't populated the relevant parts of
# /etc. Duh. So do this, and think about how fun it must have been to
# track down the sources of these messages.
sudo cp -r $(./pre-inst-env guix build snapper)/etc/snapper /etc/snapper
sudo install -D -m 644 $(./pre-inst-env guix build --source snapper)/data/sysconfig.snapper /etc/sysconfig/snapper
# Add snapper-service-type to an operating-system. I assume you know how to do it on your own.
# If anything breaks:
sudo herd status snapperd
# It sometimes stops for some reason? Just enable it and restart.
# Anyways, now you can create a configuration. :D
sudo snapper -c my-config-name create-config path/to/subvolume
```

Now just continue trying stuff from one of the many Snapper tutorials
around the net, like the Arch Wiki one.

Yay! Was that unnecessarily hard? It was. That's why this is a WIP
patchset and why I need some tips on making it work better.

* Should snapper be configured to use something other than
  /etc/sysconfig? The sysconfig subdirectory doesn't really make sense
  on Guix, but maybe it's not worth bikeshedding.
* (How) should homes be converted to BTRFS subvolumes?
* Snapshot scheduling is still TODO.
* Should /etc/snapper even be mutable? How to handle the configuration
  data type?

TLDR: I have some ideas on how to proceed, but would welcome some input
from people who have some experience setting up backups and
snapshotting, either on Guix, or Nix, or some traditional distro.
diff mbox series

Patch

From 2fb7a62710d17cee87c5cf4c73df49fdee3b668f Mon Sep 17 00:00:00 2001
From: raingloom <raingloom@riseup.net>
Date: Fri, 8 Jan 2021 23:02:01 +0100
Subject: [PATCH 3/3] WIP: gnu: services: Added basics of snapper service.

---
 gnu/packages/linux.scm |  7 ++++-
 gnu/services/linux.scm | 60 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/gnu/packages/linux.scm b/gnu/packages/linux.scm
index 16279e50c3..dca77e2fa4 100644
--- a/gnu/packages/linux.scm
+++ b/gnu/packages/linux.scm
@@ -5083,6 +5083,10 @@  obviously it can be shared with files outside our set).")
     (license license:gpl2+)))
 
 (define-public snapper
+  ;; TODO: create full system tests
+  ;; FIXME: client can't find "config template". what even is that.
+  ;; TODO: generate /etc/sysconfig/snapper from Guix
+  ;; TODO: snapperd should take a command line argument instead of hardcoding config path
   (package
     (name "snapper")
     (version "0.8.15")
@@ -5146,7 +5150,8 @@  obviously it can be shared with files outside our set).")
                  (("(pam_snapperdir = )/usr(/lib/pam_snapper)" _ before after)
                   (string-append before out after)))
                (substitute* "data/Makefile.am"
-                 (("\\$\\(DESTDIR\\)") out))
+                 (("\\$\\(DESTDIR\\)") out)
+                 (("/usr/") "/"))
                (substitute* "pam/Makefile.am"
                  (("(securelibdir = )\\$\\(shell echo /`basename \\$\\(libdir\\)`/security\\)" _ before)
                   (string-append before out "/lib/security"))))))
diff --git a/gnu/services/linux.scm b/gnu/services/linux.scm
index 1046a7e0c2..7dfee8d9cd 100644
--- a/gnu/services/linux.scm
+++ b/gnu/services/linux.scm
@@ -24,6 +24,7 @@ 
   #:use-module (guix modules)
   #:use-module (gnu services)
   #:use-module (gnu services base)
+  #:use-module (gnu services dbus)
   #:use-module (gnu services shepherd)
   #:use-module (gnu packages linux)
   #:use-module (srfi srfi-1)
@@ -46,6 +47,11 @@ 
 
             kernel-module-loader-service-type
 
+            snapper-configuration
+            snapper-configuration?
+            snapper-configuration-snapper
+            snapper-service-type
+
             zram-device-configuration
             zram-device-configuration?
             zram-device-configuration-size
@@ -187,6 +193,60 @@  representation."
    (extend append)
    (default-value '())))
 
+
+;;;
+;;; File system snapshotter
+;;;
+
+;; TODO: other services might want to extend it with filters
+;; TODO: extend PAM and snapshot home on login (see man pam_snapper)
+;; TODO: convert pam_snapper_homeconvert.sh into a shepherd service
+;; TODO: data type for snapper configs
+
+(define-record-type* <snapper-configuration>
+  snapper-configuration make-snapper-configuration
+  snapper-configuration?
+  (snapper snapper-configuration-snapper
+           (default snapper)))
+
+(define (snapper-scm->config key)
+  (let* ((key-lo (string-downcase key))
+         (maybe-scm-key (assoc-ref
+                         '(("file-system-type" . "fstype")
+                           ("quote-group" . "qgroup"))
+                         key-lo)))
+    (string-upcase
+     (string-map
+      (lambda (c)
+        (if (eq? #\- c)
+            #\_
+            c))
+      (or maybe-scm-key key-lo)))))
+
+(define (snapper-shepherd-service config)
+  (shepherd-service
+   (documentation "Run the Snapper daemon (snapperd).")
+   (provision '(snapperd))
+   (start #~(make-forkexec-constructor
+             '#$(list (file-append
+                       (snapper-configuration-snapper config)
+                       "/sbin/snapperd"))
+             #:log-file "/var/log/snapperd.log"))
+   (stop #~(make-kill-destructor))))
+
+(define snapper-service-type
+  (service-type
+   (name 'snapper)
+   (extensions
+    (list
+     (service-extension shepherd-root-service-type
+                        (compose list snapper-shepherd-service))
+     (service-extension dbus-root-service-type
+                        (compose list snapper-configuration-snapper))))
+   (default-value (snapper-configuration))
+   (description
+    "Create periodic snapshots on BTRFS subvolumes and thin LVM volumes")))
+
 
 ;;;
 ;;; Kernel module loader.
-- 
2.30.0