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>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (gnu build file-systems)
22 #:use-module (gnu system uuid)
23 #:use-module (gnu system file-systems)
24 #:use-module (guix build utils)
25 #:use-module (guix build bournish)
26 #:use-module ((guix build syscalls)
27 #:hide (file-system-type))
28 #:use-module (rnrs io ports)
29 #:use-module (rnrs bytevectors)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 rdelim)
32 #:use-module (system foreign)
33 #:autoload (system repl repl) (start-repl)
34 #:use-module (srfi srfi-1)
35 #:use-module (srfi srfi-26)
36 #:export (disk-partitions
37 partition-label-predicate
38 partition-uuid-predicate
39 partition-luks-uuid-predicate
40 find-partition-by-label
41 find-partition-by-uuid
42 find-partition-by-luks-uuid
43 canonicalize-device-spec
47 read-luks-partition-uuid
57 ;;; This modules provides tools to deal with disk partitions, and to mount and
58 ;;; check file systems.
62 (define (bind-mount source target)
63 "Bind-mount SOURCE at TARGET."
64 (mount source target "" MS_BIND))
66 (define (seek* fd/port offset whence)
67 "Like 'seek' but return -1 instead of throwing to 'system-error' upon
68 EINVAL. This makes it easier to catch cases like OFFSET being too large for
72 (seek fd/port offset whence))
74 (if (= EINVAL (system-error-errno args))
76 (apply throw args)))))
78 (define (read-superblock device offset size magic?)
79 "Read a superblock of SIZE from OFFSET and DEVICE. Return the raw
80 superblock on success, and #f if no valid superblock was found. MAGIC?
81 takes a bytevector and returns #t when it's a valid superblock."
82 (call-with-input-file device
84 (and (= offset (seek* port offset SEEK_SET))
85 (let ((block (make-bytevector size)))
86 (match (get-bytevector-n! port block 0 (bytevector-length block))
90 (and (= len (bytevector-length block))
94 (define null-terminated-latin1->string
95 (cut latin1->string <> zero?))
99 ;;; Ext2 file systems.
102 ;; <http://www.nongnu.org/ext2-doc/ext2.html#DEF-SUPERBLOCK>.
103 ;; TODO: Use "packed structs" from Guile-OpenGL or similar.
105 (define-syntax %ext2-endianness
106 ;; Endianness of ext2 file systems.
107 (identifier-syntax (endianness little)))
109 (define (ext2-superblock? sblock)
110 "Return #t when SBLOCK is an ext2 superblock."
111 (let ((magic (bytevector-u16-ref sblock 56 %ext2-endianness)))
114 (define (read-ext2-superblock device)
115 "Return the raw contents of DEVICE's ext2 superblock as a bytevector, or #f
116 if DEVICE does not contain an ext2 file system."
117 (read-superblock device 1024 264 ext2-superblock?))
119 (define (ext2-superblock-uuid sblock)
120 "Return the UUID of ext2 superblock SBLOCK as a 16-byte bytevector."
121 (sub-bytevector sblock 104 16))
123 (define (ext2-superblock-volume-name sblock)
124 "Return the volume name of SBLOCK as a string of at most 16 characters, or
125 #f if SBLOCK has no volume name."
126 (null-terminated-latin1->string (sub-bytevector sblock 120 16)))
128 (define (check-ext2-file-system device)
129 "Return the health of an ext2 file system on DEVICE."
130 (match (status:exit-val
131 (system* "e2fsck" "-v" "-p" "-C" "0" device))
133 (1 'errors-corrected)
139 ;;; Btrfs file systems.
142 ;; <https://btrfs.wiki.kernel.org/index.php/On-disk_Format#Superblock>.
144 (define-syntax %btrfs-endianness
145 ;; Endianness of btrfs file systems.
146 (identifier-syntax (endianness little)))
148 (define (btrfs-superblock? sblock)
149 "Return #t when SBLOCK is a btrfs superblock."
150 (bytevector=? (sub-bytevector sblock 64 8)
151 (string->utf8 "_BHRfS_M")))
153 (define (read-btrfs-superblock device)
154 "Return the raw contents of DEVICE's btrfs superblock as a bytevector, or #f
155 if DEVICE does not contain a btrfs file system."
156 (read-superblock device 65536 4096 btrfs-superblock?))
158 (define (btrfs-superblock-uuid sblock)
159 "Return the UUID of a btrfs superblock SBLOCK as a 16-byte bytevector."
160 (sub-bytevector sblock 32 16))
162 (define (btrfs-superblock-volume-name sblock)
163 "Return the volume name of SBLOCK as a string of at most 256 characters, or
164 #f if SBLOCK has no volume name."
165 (null-terminated-latin1->string (sub-bytevector sblock 299 256)))
167 (define (check-btrfs-file-system device)
168 "Return the health of a btrfs file system on DEVICE."
169 (match (status:exit-val
170 (system* "btrfs" "device" "scan"))
176 ;;; FAT32 file systems.
179 ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf>.
181 (define (fat32-superblock? sblock)
182 "Return #t when SBLOCK is a fat32 superblock."
183 (bytevector=? (sub-bytevector sblock 82 8)
184 (string->utf8 "FAT32 ")))
186 (define (read-fat32-superblock device)
187 "Return the raw contents of DEVICE's fat32 superblock as a bytevector, or
188 #f if DEVICE does not contain a fat32 file system."
189 (read-superblock device 0 90 fat32-superblock?))
191 (define (fat32-superblock-uuid sblock)
192 "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
193 (sub-bytevector sblock 67 4))
195 (define (fat32-superblock-volume-name sblock)
196 "Return the volume name of SBLOCK as a string of at most 11 characters, or
197 #f if SBLOCK has no volume name. The volume name is a latin1 string.
198 Trailing spaces are trimmed."
199 (string-trim-right (latin1->string (sub-bytevector sblock 71 11) (lambda (c) #f)) #\space))
201 (define (check-fat-file-system device)
202 "Return the health of a fat file system on DEVICE."
203 (match (status:exit-val
204 (system* "fsck.vfat" "-v" "-a" device))
206 (1 'errors-corrected)
211 ;;; FAT16 file systems.
214 (define (fat16-superblock? sblock)
215 "Return #t when SBLOCK is a fat16 boot record."
216 (bytevector=? (sub-bytevector sblock 54 8)
217 (string->utf8 "FAT16 ")))
219 (define (read-fat16-superblock device)
220 "Return the raw contents of DEVICE's fat16 superblock as a bytevector, or
221 #f if DEVICE does not contain a fat16 file system."
222 (read-superblock device 0 62 fat16-superblock?))
224 (define (fat16-superblock-uuid sblock)
225 "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
226 (sub-bytevector sblock 39 4))
228 (define (fat16-superblock-volume-name sblock)
229 "Return the volume name of SBLOCK as a string of at most 11 characters, or
230 #f if SBLOCK has no volume name. The volume name is a latin1 string.
231 Trailing spaces are trimmed."
232 (string-trim-right (latin1->string (sub-bytevector sblock 43 11)
238 ;;; ISO9660 file systems.
241 ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf>.
243 (define (iso9660-superblock? sblock)
244 "Return #t when SBLOCK is an iso9660 volume descriptor."
245 (bytevector=? (sub-bytevector sblock 1 6)
246 ;; Note: "\x01" is the volume descriptor format version
247 (string->utf8 "CD001\x01")))
249 (define (read-iso9660-primary-volume-descriptor device offset)
250 "Find and read the first primary volume descriptor, starting at OFFSET.
251 Return #f if not found."
252 (let* ((sblock (read-superblock device offset 2048 iso9660-superblock?))
253 (type-code (if sblock
254 (bytevector-u8-ref sblock 0)
256 "Could not read ISO9660 primary
257 volume descriptor from ~s"
260 (255 #f) ; Volume Descriptor Set Terminator.
261 (1 sblock) ; Primary Volume Descriptor
262 (_ (read-iso9660-primary-volume-descriptor device (+ offset 2048))))))
264 (define (read-iso9660-superblock device)
265 "Return the raw contents of DEVICE's iso9660 primary volume descriptor
266 as a bytevector, or #f if DEVICE does not contain an iso9660 file system."
267 ;; Start reading at sector 16.
268 ;; Since we are not sure that the device contains an ISO9660 file system,
269 ;; we have to find that out first.
270 (if (read-superblock device (* 2048 16) 2048 iso9660-superblock?)
271 (read-iso9660-primary-volume-descriptor device (* 2048 16))
272 #f)) ; Device does not contain an iso9660 file system.
274 (define (iso9660-superblock-uuid sblock)
275 "Return the modification time of an iso9660 primary volume descriptor
276 SBLOCK as a bytevector. If that's not set, returns the creation time."
277 ;; Drops GMT offset for compatibility with Grub, blkid and /dev/disk/by-uuid.
278 ;; Compare Grub: "2014-12-02-19-30-23-00".
279 ;; Compare blkid result: "2014-12-02-19-30-23-00".
280 ;; Compare /dev/disk/by-uuid entry: "2014-12-02-19-30-23-00".
281 (let* ((creation-time (sub-bytevector sblock 813 17))
282 (modification-time (sub-bytevector sblock 830 17))
283 (unset-time (make-bytevector 17 0))
284 (time (if (bytevector=? unset-time modification-time)
287 (sub-bytevector time 0 16))) ; strips GMT offset.
289 (define (iso9660-superblock-volume-name sblock)
290 "Return the volume name of SBLOCK as a string. The volume name is an ASCII
291 string. Trailing spaces are trimmed."
292 ;; Note: Valid characters are of the set "[0-9][A-Z]_" (ECMA-119 Appendix A)
293 (string-trim-right (latin1->string (sub-bytevector sblock 40 32)
294 (lambda (c) #f)) #\space))
298 ;;; LUKS encrypted devices.
301 ;; The LUKS header format is described in "LUKS On-Disk Format Specification":
302 ;; <https://gitlab.com/cryptsetup/cryptsetup/wikis/Specification>. We follow
303 ;; version 1.2.1 of this document.
305 (define-syntax %luks-endianness
306 ;; Endianness of LUKS headers.
307 (identifier-syntax (endianness big)))
309 (define (luks-superblock? sblock)
310 "Return #t when SBLOCK is a luks superblock."
312 ;; The 'LUKS_MAGIC' constant.
313 (u8-list->bytevector (append (map char->integer (string->list "LUKS"))
315 (let ((magic (sub-bytevector sblock 0 6))
316 (version (bytevector-u16-ref sblock 6 %luks-endianness)))
317 (and (bytevector=? magic %luks-magic)
320 (define (read-luks-header file)
321 "Read a LUKS header from FILE. Return the raw header on success, and #f if
322 not valid header was found."
323 ;; Size in bytes of the LUKS header, including key slots.
324 (read-superblock file 0 592 luks-superblock?))
326 (define (luks-header-uuid header)
327 "Return the LUKS UUID from HEADER, as a 16-byte bytevector."
328 ;; 40 bytes are reserved for the UUID, but in practice, it contains the 36
329 ;; bytes of its ASCII representation.
330 (let ((uuid (sub-bytevector header 168 36)))
331 (string->uuid (utf8->string uuid))))
335 ;;; Partition lookup.
338 (define (disk-partitions)
339 "Return the list of device names corresponding to valid disk partitions."
340 (define (partition? name major minor)
341 ;; grub-mkrescue does some funny things for EFI support which
342 ;; makes it a lot more difficult than one would expect to support
343 ;; booting an ISO-9660 image from an USB flash drive.
344 ;; For example there's a buggy (too small) hidden partition in it
345 ;; which Linux mounts and then proceeds to fail while trying to
346 ;; fall off the edge.
347 ;; In any case, partition tables are supposed to be optional so
348 ;; here we allow checking entire disks for file systems, too.
349 (> major 2)) ;ignore RAM disks and floppy disks
351 (call-with-input-file "/proc/partitions"
353 ;; Skip the two header lines.
357 ;; Read each subsequent line, and extract the last space-separated
359 (let loop ((parts '()))
360 (let ((line (read-line port)))
361 (if (eof-object? line)
363 (match (string-tokenize line)
364 (((= string->number major) (= string->number minor)
366 (if (partition? name major minor)
367 (loop (cons name parts))
368 (loop parts))))))))))
370 (define (ENOENT-safe proc)
371 "Wrap the one-argument PROC such that ENOENT errors are caught and lead to a
372 warning and #f as the result."
378 ;; When running on the hand-made /dev,
379 ;; 'disk-partitions' could return partitions for which
380 ;; we have no /dev node. Handle that gracefully.
381 (let ((errno (system-error-errno args)))
382 (cond ((= ENOENT errno)
383 (format (current-error-port)
384 "warning: device '~a' not found~%" device)
386 ((= ENOMEDIUM errno) ;for removable media
388 ((= EIO errno) ;unreadable hardware like audio CDs
389 (format (current-error-port)
390 "warning: failed to read from device '~a'~%" device)
393 (apply throw args))))))))
395 (define (partition-field-reader read field)
396 "Return a procedure that takes a device and returns the value of a FIELD in
397 the partition superblock or #f."
398 (let ((read (ENOENT-safe read)))
400 (let ((sblock (read device)))
404 (define (read-partition-field device partition-field-readers)
405 "Returns the value of a FIELD in the partition superblock of DEVICE or #f. It
406 takes a list of PARTITION-FIELD-READERS and returns the result of the first
407 partition field reader that returned a value."
408 (match (filter-map (cut apply <> (list device)) partition-field-readers)
412 (define %partition-label-readers
413 (list (partition-field-reader read-iso9660-superblock
414 iso9660-superblock-volume-name)
415 (partition-field-reader read-ext2-superblock
416 ext2-superblock-volume-name)
417 (partition-field-reader read-btrfs-superblock
418 btrfs-superblock-volume-name)
419 (partition-field-reader read-fat32-superblock
420 fat32-superblock-volume-name)
421 (partition-field-reader read-fat16-superblock
422 fat16-superblock-volume-name)))
424 (define %partition-uuid-readers
425 (list (partition-field-reader read-iso9660-superblock
426 iso9660-superblock-uuid)
427 (partition-field-reader read-ext2-superblock
428 ext2-superblock-uuid)
429 (partition-field-reader read-btrfs-superblock
430 btrfs-superblock-uuid)
431 (partition-field-reader read-fat32-superblock
432 fat32-superblock-uuid)
433 (partition-field-reader read-fat16-superblock
434 fat16-superblock-uuid)))
436 (define read-partition-label
437 (cut read-partition-field <> %partition-label-readers))
439 (define read-partition-uuid
440 (cut read-partition-field <> %partition-uuid-readers))
442 (define luks-partition-field-reader
443 (partition-field-reader read-luks-header luks-header-uuid))
445 (define read-luks-partition-uuid
446 (cut read-partition-field <> (list luks-partition-field-reader)))
448 (define (partition-predicate reader =)
449 "Return a predicate that returns true if the FIELD of partition header that
450 was READ is = to the given value."
453 (let ((actual (reader device)))
455 (= actual expected))))))
457 (define partition-label-predicate
458 (partition-predicate read-partition-label string=?))
460 (define partition-uuid-predicate
461 (partition-predicate read-partition-uuid uuid=?))
463 (define luks-partition-uuid-predicate
464 (partition-predicate luks-partition-field-reader uuid=?))
466 (define (find-partition predicate)
467 "Return the first partition found that matches PREDICATE, or #f if none
470 (find (predicate expected)
471 (map (cut string-append "/dev/" <>)
472 (disk-partitions)))))
474 (define find-partition-by-label
475 (find-partition partition-label-predicate))
477 (define find-partition-by-uuid
478 (find-partition partition-uuid-predicate))
480 (define find-partition-by-luks-uuid
481 (find-partition luks-partition-uuid-predicate))
484 (define (canonicalize-device-spec spec)
485 "Return the device name corresponding to SPEC, which can be a <uuid>, a
486 <file-system-label>, or a string (typically a /dev file name)."
488 ;; Number of times we retry partition label resolution, 1 second per
489 ;; trial. Note: somebody reported a delay of 16 seconds (!) before their
490 ;; USB key would be detected by the kernel, so we must wait for at least
494 (define (resolve find-partition spec fmt)
495 (let loop ((count 0))
496 (let ((device (find-partition spec)))
498 ;; Some devices take a bit of time to appear, most notably USB
499 ;; storage devices. Thus, wait for the device to appear.
500 (if (> count max-trials)
501 (error "failed to resolve partition" (fmt spec))
503 (format #t "waiting for partition '~a' to appear...~%"
506 (loop (+ 1 count))))))))
510 ;; Nothing to do, but wait until SPEC shows up.
511 (resolve identity spec identity))
512 ((? file-system-label?)
513 ;; Resolve the label.
514 (resolve find-partition-by-label
515 (file-system-label->string spec)
518 (resolve find-partition-by-uuid
519 (uuid-bytevector spec)
522 (define (check-file-system device type)
523 "Run a file system check of TYPE on DEVICE."
524 (define check-procedure
526 ((string-prefix? "ext" type) check-ext2-file-system)
527 ((string-prefix? "btrfs" type) check-btrfs-file-system)
528 ((string-suffix? "fat" type) check-fat-file-system)
532 (match (check-procedure device)
536 (format (current-error-port)
537 "File system check corrected errors on ~a; continuing~%"
540 (format (current-error-port)
541 "File system check corrected errors on ~a; rebooting~%"
546 (format (current-error-port) "File system check on ~a failed~%"
549 ;; Spawn a REPL only if someone would be able to interact with it.
550 (when (isatty? (current-input-port))
551 (format (current-error-port) "Spawning Bourne-like REPL.~%")
553 ;; 'current-output-port' is typically connected to /dev/klog (in
554 ;; PID 1), but here we want to make sure we talk directly to the
556 (with-output-to-file "/dev/console"
558 (start-repl %bournish-language))))))
559 (format (current-error-port)
560 "No file system check procedure for ~a; skipping~%"
563 (define (mount-flags->bit-mask flags)
564 "Return the number suitable for the 'flags' argument of 'mount' that
565 corresponds to the symbols listed in FLAGS."
566 (let loop ((flags flags))
568 (('read-only rest ...)
569 (logior MS_RDONLY (loop rest)))
570 (('bind-mount rest ...)
571 (logior MS_BIND (loop rest)))
573 (logior MS_NOSUID (loop rest)))
575 (logior MS_NODEV (loop rest)))
577 (logior MS_NOEXEC (loop rest)))
578 (('no-atime rest ...)
579 (logior MS_NOATIME (loop rest)))
583 (define* (mount-file-system fs #:key (root "/root"))
584 "Mount the file system described by FS, a <file-system> object, under ROOT.
586 DEVICE, MOUNT-POINT, and TYPE must be strings; OPTIONS can be a string or #f;
587 FLAGS must be a list of symbols. CHECK? is a Boolean indicating whether to
588 run a file system check."
590 (define (mount-nfs source mount-point type flags options)
591 (let* ((idx (string-rindex source #\:))
592 (host-part (string-take source idx))
593 ;; Strip [] from around host if present
594 (host (match (string-split host-part (string->char-set "[]"))
597 (aa (match (getaddrinfo host "nfs") ((x . _) x)))
598 (sa (addrinfo:addr aa))
599 (inet-addr (inet-ntop (sockaddr:fam sa)
600 (sockaddr:addr sa))))
602 ;; Mounting an NFS file system requires passing the address
603 ;; of the server in the addr= option
604 (mount source mount-point type flags
605 (string-append "addr="
608 (string-append "," options)
610 (let ((type (file-system-type fs))
611 (options (file-system-options fs))
612 (source (canonicalize-device-spec (file-system-device fs)))
613 (mount-point (string-append root "/"
614 (file-system-mount-point fs)))
615 (flags (mount-flags->bit-mask (file-system-flags fs))))
616 (when (file-system-check? fs)
617 (check-file-system source type))
619 ;; Create the mount point. Most of the time this is a directory, but
620 ;; in the case of a bind mount, a regular file or socket may be needed.
621 (if (and (= MS_BIND (logand flags MS_BIND))
622 (not (file-is-directory? source)))
623 (unless (file-exists? mount-point)
624 (mkdir-p (dirname mount-point))
625 (call-with-output-file mount-point (const #t)))
626 (mkdir-p mount-point))
629 ((string-prefix? "nfs" type)
630 (mount-nfs source mount-point type flags options))
632 (mount source mount-point type flags options)))
634 ;; For read-only bind mounts, an extra remount is needed, as per
635 ;; <http://lwn.net/Articles/281157/>, which still applies to Linux 4.0.
636 (when (and (= MS_BIND (logand flags MS_BIND))
637 (= MS_RDONLY (logand flags MS_RDONLY)))
638 (let ((flags (logior MS_BIND MS_REMOUNT MS_RDONLY)))
639 (mount source mount-point type flags #f)))))
641 ;;; file-systems.scm ends here