1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014-2018, 2020-2022 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016, 2017 David Craven <david@craven.ch>
4 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
5 ;;; Copyright © 2019 Guillaume Le Vaillant <glv@posteo.net>
6 ;;; Copyright © 2019–2021 Tobias Geerinckx-Rice <me@tobias.gr>
7 ;;; Copyright © 2019 David C. Trudgian <dave@trudgian.net>
8 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
10 ;;; This file is part of GNU Guix.
12 ;;; GNU Guix is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 3 of the License, or (at
15 ;;; your option) any later version.
17 ;;; GNU Guix is distributed in the hope that it will be useful, but
18 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
25 (define-module (gnu build file-systems)
26 #:use-module (gnu system uuid)
27 #:use-module (gnu system file-systems)
28 #:use-module (guix build utils)
29 #:use-module (guix build bournish)
30 #:use-module ((guix build syscalls)
31 #:hide (file-system-type))
32 #:use-module (guix diagnostics)
33 #:use-module (guix i18n)
34 #:use-module (rnrs io ports)
35 #:use-module (rnrs bytevectors)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 rdelim)
38 #:use-module (system foreign)
39 #:autoload (system repl repl) (start-repl)
40 #:use-module (srfi srfi-1)
41 #:use-module (srfi srfi-26)
42 #:export (disk-partitions
43 partition-label-predicate
44 partition-uuid-predicate
45 partition-luks-uuid-predicate
46 find-partition-by-label
47 find-partition-by-uuid
48 find-partition-by-luks-uuid
49 canonicalize-device-spec
53 read-luks-partition-uuid
55 cleanly-unmounted-ext2?
64 swap-space->flags-bit-mask))
68 ;;; This modules provides tools to deal with disk partitions, and to mount and
69 ;;; check file systems.
73 (define (system*/console program . args)
74 "Run PROGRAM with ARGS in a tty on top of /dev/console. The return value is
76 (match (primitive-fork)
81 (login-tty (open-fdes "/dev/console" O_RDWR))
82 (apply execlp program program args))
84 (primitive-_exit 127))))
86 (cdr (waitpid pid)))))
88 (define (system*/tty program . args)
89 "Run PROGRAM with ARGS, creating a tty if its standard input isn't one.
90 The return value is as for 'system*'.
92 This is necessary for commands such as 'cryptsetup open' or 'fsck' that may
93 need to interact with the user but might be invoked from shepherd, where
94 standard input is /dev/null."
95 (apply (if (isatty? (current-input-port))
100 (define (bind-mount source target)
101 "Bind-mount SOURCE at TARGET."
102 (mount source target "" MS_BIND))
104 (define (seek* fd/port offset whence)
105 "Like 'seek' but return -1 instead of throwing to 'system-error' upon
106 EINVAL. This makes it easier to catch cases like OFFSET being too large for
110 (seek fd/port offset whence))
112 (if (= EINVAL (system-error-errno args))
114 (apply throw args)))))
116 (define (read-superblock device offset size magic?)
117 "Read a superblock of SIZE from OFFSET and DEVICE. Return the raw
118 superblock on success, and #f if no valid superblock was found. MAGIC?
119 takes a bytevector and returns #t when it's a valid superblock."
120 (call-with-input-file device
122 (and (= offset (seek* port offset SEEK_SET))
123 (let ((block (make-bytevector size)))
124 (match (get-bytevector-n! port block 0 (bytevector-length block))
128 (and (= len (bytevector-length block))
132 (define null-terminated-latin1->string
133 (cut latin1->string <> zero?))
135 (define (bytevector-utf16-length bv)
136 "Given a bytevector BV containing a NUL-terminated UTF16-encoded string,
137 determine where the NUL terminator is and return its index. If there's no
138 NUL terminator, return the size of the bytevector."
139 (let ((length (bytevector-length bv)))
140 (let loop ((index 0))
142 (if (zero? (bytevector-u16-ref bv index 'little))
147 (define* (bytevector->u16-list bv endianness #:optional (index 0))
148 (if (< index (bytevector-length bv))
149 (cons (bytevector-u16-ref bv index endianness)
150 (bytevector->u16-list bv endianness (+ index 2)))
153 ;; The initrd doesn't have iconv data, so do the conversion ourselves.
154 (define (utf16->string bv endianness)
158 (let loop ((remainder (bytevector->u16-list bv endianness))
162 ((a) (cons a result))
164 (if (and (>= a #xD800) (< a #xDC00) ; high surrogate
165 (>= b #xDC00) (< b #xE000)) ; low surrogate
166 (loop x (cons (+ #x10000
167 (* #x400 (- a #xD800))
170 (loop (cons b x) (cons a result))))))))))
172 (define (null-terminated-utf16->string bv endianness)
173 (utf16->string (sub-bytevector bv 0 (bytevector-utf16-length bv))
178 ;;; Ext2 file systems.
181 ;; <http://www.nongnu.org/ext2-doc/ext2.html#DEF-SUPERBLOCK>.
182 ;; TODO: Use "packed structs" from Guile-OpenGL or similar.
184 (define-syntax %ext2-endianness
185 ;; Endianness of ext2 file systems.
186 (identifier-syntax (endianness little)))
188 (define (ext2-superblock? sblock)
189 "Return #t when SBLOCK is an ext2 superblock."
190 (let ((magic (bytevector-u16-ref sblock 56 %ext2-endianness)))
193 (define (read-ext2-superblock device)
194 "Return the raw contents of DEVICE's ext2 superblock as a bytevector, or #f
195 if DEVICE does not contain an ext2 file system."
196 (read-superblock device 1024 264 ext2-superblock?))
198 (define (ext2-superblock-cleanly-unmounted? sblock)
199 "Return true if SBLOCK denotes a file system that was cleanly unmounted,
201 (define EXT2_VALID_FS 1) ;cleanly unmounted
202 (define EXT2_ERROR_FS 2) ;errors detected
204 (define EXT3_FEATURE_INCOMPAT_RECOVER #x0004) ;journal needs recovery
206 (let ((state (bytevector-u16-ref sblock 58 %ext2-endianness)))
207 (cond ((= state EXT2_VALID_FS)
208 (let ((incompatible-features
209 (bytevector-u32-ref sblock 96 %ext2-endianness)))
210 (zero? (logand incompatible-features
211 EXT3_FEATURE_INCOMPAT_RECOVER))))
212 ((= state EXT2_ERROR_FS) #f)
213 (else (error "invalid ext2 superblock state" state)))))
215 (define (ext2-superblock-uuid sblock)
216 "Return the UUID of ext2 superblock SBLOCK as a 16-byte bytevector."
217 (sub-bytevector sblock 104 16))
219 (define (ext2-superblock-volume-name sblock)
220 "Return the volume name of ext2 superblock SBLOCK as a string of at most 16
221 characters, or #f if SBLOCK has no volume name."
222 (null-terminated-latin1->string (sub-bytevector sblock 120 16)))
224 (define (check-ext2-file-system device force? repair)
225 "Return the health of an unmounted ext2 file system on DEVICE. If FORCE? is
226 true, check the file system even if it's marked as clean. If REPAIR is false,
227 do not write to the file system to fix errors. If it's #t, fix all
228 errors. Otherwise, fix only those considered safe to repair automatically."
229 (match (status:exit-val
230 (apply system*/tty "e2fsck" "-v" "-C" "0"
231 `(,@(if force? '("-f") '())
238 (1 'errors-corrected)
242 (define (cleanly-unmounted-ext2? device) ;convenience procedure
243 "Return true if DEVICE is an ext2 file system and if it was cleanly
245 (ext2-superblock-cleanly-unmounted? (read-ext2-superblock device)))
252 ;; Linux "swap space" is not a file system but it has a UUID and volume name,
253 ;; like actual file systems, and we want to be able to look up swap partitions
254 ;; by UUID and by label.
256 (define %linux-swap-magic
257 (string->utf8 "SWAPSPACE2"))
259 ;; Like 'PAGE_SIZE' in Linux, arch/x86/include/asm/page.h.
260 ;; XXX: This is always 4K on x86_64, i386, and ARMv7. However, on AArch64,
261 ;; this is determined by 'CONFIG_ARM64_PAGE_SHIFT' in the kernel, which is 12
262 ;; by default (4K) but can be 14 or 16.
263 (define %page-size 4096)
265 (define (linux-swap-superblock? sblock)
266 "Return #t when SBLOCK is an linux-swap superblock."
267 (and (= (bytevector-length sblock) %page-size)
268 (bytevector=? (sub-bytevector sblock (- %page-size 10) 10)
271 (define (read-linux-swap-superblock device)
272 "Return the raw contents of DEVICE's linux-swap superblock as a bytevector, or #f
273 if DEVICE does not contain an linux-swap file system."
274 (read-superblock device 0 %page-size linux-swap-superblock?))
276 ;; See 'union swap_header' in 'include/linux/swap.h'.
278 (define (linux-swap-superblock-uuid sblock)
279 "Return the UUID of Linux-swap superblock SBLOCK as a 16-byte bytevector."
280 (sub-bytevector sblock (+ 1024 4 4 4) 16))
282 (define (linux-swap-superblock-volume-name sblock)
283 "Return the label of Linux-swap superblock SBLOCK as a string."
284 (null-terminated-latin1->string
285 (sub-bytevector sblock (+ 1024 4 4 4 16) 16)))
287 (define (swap-space->flags-bit-mask swap)
288 "Return the number suitable for the 'flags' argument of 'mount'
289 that corresponds to the swap-space SWAP."
291 (let ((p (swap-space-priority swap))
292 (max (ash SWAP_FLAG_PRIO_MASK (- SWAP_FLAG_PRIO_SHIFT))))
294 (logior SWAP_FLAG_PREFER
298 (G_ "Given swap priority ~a is
299 negative, defaulting to 0.~%") p)
303 (G_ "Limiting swap priority ~a to
308 SWAP_FLAG_PRIO_SHIFT))
311 (if (swap-space-discard? swap)
314 (logior prio-flag delayed-flag))
319 ;;; Bcachefs file systems.
322 ;; <https://evilpiepirate.org/git/bcachefs-tools.git/tree/libbcachefs/bcachefs_format.h>
324 (define-syntax %bcachefs-endianness
325 ;; Endianness of bcachefs file systems.
326 (identifier-syntax (endianness little)))
328 (define (bcachefs-superblock? sblock)
329 "Return #t when SBLOCK is an bcachefs superblock."
330 (bytevector=? (sub-bytevector sblock 24 16)
331 #vu8(#xc6 #x85 #x73 #xf6 #x4e #x1a #x45 #xca
332 #x82 #x65 #xf5 #x7f #x48 #xba #x6d #x81)))
334 (define (read-bcachefs-superblock device)
335 "Return the raw contents of DEVICE's bcachefs superblock as a bytevector, or #f
336 if DEVICE does not contain a bcachefs file system."
337 ;; Field offsets & lengths, in bytes. There are more (and the superblock is
338 ;; extensible) but we need only some basic information here:
342 ;; 40 16 uuid ← ‘internal’: you probably don't want this one
343 ;; 56 16 user_uuid ← ‘external’: user-visible one by which to mount
345 ;; Assume a sane file system: ignore the back-up superblock & checksums.
346 (read-superblock device 4096 104 bcachefs-superblock?))
348 (define (bcachefs-superblock-external-uuid sblock)
349 "Return the external UUID of bcachefs superblock SBLOCK as a 16-byte
351 (sub-bytevector sblock 56 16))
353 (define (bcachefs-superblock-volume-name sblock)
354 "Return the volume name of bcachefs superblock SBLOCK as a string of at most
355 32 characters, or #f if SBLOCK has no volume name."
356 (null-terminated-latin1->string (sub-bytevector sblock 72 32)))
358 (define (check-bcachefs-file-system device force? repair)
359 "Return the health of an unmounted bcachefs file system on DEVICE. If FORCE?
360 is true, check the file system even if it's marked as clean. If REPAIR is
361 false, do not write to the file system to fix errors. If it's #t, fix all
362 errors. Otherwise, fix only those considered safe to repair automatically."
363 (let ((ignored-bits (logior 2)) ; DEVICE was mounted read-only
365 ;; A number, or #f on abnormal termination (e.g., assertion failure).
367 (apply system*/tty "bcachefs" "fsck" "-v"
368 `(,@(if force? '("-f") '())
373 ;; Make each multi-device member a separate argument.
374 ,@(string-split device #\:))))))
375 (match (and=> status (cut logand <> (lognot ignored-bits)))
377 (1 'errors-corrected)
382 ;;; Btrfs file systems.
385 ;; <https://btrfs.wiki.kernel.org/index.php/On-disk_Format#Superblock>.
387 (define-syntax %btrfs-endianness
388 ;; Endianness of btrfs file systems.
389 (identifier-syntax (endianness little)))
391 (define (btrfs-superblock? sblock)
392 "Return #t when SBLOCK is a btrfs superblock."
393 (bytevector=? (sub-bytevector sblock 64 8)
394 (string->utf8 "_BHRfS_M")))
396 (define (read-btrfs-superblock device)
397 "Return the raw contents of DEVICE's btrfs superblock as a bytevector, or #f
398 if DEVICE does not contain a btrfs file system."
399 (read-superblock device 65536 4096 btrfs-superblock?))
401 (define (btrfs-superblock-uuid sblock)
402 "Return the UUID of a btrfs superblock SBLOCK as a 16-byte bytevector."
403 (sub-bytevector sblock 32 16))
405 (define (btrfs-superblock-volume-name sblock)
406 "Return the volume name of btrfs superblock SBLOCK as a string of at most 256
407 characters, or #f if SBLOCK has no volume name."
408 (null-terminated-latin1->string (sub-bytevector sblock 299 256)))
410 (define (check-btrfs-file-system device force? repair)
411 "Return the health of an unmounted btrfs file system on DEVICE. If FORCE? is
412 false, return 'PASS unconditionally as btrfs claims no need for off-line checks.
413 When FORCE? is true, do perform a real check. This is not recommended! See
414 @uref{https://bugzilla.redhat.com/show_bug.cgi?id=625967#c8}. If REPAIR is
415 false, do not write to DEVICE. If it's #t, fix any errors found. Otherwise,
416 fix only those considered safe to repair automatically."
418 (match (status:exit-val
419 (apply system*/tty "btrfs" "check" "--progress"
420 ;; Btrfs's ‘--force’ is not relevant to us here.
422 ;; Upstream considers ALL repairs dangerous
423 ;; and will warn the user at run time.
425 (_ '("--readonly" ; a no-op for clarity
426 ;; A 466G file system with 180G used is
427 ;; enough to kill btrfs with 6G of RAM.
436 ;;; FAT32 file systems.
439 ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf>.
441 (define (fat32-superblock? sblock)
442 "Return #t when SBLOCK is a fat32 superblock."
443 (bytevector=? (sub-bytevector sblock 82 8)
444 (string->utf8 "FAT32 ")))
446 (define (read-fat32-superblock device)
447 "Return the raw contents of DEVICE's fat32 superblock as a bytevector, or
448 #f if DEVICE does not contain a fat32 file system."
449 (read-superblock device 0 90 fat32-superblock?))
451 (define (fat32-superblock-uuid sblock)
452 "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
453 (sub-bytevector sblock 67 4))
455 (define (fat32-superblock-volume-name sblock)
456 "Return the volume name of fat superblock SBLOCK as a string of at most 11
457 characters, or #f if SBLOCK has no volume name. The volume name is a latin1
458 string. Trailing spaces are trimmed."
459 (string-trim-right (latin1->string (sub-bytevector sblock 71 11) (lambda (c) #f)) #\space))
461 (define (check-fat-file-system device force? repair)
462 "Return the health of an unmounted FAT file system on DEVICE. FORCE? is
463 ignored: a full file system scan is always performed. If REPAIR is false, do
464 not write to the file system to fix errors. Otherwise, automatically fix them
465 using the least destructive approach."
466 (match (status:exit-val
467 (system*/tty "fsck.vfat" "-v"
470 (_ "-a")) ;no 'safe/#t distinction
473 (1 'errors-corrected)
478 ;;; FAT16 file systems.
481 (define (fat16-superblock? sblock)
482 "Return #t when SBLOCK is a fat16 boot record."
483 (bytevector=? (sub-bytevector sblock 54 8)
484 (string->utf8 "FAT16 ")))
486 (define (read-fat16-superblock device)
487 "Return the raw contents of DEVICE's fat16 superblock as a bytevector, or
488 #f if DEVICE does not contain a fat16 file system."
489 (read-superblock device 0 62 fat16-superblock?))
491 (define (fat16-superblock-uuid sblock)
492 "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
493 (sub-bytevector sblock 39 4))
495 (define (fat16-superblock-volume-name sblock)
496 "Return the volume name of fat superblock SBLOCK as a string of at most 11
497 characters, or #f if SBLOCK has no volume name. The volume name is a latin1
498 string. Trailing spaces are trimmed."
499 (string-trim-right (latin1->string (sub-bytevector sblock 43 11)
505 ;;; ISO9660 file systems.
508 ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf>.
510 (define (iso9660-superblock? sblock)
511 "Return #t when SBLOCK is an iso9660 volume descriptor."
512 (bytevector=? (sub-bytevector sblock 1 6)
513 ;; Note: "\x01" is the volume descriptor format version
514 (string->utf8 "CD001\x01")))
516 (define (read-iso9660-primary-volume-descriptor device offset)
517 "Find and read the first primary volume descriptor, starting at OFFSET.
518 Return #f if not found."
519 (let* ((sblock (read-superblock device offset 2048 iso9660-superblock?))
520 (type-code (if sblock
521 (bytevector-u8-ref sblock 0)
523 "Could not read ISO9660 primary
524 volume descriptor from ~s"
527 (255 #f) ; Volume Descriptor Set Terminator.
528 (1 sblock) ; Primary Volume Descriptor
529 (_ (read-iso9660-primary-volume-descriptor device (+ offset 2048))))))
531 (define (read-iso9660-superblock device)
532 "Return the raw contents of DEVICE's iso9660 primary volume descriptor
533 as a bytevector, or #f if DEVICE does not contain an iso9660 file system."
534 ;; Start reading at sector 16.
535 ;; Since we are not sure that the device contains an ISO9660 file system,
536 ;; we have to find that out first.
537 (if (read-superblock device (* 2048 16) 2048 iso9660-superblock?)
538 (read-iso9660-primary-volume-descriptor device (* 2048 16))
539 #f)) ; Device does not contain an iso9660 file system.
541 (define (iso9660-superblock-uuid sblock)
542 "Return the modification time of an iso9660 primary volume descriptor
543 SBLOCK as a bytevector. If that's not set, returns the creation time."
544 ;; Drops GMT offset for compatibility with Grub, blkid and /dev/disk/by-uuid.
545 ;; Compare Grub: "2014-12-02-19-30-23-00".
546 ;; Compare blkid result: "2014-12-02-19-30-23-00".
547 ;; Compare /dev/disk/by-uuid entry: "2014-12-02-19-30-23-00".
548 (let* ((creation-time (sub-bytevector sblock 813 17))
549 (modification-time (sub-bytevector sblock 830 17))
550 (unset-time (make-bytevector 17 0))
551 (time (if (bytevector=? unset-time modification-time)
554 (sub-bytevector time 0 16))) ; strips GMT offset.
556 (define (iso9660-superblock-volume-name sblock)
557 "Return the volume name of iso9660 superblock SBLOCK as a string. The volume
558 name is an ASCII string. Trailing spaces are trimmed."
559 ;; Note: Valid characters are of the set "[0-9][A-Z]_" (ECMA-119 Appendix A)
560 (string-trim-right (latin1->string (sub-bytevector sblock 40 32)
561 (lambda (c) #f)) #\space))
565 ;;; JFS file systems.
568 ;; Taken from <linux-libre>/fs/jfs/jfs_superblock.h.
570 (define-syntax %jfs-endianness
571 ;; Endianness of JFS file systems.
572 (identifier-syntax (endianness little)))
574 (define (jfs-superblock? sblock)
575 "Return #t when SBLOCK is a JFS superblock."
576 (bytevector=? (sub-bytevector sblock 0 4)
577 (string->utf8 "JFS1")))
579 (define (read-jfs-superblock device)
580 "Return the raw contents of DEVICE's JFS superblock as a bytevector, or #f
581 if DEVICE does not contain a JFS file system."
582 (read-superblock device 32768 184 jfs-superblock?))
584 (define (jfs-superblock-uuid sblock)
585 "Return the UUID of JFS superblock SBLOCK as a 16-byte bytevector."
586 (sub-bytevector sblock 136 16))
588 (define (jfs-superblock-volume-name sblock)
589 "Return the volume name of JFS superblock SBLOCK as a string of at most 16
590 characters, or #f if SBLOCK has no volume name."
591 (null-terminated-latin1->string (sub-bytevector sblock 152 16)))
593 (define (check-jfs-file-system device force? repair)
594 "Return the health of an unmounted JFS file system on DEVICE. If FORCE? is
595 true, check the file system even if it's marked as clean. If REPAIR is false,
596 do not write to the file system to fix errors, and replay the transaction log
597 only if FORCE? is true. Otherwise, replay the transaction log before checking
598 and automatically fix found errors."
599 (match (status:exit-val
602 ;; The ‘LEVEL’ logic is convoluted. To quote fsck/xchkdsk.c
603 ;; (‘-p’, ‘-a’, and ‘-r’ are aliases in every way):
604 ;; “If -f was chosen, have it override [-p] by [forcing] a
605 ;; check regardless of the outcome after the log is
607 ;; “If -n is specified by itself, don't replay the journal.
608 ;; If -n is specified with [-p], replay the journal but
609 ;; don't make any other changes”.
610 ,@(if force? '("-f") '())
613 (_ '("-p"))) ; no 'safe/#t distinction
616 (1 'errors-corrected)
622 ;;; F2FS (Flash-Friendly File System)
625 ;;; https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git/tree/include/linux/f2fs_fs.h
626 ;;; (but using xxd proved to be simpler)
628 (define-syntax %f2fs-endianness
629 ;; Endianness of F2FS file systems
630 (identifier-syntax (endianness little)))
632 ;; F2FS actually stores two adjacent copies of the superblock.
633 ;; should we read both?
634 (define (f2fs-superblock? sblock)
635 "Return #t when SBLOCK is an F2FS superblock."
636 (let ((magic (bytevector-u32-ref sblock 0 %f2fs-endianness)))
637 (= magic #xF2F52010)))
639 (define (read-f2fs-superblock device)
640 "Return the raw contents of DEVICE's F2FS superblock as a bytevector, or #f
641 if DEVICE does not contain an F2FS file system."
642 (read-superblock device
643 ;; offset of magic in first copy
645 ;; difference between magic of second
650 (define (f2fs-superblock-uuid sblock)
651 "Return the UUID of F2FS superblock SBLOCK as a 16-byte bytevector."
652 (sub-bytevector sblock
654 ;; subtract superblock offset
658 (define (f2fs-superblock-volume-name sblock)
659 "Return the volume name of F2FS superblock SBLOCK as a string of at most 512
660 characters, or #f if SBLOCK has no volume name."
661 (null-terminated-utf16->string
662 (sub-bytevector sblock (- (+ #x470 12) #x400) 512)
665 (define (check-f2fs-file-system device force? repair)
666 "Return the health of an unmuounted F2FS file system on DEVICE. If FORCE? is
667 true, check the file system even if it's marked as clean. If either FORCE? or
668 REPAIR are true, automatically fix found errors."
669 ;; There's no ‘-n’ equivalent (‘--dry-run’ does not disable writes).
670 ;; ’-y’ is an alias of ‘-f’. The man page is bad: read main.c.
671 (when (and force? (not repair))
672 (format (current-error-port)
673 "warning: forced check of F2FS ~a implies repairing any errors~%"
675 (match (status:exit-val
676 (apply system*/tty "fsck.f2fs"
677 `(,@(if force? '("-f") '())
678 ,@(if repair '("-p") '("--dry-run"))
680 ;; 0 and -1 are the only two possibilities according to the man page.
686 ;;; LUKS encrypted devices.
689 ;; The LUKS header format is described in "LUKS On-Disk Format Specification":
690 ;; <https://gitlab.com/cryptsetup/cryptsetup/wikis/Specification>. We follow
691 ;; version 1.2.1 of this document.
693 ;; The LUKS2 header format is described in "LUKS2 On-Disk Format Specification":
694 ;; <https://gitlab.com/cryptsetup/LUKS2-docs/blob/master/luks2_doc_wip.pdf>.
695 ;; It is a WIP document.
697 (define-syntax %luks-endianness
698 ;; Endianness of LUKS headers.
699 (identifier-syntax (endianness big)))
701 (define (luks-superblock? sblock)
702 "Return #t when SBLOCK is a luks superblock."
704 ;; The 'LUKS_MAGIC' constant.
705 (u8-list->bytevector (append (map char->integer (string->list "LUKS"))
707 (let ((magic (sub-bytevector sblock 0 6))
708 (version (bytevector-u16-ref sblock 6 %luks-endianness)))
709 (and (bytevector=? magic %luks-magic)
710 (or (= version 1) (= version 2)))))
712 (define (read-luks-header file)
713 "Read a LUKS header from FILE. Return the raw header on success, and #f if
714 not valid header was found."
715 ;; Size in bytes of the LUKS binary header, which includes key slots in
716 ;; LUKS1. In LUKS2 the binary header is partially backward compatible, so
717 ;; that UUID can be extracted as for LUKS1. Keyslots and other metadata are
718 ;; not part of this header in LUKS2, but are included in the JSON metadata
719 ;; area that follows.
720 (read-superblock file 0 592 luks-superblock?))
722 (define (luks-header-uuid header)
723 "Return the LUKS UUID from HEADER, as a 16-byte bytevector."
724 ;; 40 bytes are reserved for the UUID, but in practice, it contains the 36
725 ;; bytes of its ASCII representation.
726 (let ((uuid (sub-bytevector header 168 36)))
727 (string->uuid (utf8->string uuid))))
731 ;;; NTFS file systems.
734 ;; Taken from <linux-libre>/fs/ntfs/layout.h
736 (define-syntax %ntfs-endianness
737 ;; Endianness of NTFS file systems.
738 (identifier-syntax (endianness little)))
740 (define (ntfs-superblock? sblock)
741 "Return #t when SBLOCK is a NTFS superblock."
742 (bytevector=? (sub-bytevector sblock 3 8)
743 (string->utf8 "NTFS ")))
745 (define (read-ntfs-superblock device)
746 "Return the raw contents of DEVICE's NTFS superblock as a bytevector, or #f
747 if DEVICE does not contain a NTFS file system."
748 (read-superblock device 0 511 ntfs-superblock?))
750 (define (ntfs-superblock-uuid sblock)
751 "Return the UUID of NTFS superblock SBLOCK as a 8-byte bytevector."
752 (sub-bytevector sblock 72 8))
754 ;; TODO: Add ntfs-superblock-volume-name. The partition label is not stored
755 ;; in the BOOT SECTOR like the UUID, but in the MASTER FILE TABLE, which seems
756 ;; way harder to access.
758 (define (check-ntfs-file-system device force? repair)
759 "Return the health of an unmounted NTFS file system on DEVICE. FORCE? is
760 ignored: a full check is always performed. Repair is not possible: if REPAIR is
761 true and the volume has been repaired by an external tool, clear the volume
762 dirty flag to indicate that it's now safe to mount."
763 (match (status:exit-val
764 (system*/tty "ntfsfix"
765 (if repair "--clear-dirty" "--no-action")
773 ;;; XFS file systems.
776 ;; <https://git.kernel.org/pub/scm/fs/xfs/xfs-documentation.git/tree/design/XFS_Filesystem_Structure/allocation_groups.asciidoc>
778 (define-syntax %xfs-endianness
779 ;; Endianness of XFS file systems.
780 (identifier-syntax (endianness big)))
782 (define (xfs-superblock? sblock)
783 "Return #t when SBLOCK is an XFS superblock."
784 (bytevector=? (sub-bytevector sblock 0 4)
785 (string->utf8 "XFSB")))
787 (define (read-xfs-superblock device)
788 "Return the raw contents of DEVICE's XFS superblock as a bytevector, or #f
789 if DEVICE does not contain an XFS file system."
790 (read-superblock device 0 120 xfs-superblock?))
792 (define (xfs-superblock-uuid sblock)
793 "Return the UUID of XFS superblock SBLOCK as a 16-byte bytevector."
794 (sub-bytevector sblock 32 16))
796 (define (xfs-superblock-volume-name sblock)
797 "Return the volume name of XFS superblock SBLOCK as a string of at most 12
798 characters, or #f if SBLOCK has no volume name."
799 (null-terminated-latin1->string (sub-bytevector sblock 108 12)))
801 (define (check-xfs-file-system device force? repair)
802 "Return the health of an unmounted XFS file system on DEVICE. If FORCE? is
803 false, return 'PASS unconditionally as XFS claims no need for off-line checks.
804 When FORCE? is true, do perform a thorough check. If REPAIR is false, do not
805 write to DEVICE. If it's #t, replay the log, check, and fix any errors found.
806 Otherwise, only replay the log, and check without attempting further repairs."
809 (system*/tty "xfs_repair" "-Pv"
812 (_ "-n")) ;will miss some errors
815 ;; xfs_repair fails with exit status 2 if the log is dirty, which is
816 ;; likely in situations where you're running xfs_repair. Only the kernel
817 ;; can replay the log by {,un}mounting it cleanly.
818 (match (let ((status (xfs_repair)))
819 (if (and repair (eq? 2 status))
820 (let ((target "/replay-XFS-log"))
821 ;; The kernel helpfully prints a ‘Mounting…’ notice for us.
823 (mount device target "xfs")
829 (4 'errors-corrected)
835 ;;; Partition lookup.
838 (define (disk-partitions)
839 "Return the list of device names corresponding to valid disk partitions."
840 (define (partition? name major minor)
841 ;; grub-mkrescue does some funny things for EFI support which
842 ;; makes it a lot more difficult than one would expect to support
843 ;; booting an ISO-9660 image from an USB flash drive.
844 ;; For example there's a buggy (too small) hidden partition in it
845 ;; which Linux mounts and then proceeds to fail while trying to
846 ;; fall off the edge.
847 ;; In any case, partition tables are supposed to be optional so
848 ;; here we allow checking entire disks for file systems, too.
849 (> major 2)) ;ignore RAM disks and floppy disks
851 (call-with-input-file "/proc/partitions"
853 ;; Skip the two header lines.
857 ;; Read each subsequent line, and extract the last space-separated
859 (let loop ((parts '()))
860 (let ((line (read-line port)))
861 (if (eof-object? line)
863 (match (string-tokenize line)
864 (((= string->number major) (= string->number minor)
866 (if (partition? name major minor)
867 (loop (cons name parts))
868 (loop parts))))))))))
870 (define (ENOENT-safe proc)
871 "Wrap the one-argument PROC such that ENOENT, EIO, and ENOMEDIUM errors are
872 caught and lead to a warning and #f as the result."
878 (let ((errno (system-error-errno args)))
879 (cond ((= ENOENT errno)
880 (format (current-error-port)
881 "warning: device '~a' not found~%" device)
883 ((= ENOMEDIUM errno) ;for removable media
885 ((= EIO errno) ;unreadable hardware like audio CDs
886 (format (current-error-port)
887 "warning: failed to read from device '~a'~%" device)
890 (apply throw args))))))))
892 (define (partition-field-reader read field)
893 "Return a procedure that takes a device and returns the value of a FIELD in
894 the partition superblock or #f."
896 (let ((sblock (read device)))
900 (define (read-partition-field device partition-field-readers)
901 "Returns the value of a FIELD in the partition superblock of DEVICE or #f. It
902 takes a list of PARTITION-FIELD-READERS and returns the result of the first
903 partition field reader that returned a value."
904 (match (filter-map (cut apply <> (list device)) partition-field-readers)
908 (define %partition-label-readers
909 (list (partition-field-reader read-iso9660-superblock
910 iso9660-superblock-volume-name)
911 (partition-field-reader read-ext2-superblock
912 ext2-superblock-volume-name)
913 (partition-field-reader read-linux-swap-superblock
914 linux-swap-superblock-volume-name)
915 (partition-field-reader read-bcachefs-superblock
916 bcachefs-superblock-volume-name)
917 (partition-field-reader read-btrfs-superblock
918 btrfs-superblock-volume-name)
919 (partition-field-reader read-fat32-superblock
920 fat32-superblock-volume-name)
921 (partition-field-reader read-fat16-superblock
922 fat16-superblock-volume-name)
923 (partition-field-reader read-jfs-superblock
924 jfs-superblock-volume-name)
925 (partition-field-reader read-f2fs-superblock
926 f2fs-superblock-volume-name)
927 (partition-field-reader read-xfs-superblock
928 xfs-superblock-volume-name)))
930 (define %partition-uuid-readers
931 (list (partition-field-reader read-iso9660-superblock
932 iso9660-superblock-uuid)
933 (partition-field-reader read-ext2-superblock
934 ext2-superblock-uuid)
935 (partition-field-reader read-linux-swap-superblock
936 linux-swap-superblock-uuid)
937 (partition-field-reader read-bcachefs-superblock
938 bcachefs-superblock-external-uuid)
939 (partition-field-reader read-btrfs-superblock
940 btrfs-superblock-uuid)
941 (partition-field-reader read-fat32-superblock
942 fat32-superblock-uuid)
943 (partition-field-reader read-fat16-superblock
944 fat16-superblock-uuid)
945 (partition-field-reader read-jfs-superblock
947 (partition-field-reader read-f2fs-superblock
948 f2fs-superblock-uuid)
949 (partition-field-reader read-ntfs-superblock
950 ntfs-superblock-uuid)
951 (partition-field-reader read-xfs-superblock
952 xfs-superblock-uuid)))
954 (define read-partition-label
955 (cut read-partition-field <> %partition-label-readers))
957 (define read-partition-uuid
958 (cut read-partition-field <> %partition-uuid-readers))
960 (define luks-partition-field-reader
961 (partition-field-reader read-luks-header luks-header-uuid))
963 (define read-luks-partition-uuid
964 (cut read-partition-field <> (list luks-partition-field-reader)))
966 (define (partition-predicate reader =)
967 "Return a predicate that returns true if the FIELD of partition header that
968 was READ is = to the given value."
969 ;; When running on the hand-made /dev, 'disk-partitions' could return
970 ;; partitions for which we have no /dev node. Handle that gracefully.
971 (let ((reader (ENOENT-safe reader)))
974 (let ((actual (reader device)))
976 (= actual expected)))))))
978 (define partition-label-predicate
979 (partition-predicate read-partition-label string=?))
981 (define partition-uuid-predicate
982 (partition-predicate read-partition-uuid uuid=?))
984 (define luks-partition-uuid-predicate
985 (partition-predicate luks-partition-field-reader uuid=?))
987 (define (find-partition predicate)
988 "Return the first partition found that matches PREDICATE, or #f if none
991 (find (predicate expected)
992 (map (cut string-append "/dev/" <>)
993 (disk-partitions)))))
995 (define find-partition-by-label
996 (find-partition partition-label-predicate))
998 (define find-partition-by-uuid
999 (find-partition partition-uuid-predicate))
1001 (define find-partition-by-luks-uuid
1002 (find-partition luks-partition-uuid-predicate))
1005 (define (canonicalize-device-spec spec)
1006 "Return the device name corresponding to SPEC, which can be a <uuid>, a
1007 <file-system-label>, or a string (typically a /dev file name or an nfs-root
1010 ;; Number of times we retry partition label resolution, 1 second per
1011 ;; trial. Note: somebody reported a delay of 16 seconds (!) before their
1012 ;; USB key would be detected by the kernel, so we must wait for at least
1016 (define (resolve find-partition spec fmt)
1017 (let loop ((count 0))
1018 (let ((device (find-partition spec)))
1020 ;; Some devices take a bit of time to appear, most notably USB
1021 ;; storage devices. Thus, wait for the device to appear.
1022 (if (> count max-trials)
1023 (error "failed to resolve partition" (fmt spec))
1025 (format #t "waiting for partition '~a' to appear...~%"
1028 (loop (+ 1 count))))))))
1032 (if (string-contains spec ":/")
1033 spec ; do not resolve NFS devices
1034 ;; Nothing to do, but wait until SPEC shows up.
1035 (resolve identity spec identity)))
1036 ((? file-system-label?)
1037 ;; Resolve the label.
1038 (resolve find-partition-by-label
1039 (file-system-label->string spec)
1042 (resolve find-partition-by-uuid
1043 (uuid-bytevector spec)
1046 (define (check-file-system device type force? repair)
1047 "Check an unmounted TYPE file system on DEVICE. Do nothing but warn if it is
1048 mounted. If FORCE? is true, check even when considered unnecessary. If REPAIR
1049 is false, try not to write to DEVICE at all. If it's #t, try to fix all errors
1050 found. Otherwise, fix only those considered safe to repair automatically. Not
1051 all TYPEs support all values or combinations of FORCE? and REPAIR. Don't throw
1052 an exception in such cases but perform the nearest sane action."
1053 (define check-procedure
1055 ((string-prefix? "ext" type) check-ext2-file-system)
1056 ((string-prefix? "bcachefs" type) check-bcachefs-file-system)
1057 ((string-prefix? "btrfs" type) check-btrfs-file-system)
1058 ((string-suffix? "fat" type) check-fat-file-system)
1059 ((string-prefix? "jfs" type) check-jfs-file-system)
1060 ((string-prefix? "f2fs" type) check-f2fs-file-system)
1061 ((string-prefix? "ntfs" type) check-ntfs-file-system)
1062 ((string-prefix? "nfs" type) (const 'pass))
1063 ((string-prefix? "xfs" type) check-xfs-file-system)
1067 (let ((mount (find (lambda (mount)
1068 (string=? device (mount-source mount)))
1071 (format (current-error-port)
1072 "Refusing to check ~a file system already mounted at ~a~%"
1073 device (mount-point mount))
1074 (match (check-procedure device force? repair)
1078 (format (current-error-port)
1079 "File system check corrected errors on ~a; continuing~%"
1082 (format (current-error-port)
1083 "File system check corrected errors on ~a; rebooting~%"
1088 (format (current-error-port) "File system check on ~a failed~%"
1091 ;; Spawn a REPL only if someone might interact with it.
1092 (when (isatty? (current-input-port))
1093 (format (current-error-port) "Spawning Bourne-like REPL.~%")
1095 ;; 'current-output-port' is typically connected to /dev/klog
1096 ;; (in PID 1), but here we want to make sure we talk directly
1098 (with-output-to-file "/dev/console"
1100 (start-repl %bournish-language))))))))
1101 (format (current-error-port)
1102 "No file system check procedure for ~a; skipping~%"
1105 (define (mount-flags->bit-mask flags)
1106 "Return the number suitable for the 'flags' argument of 'mount' that
1107 corresponds to the symbols listed in FLAGS."
1108 (let loop ((flags flags))
1110 (('read-only rest ...)
1111 (logior MS_RDONLY (loop rest)))
1112 (('bind-mount rest ...)
1113 (logior MS_BIND (loop rest)))
1114 (('no-suid rest ...)
1115 (logior MS_NOSUID (loop rest)))
1117 (logior MS_NODEV (loop rest)))
1118 (('no-exec rest ...)
1119 (logior MS_NOEXEC (loop rest)))
1120 (('no-atime rest ...)
1121 (logior MS_NOATIME (loop rest)))
1122 (('strict-atime rest ...)
1123 (logior MS_STRICTATIME (loop rest)))
1124 (('lazy-time rest ...)
1125 (logior MS_LAZYTIME (loop rest)))
1129 (define* (mount-file-system fs #:key (root "/root")
1130 (check? (file-system-check? fs))
1131 (skip-check-if-clean?
1132 (file-system-skip-check-if-clean? fs))
1133 (repair (file-system-repair fs)))
1134 "Mount the file system described by FS, a <file-system> object, under ROOT."
1136 (define (mount-nfs source mount-point type flags options)
1137 (let* ((idx (string-rindex source #\:))
1138 (host-part (string-take source idx))
1139 ;; Strip [] from around host if present
1140 (host (match (string-split host-part (string->char-set "[]"))
1143 (aa (match (getaddrinfo host "nfs") ((x . _) x)))
1144 (sa (addrinfo:addr aa))
1145 (inet-addr (inet-ntop (sockaddr:fam sa)
1146 (sockaddr:addr sa))))
1148 ;; Mounting an NFS file system requires passing the address
1149 ;; of the server in the addr= option
1150 (mount source mount-point type flags
1151 (string-append "addr="
1154 (string-append "," options)
1156 (let* ((type (file-system-type fs))
1157 (source (canonicalize-device-spec (file-system-device fs)))
1158 (target (string-append root "/"
1159 (file-system-mount-point fs)))
1160 (flags (logior (mount-flags->bit-mask (file-system-flags fs))
1162 ;; For bind mounts, preserve the original flags such
1163 ;; as MS_NOSUID, etc. Failing to do that, the
1164 ;; MS_REMOUNT call below fails with EPERM.
1165 ;; See <https://bugs.gnu.org/46292>
1166 (if (memq 'bind-mount (file-system-flags fs))
1167 (statfs-flags->mount-flags
1168 (file-system-mount-flags (statfs source)))
1170 (options (file-system-options fs)))
1172 (check-file-system source type (not skip-check-if-clean?) repair))
1174 (catch 'system-error
1176 ;; Create the mount point. Most of the time this is a directory, but
1177 ;; in the case of a bind mount, a regular file or socket may be
1179 (if (and (= MS_BIND (logand flags MS_BIND))
1180 (not (file-is-directory? source)))
1181 (unless (file-exists? target)
1182 (mkdir-p (dirname target))
1183 (call-with-output-file target (const #t)))
1187 ((string-prefix? "nfs" type)
1188 (mount-nfs source target type flags options))
1190 (mount source target type flags options)))
1192 ;; For read-only bind mounts, an extra remount is needed, as per
1193 ;; <http://lwn.net/Articles/281157/>, which still applies to Linux
1195 (when (and (= MS_BIND (logand flags MS_BIND))
1196 (= MS_RDONLY (logand flags MS_RDONLY)))
1197 (let ((flags (logior MS_REMOUNT flags)))
1198 (mount source target type flags options))))
1200 (or (file-system-mount-may-fail? fs)
1201 (apply throw args))))))
1203 ;;; file-systems.scm ends here