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>
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 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
40 file-system-device->string
41 file-system-title ;deprecated
42 file-system-mount-point
44 file-system-needed-for-boot?
47 file-system-options->alist
48 alist->file-system-options
51 file-system-mount-may-fail?
53 file-system-create-mount-point?
54 file-system-dependencies
57 file-system-type-predicate
59 btrfs-store-subvolume-file-name
63 file-system-label->string
67 specification->file-system-mapping
69 %pseudo-file-system-types
70 %fuse-control-file-system
71 %binary-format-file-system
74 %shared-memory-file-system
75 %pseudo-terminal-file-system
82 %container-file-systems
87 file-system-mapping-source
88 file-system-mapping-target
89 file-system-mapping-writable?
91 file-system-mapping->bind-mount
94 %network-configuration-files
95 %network-file-mappings))
99 ;;; Declaring file systems to be mounted.
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.
106 ;; File system declaration.
107 (define-record-type* <file-system> %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
115 (options file-system-options ; string or #f
117 (mount? file-system-mount? ; Boolean
119 (mount-may-fail? file-system-mount-may-fail? ; Boolean
121 (needed-for-boot? %file-system-needed-for-boot? ; Boolean
123 (check? file-system-check? ; Boolean
125 (create-mount-point? file-system-create-mount-point? ; Boolean
127 (dependencies file-system-dependencies ; list of <file-system>
128 (default '())) ; or <mapped-device>
129 (location file-system-location
130 (default (current-source-location))
133 ;; A file system label for use in the 'device' field.
134 (define-record-type <file-system-label>
135 (file-system-label label)
137 (label file-system-label->string))
139 (set-record-type-printer! <file-system-label>
141 (format port "#<file-system-label ~s>"
142 (file-system-label->string obj))))
144 (define-syntax report-deprecation
146 "Report the use of the now-deprecated 'title' field."
149 (let* ((source (syntax-source #'field))
150 (file (and source (assq-ref source 'filename)))
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~%"
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)
170 ((label) (file-system-label dev))
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 _)
188 (report-deprecation (title titl))
189 (process-file-system-declaration (rest ...)
192 ((_ ((device dev) rest ...) (previous ...) _ titl)
193 (process-file-system-declaration (rest ...)
196 ((_ (field rest ...) (previous ...) dev titl)
197 (process-file-system-declaration (rest ...)
201 (define-syntax-rule (file-system fields ...)
202 (process-file-system-declaration (fields ...) () #f #f))
204 (define (file-system-title fs) ;deprecated
205 (match (file-system-device fs)
206 ((? file-system-label?) 'label)
208 ((? string?) 'device)))
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)
220 (module-variable store '%store-prefix)))
223 ((variable-ref variable))))
224 ((getenv "NIX_STORE")
230 (char-set-complement (char-set #\/)))
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:
236 (file-prefix? \"/gnu\" \"/gnu/store\")
239 (file-prefix? \"/gn\" \"/gnu/store\")
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)))
252 (and (string=? head1 head2) (loop tail1 tail2)))
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."
261 ((? file-system-label?)
262 (file-system-label->string device))
265 (uuid->string (uuid-bytevector device) uuid-type)
266 (uuid->string device)))
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."
274 (let ((options (string-split string #\,)))
276 (let ((=index (string-index param #\=)))
278 (cons (string-take param =index)
279 (string-drop param (1+ =index)))
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."
289 (string-join (map (match-lambda
291 (string-append key "=" value))
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))))))
304 (define (file-system->spec fs)
305 "Return a list corresponding to file-system FS that can be passed to the
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)))
316 mount-point type flags options mount-may-fail? check?))))
318 (define (spec->file-system sexp)
319 "Deserialize SEXP, a list, to the corresponding <file-system> object."
321 ((device mount-point type flags options mount-may-fail? check?
322 _ ...) ;placeholder for new fields
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))
331 (mount-point mount-point) (type type)
332 (flags flags) (options options)
333 (mount-may-fail? mount-may-fail?)
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 #\=)))
345 (source (substring spec 0 index))
346 (target (substring spec (+ 1 index)))
347 (writable? writable?))
351 (writable? writable?)))))
355 ;;; Common file systems.
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"))
364 (define %fuse-control-file-system
365 ;; Control file system for Linux' file systems in user-space (FUSE).
368 (mount-point "/sys/fs/fuse/connections")
372 (define %binary-format-file-system
373 ;; Support for arbitrary executable binary format.
375 (device "binfmt_misc")
376 (mount-point "/proc/sys/fs/binfmt_misc")
380 (define %debug-file-system
384 (mount-point "/sys/kernel/debug")
386 (create-mount-point? #t)))
388 (define %efivars-file-system
389 ;; Support for EFI variables file system.
392 (mount-point "/sys/firmware/efi/efivars")
395 (needed-for-boot? #f)
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.
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.)
409 (mount-point "/dev/pts")
412 (needed-for-boot? #f)
413 (create-mount-point? #t)
414 (options (string-append "gid=" (number->string %tty-gid) ",mode=620"))))
416 (define %shared-memory-file-system
420 (mount-point "/dev/shm")
423 (flags '(no-suid no-dev))
424 (options "size=50%") ;TODO: make size configurable
425 (create-mount-point? #t)))
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
432 (device (%store-prefix))
433 (mount-point (%store-prefix))
436 (flags '(read-only bind-mount no-atime))))
438 (define %control-groups
439 (let ((parent (file-system
441 (mount-point "/sys/fs/cgroup")
445 (map (lambda (subsystem)
448 (mount-point (string-append "/sys/fs/cgroup/" subsystem))
452 (create-mount-point? #t)
454 ;; This must be mounted after, and unmounted before the
456 (dependencies (list parent))))
457 '("cpuset" "cpu" "cpuacct" "memory" "devices" "freezer"
458 "blkio" "perf_event" "pids")))))
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.
466 (mount-point "/run/systemd")
469 (flags '(no-suid no-dev no-exec))
470 (options "mode=0755")
471 (create-mount-point? #t))
474 (mount-point "/run/user")
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").
485 (mount-point "/sys/fs/cgroup/elogind")
488 (options "none,name=elogind")
489 (create-mount-point? #t)
490 (dependencies (list (car %control-groups)))))
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
498 %shared-memory-file-system
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.
506 ;; The file system flags and options conform to the libcontainer
508 ;; https://github.com/docker/libcontainer/blob/master/SPEC.md#filesystem
509 (define %container-file-systems
511 ;; Pseudo-terminal file system.
514 (mount-point "/dev/pts")
516 (flags '(no-exec no-suid))
517 (needed-for-boot? #t)
518 (create-mount-point? #t)
520 (options "newinstance,ptmxmode=0666,mode=620"))
521 ;; Shared memory file system.
524 (mount-point "/dev/shm")
526 (flags '(no-exec no-suid no-dev))
527 (options "mode=1777,size=65536k")
528 (needed-for-boot? #t)
529 (create-mount-point? #t)
531 ;; Message queue file system.
534 (mount-point "/dev/mqueue")
536 (flags '(no-exec no-suid no-dev))
537 (needed-for-boot? #t)
538 (create-mount-point? #t)
543 ;;; Shared file systems, for VMs/containers.
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
550 (source file-system-mapping-source) ;string
551 (target file-system-mapping-target) ;string
552 (writable? file-system-mapping-writable? ;Boolean
555 (define (file-system-mapping->bind-mount mapping)
556 "Return a file system that realizes MAPPING, a <file-system-mapping>, using
559 (($ <file-system-mapping> source target writable?)
566 '(bind-mount read-only)))
568 (create-mount-point? #t)))))
570 (define %store-mapping
571 ;; Mapping of the host's store into the guest.
573 (source (%store-prefix))
574 (target (%store-prefix))
577 (define %network-configuration-files
578 ;; List of essential network configuration files.
584 (define %network-file-mappings
585 ;; List of file mappings for essential network files.
586 (filter-map (lambda (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)))
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."
603 (string=? (file-system-type fs) type)))
607 ;;; Btrfs specific helpers.
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
616 (file-system-options->alist
617 (file-system-options fs)))))
618 (find (cut string-prefix? "subvol" <>) option-keys)))
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."
624 (define (prepend-slash/maybe s)
625 (if (string=? "/" (string-take s 1))
627 (string-append "/" s)))
629 (define (file-name-depth file-name)
630 (length (string-tokenize file-name %not-slash)))
632 (and-let* ((btrfs-subvolume-fs (filter btrfs-subvolume? file-systems))
634 (sort btrfs-subvolume-fs
636 (> (file-name-depth (file-system-mount-point fs1))
637 (file-name-depth (file-system-mount-point fs2))))))
639 (find (lambda (fs) (file-prefix? (file-system-mount-point fs)
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)
649 (message "The store is on a Btrfs subvolume, but the \
650 subvolume name is unknown."))
653 (G_ "Use the @code{subvol} Btrfs file system option."))))))))
656 ;;; file-systems.scm ends here