all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Massimo Zaniboni <mzan@dokmelody.org>
To: 74273@debbugs.gnu.org
Cc: "Massimo Zaniboni" <mzan@dokmelody.org>,
	"Christopher Baines" <guix@cbaines.net>,
	"Josselin Poiret" <dev@jpoiret.xyz>,
	"Ludovic Courtès" <ludo@gnu.org>,
	"Mathieu Othacehe" <othacehe@gnu.org>,
	"Maxim Cournoyer" <maxim.cournoyer@gmail.com>,
	"Simon Tournier" <zimon.toutoune@gmail.com>,
	"Tobias Geerinckx-Rice" <me@tobias.gr>
Subject: [bug#74273] [PATCH v2] Improve bcachefs support
Date: Thu, 14 Nov 2024 20:18:56 +0100	[thread overview]
Message-ID: <6c9b20a33f44c3412a094ba0f75b9a6cb4eecb02.1731611936.git.mzan@dokmelody.org> (raw)
In-Reply-To: <2142f04036761f24a045a176098b1d0f958ce3bf.1731111823.git.mzan@dokmelody.org>

Improve bcachefs support: 
- recognize multi-device setup;
- mount degraded file-system with missing devices; 
- use the built-in kernel fscheck instead of user-space bcachefs-tools;

Change-Id: Ic741b70a7bce930da02c821c83c0a060875f4771
---

 doc/guix.texi               |  22 ++++++++
 gnu/build/file-systems.scm  | 105 ++++++++++++++++++++++++++++++------
 gnu/build/linux-boot.scm    |   3 +-
 gnu/machine/ssh.scm         |  23 +++++++-
 gnu/system/file-systems.scm |  15 ++++++
 guix/scripts/system.scm     |  25 ++++++++-
 6 files changed, 175 insertions(+), 18 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 2ab78d6..d962536 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -18152,6 +18152,28 @@ Btrfs file system
 compress-force=zstd,space_cache=v2"))
 @end lisp
 
+@menu
+* Bcachefs file system::
+@end menu
+
+@node Bcachefs file system
+@subsection Bcachefs file system
+
+Bcachefs supports RAID1/10-style redundancy, replicating data across multiple devices.
+To mount a file system with potentially missing devices but all data intact,
+the @code{degraded} option is required. This is an example of a multi-device setup:
+
+@lisp
+(file-system
+  (mount-point "/home")
+    (device "/dev/sdb:/dev/sdc:/dev/sdd")
+    (type "bcachefs")
+    (options "degraded")
+@end lisp
+
+Currently, bcachefs cannot be used as the root file-system in Guix,
+nor can it contain the Guix store.
+
 @node Mapped Devices
 @section Mapped Devices
 
diff --git a/gnu/build/file-systems.scm b/gnu/build/file-systems.scm
index 6fd9f95..89ef18c 100644
--- a/gnu/build/file-systems.scm
+++ b/gnu/build/file-systems.scm
@@ -10,6 +10,7 @@
 ;;; Copyright © 2024 Nicolas Graves <ngraves@ngraves.fr>
 ;;; Copyright © 2024 Richard Sent <richard@freakingpenguin.com>
 ;;; Copyright © 2024 Janneke Nieuwenhuizen <janneke@gnu.org>
+;;; Copyright © 2024 Massimo Zaniboni <mzan@dokmelody.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -348,6 +349,8 @@ (define-syntax %bcachefs-endianness
   ;; Endianness of bcachefs file systems.
   (identifier-syntax (endianness little)))
 
+;; FIXME at least since Linux kernel 6.11, the superblock is not
+;; recognized anymore.
 (define (bcachefs-superblock? sblock)
   "Return #t when SBLOCK is an bcachefs superblock."
   (bytevector=? (sub-bytevector sblock 24 16)
@@ -1143,10 +1146,10 @@ (define find-partition-by-luks-uuid
   (find-partition luks-partition-uuid-predicate))
 
 \f
-(define (canonicalize-device-spec spec)
-  "Return the device name corresponding to SPEC, which can be a <uuid>, a
-<file-system-label>, the string 'none' or another string (typically a /dev
-file name or an nfs-root containing ':/')."
+(define* (canonicalize-device-spec spec #:optional file-system-type)
+  "Return, usually at boot-time, the device name corresponding to SPEC,
+which can be a <uuid>, a <file-system-label>, the string 'none'
+or another string like a device, a multi-device, file name, nfs-root."
   (define max-trials
     ;; Number of times we retry partition label resolution, 1 second per
     ;; trial.  Note: somebody reported a delay of 16 seconds (!) before their
@@ -1154,6 +1157,11 @@ (define (canonicalize-device-spec spec)
     ;; this long.
     20)
 
+  (define file-system-type-str
+    (if (string? file-system-type)
+          file-system-type
+          "unknown"))
+
   (define (resolve find-partition spec fmt)
     (let loop ((count 0))
       (let ((device (find-partition spec)))
@@ -1168,20 +1176,73 @@ (define (canonicalize-device-spec spec)
                   (sleep 1)
                   (loop (+ 1 count))))))))
 
+  (define (stat-device device)
+    (stat device #f))
+
+  (define (check-bcachefs-superblock dev)
+    (= 0 (system*/tty "bcachefs" "show-super" "--field-only" "disk_groups" dev)))
+
+  (define (resolve-bcachefs-multi-device multi-device)
+    (let ((devices (string-split multi-device #\:)))
+      ;; Some devices take a bit of time to appear, most notably USB
+      ;; storage devices. Thus, wait for the device to appear.
+      ;; NOTE: it will wait MAX-TRIALS for all the devices,
+      ;; and not for any device.
+      (let loop
+       ((count 0))
+       (let ((missing-dev (find (lambda (d) (not (stat-device d))) devices)))
+         (when (and missing-dev (<= count max-trials))
+           (format #t "waiting for device '~a' to appear...~%" missing-dev)
+           (sleep 1)
+           (loop (+ 1 count)))))
+
+      ;; bcachefs can work in degraded mode using only few of the devices.
+      ;; As of Linux kernel 6.11.6, it requires that the missing/fault
+      ;; devices are removed from the multi-device specification,
+      ;; and that it is mounted with the "degraded" option.
+      (let ((valid-specs
+               (filter
+                 (lambda (d) (and (stat-device d)
+                                  (check-bcachefs-superblock d)))
+                 devices)))
+        (if (null? valid-specs)
+            (error "failed to resolve multi-device " multi-device))
+            (string-join valid-specs ":"))))
+
   (match spec
     ((? string?)
-     (if (or (string-contains spec ":/") ;nfs
-             (and (>= (string-length spec) 2)
-                  (equal? (string-take spec 2) "//")) ;cifs
-             (string=? spec "none"))
-         spec                  ; do not resolve NFS / CIFS / tmpfs devices
-         ;; Nothing to do, but wait until SPEC shows up.
-         (resolve identity spec identity)))
+     (cond
+       ((multi-device-spec? spec)
+        (cond
+         ((string=? file-system-type-str "bcachefs")
+            (resolve-bcachefs-multi-device spec))
+         (else (error
+                (string-append
+                   "unsupported multi-device specification "
+                   spec
+                   " for file-system type "
+                   file-system-type-str)))))
+       ((string-contains spec ":/")
+         ;NFS, something like 'server:/some/path'
+        spec)
+       ((and (>= (string-length spec) 2)
+             (equal? (string-take spec 2) "//"))
+         ;CIFS
+        spec)
+       ((string=? spec "none")
+         ;tmpfs
+        spec)
+       (else
+        ;; Nothing to do, but wait until SPEC shows up.
+        ; TODO it should use STAT on some devices instead of IDENTITY.
+        ; But using STAT on all DEVICES, the boot process will block.
+        ; At least, all other devices specified using labels and UUID are
+        ; processed using the more robust STAT function.
+        (resolve identity spec identity))))
     ((? file-system-label?)
      ;; Resolve the label.
      (resolve find-partition-by-label
-              (file-system-label->string spec)
-              identity))
+              (file-system-label->string spec) identity))
     ((? uuid?)
      (resolve find-partition-by-uuid
               (uuid-bytevector spec)
@@ -1194,10 +1255,24 @@ (define (check-file-system device type force? repair)
 found.  Otherwise, fix only those considered safe to repair automatically.  Not
 all TYPEs support all values or combinations of FORCE? and REPAIR.  Don't throw
 an exception in such cases but perform the nearest sane action."
+
+  (define (built-in-file-system-check device force? repair)
+    'pass)
+
   (define check-procedure
     (cond
      ((string-prefix? "ext" type) check-ext2-file-system)
-     ((string-prefix? "bcachefs" type) check-bcachefs-file-system)
+     ((string-prefix? "bcachefs" type)
+       ;; According bcachefs manual: "No special handling is needed for recovering
+       ;; from unclean shutdown. Journal replay happens automatically,
+       ;; and diagnostic messages in the dmesg log will indicate whether recovery
+       ;; was from clean or unclean shutdown."
+       ;; Moreover, at least until Linux kernel 6.11, the bcachefs-tools package
+       ;; does not try to respect the bcachefs format supported by the kernel.
+       ;; So, the fsck of bcachefs-tools is called only if explicitely stated.
+        (if force?
+          check-bcachefs-file-system
+          built-in-file-system-check))
      ((string-prefix? "btrfs" type) check-btrfs-file-system)
      ((string-suffix? "exfat" type) check-exfat-file-system)
      ((string-suffix? "fat" type) check-fat-file-system)
@@ -1385,7 +1460,7 @@ (define* (mount-file-system fs #:key (root "/root")
                                 "")))))
 
   (let* ((type    (file-system-type fs))
-         (source  (canonicalize-device-spec (file-system-device fs)))
+         (source  (canonicalize-device-spec (file-system-device fs) type))
          (target  (string-append root "/"
                                  (file-system-mount-point fs)))
          (flags   (logior (mount-flags->bit-mask (file-system-flags fs))
diff --git a/gnu/build/linux-boot.scm b/gnu/build/linux-boot.scm
index 548e28a..d184fcd 100644
--- a/gnu/build/linux-boot.scm
+++ b/gnu/build/linux-boot.scm
@@ -635,7 +635,8 @@ (define* (boot-system #:key
 
         ;; Mount the root file system.
         (mount-root-file-system (canonicalize-device-spec
-                                 (file-system-device root-fs))
+                                 (file-system-device root-fs)
+                                 (file-system-type root-fs))
                                 (file-system-type root-fs)
                                 #:volatile-root? volatile-root?
                                 #:flags (mount-flags->bit-mask
diff --git a/gnu/machine/ssh.scm b/gnu/machine/ssh.scm
index 3e10d98..0054adf 100644
--- a/gnu/machine/ssh.scm
+++ b/gnu/machine/ssh.scm
@@ -2,6 +2,7 @@
 ;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.org>
 ;;; Copyright © 2020-2023 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2024 Ricardo <rekado@elephly.net>
+;;; Copyright © 2024 Massimo Zaniboni <mzan@dokmelody.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -241,6 +242,22 @@ (define (machine-check-file-system-availability machine)
                                   (file-system-device fs)
                                   (strerror errno))))))
 
+  (define (check-multi-device-file-system fs)
+    (define multi-device (file-system-device fs))
+    (define devices (string-split multi-device #\:))
+    (define (check-device device)
+      (remote-let ((errno #~(catch 'system-error
+                              (lambda ()
+                                (stat #$device)
+                              #t)
+                            (lambda args
+                              (system-error-errno args)))))
+        (when (number? errno)
+          (raise (formatted-message (G_ "device '~a' not found: ~a")
+                                    device
+                                    (strerror errno))))))
+    (map check-device devices))
+
   (define (check-labeled-file-system fs)
     (define remote-exp
       (with-imported-modules (source-module-closure
@@ -278,8 +295,12 @@ (define (machine-check-file-system-availability machine)
        (machine-configuration machine))
       (append (map check-literal-file-system
                    (filter (lambda (fs)
-                             (string? (file-system-device fs)))
+                             (single-device-spec? (file-system-device fs)))
                            file-systems))
+              (append-map check-multi-device-file-system
+                          (filter (lambda (fs)
+                                    (multi-device-spec? (file-system-device fs)))
+                                  file-systems))
               (map check-labeled-file-system
                    (filter (lambda (fs)
                              (file-system-label? (file-system-device fs)))
diff --git a/gnu/system/file-systems.scm b/gnu/system/file-systems.scm
index 4ea8237..9f91bd7 100644
--- a/gnu/system/file-systems.scm
+++ b/gnu/system/file-systems.scm
@@ -5,6 +5,7 @@
 ;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
 ;;; Copyright © 2021 Tobias Geerinckx-Rice <me@tobias.gr>
 ;;; Copyright © 2022 Oleg Pykhalov <go.wigust@gmail.com>
+;;; Copyright © 2024 Massimo Zaniboni <mzan@dokmelody.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -73,6 +74,9 @@ (define-module (gnu system file-systems)
             spec->file-system
             specification->file-system-mapping
 
+            multi-device-spec?
+            single-device-spec?
+
             %pseudo-file-system-types
             %fuse-control-file-system
             %binary-format-file-system
@@ -309,6 +313,17 @@ (define (file-system-needed-for-boot? fs)
       (and (file-prefix? (file-system-mount-point fs) (%store-prefix))
            (not (memq 'bind-mount (file-system-flags fs))))))
 
+(define (multi-device-spec? spec)
+  "Return #t if the specification is like '/dev/sda:/dev/sdb'."
+  (and (string? spec)
+       (string-contains spec ":/")
+       (string-prefix? "/dev/" spec)))
+
+(define (single-device-spec? spec)
+  "Return #t if the specification is a string, but not a multi-device."
+  (and (string? spec)
+       (not (multi-device-spec? spec))))
+
 (define (file-system->spec fs)
   "Return a list corresponding to file-system FS that can be passed to the
 initrd code."
diff --git a/guix/scripts/system.scm b/guix/scripts/system.scm
index 7989b18..4e9c581 100644
--- a/guix/scripts/system.scm
+++ b/guix/scripts/system.scm
@@ -11,6 +11,7 @@
 ;;; Copyright © 2021 Brice Waegeneire <brice@waegenei.re>
 ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
 ;;; Copyright © 2022 Tobias Geerinckx-Rice <me@tobias.gr>
+;;; Copyright © 2024 Massimo Zaniboni <mzan@dokmelody.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -605,9 +606,16 @@ (define (check-file-system-availability file-systems)
               (file-system-label? (file-system-device fs)))
             relevant))
 
+  (define multi-device
+    (filter (lambda (fs)
+              (and (string? (file-system-device fs))
+                   (multi-device-spec? (file-system-device fs))))
+            relevant))
+
   (define literal
     (filter (lambda (fs)
-              (string? (file-system-device fs)))
+              (and (string? (file-system-device fs))
+                   (single-device-spec? (file-system-device fs))))
             relevant))
 
   (define uuid
@@ -641,6 +649,21 @@ (define (check-file-system-availability file-systems)
 label, write @code{(file-system-label ~s)} in your @code{device} field.")
                                       device device))))))
               literal)
+    (for-each
+       (lambda (fs)
+         (let* ((devices-str (file-system-device fs))
+                (devices (string-split devices-str #\:)))
+            (for-each
+              (lambda (device)
+                (catch 'system-error
+                  (lambda () (stat device))
+                  (lambda args
+                    (let ((errno  (system-error-errno args)))
+                          (error (file-system-location* fs)
+                                 (G_ " #8605 device '~a' not found in multi-device '~a': ~a~%")
+                                 device devices-str (strerror errno))))))
+            devices)))
+       multi-device)
     (for-each (lambda (fs)
                 (let ((label (file-system-label->string
                               (file-system-device fs))))

base-commit: c1cb7f1031c5dde2a260d8d8ad7547d6c79cc532
prerequisite-patch-id: e3ec1271b30da286e1a2fdd1519a8c504e52d64a
prerequisite-patch-id: 25d78fbfbd3268c16c93cd5d222386a7f421979b
prerequisite-patch-id: 8ca774fc68440ec5233b5353f11886b1712e6b43
prerequisite-patch-id: 0000000000000000000000000000000000000000
prerequisite-patch-id: e22870a8d4b3ab67b12e05b6242b7f1bf5ac193b
-- 
2.46.0





      reply	other threads:[~2024-11-14 20:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-09  0:23 [bug#74273] [PATCH] Support for bcachefs-like multi-device file-systems Massimo Zaniboni
2024-11-14 19:18 ` Massimo Zaniboni [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6c9b20a33f44c3412a094ba0f75b9a6cb4eecb02.1731611936.git.mzan@dokmelody.org \
    --to=mzan@dokmelody.org \
    --cc=74273@debbugs.gnu.org \
    --cc=dev@jpoiret.xyz \
    --cc=guix@cbaines.net \
    --cc=ludo@gnu.org \
    --cc=maxim.cournoyer@gmail.com \
    --cc=me@tobias.gr \
    --cc=othacehe@gnu.org \
    --cc=zimon.toutoune@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.