;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2014-2018, 2020-2022 Ludovic Courtès <ludo@gnu.org> ;;; Copyright © 2016, 2017 David Craven <david@craven.ch> ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com> ;;; Copyright © 2019 Guillaume Le Vaillant <glv@posteo.net> ;;; Copyright © 2019–2021 Tobias Geerinckx-Rice <me@tobias.gr> ;;; Copyright © 2019 David C. Trudgian <dave@trudgian.net> ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com> ;;; Copyright © 2022 Oleg Pykhalov <go.wigust@gmail.com> ;;; Copyright © 2024 Nicolas Graves <ngraves@ngraves.fr> ;;; Copyright © 2024 Richard Sent <richard@freakingpenguin.com> ;;; ;;; This file is part of GNU Guix. ;;; ;;; GNU Guix is free software; you can redistribute it and/or modify it ;;; under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 3 of the License, or (at ;;; your option) any later version. ;;; ;;; GNU Guix is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. (define-module (gnu build file-systems) #:use-module (gnu system uuid) #:use-module (gnu system file-systems) #:use-module (guix build utils) #:use-module (guix build bournish) #:use-module ((guix build syscalls) #:hide (file-system-type)) #:use-module (guix diagnostics) #:use-module (guix i18n) #:use-module (rnrs io ports) #:use-module (rnrs bytevectors) #:use-module (ice-9 match) #:use-module (ice-9 rdelim) #:use-module (ice-9 regex) #:use-module (ice-9 string-fun) #:use-module (system foreign) #:autoload (system repl repl) (start-repl) #:use-module (srfi srfi-1) #:use-module (srfi srfi-26) #:export (disk-partitions partition-label-predicate partition-uuid-predicate partition-luks-uuid-predicate find-partition-by-label find-partition-by-uuid find-partition-by-luks-uuid canonicalize-device-spec read-partition-label read-partition-uuid read-luks-partition-uuid cleanly-unmounted-ext2? bind-mount system*/tty mount-flags->bit-mask check-file-system mount-file-system swap-space->flags-bit-mask)) ;;; Commentary: ;;; ;;; This modules provides tools to deal with disk partitions, and to mount and ;;; check file systems. ;;; ;;; Code: (define (system*/console program . args) "Run PROGRAM with ARGS in a tty on top of /dev/console. The return value is as for 'system*'." (match (primitive-fork) (0 (dynamic-wind (const #t) (lambda () (login-tty (open-fdes "/dev/console" O_RDWR)) (apply execlp program program args)) (lambda () (primitive-_exit 127)))) (pid (cdr (waitpid pid))))) (define (system*/tty program . args) "Run PROGRAM with ARGS, creating a tty if its standard input isn't one. The return value is as for 'system*'. This is necessary for commands such as 'cryptsetup open' or 'fsck' that may need to interact with the user but might be invoked from shepherd, where standard input is /dev/null." (apply (if (isatty? (current-input-port)) system* system*/console) program args)) (define (call-with-input-file file proc) "Like 'call-with-input-file', but pass O_CLOEXEC." (let ((port #f)) (dynamic-wind (lambda () (set! port (open file (logior O_RDONLY O_CLOEXEC)))) (lambda () (proc port)) (lambda () (close-port port) (set! port #f))))) (define (bind-mount source target) "Bind-mount SOURCE at TARGET." (mount source target "" MS_BIND)) (define (seek* fd/port offset whence) "Like 'seek' but return -1 instead of throwing to 'system-error' upon EINVAL. This makes it easier to catch cases like OFFSET being too large for FD/PORT." (catch 'system-error (lambda () (seek fd/port offset whence)) (lambda args (if (= EINVAL (system-error-errno args)) -1 (apply throw args))))) (define (read-superblock device offset size magic?) "Read a superblock of SIZE from OFFSET and DEVICE. Return the raw superblock on success, and #f if no valid superblock was found. MAGIC? takes a bytevector and returns #t when it's a valid superblock." (call-with-input-file device (lambda (port) (and (= offset (seek* port offset SEEK_SET)) (let ((block (make-bytevector size))) (match (get-bytevector-n! port block 0 (bytevector-length block)) ((? eof-object?) #f) ((? number? len) (and (= len (bytevector-length block)) (and (magic? block) block))))))))) (define null-terminated-latin1->string (cut latin1->string <> zero?)) (define (bytevector-utf16-length bv) "Given a bytevector BV containing a NUL-terminated UTF16-encoded string, determine where the NUL terminator is and return its index. If there's no NUL terminator, return the size of the bytevector." (let ((length (bytevector-length bv))) (let loop ((index 0)) (if (< index length) (if (zero? (bytevector-u16-ref bv index 'little)) index (loop (+ index 2))) length)))) (define* (bytevector->u16-list bv endianness #:optional (index 0)) (if (< index (bytevector-length bv)) (cons (bytevector-u16-ref bv index endianness) (bytevector->u16-list bv endianness (+ index 2))) '())) ;; The initrd doesn't have iconv data, so do the conversion ourselves. (define (utf16->string bv endianness) (list->string (map integer->char (reverse (let loop ((remainder (bytevector->u16-list bv endianness)) (result '())) (match remainder (() result) ((a) (cons a result)) ((a b x ...) (if (and (>= a #xD800) (< a #xDC00) ; high surrogate (>= b #xDC00) (< b #xE000)) ; low surrogate (loop x (cons (+ #x10000 (* #x400 (- a #xD800)) (- b #xDC00)) result)) (loop (cons b x) (cons a result)))))))))) (define (null-terminated-utf16->string bv endianness) (utf16->string (sub-bytevector bv 0 (bytevector-utf16-length bv)) endianness)) ;;; ;;; Ext2 file systems. ;;; ;; <http://www.nongnu.org/ext2-doc/ext2.html#DEF-SUPERBLOCK>. ;; TODO: Use "packed structs" from Guile-OpenGL or similar. (define-syntax %ext2-endianness ;; Endianness of ext2 file systems. (identifier-syntax (endianness little))) (define (ext2-superblock? sblock) "Return #t when SBLOCK is an ext2 superblock." (let ((magic (bytevector-u16-ref sblock 56 %ext2-endianness))) (= magic #xef53))) (define (read-ext2-superblock device) "Return the raw contents of DEVICE's ext2 superblock as a bytevector, or #f if DEVICE does not contain an ext2 file system." (read-superblock device 1024 264 ext2-superblock?)) (define (ext2-superblock-cleanly-unmounted? sblock) "Return true if SBLOCK denotes a file system that was cleanly unmounted, false otherwise." (define EXT2_VALID_FS 1) ;cleanly unmounted (define EXT2_ERROR_FS 2) ;errors detected (define EXT3_FEATURE_INCOMPAT_RECOVER #x0004) ;journal needs recovery (let ((state (bytevector-u16-ref sblock 58 %ext2-endianness))) (cond ((= state EXT2_VALID_FS) (let ((incompatible-features (bytevector-u32-ref sblock 96 %ext2-endianness))) (zero? (logand incompatible-features EXT3_FEATURE_INCOMPAT_RECOVER)))) ((= state EXT2_ERROR_FS) #f) (else (error "invalid ext2 superblock state" state))))) (define (ext2-superblock-uuid sblock) "Return the UUID of ext2 superblock SBLOCK as a 16-byte bytevector." (sub-bytevector sblock 104 16)) (define (ext2-superblock-volume-name sblock) "Return the volume name of ext2 superblock SBLOCK as a string of at most 16 characters, or #f if SBLOCK has no volume name." (null-terminated-latin1->string (sub-bytevector sblock 120 16))) (define (check-ext2-file-system device force? repair) "Return the health of an unmounted ext2 file system on DEVICE. If FORCE? is true, check the file system even if it's marked as clean. If REPAIR is false, do not write to the file system to fix errors. If it's #t, fix all errors. Otherwise, fix only those considered safe to repair automatically." (match (status:exit-val (apply system*/tty "e2fsck" "-v" "-C" "0" `(,@(if force? '("-f") '()) ,@(match repair (#f '("-n")) (#t '("-y")) (_ '("-p"))) ,device))) (0 'pass) (1 'errors-corrected) (2 'reboot-required) (_ 'fatal-error))) (define (cleanly-unmounted-ext2? device) ;convenience procedure "Return true if DEVICE is an ext2 file system and if it was cleanly unmounted." (ext2-superblock-cleanly-unmounted? (read-ext2-superblock device))) ;;; ;;; Linux swap. ;;; ;; Linux "swap space" is not a file system but it has a UUID and volume name, ;; like actual file systems, and we want to be able to look up swap partitions ;; by UUID and by label. (define %linux-swap-magic (string->utf8 "SWAPSPACE2")) ;; Like 'PAGE_SIZE' in Linux, arch/x86/include/asm/page.h. ;; XXX: This is always 4K on x86_64, i386, and ARMv7. However, on AArch64, ;; this is determined by 'CONFIG_ARM64_PAGE_SHIFT' in the kernel, which is 12 ;; by default (4K) but can be 14 or 16. (define %page-size 4096) (define (linux-swap-superblock? sblock) "Return #t when SBLOCK is an linux-swap superblock." (and (= (bytevector-length sblock) %page-size) (bytevector=? (sub-bytevector sblock (- %page-size 10) 10) %linux-swap-magic))) (define (read-linux-swap-superblock device) "Return the raw contents of DEVICE's linux-swap superblock as a bytevector, or #f if DEVICE does not contain an linux-swap file system." (read-superblock device 0 %page-size linux-swap-superblock?)) ;; See 'union swap_header' in 'include/linux/swap.h'. (define (linux-swap-superblock-uuid sblock) "Return the UUID of Linux-swap superblock SBLOCK as a 16-byte bytevector." (sub-bytevector sblock (+ 1024 4 4 4) 16)) (define (linux-swap-superblock-volume-name sblock) "Return the label of Linux-swap superblock SBLOCK as a string." (null-terminated-latin1->string (sub-bytevector sblock (+ 1024 4 4 4 16) 16))) (define (swap-space->flags-bit-mask swap) "Return the number suitable for the 'flags' argument of 'mount' that corresponds to the swap-space SWAP." (define prio-flag (let ((p (swap-space-priority swap)) (max (ash SWAP_FLAG_PRIO_MASK (- SWAP_FLAG_PRIO_SHIFT)))) (if p (logior SWAP_FLAG_PREFER (ash (cond ((< p 0) (begin (warning (G_ "Given swap priority ~a is negative, defaulting to 0.~%") p) 0)) ((> p max) (begin (warning (G_ "Limiting swap priority ~a to ~a.~%") p max) max)) (else p)) SWAP_FLAG_PRIO_SHIFT)) 0))) (define delayed-flag (if (swap-space-discard? swap) SWAP_FLAG_DISCARD 0)) (logior prio-flag delayed-flag)) ;;; ;;; Bcachefs file systems. ;;; ;; <https://evilpiepirate.org/git/bcachefs-tools.git/tree/libbcachefs/bcachefs_format.h> (define-syntax %bcachefs-endianness ;; Endianness of bcachefs file systems. (identifier-syntax (endianness little))) (define (bcachefs-superblock? sblock) "Return #t when SBLOCK is an bcachefs superblock." (bytevector=? (sub-bytevector sblock 24 16) #vu8(#xc6 #x85 #x73 #xf6 #x4e #x1a #x45 #xca #x82 #x65 #xf5 #x7f #x48 #xba #x6d #x81))) (define (read-bcachefs-superblock device) "Return the raw contents of DEVICE's bcachefs superblock as a bytevector, or #f if DEVICE does not contain a bcachefs file system." ;; Field offsets & lengths, in bytes. There are more (and the superblock is ;; extensible) but we need only some basic information here: ;; 0 16 bch_csum ;; 16 8 version ;; 24 16 magic ;; 40 16 uuid ← ‘internal’: you probably don't want this one ;; 56 16 user_uuid ← ‘external’: user-visible