From 602994847b748937b6fa39a7b819429857cdd8d3 Mon Sep 17 00:00:00 2001 From: Tobias Geerinckx-Rice Date: Sat, 15 May 2021 15:29:40 +0200 Subject: file-systems: Support forced checks & repairs. * gnu/build/file-systems.scm (check-ext2-file-system) (check-bcachefs-file-system, check-btrfs-file-system) (check-fat-file-system, check-jfs-file-system, check-f2fs-file-system) (check-ntfs-file-system, check-file-system): Take and honour new FORCE? and REPAIR arguments. Update the docstring. Adjust all callers. * gnu/system/file-systems.scm : Add new SKIP-CHECK-IF-CLEAN? and REPAIR fields. (file-system->spec, spec->file-system): Adjust accordingly. * gnu/build/linux-boot.scm (mount-root-file-system): Take new SKIP-CHECK-IF-CLEAN? and REPAIR keyword arguments. Thread them through to CHECK-FILE-SYSTEM. * doc/guix.texi (File Systems): Document both new options. --- gnu/build/linux-boot.scm | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'gnu/build/linux-boot.scm') diff --git a/gnu/build/linux-boot.scm b/gnu/build/linux-boot.scm index 95d0a1fe79..ab05d1ba5e 100644 --- a/gnu/build/linux-boot.scm +++ b/gnu/build/linux-boot.scm @@ -408,12 +408,17 @@ the last argument of `mknod'." (define* (mount-root-file-system root type #:key volatile-root? (flags 0) options - check?) + check? skip-check-if-clean? repair) "Mount the root file system of type TYPE at device ROOT. If VOLATILE-ROOT? is true, mount ROOT read-only and make it an overlay with a writable tmpfs using the kernel built-in overlayfs. FLAGS and OPTIONS indicates the options to use to mount ROOT, and behave the same as for the `mount' procedure. -If CHECK? is true, first run ROOT's fsck tool (if any) non-interactively." + +If CHECK? is true, first run ROOT's fsck tool (if any) non-interactively. +If SKIP-CHECK-IF-CLEAN? is true, ask fsck to return immediately if ROOT is +marked as clean. If REPAIR is true, fsck may write to ROOT to perform repairs. +If REPAIR is also 'PREEN, ask fsck to perform only those repairs that it +considers safe." (if volatile-root? (begin @@ -435,7 +440,7 @@ If CHECK? is true, first run ROOT's fsck tool (if any) non-interactively." "lowerdir=/real-root,upperdir=/rw-root/upper,workdir=/rw-root/work")) (begin (when check? - (check-file-system root type)) + (check-file-system root type (not skip-check-if-clean?) repair)) (mount root "/root" type flags options))) ;; Make sure /root/etc/mtab is a symlink to /proc/self/mounts. @@ -612,7 +617,13 @@ upon error." #:options root-options #:check? (if root-fs (file-system-check? root-fs) - #t)) + #t) + #:skip-check-if-clean? + (and=> root-fs + file-system-skip-check-if-clean?) + #:repair (if root-fs + (file-system-repair root-fs) + 'preen)) (mount "none" "/root" "tmpfs")) ;; Mount the specified file systems. -- cgit v1.2.3 From a75a3d71329d3ca07a2ef18b81fc7b463f703ed7 Mon Sep 17 00:00:00 2001 From: Tobias Geerinckx-Rice Date: Sun, 23 May 2021 17:02:29 +0200 Subject: linux-boot: Honour fsck.mode & fsck.repair. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * gnu/build/linux-boot.scm (boot-system): Honour ‘fsck.mode=’ and ‘fsck.repair=’ kernel command line options. * doc/guix.texi (Initial RAM Disk): Document both. --- doc/guix.texi | 19 +++++++++++++ gnu/build/linux-boot.scm | 72 +++++++++++++++++++++++++++++------------------- 2 files changed, 63 insertions(+), 28 deletions(-) (limited to 'gnu/build/linux-boot.scm') diff --git a/doc/guix.texi b/doc/guix.texi index a62578be26..dc9b039aab 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -33285,6 +33285,25 @@ name like @code{/dev/sda1}, a file system label, or a file system UUID. When unspecified, the device name from the root file system of the operating system declaration is used. +@item fsck.mode=@var{mode} +Whether to check the @var{root} file system for errors before mounting +it. @var{mode} is one of @code{skip} (never check), @code{force} (always +check), or @code{auto} to respect the root file-system object's 'check?' +setting (@pxref{File Systems}) and run a full scan only if the file system +was not cleanly shut down. + +@code{auto} is the default if this option is not present or if @var{mode} +is not one of the above. + +@item fsck.repair=@var{level} +The level of repairs to perform automatically if errors are found in the +@var{root} file system. @var{level} is one of @code{no} (do not write to +@var{root} at all if possible), @code{yes} (repair as much as possible), +or @code{preen} to repair problems considered safe to repair automatically. + +@code{preen} is the default if this option is not present or if @var{level} +is not one of the above. + @item --system=@var{system} Have @file{/run/booted-system} and @file{/run/current-system} point to @var{system}. diff --git a/gnu/build/linux-boot.scm b/gnu/build/linux-boot.scm index ab05d1ba5e..8f0f3eb2fc 100644 --- a/gnu/build/linux-boot.scm +++ b/gnu/build/linux-boot.scm @@ -541,21 +541,36 @@ upon error." (mount-essential-file-systems) (let* ((args (linux-command-line)) (to-load (find-long-option "--load" args)) - (root-fs (find root-mount-point? mounts)) - (root-fs-type (or (and=> root-fs file-system-type) - "ext4")) - (root-fs-device (and=> root-fs file-system-device)) - (root-fs-flags (mount-flags->bit-mask - (or (and=> root-fs file-system-flags) - '()))) - (root-options (if root-fs - (file-system-options root-fs) - #f)) - ;; --root takes precedence over the 'device' field of the root - ;; record. - (root-device (or (and=> (find-long-option "--root" args) - device-string->file-system-device) - root-fs-device))) + ;; If present, ‘--root’ on the kernel command line takes precedence + ;; over the ‘device’ field of the root record. + (root-device (and=> (find-long-option "--root" args) + device-string->file-system-device)) + (root-fs (or (find root-mount-point? mounts) + ;; Fall back to fictitious defaults. + (file-system (device (or root-device "/dev/root")) + (mount-point "/") + (type "ext4")))) + (fsck.mode (find-long-option "fsck.mode" args))) + + (define (check? fs) + (match fsck.mode + ("skip" #f) + ("force" #t) + (_ (file-system-check? fs)))) ; assume "auto" + + (define (skip-check-if-clean? fs) + (match fsck.mode + ("force" #f) + (_ (file-system-skip-check-if-clean? fs)))) + + (define (repair fs) + (let ((arg (find-long-option "fsck.repair" args))) + (if arg + (match arg + ("no" #f) + ("yes" #t) + (_ 'preen)) + (file-system-repair fs)))) (when (member "--repl" args) (start-repl)) @@ -611,23 +626,24 @@ upon error." (if root-device (mount-root-file-system (canonicalize-device-spec root-device) - root-fs-type + (file-system-type root-fs) #:volatile-root? volatile-root? - #:flags root-fs-flags - #:options root-options - #:check? (if root-fs - (file-system-check? root-fs) - #t) + #:flags (mount-flags->bit-mask + (file-system-flags root-fs)) + #:options (file-system-options root-fs) + #:check? (check? root-fs) #:skip-check-if-clean? - (and=> root-fs - file-system-skip-check-if-clean?) - #:repair (if root-fs - (file-system-repair root-fs) - 'preen)) + (skip-check-if-clean? root-fs) + #:repair (repair root-fs)) (mount "none" "/root" "tmpfs")) - ;; Mount the specified file systems. - (for-each mount-file-system + ;; Mount the specified non-root file systems. + (for-each (lambda (fs) + (mount-file-system fs + #:check? (check? fs) + #:skip-check-if-clean? + (skip-check-if-clean? fs) + #:repair (repair fs))) (remove root-mount-point? mounts)) (setenv "EXT2FS_NO_MTAB_OK" #f) -- cgit v1.2.3 From 348f0c61efc0f35aedcd0e44bc9fa7bf7f067942 Mon Sep 17 00:00:00 2001 From: Tobias Geerinckx-Rice Date: Sun, 12 Sep 2021 18:07:54 +0200 Subject: syscalls: Deduplicate device number conversion. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * guix/cpio.scm (device-number, device->major+minor): Move to, and subsequently import from, … * guix/build/syscalls.scm (device-number, device-number->major+minor): …here. Note the slight name change. (mounts): Replace 16-bit open code with a DEVICE-NUMBER call. * gnu/build/linux-boot.scm (device-number): Remove duplicate 16-bit implementation in favour of the one above. (resume-if-hibernated): Reuse DEVICE-NUMBER->MAJOR+MINOR. --- gnu/build/linux-boot.scm | 18 ++++-------------- guix/build/syscalls.scm | 29 ++++++++++++++++++++++++++++- guix/cpio.scm | 21 ++++----------------- 3 files changed, 36 insertions(+), 32 deletions(-) (limited to 'gnu/build/linux-boot.scm') diff --git a/gnu/build/linux-boot.scm b/gnu/build/linux-boot.scm index 8f0f3eb2fc..8efe6e5f9c 100644 --- a/gnu/build/linux-boot.scm +++ b/gnu/build/linux-boot.scm @@ -25,6 +25,7 @@ #:autoload (system repl repl) (start-repl) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) + #:use-module (srfi srfi-11) #:use-module (srfi srfi-26) #:use-module (ice-9 match) #:use-module (ice-9 rdelim) @@ -44,7 +45,6 @@ make-static-device-nodes configure-qemu-networking - device-number boot-system)) ;;; Commentary: @@ -134,14 +134,9 @@ succeeds. Return nothing otherwise. The kernel logs any details to dmesg." ;; is found on the command line; our canonicalize-device-spec gives ;; up after 20 seconds. We could emulate the former by looping… (device (canonicalize-device-spec spec)) - (rdev (stat:rdev (stat device))) - ;; For backwards compatibility, device numbering is a baroque affair. - ;; This is the full 64-bit scheme used by glibc's . - (major (logior (ash (logand #x00000000000fff00 rdev) -8) - (ash (logand #xfffff00000000000 rdev) -32))) - (minor (logior (logand #x00000000000000ff rdev) - (ash (logand #x00000ffffff00000 rdev) -12)))) - (format #f "~a:~a" major minor))) + (rdev (stat:rdev (stat device)))) + (let-values (((major minor) (device-number->major+minor rdev))) + (format #f "~a:~a" major minor)))) ;; Write the resume DEVICE to this magic file, using the MAJOR:MINOR device ;; numbers if possible. The kernel will immediately try to resume from it. @@ -392,11 +387,6 @@ networking values.) Return #t if INTERFACE is up, #f otherwise." (logand (network-interface-flags sock interface) IFF_UP))) -(define (device-number major minor) - "Return the device number for the device with MAJOR and MINOR, for use as -the last argument of `mknod'." - (+ (* major 256) minor)) - (define (pidof program) "Return the PID of the first presumed instance of PROGRAM." (let ((program (basename program))) diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm index ac1b0c2eea..99a3b45004 100644 --- a/guix/build/syscalls.scm +++ b/guix/build/syscalls.scm @@ -7,6 +7,7 @@ ;;; Copyright © 2020 Julien Lepiller ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen ;;; Copyright © 2021 Chris Marusich +;;; Copyright © 2021 Tobias Geerinckx-Rice ;;; ;;; This file is part of GNU Guix. ;;; @@ -56,6 +57,9 @@ restart-on-EINTR + device-number + device-number->major+minor + mount? mount-device-number mount-source @@ -448,6 +452,29 @@ the returned procedure is called." (module-define! (current-module) 'variable value) (module-export! (current-module) '(variable))))))) + +;;; +;;; Block devices. +;;; + +;; Convert between major:minor pairs and packed ‘device number’ representation. +;; XXX These aren't syscalls, but if you squint very hard they are part of the +;; FFI or however you want to justify me not finding a better fit… :-) +(define (device-number major minor) ; see glibc's + "Return the device number for the device with MAJOR and MINOR, for use as +the last argument of `mknod'." + (logior (ash (logand #x00000fff major) 8) + (ash (logand #xfffff000 major) 32) + (logand #x000000ff minor) + (ash (logand #xffffff00 minor) 12))) + +(define (device-number->major+minor device) ; see glibc's + "Return two values: the major and minor device numbers that make up DEVICE." + (values (logior (ash (logand #x00000000000fff00 device) -8) + (ash (logand #xfffff00000000000 device) -32)) + (logior (logand #x00000000000000ff device) + (ash (logand #x00000ffffff00000 device) -12)))) + ;;; ;;; File systems. @@ -628,7 +655,7 @@ current process." (define (string->device-number str) (match (string-split str #\:) (((= string->number major) (= string->number minor)) - (+ (* major 256) minor)))) + (device-number major minor)))) (call-with-input-file "/proc/self/mountinfo" (lambda (port) diff --git a/guix/cpio.scm b/guix/cpio.scm index 8038a11f3c..d4a7d5f1e0 100644 --- a/guix/cpio.scm +++ b/guix/cpio.scm @@ -18,6 +18,8 @@ ;;; along with GNU Guix. If not, see . (define-module (guix cpio) + #:use-module ((guix build syscalls) #:select (device-number + device-number->major+minor)) #:use-module ((guix build utils) #:select (dump-port)) #:use-module (srfi srfi-9) #:use-module (srfi srfi-11) @@ -129,8 +131,8 @@ (nlink 1) (mtime 0) (size 0) (dev 0) (rdev 0) (name-size 0)) "Return a new cpio file header." - (let-values (((major minor) (device->major+minor dev)) - ((rmajor rminor) (device->major+minor rdev))) + (let-values (((major minor) (device-number->major+minor dev)) + ((rmajor rminor) (device-number->major+minor rdev))) (%make-cpio-header MAGIC inode mode uid gid nlink mtime @@ -154,21 +156,6 @@ denotes, similar to 'stat:type'." (else (error "unsupported file type" mode))))) -(define (device-number major minor) ; see glibc's - "Return the device number for the device with MAJOR and MINOR, for use as -the last argument of `mknod'." - (logior (ash (logand #x00000fff major) 8) - (ash (logand #xfffff000 major) 32) - (logand #x000000ff minor) - (ash (logand #xffffff00 minor) 12))) - -(define (device->major+minor device) ; see glibc's - "Return two values: the major and minor device numbers that make up DEVICE." - (values (logior (ash (logand #x00000000000fff00 device) -8) - (ash (logand #xfffff00000000000 device) -32)) - (logior (logand #x00000000000000ff device) - (ash (logand #x00000ffffff00000 device) -12)))) - (define* (file->cpio-header file #:optional (file-name file) #:key (stat lstat)) "Return a cpio header corresponding to the info returned by STAT for FILE, -- cgit v1.2.3