tests: nfs: Improve "nfs-root-fs".
[jackhill/guix/guix.git] / gnu / build / file-systems.scm
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>
9 ;;;
10 ;;; This file is part of GNU Guix.
11 ;;;
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.
16 ;;;
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.
21 ;;;
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/>.
24
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
48
49 read-partition-label
50 read-partition-uuid
51 read-luks-partition-uuid
52
53 bind-mount
54
55 mount-flags->bit-mask
56 check-file-system
57 mount-file-system))
58
59 ;;; Commentary:
60 ;;;
61 ;;; This modules provides tools to deal with disk partitions, and to mount and
62 ;;; check file systems.
63 ;;;
64 ;;; Code:
65
66 (define (bind-mount source target)
67 "Bind-mount SOURCE at TARGET."
68 (mount source target "" MS_BIND))
69
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
73 FD/PORT."
74 (catch 'system-error
75 (lambda ()
76 (seek fd/port offset whence))
77 (lambda args
78 (if (= EINVAL (system-error-errno args))
79 -1
80 (apply throw args)))))
81
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
87 (lambda (port)
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))
91 ((? eof-object?)
92 #f)
93 ((? number? len)
94 (and (= len (bytevector-length block))
95 (and (magic? block)
96 block)))))))))
97
98 (define null-terminated-latin1->string
99 (cut latin1->string <> zero?))
100
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))
107 (if (< index length)
108 (if (zero? (bytevector-u16-ref bv index 'little))
109 index
110 (loop (+ index 2)))
111 length))))
112
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)))
117 '()))
118
119 ;; The initrd doesn't have iconv data, so do the conversion ourselves.
120 (define (utf16->string bv endianness)
121 (list->string
122 (map integer->char
123 (reverse
124 (let loop ((remainder (bytevector->u16-list bv endianness))
125 (result '()))
126 (match remainder
127 (() result)
128 ((a) (cons a result))
129 ((a b x ...)
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))
134 (- b #xDC00))
135 result))
136 (loop (cons b x) (cons a result))))))))))
137
138 (define (null-terminated-utf16->string bv endianness)
139 (utf16->string (sub-bytevector bv 0 (bytevector-utf16-length bv))
140 endianness))
141
142 \f
143 ;;;
144 ;;; Ext2 file systems.
145 ;;;
146
147 ;; <http://www.nongnu.org/ext2-doc/ext2.html#DEF-SUPERBLOCK>.
148 ;; TODO: Use "packed structs" from Guile-OpenGL or similar.
149
150 (define-syntax %ext2-endianness
151 ;; Endianness of ext2 file systems.
152 (identifier-syntax (endianness little)))
153
154 (define (ext2-superblock? sblock)
155 "Return #t when SBLOCK is an ext2 superblock."
156 (let ((magic (bytevector-u16-ref sblock 56 %ext2-endianness)))
157 (= magic #xef53)))
158
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?))
163
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))
167
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)))
172
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))
177 (0 'pass)
178 (1 'errors-corrected)
179 (2 'reboot-required)
180 (_ 'fatal-error)))
181
182 \f
183 ;;;
184 ;;; Btrfs file systems.
185 ;;;
186
187 ;; <https://btrfs.wiki.kernel.org/index.php/On-disk_Format#Superblock>.
188
189 (define-syntax %btrfs-endianness
190 ;; Endianness of btrfs file systems.
191 (identifier-syntax (endianness little)))
192
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")))
197
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?))
202
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))
206
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)))
211
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"))
216 (0 'pass)
217 (_ 'fatal-error)))
218
219 \f
220 ;;;
221 ;;; FAT32 file systems.
222 ;;;
223
224 ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf>.
225
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 ")))
230
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?))
235
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))
239
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))
245
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))
250 (0 'pass)
251 (1 'errors-corrected)
252 (_ 'fatal-error)))
253
254 \f
255 ;;;
256 ;;; FAT16 file systems.
257 ;;;
258
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 ")))
263
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?))
268
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))
272
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)
278 (lambda (c) #f))
279 #\space))
280
281 \f
282 ;;;
283 ;;; ISO9660 file systems.
284 ;;;
285
286 ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf>.
287
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")))
293
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)
300 (error (format #f
301 "Could not read ISO9660 primary
302 volume descriptor from ~s"
303 device)))))
304 (match type-code
305 (255 #f) ; Volume Descriptor Set Terminator.
306 (1 sblock) ; Primary Volume Descriptor
307 (_ (read-iso9660-primary-volume-descriptor device (+ offset 2048))))))
308
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.
318
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)
330 creation-time
331 modification-time)))
332 (sub-bytevector time 0 16))) ; strips GMT offset.
333
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))
340
341 \f
342 ;;;
343 ;;; JFS file systems.
344 ;;;
345
346 ;; Taken from <linux-libre>/fs/jfs/jfs_superblock.h.
347
348 (define-syntax %jfs-endianness
349 ;; Endianness of JFS file systems.
350 (identifier-syntax (endianness little)))
351
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")))
356
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?))
361
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))
365
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)))
370
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))
375 (0 'pass)
376 (1 'errors-corrected)
377 (2 'reboot-required)
378 (_ 'fatal-error)))
379
380 \f
381 ;;;
382 ;;; F2FS (Flash-Friendly File System)
383 ;;;
384
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)
387
388 (define-syntax %f2fs-endianness
389 ;; Endianness of F2FS file systems
390 (identifier-syntax (endianness little)))
391
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)))
398
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
404 #x400
405 ;; difference between magic of second
406 ;; and first copies
407 (- #x1400 #x400)
408 f2fs-superblock?))
409
410 (define (f2fs-superblock-uuid sblock)
411 "Return the UUID of F2FS superblock SBLOCK as a 16-byte bytevector."
412 (sub-bytevector sblock
413 (- (+ #x460 12)
414 ;; subtract superblock offset
415 #x400)
416 16))
417
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)
423 %f2fs-endianness))
424
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)
431 (0 'pass)
432 (_ 'fatal-error)))
433
434 \f
435 ;;;
436 ;;; LUKS encrypted devices.
437 ;;;
438
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.
442
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.
446
447 (define-syntax %luks-endianness
448 ;; Endianness of LUKS headers.
449 (identifier-syntax (endianness big)))
450
451 (define (luks-superblock? sblock)
452 "Return #t when SBLOCK is a luks superblock."
453 (define %luks-magic
454 ;; The 'LUKS_MAGIC' constant.
455 (u8-list->bytevector (append (map char->integer (string->list "LUKS"))
456 (list #xba #xbe))))
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)))))
461
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?))
471
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))))
478
479 \f
480 ;;;
481 ;;; NTFS file systems.
482 ;;;
483
484 ;; Taken from <linux-libre>/fs/ntfs/layout.h
485
486 (define-syntax %ntfs-endianness
487 ;; Endianness of NTFS file systems.
488 (identifier-syntax (endianness little)))
489
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 ")))
494
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?))
499
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))
503
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.
507
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))
512 (0 'pass)
513 (_ 'fatal-error)))
514
515 \f
516 ;;;
517 ;;; Partition lookup.
518 ;;;
519
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
532
533 (call-with-input-file "/proc/partitions"
534 (lambda (port)
535 ;; Skip the two header lines.
536 (read-line port)
537 (read-line port)
538
539 ;; Read each subsequent line, and extract the last space-separated
540 ;; field.
541 (let loop ((parts '()))
542 (let ((line (read-line port)))
543 (if (eof-object? line)
544 (reverse parts)
545 (match (string-tokenize line)
546 (((= string->number major) (= string->number minor)
547 blocks name)
548 (if (partition? name major minor)
549 (loop (cons name parts))
550 (loop parts))))))))))
551
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."
555 (lambda (device)
556 (catch 'system-error
557 (lambda ()
558 (proc device))
559 (lambda args
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)
567 #f)
568 ((= ENOMEDIUM errno) ;for removable media
569 #f)
570 ((= EIO errno) ;unreadable hardware like audio CDs
571 (format (current-error-port)
572 "warning: failed to read from device '~a'~%" device)
573 #f)
574 (else
575 (apply throw args))))))))
576
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)))
581 (lambda (device)
582 (let ((sblock (read device)))
583 (and sblock
584 (field sblock))))))
585
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)
591 ((field . _) field)
592 (_ #f)))
593
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)))
609
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
622 jfs-superblock-uuid)
623 (partition-field-reader read-f2fs-superblock
624 f2fs-superblock-uuid)
625 (partition-field-reader read-ntfs-superblock
626 ntfs-superblock-uuid)))
627
628 (define read-partition-label
629 (cut read-partition-field <> %partition-label-readers))
630
631 (define read-partition-uuid
632 (cut read-partition-field <> %partition-uuid-readers))
633
634 (define luks-partition-field-reader
635 (partition-field-reader read-luks-header luks-header-uuid))
636
637 (define read-luks-partition-uuid
638 (cut read-partition-field <> (list luks-partition-field-reader)))
639
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."
643 (lambda (expected)
644 (lambda (device)
645 (let ((actual (reader device)))
646 (and actual
647 (= actual expected))))))
648
649 (define partition-label-predicate
650 (partition-predicate read-partition-label string=?))
651
652 (define partition-uuid-predicate
653 (partition-predicate read-partition-uuid uuid=?))
654
655 (define luks-partition-uuid-predicate
656 (partition-predicate luks-partition-field-reader uuid=?))
657
658 (define (find-partition predicate)
659 "Return the first partition found that matches PREDICATE, or #f if none
660 were found."
661 (lambda (expected)
662 (find (predicate expected)
663 (map (cut string-append "/dev/" <>)
664 (disk-partitions)))))
665
666 (define find-partition-by-label
667 (find-partition partition-label-predicate))
668
669 (define find-partition-by-uuid
670 (find-partition partition-uuid-predicate))
671
672 (define find-partition-by-luks-uuid
673 (find-partition luks-partition-uuid-predicate))
674
675 \f
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
679 containing ':/')."
680 (define max-trials
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
684 ;; this long.
685 20)
686
687 (define (resolve find-partition spec fmt)
688 (let loop ((count 0))
689 (let ((device (find-partition spec)))
690 (or device
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))
695 (begin
696 (format #t "waiting for partition '~a' to appear...~%"
697 (fmt spec))
698 (sleep 1)
699 (loop (+ 1 count))))))))
700
701 (match spec
702 ((? string?)
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)
711 identity))
712 ((? uuid?)
713 (resolve find-partition-by-uuid
714 (uuid-bytevector spec)
715 uuid->string))))
716
717 (define (check-file-system device type)
718 "Run a file system check of TYPE on DEVICE."
719 (define check-procedure
720 (cond
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))
728 (else #f)))
729
730 (if check-procedure
731 (match (check-procedure device)
732 ('pass
733 #t)
734 ('errors-corrected
735 (format (current-error-port)
736 "File system check corrected errors on ~a; continuing~%"
737 device))
738 ('reboot-required
739 (format (current-error-port)
740 "File system check corrected errors on ~a; rebooting~%"
741 device)
742 (sleep 3)
743 (reboot))
744 ('fatal-error
745 (format (current-error-port) "File system check on ~a failed~%"
746 device)
747
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.~%")
751
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
754 ;; user.
755 (with-output-to-file "/dev/console"
756 (lambda ()
757 (start-repl %bournish-language))))))
758 (format (current-error-port)
759 "No file system check procedure for ~a; skipping~%"
760 device)))
761
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))
766 (match flags
767 (('read-only rest ...)
768 (logior MS_RDONLY (loop rest)))
769 (('bind-mount rest ...)
770 (logior MS_BIND (loop rest)))
771 (('no-suid rest ...)
772 (logior MS_NOSUID (loop rest)))
773 (('no-dev rest ...)
774 (logior MS_NODEV (loop rest)))
775 (('no-exec 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)))
783 (()
784 0))))
785
786 (define* (mount-file-system fs #:key (root "/root"))
787 "Mount the file system described by FS, a <file-system> object, under ROOT."
788
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 "[]"))
794 (("" h "") h)
795 ((h) h)))
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))))
800
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="
805 inet-addr
806 (if options
807 (string-append "," options)
808 "")))))
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))
817
818 (catch 'system-error
819 (lambda ()
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
822 ;; needed.
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))
829
830 (cond
831 ((string-prefix? "nfs" type)
832 (mount-nfs source mount-point type flags options))
833 (else
834 (mount source mount-point type flags options)))
835
836 ;; For read-only bind mounts, an extra remount is needed, as per
837 ;; <http://lwn.net/Articles/281157/>, which still applies to Linux
838 ;; 4.0.
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))))
843 (lambda args
844 (or (file-system-mount-may-fail? fs)
845 (apply throw args))))))
846
847 ;;; file-systems.scm ends here