gnu: Add rust-serial-test@0.4.0.
[jackhill/guix/guix.git] / gnu / system / file-systems.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
4 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@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 system file-systems)
22 #:use-module (ice-9 match)
23 #:use-module (rnrs bytevectors)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-2)
26 #:use-module (srfi srfi-9)
27 #:use-module (srfi srfi-26)
28 #:use-module (srfi srfi-35)
29 #:use-module (srfi srfi-9 gnu)
30 #:use-module (guix records)
31 #:use-module ((guix diagnostics) #:select (&fix-hint))
32 #:use-module (guix i18n)
33 #:use-module (gnu system uuid)
34 #:re-export (uuid ;backward compatibility
35 string->uuid
36 uuid->string)
37 #:export (file-system
38 file-system?
39 file-system-device
40 file-system-device->string
41 file-system-title ;deprecated
42 file-system-mount-point
43 file-system-type
44 file-system-needed-for-boot?
45 file-system-flags
46 file-system-options
47 file-system-options->alist
48 alist->file-system-options
49
50 file-system-mount?
51 file-system-mount-may-fail?
52 file-system-check?
53 file-system-create-mount-point?
54 file-system-dependencies
55 file-system-location
56
57 file-system-type-predicate
58 btrfs-subvolume?
59 btrfs-store-subvolume-file-name
60
61 file-system-label
62 file-system-label?
63 file-system-label->string
64
65 file-system->spec
66 spec->file-system
67 specification->file-system-mapping
68
69 %pseudo-file-system-types
70 %fuse-control-file-system
71 %binary-format-file-system
72 %debug-file-system
73 %efivars-file-system
74 %shared-memory-file-system
75 %pseudo-terminal-file-system
76 %tty-gid
77 %immutable-store
78 %control-groups
79 %elogind-file-systems
80
81 %base-file-systems
82 %container-file-systems
83
84 <file-system-mapping>
85 file-system-mapping
86 file-system-mapping?
87 file-system-mapping-source
88 file-system-mapping-target
89 file-system-mapping-writable?
90
91 file-system-mapping->bind-mount
92
93 %store-mapping
94 %network-configuration-files
95 %network-file-mappings))
96
97 ;;; Commentary:
98 ;;;
99 ;;; Declaring file systems to be mounted.
100 ;;;
101 ;;; Note: this file system is used both in the Shepherd and on the "host
102 ;;; side", so it must not include (gnu packages …) modules.
103 ;;;
104 ;;; Code:
105
106 ;; File system declaration.
107 (define-record-type* <file-system> %file-system
108 make-file-system
109 file-system?
110 (device file-system-device) ; string | <uuid> | <file-system-label>
111 (mount-point file-system-mount-point) ; string
112 (type file-system-type) ; string
113 (flags file-system-flags ; list of symbols
114 (default '()))
115 (options file-system-options ; string or #f
116 (default #f))
117 (mount? file-system-mount? ; Boolean
118 (default #t))
119 (mount-may-fail? file-system-mount-may-fail? ; Boolean
120 (default #f))
121 (needed-for-boot? %file-system-needed-for-boot? ; Boolean
122 (default #f))
123 (check? file-system-check? ; Boolean
124 (default #t))
125 (create-mount-point? file-system-create-mount-point? ; Boolean
126 (default #f))
127 (dependencies file-system-dependencies ; list of <file-system>
128 (default '())) ; or <mapped-device>
129 (location file-system-location
130 (default (current-source-location))
131 (innate)))
132
133 ;; A file system label for use in the 'device' field.
134 (define-record-type <file-system-label>
135 (file-system-label label)
136 file-system-label?
137 (label file-system-label->string))
138
139 (set-record-type-printer! <file-system-label>
140 (lambda (obj port)
141 (format port "#<file-system-label ~s>"
142 (file-system-label->string obj))))
143
144 (define-syntax report-deprecation
145 (lambda (s)
146 "Report the use of the now-deprecated 'title' field."
147 (syntax-case s ()
148 ((_ field)
149 (let* ((source (syntax-source #'field))
150 (file (and source (assq-ref source 'filename)))
151 (line (and source
152 (and=> (assq-ref source 'line) 1+)))
153 (column (and source (assq-ref source 'column))))
154 (format (current-error-port)
155 "~a:~a:~a: warning: 'title' field is deprecated~%"
156 file line column)
157 #t)))))
158
159 ;; Helper for 'process-file-system-declaration'.
160 (define-syntax device-expression
161 (syntax-rules (quote label uuid device)
162 ((_ (quote label) dev)
163 (file-system-label dev))
164 ((_ (quote uuid) dev)
165 (if (uuid? dev) dev (uuid dev)))
166 ((_ (quote device) dev)
167 dev)
168 ((_ title dev)
169 (case title
170 ((label) (file-system-label dev))
171 ((uuid) (uuid dev))
172 (else dev)))))
173
174 ;; Helper to interpret the now-deprecated 'title' field. Detect forms like
175 ;; (title 'label), remove them, and adjust the 'device' field accordingly.
176 ;; TODO: Remove this once 'title' has been deprecated long enough.
177 (define-syntax process-file-system-declaration
178 (syntax-rules (device title)
179 ((_ () (rest ...) #f #f) ;no 'title' and no 'device' field
180 (%file-system rest ...))
181 ((_ () (rest ...) dev #f) ;no 'title' field
182 (%file-system rest ... (device dev)))
183 ((_ () (rest ...) dev titl) ;got a 'title' field
184 (%file-system rest ...
185 (device (device-expression titl dev))))
186 ((_ ((title titl) rest ...) (previous ...) dev _)
187 (begin
188 (report-deprecation (title titl))
189 (process-file-system-declaration (rest ...)
190 (previous ...)
191 dev titl)))
192 ((_ ((device dev) rest ...) (previous ...) _ titl)
193 (process-file-system-declaration (rest ...)
194 (previous ...)
195 dev titl))
196 ((_ (field rest ...) (previous ...) dev titl)
197 (process-file-system-declaration (rest ...)
198 (previous ... field)
199 dev titl))))
200
201 (define-syntax-rule (file-system fields ...)
202 (process-file-system-declaration (fields ...) () #f #f))
203
204 (define (file-system-title fs) ;deprecated
205 (match (file-system-device fs)
206 ((? file-system-label?) 'label)
207 ((? uuid?) 'uuid)
208 ((? string?) 'device)))
209
210 ;; Note: This module is used both on the build side and on the host side.
211 ;; Arrange not to pull (guix store) and (guix config) because the latter
212 ;; differs from user to user.
213 (define (%store-prefix)
214 "Return the store prefix."
215 ;; Note: If we have (guix store database) in the search path and we do *not*
216 ;; have (guix store) proper, 'resolve-module' returns an empty (guix store)
217 ;; with one sub-module.
218 (cond ((and=> (resolve-module '(guix store) #:ensure #f)
219 (lambda (store)
220 (module-variable store '%store-prefix)))
221 =>
222 (lambda (variable)
223 ((variable-ref variable))))
224 ((getenv "NIX_STORE")
225 => identity)
226 (else
227 "/gnu/store")))
228
229 (define %not-slash
230 (char-set-complement (char-set #\/)))
231
232 (define (file-prefix? file1 file2)
233 "Return #t if FILE1 denotes the name of a file that is a parent of FILE2,
234 where both FILE1 and FILE2 are absolute file name. For example:
235
236 (file-prefix? \"/gnu\" \"/gnu/store\")
237 => #t
238
239 (file-prefix? \"/gn\" \"/gnu/store\")
240 => #f
241 "
242 (and (string-prefix? "/" file1)
243 (string-prefix? "/" file2)
244 (let loop ((file1 (string-tokenize file1 %not-slash))
245 (file2 (string-tokenize file2 %not-slash)))
246 (match file1
247 (()
248 #t)
249 ((head1 tail1 ...)
250 (match file2
251 ((head2 tail2 ...)
252 (and (string=? head1 head2) (loop tail1 tail2)))
253 (()
254 #f)))))))
255
256 (define* (file-system-device->string device #:key uuid-type)
257 "Return the string representations of the DEVICE field of a <file-system>
258 record. When the device is a UUID, its representation is chosen depending on
259 UUID-TYPE, a symbol such as 'dce or 'iso9660."
260 (match device
261 ((? file-system-label?)
262 (file-system-label->string device))
263 ((? uuid?)
264 (if uuid-type
265 (uuid->string (uuid-bytevector device) uuid-type)
266 (uuid->string device)))
267 ((? string?)
268 device)))
269
270 (define (file-system-options->alist string)
271 "Translate the option string format of a <file-system> record into an
272 association list of options or option/value pairs."
273 (if string
274 (let ((options (string-split string #\,)))
275 (map (lambda (param)
276 (let ((=index (string-index param #\=)))
277 (if =index
278 (cons (string-take param =index)
279 (string-drop param (1+ =index)))
280 param)))
281 options))
282 '()))
283
284 (define (alist->file-system-options options)
285 "Return the string representation of OPTIONS, an association list. The
286 string obtained can be used as the option field of a <file-system> record."
287 (if (null? options)
288 #f
289 (string-join (map (match-lambda
290 ((key . value)
291 (string-append key "=" value))
292 (key
293 key))
294 options)
295 ",")))
296
297 (define (file-system-needed-for-boot? fs)
298 "Return true if FS has the 'needed-for-boot?' flag set, or if it holds the
299 store--e.g., if FS is the root file system."
300 (or (%file-system-needed-for-boot? fs)
301 (and (file-prefix? (file-system-mount-point fs) (%store-prefix))
302 (not (memq 'bind-mount (file-system-flags fs))))))
303
304 (define (file-system->spec fs)
305 "Return a list corresponding to file-system FS that can be passed to the
306 initrd code."
307 (match fs
308 (($ <file-system> device mount-point type flags options mount?
309 mount-may-fail? needed-for-boot? check?)
310 ;; Note: Add new fields towards the end for compatibility.
311 (list (cond ((uuid? device)
312 `(uuid ,(uuid-type device) ,(uuid-bytevector device)))
313 ((file-system-label? device)
314 `(file-system-label ,(file-system-label->string device)))
315 (else device))
316 mount-point type flags options mount-may-fail? check?))))
317
318 (define (spec->file-system sexp)
319 "Deserialize SEXP, a list, to the corresponding <file-system> object."
320 (match sexp
321 ((device mount-point type flags options mount-may-fail? check?
322 _ ...) ;placeholder for new fields
323 (file-system
324 (device (match device
325 (('uuid (? symbol? type) (? bytevector? bv))
326 (bytevector->uuid bv type))
327 (('file-system-label (? string? label))
328 (file-system-label label))
329 (_
330 device)))
331 (mount-point mount-point) (type type)
332 (flags flags) (options options)
333 (mount-may-fail? mount-may-fail?)
334 (check? check?)))))
335
336 (define (specification->file-system-mapping spec writable?)
337 "Read the SPEC and return the corresponding <file-system-mapping>. SPEC is
338 a string of the form \"SOURCE\" or \"SOURCE=TARGET\". The former specifies
339 that SOURCE from the host should be mounted at SOURCE in the other system.
340 The latter format specifies that SOURCE from the host should be mounted at
341 TARGET in the other system."
342 (let ((index (string-index spec #\=)))
343 (if index
344 (file-system-mapping
345 (source (substring spec 0 index))
346 (target (substring spec (+ 1 index)))
347 (writable? writable?))
348 (file-system-mapping
349 (source spec)
350 (target spec)
351 (writable? writable?)))))
352
353 \f
354 ;;;
355 ;;; Common file systems.
356 ;;;
357
358 (define %pseudo-file-system-types
359 ;; List of know pseudo file system types. This is used when validating file
360 ;; system definitions.
361 '("binfmt_misc" "cgroup" "debugfs" "devpts" "devtmpfs" "efivarfs" "fusectl"
362 "hugetlbfs" "overlay" "proc" "securityfs" "sysfs" "tmpfs"))
363
364 (define %fuse-control-file-system
365 ;; Control file system for Linux' file systems in user-space (FUSE).
366 (file-system
367 (device "fusectl")
368 (mount-point "/sys/fs/fuse/connections")
369 (type "fusectl")
370 (check? #f)))
371
372 (define %binary-format-file-system
373 ;; Support for arbitrary executable binary format.
374 (file-system
375 (device "binfmt_misc")
376 (mount-point "/proc/sys/fs/binfmt_misc")
377 (type "binfmt_misc")
378 (check? #f)))
379
380 (define %debug-file-system
381 (file-system
382 (type "debugfs")
383 (device "none")
384 (mount-point "/sys/kernel/debug")
385 (check? #f)
386 (create-mount-point? #t)))
387
388 (define %efivars-file-system
389 ;; Support for EFI variables file system.
390 (file-system
391 (device "efivarfs")
392 (mount-point "/sys/firmware/efi/efivars")
393 (type "efivarfs")
394 (mount-may-fail? #t)
395 (needed-for-boot? #f)
396 (check? #f)))
397
398 (define %tty-gid
399 ;; ID of the 'tty' group. Allocate it statically to make it easy to refer
400 ;; to it from here and from the 'tty' group definitions.
401 996)
402
403 (define %pseudo-terminal-file-system
404 ;; The pseudo-terminal file system. It needs to be mounted so that
405 ;; statfs(2) returns DEVPTS_SUPER_MAGIC like libc's getpt(3) expects (and
406 ;; thus openpty(3) and its users, such as xterm.)
407 (file-system
408 (device "none")
409 (mount-point "/dev/pts")
410 (type "devpts")
411 (check? #f)
412 (needed-for-boot? #f)
413 (create-mount-point? #t)
414 (options (string-append "gid=" (number->string %tty-gid) ",mode=620"))))
415
416 (define %shared-memory-file-system
417 ;; Shared memory.
418 (file-system
419 (device "tmpfs")
420 (mount-point "/dev/shm")
421 (type "tmpfs")
422 (check? #f)
423 (flags '(no-suid no-dev))
424 (options "size=50%") ;TODO: make size configurable
425 (create-mount-point? #t)))
426
427 (define %immutable-store
428 ;; Read-only store to avoid users or daemons accidentally modifying it.
429 ;; 'guix-daemon' has provisions to remount it read-write in its own name
430 ;; space.
431 (file-system
432 (device (%store-prefix))
433 (mount-point (%store-prefix))
434 (type "none")
435 (check? #f)
436 (flags '(read-only bind-mount no-atime))))
437
438 (define %control-groups
439 (let ((parent (file-system
440 (device "cgroup")
441 (mount-point "/sys/fs/cgroup")
442 (type "tmpfs")
443 (check? #f))))
444 (cons parent
445 (map (lambda (subsystem)
446 (file-system
447 (device "cgroup")
448 (mount-point (string-append "/sys/fs/cgroup/" subsystem))
449 (type "cgroup")
450 (check? #f)
451 (options subsystem)
452 (create-mount-point? #t)
453
454 ;; This must be mounted after, and unmounted before the
455 ;; parent directory.
456 (dependencies (list parent))))
457 '("cpuset" "cpu" "cpuacct" "memory" "devices" "freezer"
458 "blkio" "perf_event" "pids")))))
459
460 (define %elogind-file-systems
461 ;; We don't use systemd, but these file systems are needed for elogind,
462 ;; which was extracted from systemd.
463 (append
464 (list (file-system
465 (device "none")
466 (mount-point "/run/systemd")
467 (type "tmpfs")
468 (check? #f)
469 (flags '(no-suid no-dev no-exec))
470 (options "mode=0755")
471 (create-mount-point? #t))
472 (file-system
473 (device "none")
474 (mount-point "/run/user")
475 (type "tmpfs")
476 (check? #f)
477 (flags '(no-suid no-dev no-exec))
478 (options "mode=0755")
479 (create-mount-point? #t))
480 ;; Elogind uses cgroups to organize processes, allowing it to map PIDs
481 ;; to sessions. Elogind's cgroup hierarchy isn't associated with any
482 ;; resource controller ("subsystem").
483 (file-system
484 (device "cgroup")
485 (mount-point "/sys/fs/cgroup/elogind")
486 (type "cgroup")
487 (check? #f)
488 (options "none,name=elogind")
489 (create-mount-point? #t)
490 (dependencies (list (car %control-groups)))))
491 %control-groups))
492
493 (define %base-file-systems
494 ;; List of basic file systems to be mounted. Note that /proc and /sys are
495 ;; currently mounted by the initrd.
496 (list %pseudo-terminal-file-system
497 %debug-file-system
498 %shared-memory-file-system
499 %efivars-file-system
500 %immutable-store))
501
502 ;; File systems for Linux containers differ from %base-file-systems in that
503 ;; they impose additional restrictions such as no-exec or need different
504 ;; options to function properly.
505 ;;
506 ;; The file system flags and options conform to the libcontainer
507 ;; specification:
508 ;; https://github.com/docker/libcontainer/blob/master/SPEC.md#filesystem
509 (define %container-file-systems
510 (list
511 ;; Pseudo-terminal file system.
512 (file-system
513 (device "none")
514 (mount-point "/dev/pts")
515 (type "devpts")
516 (flags '(no-exec no-suid))
517 (needed-for-boot? #t)
518 (create-mount-point? #t)
519 (check? #f)
520 (options "newinstance,ptmxmode=0666,mode=620"))
521 ;; Shared memory file system.
522 (file-system
523 (device "tmpfs")
524 (mount-point "/dev/shm")
525 (type "tmpfs")
526 (flags '(no-exec no-suid no-dev))
527 (options "mode=1777,size=65536k")
528 (needed-for-boot? #t)
529 (create-mount-point? #t)
530 (check? #f))
531 ;; Message queue file system.
532 (file-system
533 (device "mqueue")
534 (mount-point "/dev/mqueue")
535 (type "mqueue")
536 (flags '(no-exec no-suid no-dev))
537 (needed-for-boot? #t)
538 (create-mount-point? #t)
539 (check? #f))))
540
541 \f
542 ;;;
543 ;;; Shared file systems, for VMs/containers.
544 ;;;
545
546 ;; Mapping of host file system SOURCE to mount point TARGET in the guest.
547 (define-record-type* <file-system-mapping> file-system-mapping
548 make-file-system-mapping
549 file-system-mapping?
550 (source file-system-mapping-source) ;string
551 (target file-system-mapping-target) ;string
552 (writable? file-system-mapping-writable? ;Boolean
553 (default #f)))
554
555 (define (file-system-mapping->bind-mount mapping)
556 "Return a file system that realizes MAPPING, a <file-system-mapping>, using
557 a bind mount."
558 (match mapping
559 (($ <file-system-mapping> source target writable?)
560 (file-system
561 (mount-point target)
562 (device source)
563 (type "none")
564 (flags (if writable?
565 '(bind-mount)
566 '(bind-mount read-only)))
567 (check? #f)
568 (create-mount-point? #t)))))
569
570 (define %store-mapping
571 ;; Mapping of the host's store into the guest.
572 (file-system-mapping
573 (source (%store-prefix))
574 (target (%store-prefix))
575 (writable? #f)))
576
577 (define %network-configuration-files
578 ;; List of essential network configuration files.
579 '("/etc/resolv.conf"
580 "/etc/nsswitch.conf"
581 "/etc/services"
582 "/etc/hosts"))
583
584 (define %network-file-mappings
585 ;; List of file mappings for essential network files.
586 (filter-map (lambda (file)
587 (file-system-mapping
588 (source file)
589 (target file)
590 ;; XXX: On some GNU/Linux systems, /etc/resolv.conf is a
591 ;; symlink to a file in a tmpfs which, for an unknown reason,
592 ;; cannot be bind mounted read-only within the container.
593 ;; The same goes with /var/run/nscd, as discussed in
594 ;; <https://bugs.gnu.org/37967>.
595 (writable? (or (string=? file "/etc/resolv.conf")
596 (string=? file "/var/run/nscd")))))
597 (cons "/var/run/nscd" %network-configuration-files)))
598
599 (define (file-system-type-predicate type)
600 "Return a predicate that, when passed a file system, returns #t if that file
601 system has the given TYPE."
602 (lambda (fs)
603 (string=? (file-system-type fs) type)))
604
605 \f
606 ;;;
607 ;;; Btrfs specific helpers.
608 ;;;
609
610 (define (btrfs-subvolume? fs)
611 "Predicate to check if FS, a file-system object, is a Btrfs subvolume."
612 (and-let* ((btrfs-file-system? (string= "btrfs" (file-system-type fs)))
613 (option-keys (map (match-lambda
614 ((key . value) key)
615 (key key))
616 (file-system-options->alist
617 (file-system-options fs)))))
618 (find (cut string-prefix? "subvol" <>) option-keys)))
619
620 (define (btrfs-store-subvolume-file-name file-systems)
621 "Return the subvolume file name within the Btrfs top level onto which the
622 store is located, else #f."
623
624 (define (prepend-slash/maybe s)
625 (if (string=? "/" (string-take s 1))
626 s
627 (string-append "/" s)))
628
629 (define (file-name-depth file-name)
630 (length (string-tokenize file-name %not-slash)))
631
632 (and-let* ((btrfs-subvolume-fs (filter btrfs-subvolume? file-systems))
633 (btrfs-subvolume-fs*
634 (sort btrfs-subvolume-fs
635 (lambda (fs1 fs2)
636 (> (file-name-depth (file-system-mount-point fs1))
637 (file-name-depth (file-system-mount-point fs2))))))
638 (store-subvolume-fs
639 (find (lambda (fs) (file-prefix? (file-system-mount-point fs)
640 (%store-prefix)))
641 btrfs-subvolume-fs*))
642 (options (file-system-options->alist
643 (file-system-options store-subvolume-fs))))
644 ;; XXX: Deriving the subvolume name based from a subvolume ID is not
645 ;; supported, as we'd need to query the actual file system.
646 (or (and=> (assoc-ref options "subvol") prepend-slash/maybe)
647 (raise (condition
648 (&message
649 (message "The store is on a Btrfs subvolume, but the \
650 subvolume name is unknown."))
651 (&fix-hint
652 (hint
653 (G_ "Use the @code{subvol} Btrfs file system option."))))))))
654
655
656 ;;; file-systems.scm ends here