Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / gnu / build / file-systems.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017 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 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
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.
12 ;;;
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.
17 ;;;
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/>.
20
21 (define-module (gnu build file-systems)
22 #:use-module (guix build utils)
23 #:use-module (guix build bournish)
24 #:use-module (guix build syscalls)
25 #:use-module (rnrs io ports)
26 #:use-module (rnrs bytevectors)
27 #:use-module (ice-9 match)
28 #:use-module (ice-9 rdelim)
29 #:use-module (ice-9 format)
30 #:use-module (ice-9 regex)
31 #:use-module (system foreign)
32 #:autoload (system repl repl) (start-repl)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-26)
35 #:export (disk-partitions
36 partition-label-predicate
37 partition-uuid-predicate
38 partition-luks-uuid-predicate
39 find-partition-by-label
40 find-partition-by-uuid
41 find-partition-by-luks-uuid
42 canonicalize-device-spec
43
44 uuid->string
45 string->uuid
46
47 bind-mount
48
49 mount-flags->bit-mask
50 check-file-system
51 mount-file-system))
52
53 ;;; Commentary:
54 ;;;
55 ;;; This modules provides tools to deal with disk partitions, and to mount and
56 ;;; check file systems.
57 ;;;
58 ;;; Code:
59
60 (define (bind-mount source target)
61 "Bind-mount SOURCE at TARGET."
62 (mount source target "" MS_BIND))
63
64 (define (seek* fd/port offset whence)
65 "Like 'seek' but return -1 instead of throwing to 'system-error' upon
66 EINVAL. This makes it easier to catch cases like OFFSET being too large for
67 FD/PORT."
68 (catch 'system-error
69 (lambda ()
70 (seek fd/port offset whence))
71 (lambda args
72 (if (= EINVAL (system-error-errno args))
73 -1
74 (apply throw args)))))
75
76 (define (read-superblock device offset size magic?)
77 "Read a superblock of SIZE from OFFSET and DEVICE. Return the raw
78 superblock on success, and #f if no valid superblock was found. MAGIC?
79 takes a bytevector and returns #t when it's a valid superblock."
80 (call-with-input-file device
81 (lambda (port)
82 (and (= offset (seek* port offset SEEK_SET))
83 (let ((block (make-bytevector size)))
84 (match (get-bytevector-n! port block 0 (bytevector-length block))
85 ((? eof-object?)
86 #f)
87 ((? number? len)
88 (and (= len (bytevector-length block))
89 (and (magic? block)
90 block)))))))))
91
92 (define (sub-bytevector bv start size)
93 "Return a copy of the SIZE bytes of BV starting from offset START."
94 (let ((result (make-bytevector size)))
95 (bytevector-copy! bv start result 0 size)
96 result))
97
98 (define (latin1->string bv terminator)
99 "Return a string of BV, a latin1 bytevector, or #f. TERMINATOR is a predicate
100 that takes a number and returns #t when a termination character is found."
101 (let ((bytes (take-while (negate terminator) (bytevector->u8-list bv))))
102 (if (null? bytes)
103 #f
104 (list->string (map integer->char bytes)))))
105
106 (define null-terminated-latin1->string
107 (cut latin1->string <> zero?))
108
109 \f
110 ;;;
111 ;;; Ext2 file systems.
112 ;;;
113
114 ;; <http://www.nongnu.org/ext2-doc/ext2.html#DEF-SUPERBLOCK>.
115 ;; TODO: Use "packed structs" from Guile-OpenGL or similar.
116
117 (define-syntax %ext2-endianness
118 ;; Endianness of ext2 file systems.
119 (identifier-syntax (endianness little)))
120
121 (define (ext2-superblock? sblock)
122 "Return #t when SBLOCK is an ext2 superblock."
123 (let ((magic (bytevector-u16-ref sblock 56 %ext2-endianness)))
124 (= magic #xef53)))
125
126 (define (read-ext2-superblock device)
127 "Return the raw contents of DEVICE's ext2 superblock as a bytevector, or #f
128 if DEVICE does not contain an ext2 file system."
129 (read-superblock device 1024 264 ext2-superblock?))
130
131 (define (ext2-superblock-uuid sblock)
132 "Return the UUID of ext2 superblock SBLOCK as a 16-byte bytevector."
133 (sub-bytevector sblock 104 16))
134
135 (define (ext2-superblock-volume-name sblock)
136 "Return the volume name of SBLOCK as a string of at most 16 characters, or
137 #f if SBLOCK has no volume name."
138 (null-terminated-latin1->string (sub-bytevector sblock 120 16)))
139
140 (define (check-ext2-file-system device)
141 "Return the health of an ext2 file system on DEVICE."
142 (match (status:exit-val
143 (system* "e2fsck" "-v" "-p" "-C" "0" device))
144 (0 'pass)
145 (1 'errors-corrected)
146 (2 'reboot-required)
147 (_ 'fatal-error)))
148
149 \f
150 ;;;
151 ;;; Btrfs file systems.
152 ;;;
153
154 ;; <https://btrfs.wiki.kernel.org/index.php/On-disk_Format#Superblock>.
155
156 (define-syntax %btrfs-endianness
157 ;; Endianness of btrfs file systems.
158 (identifier-syntax (endianness little)))
159
160 (define (btrfs-superblock? sblock)
161 "Return #t when SBLOCK is a btrfs superblock."
162 (bytevector=? (sub-bytevector sblock 64 8)
163 (string->utf8 "_BHRfS_M")))
164
165 (define (read-btrfs-superblock device)
166 "Return the raw contents of DEVICE's btrfs superblock as a bytevector, or #f
167 if DEVICE does not contain a btrfs file system."
168 (read-superblock device 65536 4096 btrfs-superblock?))
169
170 (define (btrfs-superblock-uuid sblock)
171 "Return the UUID of a btrfs superblock SBLOCK as a 16-byte bytevector."
172 (sub-bytevector sblock 32 16))
173
174 (define (btrfs-superblock-volume-name sblock)
175 "Return the volume name of SBLOCK as a string of at most 256 characters, or
176 #f if SBLOCK has no volume name."
177 (null-terminated-latin1->string (sub-bytevector sblock 299 256)))
178
179 (define (check-btrfs-file-system device)
180 "Return the health of a btrfs file system on DEVICE."
181 (match (status:exit-val
182 (system* "btrfs" "device" "scan"))
183 (0 'pass)
184 (_ 'fatal-error)))
185
186 \f
187 ;;;
188 ;;; FAT32 file systems.
189 ;;;
190
191 ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf>.
192
193 (define-syntax %fat32-endianness
194 ;; Endianness of fat file systems.
195 (identifier-syntax (endianness little)))
196
197 (define (fat32-superblock? sblock)
198 "Return #t when SBLOCK is a fat32 superblock."
199 (bytevector=? (sub-bytevector sblock 82 8)
200 (string->utf8 "FAT32 ")))
201
202 (define (read-fat32-superblock device)
203 "Return the raw contents of DEVICE's fat32 superblock as a bytevector, or
204 #f if DEVICE does not contain a fat32 file system."
205 (read-superblock device 0 90 fat32-superblock?))
206
207 (define (fat32-superblock-uuid sblock)
208 "Return the Volume ID of a fat superblock SBLOCK as a 4-byte bytevector."
209 (sub-bytevector sblock 67 4))
210
211 (define (fat32-uuid->string uuid)
212 "Convert fat32 UUID, a 4-byte bytevector, to its string representation."
213 (let ((high (bytevector-uint-ref uuid 0 %fat32-endianness 2))
214 (low (bytevector-uint-ref uuid 2 %fat32-endianness 2)))
215 (format #f "~:@(~x-~x~)" low high)))
216
217 (define (fat32-superblock-volume-name sblock)
218 "Return the volume name of SBLOCK as a string of at most 11 characters, or
219 #f if SBLOCK has no volume name. The volume name is a latin1 string.
220 Trailing spaces are trimmed."
221 (string-trim-right (latin1->string (sub-bytevector sblock 71 11) (lambda (c) #f)) #\space))
222
223 (define (check-fat32-file-system device)
224 "Return the health of a fat file system on DEVICE."
225 (match (status:exit-val
226 (system* "fsck.vfat" "-v" "-a" device))
227 (0 'pass)
228 (1 'errors-corrected)
229 (_ 'fatal-error)))
230
231 \f
232 ;;;
233 ;;; ISO9660 file systems.
234 ;;;
235
236 ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf>.
237
238 (define (iso9660-superblock? sblock)
239 "Return #t when SBLOCK is an iso9660 volume descriptor."
240 (bytevector=? (sub-bytevector sblock 1 6)
241 ;; Note: "\x01" is the volume descriptor format version
242 (string->utf8 "CD001\x01")))
243
244 (define (read-iso9660-primary-volume-descriptor device offset)
245 "Find and read the first primary volume descriptor, starting at OFFSET.
246 Return #f if not found."
247 (let* ((sblock (read-superblock device offset 2048 iso9660-superblock?))
248 (type-code (if sblock
249 (bytevector-u8-ref sblock 0)
250 (error (format #f
251 "Could not read ISO9660 primary
252 volume descriptor from ~s"
253 device)))))
254 (match type-code
255 (255 #f) ; Volume Descriptor Set Terminator.
256 (1 sblock) ; Primary Volume Descriptor
257 (_ (read-iso9660-primary-volume-descriptor device (+ offset 2048))))))
258
259 (define (read-iso9660-superblock device)
260 "Return the raw contents of DEVICE's iso9660 primary volume descriptor
261 as a bytevector, or #f if DEVICE does not contain an iso9660 file system."
262 ;; Start reading at sector 16.
263 (read-iso9660-primary-volume-descriptor device (* 2048 16)))
264
265 (define (iso9660-superblock-uuid sblock)
266 "Return the modification time of an iso9660 primary volume descriptor
267 SBLOCK as a bytevector."
268 ;; Drops GMT offset for compatibility with Grub, blkid and /dev/disk/by-uuid.
269 ;; Compare Grub: "2014-12-02-19-30-23-00".
270 ;; Compare blkid result: "2014-12-02-19-30-23-00".
271 ;; Compare /dev/disk/by-uuid entry: "2014-12-02-19-30-23-00".
272 (sub-bytevector sblock 830 16))
273
274 (define (iso9660-uuid->string uuid)
275 "Given an UUID bytevector, return its timestamp string."
276 (define (digits->string bytes)
277 (latin1->string bytes (lambda (c) #f)))
278 (let* ((year (sub-bytevector uuid 0 4))
279 (month (sub-bytevector uuid 4 2))
280 (day (sub-bytevector uuid 6 2))
281 (hour (sub-bytevector uuid 8 2))
282 (minute (sub-bytevector uuid 10 2))
283 (second (sub-bytevector uuid 12 2))
284 (hundredths (sub-bytevector uuid 14 2))
285 (parts (list year month day hour minute second hundredths)))
286 (string-append (string-join (map digits->string parts)))))
287
288 (define (iso9660-superblock-volume-name sblock)
289 "Return the volume name of SBLOCK as a string. The volume name is an ASCII
290 string. Trailing spaces are trimmed."
291 ;; Note: Valid characters are of the set "[0-9][A-Z]_" (ECMA-119 Appendix A)
292 (string-trim-right (latin1->string (sub-bytevector sblock 40 32)
293 (lambda (c) #f)) #\space))
294
295 \f
296 ;;;
297 ;;; LUKS encrypted devices.
298 ;;;
299
300 ;; The LUKS header format is described in "LUKS On-Disk Format Specification":
301 ;; <https://gitlab.com/cryptsetup/cryptsetup/wikis/Specification>. We follow
302 ;; version 1.2.1 of this document.
303
304 (define-syntax %luks-endianness
305 ;; Endianness of LUKS headers.
306 (identifier-syntax (endianness big)))
307
308 (define (luks-superblock? sblock)
309 "Return #t when SBLOCK is a luks superblock."
310 (define %luks-magic
311 ;; The 'LUKS_MAGIC' constant.
312 (u8-list->bytevector (append (map char->integer (string->list "LUKS"))
313 (list #xba #xbe))))
314 (let ((magic (sub-bytevector sblock 0 6))
315 (version (bytevector-u16-ref sblock 6 %luks-endianness)))
316 (and (bytevector=? magic %luks-magic)
317 (= version 1))))
318
319 (define (read-luks-header file)
320 "Read a LUKS header from FILE. Return the raw header on success, and #f if
321 not valid header was found."
322 ;; Size in bytes of the LUKS header, including key slots.
323 (read-superblock file 0 592 luks-superblock?))
324
325 (define (luks-header-uuid header)
326 "Return the LUKS UUID from HEADER, as a 16-byte bytevector."
327 ;; 40 bytes are reserved for the UUID, but in practice, it contains the 36
328 ;; bytes of its ASCII representation.
329 (let ((uuid (sub-bytevector header 168 36)))
330 (string->uuid (utf8->string uuid))))
331
332 \f
333 ;;;
334 ;;; Partition lookup.
335 ;;;
336
337 (define (disk-partitions)
338 "Return the list of device names corresponding to valid disk partitions."
339 (define (last-character str)
340 (string-ref str (- (string-length str) 1)))
341
342 (define (partition? name major minor)
343 ;; Select device names that end in a digit, like libblkid's 'probe_all'
344 ;; function does. Checking for "/sys/dev/block/MAJOR:MINOR/partition"
345 ;; doesn't work for partitions coming from mapped devices.
346 (and (char-set-contains? char-set:digit (last-character name))
347 (> major 2))) ;ignore RAM disks and floppy disks
348
349 (call-with-input-file "/proc/partitions"
350 (lambda (port)
351 ;; Skip the two header lines.
352 (read-line port)
353 (read-line port)
354
355 ;; Read each subsequent line, and extract the last space-separated
356 ;; field.
357 (let loop ((parts '()))
358 (let ((line (read-line port)))
359 (if (eof-object? line)
360 (reverse parts)
361 (match (string-tokenize line)
362 (((= string->number major) (= string->number minor)
363 blocks name)
364 (if (partition? name major minor)
365 (loop (cons name parts))
366 (loop parts))))))))))
367
368 (define (ENOENT-safe proc)
369 "Wrap the one-argument PROC such that ENOENT errors are caught and lead to a
370 warning and #f as the result."
371 (lambda (device)
372 (catch 'system-error
373 (lambda ()
374 (proc device))
375 (lambda args
376 ;; When running on the hand-made /dev,
377 ;; 'disk-partitions' could return partitions for which
378 ;; we have no /dev node. Handle that gracefully.
379 (let ((errno (system-error-errno args)))
380 (cond ((= ENOENT errno)
381 (format (current-error-port)
382 "warning: device '~a' not found~%" device)
383 #f)
384 ((= ENOMEDIUM errno) ;for removable media
385 #f)
386 (else
387 (apply throw args))))))))
388
389 (define (partition-field-reader read field)
390 "Return a procedure that takes a device and returns the value of a FIELD in
391 the partition superblock or #f."
392 (let ((read (ENOENT-safe read)))
393 (lambda (device)
394 (let ((sblock (read device)))
395 (and sblock
396 (field sblock))))))
397
398 (define (read-partition-field device partition-field-readers)
399 "Returns the value of a FIELD in the partition superblock of DEVICE or #f. It
400 takes a list of PARTITION-FIELD-READERS and returns the result of the first
401 partition field reader that returned a value."
402 (match (filter-map (cut apply <> (list device)) partition-field-readers)
403 ((field . _) field)
404 (_ #f)))
405
406 (define %partition-label-readers
407 (list (partition-field-reader read-iso9660-superblock
408 iso9660-superblock-volume-name)
409 (partition-field-reader read-ext2-superblock
410 ext2-superblock-volume-name)
411 (partition-field-reader read-btrfs-superblock
412 btrfs-superblock-volume-name)
413 (partition-field-reader read-fat32-superblock
414 fat32-superblock-volume-name)))
415
416 (define %partition-uuid-readers
417 (list (partition-field-reader read-iso9660-superblock
418 iso9660-superblock-uuid)
419 (partition-field-reader read-ext2-superblock
420 ext2-superblock-uuid)
421 (partition-field-reader read-btrfs-superblock
422 btrfs-superblock-uuid)
423 (partition-field-reader read-fat32-superblock
424 fat32-superblock-uuid)))
425
426 (define read-partition-label
427 (cut read-partition-field <> %partition-label-readers))
428
429 (define read-partition-uuid
430 (cut read-partition-field <> %partition-uuid-readers))
431
432 (define (partition-predicate reader =)
433 "Return a predicate that returns true if the FIELD of partition header that
434 was READ is = to the given value."
435 (lambda (expected)
436 (lambda (device)
437 (let ((actual (reader device)))
438 (and actual
439 (= actual expected))))))
440
441 (define partition-label-predicate
442 (partition-predicate read-partition-label string=?))
443
444 (define partition-uuid-predicate
445 (partition-predicate read-partition-uuid bytevector=?))
446
447 (define luks-partition-uuid-predicate
448 (partition-predicate
449 (partition-field-reader read-luks-header luks-header-uuid)
450 bytevector=?))
451
452 (define (find-partition predicate)
453 "Return the first partition found that matches PREDICATE, or #f if none
454 were found."
455 (lambda (expected)
456 (find (predicate expected)
457 (map (cut string-append "/dev/" <>)
458 (disk-partitions)))))
459
460 (define find-partition-by-label
461 (find-partition partition-label-predicate))
462
463 (define find-partition-by-uuid
464 (find-partition partition-uuid-predicate))
465
466 (define find-partition-by-luks-uuid
467 (find-partition luks-partition-uuid-predicate))
468
469 \f
470 ;;;
471 ;;; UUIDs.
472 ;;;
473
474 (define-syntax %network-byte-order
475 (identifier-syntax (endianness big)))
476
477 (define (uuid->string uuid)
478 "Convert UUID, a 16-byte bytevector, to its string representation, something
479 like \"6b700d61-5550-48a1-874c-a3d86998990e\"."
480 ;; See <https://tools.ietf.org/html/rfc4122>.
481 (let ((time-low (bytevector-uint-ref uuid 0 %network-byte-order 4))
482 (time-mid (bytevector-uint-ref uuid 4 %network-byte-order 2))
483 (time-hi (bytevector-uint-ref uuid 6 %network-byte-order 2))
484 (clock-seq (bytevector-uint-ref uuid 8 %network-byte-order 2))
485 (node (bytevector-uint-ref uuid 10 %network-byte-order 6)))
486 (format #f "~8,'0x-~4,'0x-~4,'0x-~4,'0x-~12,'0x"
487 time-low time-mid time-hi clock-seq node)))
488
489 (define %uuid-rx
490 ;; The regexp of a UUID.
491 (make-regexp "^([[:xdigit:]]{8})-([[:xdigit:]]{4})-([[:xdigit:]]{4})-([[:xdigit:]]{4})-([[:xdigit:]]{12})$"))
492
493 (define (string->uuid str)
494 "Parse STR as a DCE UUID (see <https://tools.ietf.org/html/rfc4122>) and
495 return its contents as a 16-byte bytevector. Return #f if STR is not a valid
496 UUID representation."
497 (and=> (regexp-exec %uuid-rx str)
498 (lambda (match)
499 (letrec-syntax ((hex->number
500 (syntax-rules ()
501 ((_ index)
502 (string->number (match:substring match index)
503 16))))
504 (put!
505 (syntax-rules ()
506 ((_ bv index (number len) rest ...)
507 (begin
508 (bytevector-uint-set! bv index number
509 (endianness big) len)
510 (put! bv (+ index len) rest ...)))
511 ((_ bv index)
512 bv))))
513 (let ((time-low (hex->number 1))
514 (time-mid (hex->number 2))
515 (time-hi (hex->number 3))
516 (clock-seq (hex->number 4))
517 (node (hex->number 5))
518 (uuid (make-bytevector 16)))
519 (put! uuid 0
520 (time-low 4) (time-mid 2) (time-hi 2)
521 (clock-seq 2) (node 6)))))))
522
523 \f
524 (define* (canonicalize-device-spec spec #:optional (title 'any))
525 "Return the device name corresponding to SPEC. TITLE is a symbol, one of
526 the following:
527
528 • 'device', in which case SPEC is known to designate a device node--e.g.,
529 \"/dev/sda1\";
530 • 'label', in which case SPEC is known to designate a partition label--e.g.,
531 \"my-root-part\";
532 • 'uuid', in which case SPEC must be a UUID (a 16-byte bytevector)
533 designating a partition;
534 • 'any', in which case SPEC can be anything.
535 "
536 (define max-trials
537 ;; Number of times we retry partition label resolution, 1 second per
538 ;; trial. Note: somebody reported a delay of 16 seconds (!) before their
539 ;; USB key would be detected by the kernel, so we must wait for at least
540 ;; this long.
541 20)
542
543 (define canonical-title
544 ;; The realm of canonicalization.
545 (if (eq? title 'any)
546 (if (string? spec)
547 ;; The "--root=SPEC" kernel command-line option always provides a
548 ;; string, but the string can represent a device, a UUID, or a
549 ;; label. So check for all three.
550 (cond ((string-prefix? "/" spec) 'device)
551 ((string->uuid spec) 'uuid)
552 (else 'label))
553 'uuid)
554 title))
555
556 (define (resolve find-partition spec fmt)
557 (let loop ((count 0))
558 (let ((device (find-partition spec)))
559 (or device
560 ;; Some devices take a bit of time to appear, most notably USB
561 ;; storage devices. Thus, wait for the device to appear.
562 (if (> count max-trials)
563 (error "failed to resolve partition" (fmt spec))
564 (begin
565 (format #t "waiting for partition '~a' to appear...~%"
566 (fmt spec))
567 (sleep 1)
568 (loop (+ 1 count))))))))
569
570 (case canonical-title
571 ((device)
572 ;; Nothing to do.
573 spec)
574 ((label)
575 ;; Resolve the label.
576 (resolve find-partition-by-label spec identity))
577 ((uuid)
578 (resolve find-partition-by-uuid
579 (if (string? spec)
580 (string->uuid spec)
581 spec)
582 uuid->string))
583 (else
584 (error "unknown device title" title))))
585
586 (define (check-file-system device type)
587 "Run a file system check of TYPE on DEVICE."
588 (define check-procedure
589 (cond
590 ((string-prefix? "ext" type) check-ext2-file-system)
591 ((string-prefix? "btrfs" type) check-btrfs-file-system)
592 ((string-suffix? "fat" type) check-fat32-file-system)
593 (else #f)))
594
595 (if check-procedure
596 (match (check-procedure device)
597 ('pass
598 #t)
599 ('errors-corrected
600 (format (current-error-port)
601 "File system check corrected errors on ~a; continuing~%"
602 device))
603 ('reboot-required
604 (format (current-error-port)
605 "File system check corrected errors on ~a; rebooting~%"
606 device)
607 (sleep 3)
608 (reboot))
609 ('fatal-error
610 (format (current-error-port)
611 "File system check on ~a failed; spawning Bourne-like REPL~%"
612 device)
613 (start-repl %bournish-language)))
614 (format (current-error-port)
615 "No file system check procedure for ~a; skipping~%"
616 device)))
617
618 (define (mount-flags->bit-mask flags)
619 "Return the number suitable for the 'flags' argument of 'mount' that
620 corresponds to the symbols listed in FLAGS."
621 (let loop ((flags flags))
622 (match flags
623 (('read-only rest ...)
624 (logior MS_RDONLY (loop rest)))
625 (('bind-mount rest ...)
626 (logior MS_BIND (loop rest)))
627 (('no-suid rest ...)
628 (logior MS_NOSUID (loop rest)))
629 (('no-dev rest ...)
630 (logior MS_NODEV (loop rest)))
631 (('no-exec rest ...)
632 (logior MS_NOEXEC (loop rest)))
633 (()
634 0))))
635
636 (define* (mount-file-system spec #:key (root "/root"))
637 "Mount the file system described by SPEC under ROOT. SPEC must have the
638 form:
639
640 (DEVICE TITLE MOUNT-POINT TYPE (FLAGS ...) OPTIONS CHECK?)
641
642 DEVICE, MOUNT-POINT, and TYPE must be strings; OPTIONS can be a string or #f;
643 FLAGS must be a list of symbols. CHECK? is a Boolean indicating whether to
644 run a file system check."
645
646 (define (mount-nfs source mount-point type flags options)
647 (let* ((idx (string-rindex source #\:))
648 (host-part (string-take source idx))
649 ;; Strip [] from around host if present
650 (host (match (string-split host-part (string->char-set "[]"))
651 (("" h "") h)
652 ((h) h)))
653 (aa (match (getaddrinfo host "nfs") ((x . _) x)))
654 (sa (addrinfo:addr aa))
655 (inet-addr (inet-ntop (sockaddr:fam sa)
656 (sockaddr:addr sa))))
657
658 ;; Mounting an NFS file system requires passing the address
659 ;; of the server in the addr= option
660 (mount source mount-point type flags
661 (string-append "addr="
662 inet-addr
663 (if options
664 (string-append "," options)
665 "")))))
666 (match spec
667 ((source title mount-point type (flags ...) options check?)
668 (let ((source (canonicalize-device-spec source title))
669 (mount-point (string-append root "/" mount-point))
670 (flags (mount-flags->bit-mask flags)))
671 (when check?
672 (check-file-system source type))
673
674 ;; Create the mount point. Most of the time this is a directory, but
675 ;; in the case of a bind mount, a regular file or socket may be needed.
676 (if (and (= MS_BIND (logand flags MS_BIND))
677 (not (file-is-directory? source)))
678 (unless (file-exists? mount-point)
679 (mkdir-p (dirname mount-point))
680 (call-with-output-file mount-point (const #t)))
681 (mkdir-p mount-point))
682
683 (cond
684 ((string-prefix? "nfs" type)
685 (mount-nfs source mount-point type flags options))
686 (else
687 (mount source mount-point type flags options)))
688
689 ;; For read-only bind mounts, an extra remount is needed, as per
690 ;; <http://lwn.net/Articles/281157/>, which still applies to Linux 4.0.
691 (when (and (= MS_BIND (logand flags MS_BIND))
692 (= MS_RDONLY (logand flags MS_RDONLY)))
693 (let ((flags (logior MS_BIND MS_REMOUNT MS_RDONLY)))
694 (mount source mount-point type flags #f)))))))
695
696 ;;; file-systems.scm ends here