;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2017-2024 Ricardo Wurmus ;;; Copyright © 2018 Joshua Sierles, Nextjournal ;;; Copyright © 2018, 2020, 2022 Tobias Geerinckx-Rice ;;; Copyright © 2019, 2021, 2022, 2024 Efraim Flashner ;;; Copyright © 2019 Andreas Enge ;;; Copyright © 2020 Alexander Krotov ;;; Copyright © 2020 Pierre Langlois ;;; Copyright © 2021, 2023 Vinicius Monego ;;; Copyright © 2021 Alexandre Hannud Abdo ;;; Copyright © 2021, 2022, 2023 Maxim Cournoyer ;;; Copyright © 2022 Marius Bakke ;;; Copyright © 2023 David Elsing ;;; ;;; 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 . (define-module (gnu packages graph) #:use-module (guix download) #:use-module (guix gexp) #:use-module (guix git-download) #:use-module (guix packages) #:use-module (guix utils) #:use-module (guix build-system cmake) #:use-module (guix build-system gnu) #:use-module (guix build-system pyproject) #:use-module (guix build-system python) #:use-module (guix build-system r) #:use-module ((guix licenses) #:prefix license:) #:u;;; 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, 2024 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> ;;; Copyright © 2024 Janneke Nieuwenhuizen <janneke@gnu.org> ;;; ;;; 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 device-name->hurd-device-name device-spec->device device-spec->device-name hurd-device-name->device-name 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 one by which to mount ;; 72 32 label ;; Assume a sane file system: ignore the back-up superblock & checksums. (read-superblock device 4096 104 bcachefs-superblock?)) (define (bcachefs-superblock-external-uuid sblock) "Return the external UUID of bcachefs superblock SBLOCK as a 16-byte bytevector." (sub-bytevector sblock 56 16)) (define (bcachefs-superblock-volume-name sblock) "Return the volume name of bcachefs superblock SBLOCK as a string of at most 32 characters, or #f if SBLOCK has no volume name." (null-terminated-latin1->string (sub-bytevector sblock 72 32))) (define (check-bcachefs-file-system device force? repair) "Return the health of an unmounted bcachefs 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." (let ((ignored-bits (logior 2)) ; DEVICE was mounted read-only (status ;; A number, or #f on abnormal termination (e.g., assertion failure). (status:exit-val (apply system*/tty "bcachefs" "fsck" "-v" `(,@(if force? '("-f") '()) ,@(match repair (#f '("-n")) (#t '("-y")) (_ '("-p"))) ;; Make each multi-device member a separate argument. ,@(string-split device #\:)))))) (match (and=> status (cut logand <> (lognot ignored-bits))) (0 'pass) (1 'errors-corrected) (_ 'fatal-error)))) ;;; ;;; Btrfs file systems. ;;; ;; <https://btrfs.wiki.kernel.org/index.php/On-disk_Format#Superblock>. (define-syntax %btrfs-endianness ;; Endianness of btrfs file systems. (identifier-syntax (endianness little))) (define (btrfs-superblock? sblock) "Return #t when SBLOCK is a btrfs superblock." (bytevector=? (sub-bytevector sblock 64 8) (string->utf8 "_BHRfS_M"))) (define (read-btrfs-superblock device) "Return the raw contents of DEVICE's btrfs superblock as a bytevector, or #f if DEVICE does not contain a btrfs file system." (read-superblock device 65536 4096 btrfs-superblock?)) (define (btrfs-superblock-uuid sblock) "Return the UUID of a btrfs superblock SBLOCK as a 16-byte bytevector." (sub-bytevector sblock 32 16)) (define (btrfs-superblock-volume-name sblock) "Return the volume name of btrfs superblock SBLOCK as a string of at most 256 characters, or #f if SBLOCK has no volume name." (null-terminated-latin1->string (sub-bytevector sblock 299 256))) (define (check-btrfs-file-system device force? repair) "Return the health of an unmounted btrfs file system on DEVICE. If FORCE? is false, return 'PASS unconditionally as btrfs claims no need for off-line checks. When FORCE? is true, do perform a real check. This is not recommended! See @uref{https://bugzilla.redhat.com/show_bug.cgi?id=625967#c8}. If REPAIR is false, do not write to DEVICE. If it's #t, fix any errors found. Otherwise, fix only those considered safe to repair automatically." (if force? (match (status:exit-val (apply system*/tty "btrfs" "check" "--progress" ;; Btrfs's ‘--force’ is not relevant to us here. `(,@(match repair ;; Upstream considers ALL repairs dangerous ;; and will warn the user at run time. (#t '("--repair")) (_ '("--readonly" ; a no-op for clarity ;; A 466G file system with 180G used is ;; enough to kill btrfs with 6G of RAM. "--mode" "lowmem"))) ,device))) (0 'pass) (_ 'fatal-error)) 'pass)) ;;; ;;; exFAT file systems. ;;; ;; <https://learn.microsoft.com/en-us/windows/win32/fileio/exfat-specification>. (define-syntax %exfat-endianness (identifier-syntax (endianness little))) (define (exfat-superblock? sblock) "Return #t when SBLOCK, a bytevector of at least length 512, is an exFAT superblock, called main boot sector in the exFAT specification." (and (bytevector=? (string->utf8 "EXFAT ") (sub-bytevector sblock 3 8)) ;FileSystemName (bytevector=? (make-bytevector 53 0) (sub-bytevector sblock 11 53)) ;MustBeZero (bytevector=? #vu8(#x55 #xaa) (sub-bytevector sblock 510 2)))) ;BootSignature (define (exfat-bytes-per-sector-shift sblock) (bytevector-u8-ref sblock 108)) (define (exfat-sectors-per-cluster-shift sblock) (bytevector-u8-ref sblock 109)) (define (exfat-root-directory-offset sblock) (let ((cluster-heap-offset (bytevector-u32-ref sblock 88 %exfat-endianness)) (root-directory-cluster (bytevector-u32-ref sblock 96 %exfat-endianness))) (define (cluster->sector cluster) (let ((first-data-cluster 2)) (+ cluster-heap-offset (ash (- cluster first-data-cluster) (exfat-sectors-per-cluster-shift sblock))))) (ash (cluster->sector root-directory-cluster) (exfat-bytes-per-sector-shift sblock)))) (define (exfat-cluster-size sblock) (ash 1 (+ (exfat-bytes-per-sector-shift sblock) (exfat-sectors-per-cluster-shift sblock)))) ;; exFAT stores the volume name in a directory entry with no fixed location. We ;; search an arbitrary number of entries before giving up: 128 for devices <256M ;; (4K clusters), 1024 for those <32G (32K clusters), or 4096 for others (128K). ;; It's silly but mostly matches what util-linux's libblkid does with its higher ;; arbitrary number of 10,000 tries. To match that, we'd not only have to look ;; up subsequent clusters in the FAT, but redesign this entire module not to ;; assume that all file systems have a `superblock' that both fits neatly into ;; RAM, and just happens to politely contain all the metadata we'll ever need. (define (read-exfat-superblock+root-directory-cluster device sblock) "Return as a bytevector the raw contents of DEVICE's exFAT `superblock' from main boot sector up to the RootDirectoryCluster expected to contain the volume label." (let* ((span (+ (exfat-root-directory-offset sblock) (exfat-cluster-size sblock)))) (read-superblock device 0 span (const #t)))) (define (read-exfat-superblock device) "Return the raw contents of DEVICE's exFAT superblock as a bytevector, or #f if DEVICE does not contain an exFAT file system." (and=> (read-superblock device 0 512 exfat-superblock?) (cut read-exfat-superblock+root-directory-cluster device <>))) (define (exfat-superblock-volume-name sblock) "Return as a string the volume name belonging to an exFAT file system beginning with bytevector SBLOCK, which includes directory entries. Return #f if no volume name was found within our arbitrary limit." ;; Defined in section 7.3 of the exFAT specification. (let ((root-directory (exfat-root-directory-offset sblock)) (cluster-size (exfat-cluster-size sblock)) (entry-size 32)) ;bytes (let loop ((cluster-offset 0)) (if (< cluster-offset cluster-size) (let ((offset (+ root-directory cluster-offset))) (match (bytevector-u8-ref sblock offset) ((or #x00 ;end of directory #x03) ;type 3 & not in use #f) (#x83 ;type 3 & in use (let* ((length (min 11 (bytevector-u8-ref sblock (+ 1 offset)))) (label (sub-bytevector sblock (+ 2 offset) (* 2 length)))) (utf16->string label %exfat-endianness))) (_ (loop (+ entry-size offset))))) #f)))) (define (exfat-superblock-uuid sblock) "Return the Volume Serial Number of exFAT superblock SBLOCK as a bytevector. This 4-byte identifier is guaranteed to exist, unlike the optional 16-byte Volume GUID from section 7.5 of the exFAT specification." (sub-bytevector sblock 100 4)) (define (check-exfat-file-system device force? repair) "Return the health of an unmounted exFAT 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 "fsck.exfat" "-sv" `(,@(if force? '("-b") '()) ,@(match repair (#f '("-n")) (#t '("-y")) (_ '("-p"))) ,device))) (0 'pass) (1 'errors-corrected) (2 'reboot-required) (_ 'fatal-error))) ;;; ;;; FAT32 file systems. ;;; ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf>. (define (fat32-superblock? sblock) "Return #t when SBLOCK is a fat32 superblock." (bytevector=? (sub-bytevector sblock 82 8) (string->utf8 "FAT32 "))) (define (read-fat32-superblock device) "Return the raw contents of DEVICE's fat32 superblock as a bytevector, or #f if DEVICE does not contain a fat32 file system." (read-superblock device 0 90 fat32-superblock?)) (define (fat32-superblock-uuid sblock) "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector." (sub-bytevector sblock 67 4)) (define (fat32-superblock-volume-name sblock) "Return the volume name of fat superblock SBLOCK as a string of at most 11 characters, or #f if SBLOCK has no volume name. The volume name is a latin1 string. Trailing spaces are trimmed." (string-trim-right (latin1->string (sub-bytevector sblock 71 11) (lambda (c) #f)) #\space)) (define (check-fat-file-system device force? repair) "Return the health of an unmounted FAT file system on DEVICE. FORCE? is ignored: a full file system scan is always performed. If REPAIR is false, do not write to the file system to fix errors. Otherwise, automatically fix them using the least destructive approach." (match (status:exit-val (system*/tty "fsck.vfat" "-v" (match repair (#f "-n") (_ "-a")) ;no 'safe/#t distinction device)) (0 'pass) (1 'errors-corrected) (_ 'fatal-error))) ;;; ;;; FAT16 file systems. ;;; (define (fat16-superblock? sblock) "Return #t when SBLOCK is a fat16 boot record." (bytevector=? (sub-bytevector sblock 54 8) (string->utf8 "FAT16 "))) (define (read-fat16-superblock device) "Return the raw contents of DEVICE's fat16 superblock as a bytevector, or #f if DEVICE does not contain a fat16 file system." (read-superblock device 0 62 fat16-superblock?)) (define (fat16-superblock-uuid sblock) "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector." (sub-bytevector sblock 39 4)) (define (fat16-superblock-volume-name sblock) "Return the volume name of fat superblock SBLOCK as a string of at most 11 characters, or #f if SBLOCK has no volume name. The volume name is a latin1 string. Trailing spaces are trimmed." (string-trim-right (latin1->string (sub-bytevector sblock 43 11) (lambda (c) #f)) #\space)) ;;; ;;; ISO9660 file systems. ;;; ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf>. (define (iso9660-superblock? sblock) "Return #t when SBLOCK is an iso9660 volume descriptor." (bytevector=? (sub-bytevector sblock 1 6) ;; Note: "\x01" is the volume descriptor format version (string->utf8 "CD001\x01"))) (define (read-iso9660-primary-volume-descriptor device offset) "Find and read the first primary volume descriptor, starting at OFFSET. Return #f if not found." (let* ((sblock (read-superblock device offset 2048 iso9660-superblock?)) (type-code (if sblock (bytevector-u8-ref sblock 0) (error (format #f "Could not read ISO9660 primary volume descriptor from ~s" device))))) (match type-code (255 #f) ; Volume Descriptor Set Terminator. (1 sblock) ; Primary Volume Descriptor (_ (read-iso9660-primary-volume-descriptor device (+ offset 2048)))))) (define (read-iso9660-superblock device) "Return the raw contents of DEVICE's iso9660 primary volume descriptor as a bytevector, or #f if DEVICE does not contain an iso9660 file system." ;; Start reading at sector 16. ;; Since we are not sure that the device contains an ISO9660 file system, ;; we have to find that out first. (if (read-superblock device (* 2048 16) 2048 iso9660-superblock?) (read-iso9660-primary-volume-descriptor device (* 2048 16)) #f)) ; Device does not contain an iso9660 file system. (define (iso9660-superblock-uuid sblock) "Return the modification time of an iso9660 primary volume descriptor SBLOCK as a bytevector. If that's not set, returns the creation time." ;; Drops GMT offset for compatibility with Grub, blkid and /dev/disk/by-uuid. ;; Compare Grub: "2014-12-02-19-30-23-00". ;; Compare blkid result: "2014-12-02-19-30-23-00". ;; Compare /dev/disk/by-uuid entry: "2014-12-02-19-30-23-00". (let* ((creation-time (sub-bytevector sblock 813 17)) (modification-time (sub-bytevector sblock 830 17)) (unset-time (make-bytevector 17 0)) (time (if (bytevector=? unset-time modification-time) creation-time modification-time))) (sub-bytevector time 0 16))) ; strips GMT offset. (define (iso9660-superblock-volume-name sblock) "Return the volume name of iso9660 superblock SBLOCK as a string. The volume name is an ASCII string. Trailing spaces are trimmed." ;; Note: Valid characters are of the set "[0-9][A-Z]_" (ECMA-119 Appendix A) (string-trim-right (latin1->string (sub-bytevector sblock 40 32) (lambda (c) #f)) #\space)) ;;; ;;; JFS file systems. ;;; ;; Taken from <linux-libre>/fs/jfs/jfs_superblock.h. (define-syntax %jfs-endianness ;; Endianness of JFS file systems. (identifier-syntax (endianness little))) (define (jfs-superblock? sblock) "Return #t when SBLOCK is a JFS superblock." (bytevector=? (sub-bytevector sblock 0 4) (string->utf8 "JFS1"))) (define (read-jfs-superblock device) "Return the raw contents of DEVICE's JFS superblock as a bytevector, or #f if DEVICE does not contain a JFS file system." (read-superblock device 32768 184 jfs-superblock?)) (define (jfs-superblock-uuid sblock) "Return the UUID of JFS superblock SBLOCK as a 16-byte bytevector." (sub-bytevector sblock 136 16)) (define (jfs-superblock-volume-name sblock) "Return the volume name of JFS 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 152 16))) (define (check-jfs-file-system device force? repair) "Return the health of an unmounted JFS 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, and replay the transaction log only if FORCE? is true. Otherwise, replay the transaction log before checking and automatically fix found errors." (match (status:exit-val (apply system*/tty `("jfs_fsck" "-v" ;; The ‘LEVEL’ logic is convoluted. To quote fsck/xchkdsk.c ;; (‘-p’, ‘-a’, and ‘-r’ are aliases in every way): ;; “If -f was chosen, have it override [-p] by [forcing] a ;; check regardless of the outcome after the log is ;; replayed”. ;; “If -n is specified by itself, don't replay the journal. ;; If -n is specified with [-p], replay the journal but ;; don't make any other changes”. ,@(if force? '("-f") '()) ,@(match repair (#f '("-n")) (_ '("-p"))) ; no 'safe/#t distinction ,device))) (0 'pass) (1 'errors-corrected) (2 'reboot-required) (_ 'fatal-error))) ;;; ;;; F2FS (Flash-Friendly File System) ;;; ;;; https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git/tree/include/linux/f2fs_fs.h ;;; (but using xxd proved to be simpler) (define-syntax %f2fs-endianness ;; Endianness of F2FS file systems (identifier-syntax (endianness little))) ;; F2FS actually stores two adjacent copies of the superblock. ;; should we read both? (define (f2fs-superblock? sblock) "Return #t when SBLOCK is an F2FS superblock." (let ((magic (bytevector-u32-ref sblock 0 %f2fs-endianness))) (= magic #xF2F52010))) (define (read-f2fs-superblock device) "Return the raw contents of DEVICE's F2FS superblock as a bytevector, or #f if DEVICE does not contain an F2FS file system." (read-superblock device ;; offset of magic in first copy #x400 ;; difference between magic of second ;; and first copies (- #x1400 #x400) f2fs-superblock?)) (define (f2fs-superblock-uuid sblock) "Return the UUID of F2FS superblock SBLOCK as a 16-byte bytevector." (sub