1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018 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 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 (rnrs io ports)
33 #:use-module (rnrs bytevectors)
34 #:use-module (ice-9 match)
35 #:use-module (ice-9 rdelim)
36 #:use-module (system foreign)
37 #:autoload (system repl repl) (start-repl)
38 #:use-module (srfi srfi-1)
39 #:use-module (srfi srfi-26)
40 #:export (disk-partitions
41 partition-label-predicate
42 partition-uuid-predicate
43 partition-luks-uuid-predicate
44 find-partition-by-label
45 find-partition-by-uuid
46 find-partition-by-luks-uuid
47 canonicalize-device-spec
51 read-luks-partition-uuid
61 ;;; This modules provides tools to deal with disk partitions, and to mount and
62 ;;; check file systems.
66 (define (bind-mount source target)
67 "Bind-mount SOURCE at TARGET."
68 (mount source target "" MS_BIND))
70 (define (seek* fd/port offset whence)
71 "Like 'seek' but return -1 instead of throwing to 'system-error' upon
72 EINVAL. This makes it easier to catch cases like OFFSET being too large for
76 (seek fd/port offset whence))
78 (if (= EINVAL (system-error-errno args))
80 (apply throw args)))))
82 (define (read-superblock device offset size magic?)
83 "Read a superblock of SIZE from OFFSET and DEVICE. Return the raw
84 superblock on success, and #f if no valid superblock was found. MAGIC?
85 takes a bytevector and returns #t when it's a valid superblock."
86 (call-with-input-file device
88 (and (= offset (seek* port offset SEEK_SET))
89 (let ((block (make-bytevector size)))
90 (match (get-bytevector-n! port block 0 (bytevector-length block))
94 (and (= len (bytevector-length block))
98 (define null-terminated-latin1->string
99 (cut latin1->string <> zero?))
101 (define (bytevector-utf16-length bv)
102 "Given a bytevector BV containing a NUL-terminated UTF16-encoded string,
103 determine where the NUL terminator is and return its index. If there's no
104 NUL terminator, return the size of the bytevector."
105 (let ((length (bytevector-length bv)))
106 (let loop ((index 0))
108 (if (zero? (bytevector-u16-ref bv index 'little))
113 (define* (bytevector->u16-list bv endianness #:optional (index 0))
114 (if (< index (bytevector-length bv))
115 (cons (bytevector-u16-ref bv index endianness)
116 (bytevector->u16-list bv endianness (+ index 2)))
119 ;; The initrd doesn't have iconv data, so do the conversion ourselves.
120 (define (utf16->string bv endianness)
124 (let loop ((remainder (bytevector->u16-list bv endianness))
128 ((a) (cons a result))
130 (if (and (>= a #xD800) (< a #xDC00) ; high surrogate
131 (>= b #xDC00) (< b #xE000)) ; low surrogate
132 (loop x (cons (+ #x10000
133 (* #x400 (- a #xD800))
136 (loop (cons b x) (cons a result))))))))))
138 (define (null-terminated-utf16->string bv endianness)
139 (utf16->string (sub-bytevector bv 0 (bytevector-utf16-length bv))
144 ;;; Ext2 file systems.
147 ;; <http://www.nongnu.org/ext2-doc/ext2.html#DEF-SUPERBLOCK>.
148 ;; TODO: Use "packed structs" from Guile-OpenGL or similar.
150 (define-syntax %ext2-endianness
151 ;; Endianness of ext2 file systems.
152 (identifier-syntax (endianness little)))
154 (define (ext2-superblock? sblock)
155 "Return #t when SBLOCK is an ext2 superblock."
156 (let ((magic (bytevector-u16-ref sblock 56 %ext2-endianness)))
159 (define (read-ext2-superblock device)
160 "Return the raw contents of DEVICE's ext2 superblock as a bytevector, or #f
161 if DEVICE does not contain an ext2 file system."
162 (read-superblock device 1024 264 ext2-superblock?))
164 (define (ext2-superblock-uuid sblock)
165 "Return the UUID of ext2 superblock SBLOCK as a 16-byte bytevector."
166 (sub-bytevector sblock 104 16))
168 (define (ext2-superblock-volume-name sblock)
169 "Return the volume name of SBLOCK as a string of at most 16 characters, or
170 #f if SBLOCK has no volume name."
171 (null-terminated-latin1->string (sub-bytevector sblock 120 16)))
173 (define (check-ext2-file-system device)
174 "Return the health of an ext2 file system on DEVICE."
175 (match (status:exit-val
176 (system* "e2fsck" "-v" "-p" "-C" "0" device))
178 (1 'errors-corrected)
184 ;;; Btrfs file systems.
187 ;; <https://btrfs.wiki.kernel.org/index.php/On-disk_Format#Superblock>.
189 (define-syntax %btrfs-endianness
190 ;; Endianness of btrfs file systems.
191 (identifier-syntax (endianness little)))
193 (define (btrfs-superblock? sblock)
194 "Return #t when SBLOCK is a btrfs superblock."
195 (bytevector=? (sub-bytevector sblock 64 8)
196 (string->utf8 "_BHRfS_M")))
198 (define (read-btrfs-superblock device)
199 "Return the raw contents of DEVICE's btrfs superblock as a bytevector, or #f
200 if DEVICE does not contain a btrfs file system."
201 (read-superblock device 65536 4096 btrfs-superblock?))
203 (define (btrfs-superblock-uuid sblock)
204 "Return the UUID of a btrfs superblock SBLOCK as a 16-byte bytevector."
205 (sub-bytevector sblock 32 16))
207 (define (btrfs-superblock-volume-name sblock)
208 "Return the volume name of SBLOCK as a string of at most 256 characters, or
209 #f if SBLOCK has no volume name."
210 (null-terminated-latin1->string (sub-bytevector sblock 299 256)))
212 (define (check-btrfs-file-system device)
213 "Return the health of a btrfs file system on DEVICE."
214 (match (status:exit-val
215 (system* "btrfs" "device" "scan"))
221 ;;; FAT32 file systems.
224 ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf>.
226 (define (fat32-superblock? sblock)
227 "Return #t when SBLOCK is a fat32 superblock."
228 (bytevector=? (sub-bytevector sblock 82 8)
229 (string->utf8 "FAT32 ")))
231 (define (read-fat32-superblock device)
232 "Return the raw contents of DEVICE's fat32 superblock as a bytevector, or
233 #f if DEVICE does not contain a fat32 file system."
234 (read-superblock device 0 90 fat32-superblock?))
236 (define (fat32-superblock-uuid sblock)
237 "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
238 (sub-bytevector sblock 67 4))
240 (define (fat32-superblock-volume-name sblock)
241 "Return the volume name of SBLOCK as a string of at most 11 characters, or
242 #f if SBLOCK has no volume name. The volume name is a latin1 string.
243 Trailing spaces are trimmed."
244 (string-trim-right (latin1->string (sub-bytevector sblock 71 11) (lambda (c) #f)) #\space))
246 (define (check-fat-file-system device)
247 "Return the health of a fat file system on DEVICE."
248 (match (status:exit-val
249 (system* "fsck.vfat" "-v" "-a" device))
251 (1 'errors-corrected)
256 ;;; FAT16 file systems.
259 (define (fat16-superblock? sblock)
260 "Return #t when SBLOCK is a fat16 boot record."
261 (bytevector=? (sub-bytevector sblock 54 8)
262 (string->utf8 "FAT16 ")))
264 (define (read-fat16-superblock device)
265 "Return the raw contents of DEVICE's fat16 superblock as a bytevector, or
266 #f if DEVICE does not contain a fat16 file system."
267 (read-superblock device 0 62 fat16-superblock?))
269 (define (fat16-superblock-uuid sblock)
270 "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
271 (sub-bytevector sblock 39 4))
273 (define (fat16-superblock-volume-name sblock)
274 "Return the volume name of SBLOCK as a string of at most 11 characters, or
275 #f if SBLOCK has no volume name. The volume name is a latin1 string.
276 Trailing spaces are trimmed."
277 (string-trim-right (latin1->string (sub-bytevector sblock 43 11)
283 ;;; ISO9660 file systems.
286 ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf>.
288 (define (iso9660-superblock? sblock)
289 "Return #t when SBLOCK is an iso9660 volume descriptor."
290 (bytevector=? (sub-bytevector sblock 1 6)
291 ;; Note: "\x01" is the volume descriptor format version
292 (string->utf8 "CD001\x01")))
294 (define (read-iso9660-primary-volume-descriptor device offset)
295 "Find and read the first primary volume descriptor, starting at OFFSET.
296 Return #f if not found."
297 (let* ((sblock (read-superblock device offset 2048 iso9660-superblock?))
298 (type-code (if sblock
299 (bytevector-u8-ref sblock 0)
301 "Could not read ISO9660 primary
302 volume descriptor from ~s"
305 (255 #f) ; Volume Descriptor Set Terminator.
306 (1 sblock) ; Primary Volume Descriptor
307 (_ (read-iso9660-primary-volume-descriptor device (+ offset 2048))))))
309 (define (read-iso9660-superblock device)
310 "Return the raw contents of DEVICE's iso9660 primary volume descriptor
311 as a bytevector, or #f if DEVICE does not contain an iso9660 file system."
312 ;; Start reading at sector 16.
313 ;; Since we are not sure that the device contains an ISO9660 file system,
314 ;; we have to find that out first.
315 (if (read-superblock device (* 2048 16) 2048 iso9660-superblock?)
316 (read-iso9660-primary-volume-descriptor device (* 2048 16))
317 #f)) ; Device does not contain an iso9660 file system.
319 (define (iso9660-superblock-uuid sblock)
320 "Return the modification time of an iso9660 primary volume descriptor
321 SBLOCK as a bytevector. If that's not set, returns the creation time."
322 ;; Drops GMT offset for compatibility with Grub, blkid and /dev/disk/by-uuid.
323 ;; Compare Grub: "2014-12-02-19-30-23-00".
324 ;; Compare blkid result: "2014-12-02-19-30-23-00".
325 ;; Compare /dev/disk/by-uuid entry: "2014-12-02-19-30-23-00".
326 (let* ((creation-time (sub-bytevector sblock 813 17))
327 (modification-time (sub-bytevector sblock 830 17))
328 (unset-time (make-bytevector 17 0))
329 (time (if (bytevector=? unset-time modification-time)
332 (sub-bytevector time 0 16))) ; strips GMT offset.
334 (define (iso9660-superblock-volume-name sblock)
335 "Return the volume name of SBLOCK as a string. The volume name is an ASCII
336 string. Trailing spaces are trimmed."
337 ;; Note: Valid characters are of the set "[0-9][A-Z]_" (ECMA-119 Appendix A)
338 (string-trim-right (latin1->string (sub-bytevector sblock 40 32)
339 (lambda (c) #f)) #\space))
343 ;;; JFS file systems.
346 ;; Taken from <linux-libre>/fs/jfs/jfs_superblock.h.
348 (define-syntax %jfs-endianness
349 ;; Endianness of JFS file systems.
350 (identifier-syntax (endianness little)))
352 (define (jfs-superblock? sblock)
353 "Return #t when SBLOCK is a JFS superblock."
354 (bytevector=? (sub-bytevector sblock 0 4)
355 (string->utf8 "JFS1")))
357 (define (read-jfs-superblock device)
358 "Return the raw contents of DEVICE's JFS superblock as a bytevector, or #f
359 if DEVICE does not contain a JFS file system."
360 (read-superblock device 32768 184 jfs-superblock?))
362 (define (jfs-superblock-uuid sblock)
363 "Return the UUID of JFS superblock SBLOCK as a 16-byte bytevector."
364 (sub-bytevector sblock 136 16))
366 (define (jfs-superblock-volume-name sblock)
367 "Return the volume name of SBLOCK as a string of at most 16 characters, or
368 #f if SBLOCK has no volume name."
369 (null-terminated-latin1->string (sub-bytevector sblock 152 16)))
371 (define (check-jfs-file-system device)
372 "Return the health of a JFS file system on DEVICE."
373 (match (status:exit-val
374 (system* "jfs_fsck" "-p" "-v" device))
376 (1 'errors-corrected)
382 ;;; F2FS (Flash-Friendly File System)
385 ;;; https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git/tree/include/linux/f2fs_fs.h
386 ;;; (but using xxd proved to be simpler)
388 (define-syntax %f2fs-endianness
389 ;; Endianness of F2FS file systems
390 (identifier-syntax (endianness little)))
392 ;; F2FS actually stores two adjacent copies of the superblock.
393 ;; should we read both?
394 (define (f2fs-superblock? sblock)
395 "Return #t when SBLOCK is an F2FS superblock."
396 (let ((magic (bytevector-u32-ref sblock 0 %f2fs-endianness)))
397 (= magic #xF2F52010)))
399 (define (read-f2fs-superblock device)
400 "Return the raw contents of DEVICE's F2FS superblock as a bytevector, or #f
401 if DEVICE does not contain an F2FS file system."
402 (read-superblock device
403 ;; offset of magic in first copy
405 ;; difference between magic of second
410 (define (f2fs-superblock-uuid sblock)
411 "Return the UUID of F2FS superblock SBLOCK as a 16-byte bytevector."
412 (sub-bytevector sblock
414 ;; subtract superblock offset
418 (define (f2fs-superblock-volume-name sblock)
419 "Return the volume name of SBLOCK as a string of at most 512 characters, or
420 #f if SBLOCK has no volume name."
421 (null-terminated-utf16->string
422 (sub-bytevector sblock (- (+ #x470 12) #x400) 512)
425 (define (check-f2fs-file-system device)
426 "Return the health of a F2FS file system on DEVICE."
427 (match (status:exit-val
428 (system* "fsck.f2fs" "-p" device))
429 ;; 0 and -1 are the only two possibilities
430 ;; (according to the manpage)
436 ;;; LUKS encrypted devices.
439 ;; The LUKS header format is described in "LUKS On-Disk Format Specification":
440 ;; <https://gitlab.com/cryptsetup/cryptsetup/wikis/Specification>. We follow
441 ;; version 1.2.1 of this document.
443 ;; The LUKS2 header format is described in "LUKS2 On-Disk Format Specification":
444 ;; <https://gitlab.com/cryptsetup/LUKS2-docs/blob/master/luks2_doc_wip.pdf>.
445 ;; It is a WIP document.
447 (define-syntax %luks-endianness
448 ;; Endianness of LUKS headers.
449 (identifier-syntax (endianness big)))
451 (define (luks-superblock? sblock)
452 "Return #t when SBLOCK is a luks superblock."
454 ;; The 'LUKS_MAGIC' constant.
455 (u8-list->bytevector (append (map char->integer (string->list "LUKS"))
457 (let ((magic (sub-bytevector sblock 0 6))
458 (version (bytevector-u16-ref sblock 6 %luks-endianness)))
459 (and (bytevector=? magic %luks-magic)
460 (or (= version 1) (= version 2)))))
462 (define (read-luks-header file)
463 "Read a LUKS header from FILE. Return the raw header on success, and #f if
464 not valid header was found."
465 ;; Size in bytes of the LUKS binary header, which includes key slots in
466 ;; LUKS1. In LUKS2 the binary header is partially backward compatible, so
467 ;; that UUID can be extracted as for LUKS1. Keyslots and other metadata are
468 ;; not part of this header in LUKS2, but are included in the JSON metadata
469 ;; area that follows.
470 (read-superblock file 0 592 luks-superblock?))
472 (define (luks-header-uuid header)
473 "Return the LUKS UUID from HEADER, as a 16-byte bytevector."
474 ;; 40 bytes are reserved for the UUID, but in practice, it contains the 36
475 ;; bytes of its ASCII representation.
476 (let ((uuid (sub-bytevector header 168 36)))
477 (string->uuid (utf8->string uuid))))
481 ;;; NTFS file systems.
484 ;; Taken from <linux-libre>/fs/ntfs/layout.h
486 (define-syntax %ntfs-endianness
487 ;; Endianness of NTFS file systems.
488 (identifier-syntax (endianness little)))
490 (define (ntfs-superblock? sblock)
491 "Return #t when SBLOCK is a NTFS superblock."
492 (bytevector=? (sub-bytevector sblock 3 8)
493 (string->utf8 "NTFS ")))
495 (define (read-ntfs-superblock device)
496 "Return the raw contents of DEVICE's NTFS superblock as a bytevector, or #f
497 if DEVICE does not contain a NTFS file system."
498 (read-superblock device 0 511 ntfs-superblock?))
500 (define (ntfs-superblock-uuid sblock)
501 "Return the UUID of NTFS superblock SBLOCK as a 8-byte bytevector."
502 (sub-bytevector sblock 72 8))
504 ;; TODO: Add ntfs-superblock-volume-name. The partition label is not stored
505 ;; in the BOOT SECTOR like the UUID, but in the MASTER FILE TABLE, which seems
506 ;; way harder to access.
508 (define (check-ntfs-file-system device)
509 "Return the health of a NTFS file system on DEVICE."
510 (match (status:exit-val
511 (system* "ntfsfix" device))
517 ;;; Partition lookup.
520 (define (disk-partitions)
521 "Return the list of device names corresponding to valid disk partitions."
522 (define (partition? name major minor)
523 ;; grub-mkrescue does some funny things for EFI support which
524 ;; makes it a lot more difficult than one would expect to support
525 ;; booting an ISO-9660 image from an USB flash drive.
526 ;; For example there's a buggy (too small) hidden partition in it
527 ;; which Linux mounts and then proceeds to fail while trying to
528 ;; fall off the edge.
529 ;; In any case, partition tables are supposed to be optional so
530 ;; here we allow checking entire disks for file systems, too.
531 (> major 2)) ;ignore RAM disks and floppy disks
533 (call-with-input-file "/proc/partitions"
535 ;; Skip the two header lines.
539 ;; Read each subsequent line, and extract the last space-separated
541 (let loop ((parts '()))
542 (let ((line (read-line port)))
543 (if (eof-object? line)
545 (match (string-tokenize line)
546 (((= string->number major) (= string->number minor)
548 (if (partition? name major minor)
549 (loop (cons name parts))
550 (loop parts))))))))))
552 (define (ENOENT-safe proc)
553 "Wrap the one-argument PROC such that ENOENT errors are caught and lead to a
554 warning and #f as the result."
560 ;; When running on the hand-made /dev,
561 ;; 'disk-partitions' could return partitions for which
562 ;; we have no /dev node. Handle that gracefully.
563 (let ((errno (system-error-errno args)))
564 (cond ((= ENOENT errno)
565 (format (current-error-port)
566 "warning: device '~a' not found~%" device)
568 ((= ENOMEDIUM errno) ;for removable media
570 ((= EIO errno) ;unreadable hardware like audio CDs
571 (format (current-error-port)
572 "warning: failed to read from device '~a'~%" device)
575 (apply throw args))))))))
577 (define (partition-field-reader read field)
578 "Return a procedure that takes a device and returns the value of a FIELD in
579 the partition superblock or #f."
580 (let ((read (ENOENT-safe read)))
582 (let ((sblock (read device)))
586 (define (read-partition-field device partition-field-readers)
587 "Returns the value of a FIELD in the partition superblock of DEVICE or #f. It
588 takes a list of PARTITION-FIELD-READERS and returns the result of the first
589 partition field reader that returned a value."
590 (match (filter-map (cut apply <> (list device)) partition-field-readers)
594 (define %partition-label-readers
595 (list (partition-field-reader read-iso9660-superblock
596 iso9660-superblock-volume-name)
597 (partition-field-reader read-ext2-superblock
598 ext2-superblock-volume-name)
599 (partition-field-reader read-btrfs-superblock
600 btrfs-superblock-volume-name)
601 (partition-field-reader read-fat32-superblock
602 fat32-superblock-volume-name)
603 (partition-field-reader read-fat16-superblock
604 fat16-superblock-volume-name)
605 (partition-field-reader read-jfs-superblock
606 jfs-superblock-volume-name)
607 (partition-field-reader read-f2fs-superblock
608 f2fs-superblock-volume-name)))
610 (define %partition-uuid-readers
611 (list (partition-field-reader read-iso9660-superblock
612 iso9660-superblock-uuid)
613 (partition-field-reader read-ext2-superblock
614 ext2-superblock-uuid)
615 (partition-field-reader read-btrfs-superblock
616 btrfs-superblock-uuid)
617 (partition-field-reader read-fat32-superblock
618 fat32-superblock-uuid)
619 (partition-field-reader read-fat16-superblock
620 fat16-superblock-uuid)
621 (partition-field-reader read-jfs-superblock
623 (partition-field-reader read-f2fs-superblock
624 f2fs-superblock-uuid)
625 (partition-field-reader read-ntfs-superblock
626 ntfs-superblock-uuid)))
628 (define read-partition-label
629 (cut read-partition-field <> %partition-label-readers))
631 (define read-partition-uuid
632 (cut read-partition-field <> %partition-uuid-readers))
634 (define luks-partition-field-reader
635 (partition-field-reader read-luks-header luks-header-uuid))
637 (define read-luks-partition-uuid
638 (cut read-partition-field <> (list luks-partition-field-reader)))
640 (define (partition-predicate reader =)
641 "Return a predicate that returns true if the FIELD of partition header that
642 was READ is = to the given value."
645 (let ((actual (reader device)))
647 (= actual expected))))))
649 (define partition-label-predicate
650 (partition-predicate read-partition-label string=?))
652 (define partition-uuid-predicate
653 (partition-predicate read-partition-uuid uuid=?))
655 (define luks-partition-uuid-predicate
656 (partition-predicate luks-partition-field-reader uuid=?))
658 (define (find-partition predicate)
659 "Return the first partition found that matches PREDICATE, or #f if none
662 (find (predicate expected)
663 (map (cut string-append "/dev/" <>)
664 (disk-partitions)))))
666 (define find-partition-by-label
667 (find-partition partition-label-predicate))
669 (define find-partition-by-uuid
670 (find-partition partition-uuid-predicate))
672 (define find-partition-by-luks-uuid
673 (find-partition luks-partition-uuid-predicate))
676 (define (canonicalize-device-spec spec)
677 "Return the device name corresponding to SPEC, which can be a <uuid>, a
678 <file-system-label>, or a string (typically a /dev file name or an nfs-root
681 ;; Number of times we retry partition label resolution, 1 second per
682 ;; trial. Note: somebody reported a delay of 16 seconds (!) before their
683 ;; USB key would be detected by the kernel, so we must wait for at least
687 (define (resolve find-partition spec fmt)
688 (let loop ((count 0))
689 (let ((device (find-partition spec)))
691 ;; Some devices take a bit of time to appear, most notably USB
692 ;; storage devices. Thus, wait for the device to appear.
693 (if (> count max-trials)
694 (error "failed to resolve partition" (fmt spec))
696 (format #t "waiting for partition '~a' to appear...~%"
699 (loop (+ 1 count))))))))
703 (if (string-contains spec ":/")
704 spec ; do not resolve NFS devices
705 ;; Nothing to do, but wait until SPEC shows up.
706 (resolve identity spec identity)))
707 ((? file-system-label?)
708 ;; Resolve the label.
709 (resolve find-partition-by-label
710 (file-system-label->string spec)
713 (resolve find-partition-by-uuid
714 (uuid-bytevector spec)
717 (define (check-file-system device type)
718 "Run a file system check of TYPE on DEVICE."
719 (define check-procedure
721 ((string-prefix? "ext" type) check-ext2-file-system)
722 ((string-prefix? "btrfs" type) check-btrfs-file-system)
723 ((string-suffix? "fat" type) check-fat-file-system)
724 ((string-prefix? "jfs" type) check-jfs-file-system)
725 ((string-prefix? "f2fs" type) check-f2fs-file-system)
726 ((string-prefix? "ntfs" type) check-ntfs-file-system)
727 ((string-prefix? "nfs" type) (const 'pass))
731 (match (check-procedure device)
735 (format (current-error-port)
736 "File system check corrected errors on ~a; continuing~%"
739 (format (current-error-port)
740 "File system check corrected errors on ~a; rebooting~%"
745 (format (current-error-port) "File system check on ~a failed~%"
748 ;; Spawn a REPL only if someone would be able to interact with it.
749 (when (isatty? (current-input-port))
750 (format (current-error-port) "Spawning Bourne-like REPL.~%")
752 ;; 'current-output-port' is typically connected to /dev/klog (in
753 ;; PID 1), but here we want to make sure we talk directly to the
755 (with-output-to-file "/dev/console"
757 (start-repl %bournish-language))))))
758 (format (current-error-port)
759 "No file system check procedure for ~a; skipping~%"
762 (define (mount-flags->bit-mask flags)
763 "Return the number suitable for the 'flags' argument of 'mount' that
764 corresponds to the symbols listed in FLAGS."
765 (let loop ((flags flags))
767 (('read-only rest ...)
768 (logior MS_RDONLY (loop rest)))
769 (('bind-mount rest ...)
770 (logior MS_BIND (loop rest)))
772 (logior MS_NOSUID (loop rest)))
774 (logior MS_NODEV (loop rest)))
776 (logior MS_NOEXEC (loop rest)))
777 (('no-atime rest ...)
778 (logior MS_NOATIME (loop rest)))
779 (('strict-atime rest ...)
780 (logior MS_STRICTATIME (loop rest)))
781 (('lazy-time rest ...)
782 (logior MS_LAZYTIME (loop rest)))
786 (define* (mount-file-system fs #:key (root "/root"))
787 "Mount the file system described by FS, a <file-system> object, under ROOT."
789 (define (mount-nfs source mount-point type flags options)
790 (let* ((idx (string-rindex source #\:))
791 (host-part (string-take source idx))
792 ;; Strip [] from around host if present
793 (host (match (string-split host-part (string->char-set "[]"))
796 (aa (match (getaddrinfo host "nfs") ((x . _) x)))
797 (sa (addrinfo:addr aa))
798 (inet-addr (inet-ntop (sockaddr:fam sa)
799 (sockaddr:addr sa))))
801 ;; Mounting an NFS file system requires passing the address
802 ;; of the server in the addr= option
803 (mount source mount-point type flags
804 (string-append "addr="
807 (string-append "," options)
809 (let ((type (file-system-type fs))
810 (options (file-system-options fs))
811 (source (canonicalize-device-spec (file-system-device fs)))
812 (mount-point (string-append root "/"
813 (file-system-mount-point fs)))
814 (flags (mount-flags->bit-mask (file-system-flags fs))))
815 (when (file-system-check? fs)
816 (check-file-system source type))
820 ;; Create the mount point. Most of the time this is a directory, but
821 ;; in the case of a bind mount, a regular file or socket may be
823 (if (and (= MS_BIND (logand flags MS_BIND))
824 (not (file-is-directory? source)))
825 (unless (file-exists? mount-point)
826 (mkdir-p (dirname mount-point))
827 (call-with-output-file mount-point (const #t)))
828 (mkdir-p mount-point))
831 ((string-prefix? "nfs" type)
832 (mount-nfs source mount-point type flags options))
834 (mount source mount-point type flags options)))
836 ;; For read-only bind mounts, an extra remount is needed, as per
837 ;; <http://lwn.net/Articles/281157/>, which still applies to Linux
839 (when (and (= MS_BIND (logand flags MS_BIND))
840 (= MS_RDONLY (logand flags MS_RDONLY)))
841 (let ((flags (logior MS_BIND MS_REMOUNT MS_RDONLY)))
842 (mount source mount-point type flags #f))))
844 (or (file-system-mount-may-fail? fs)
845 (apply throw args))))))
847 ;;; file-systems.scm ends here