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