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 Google LLC
4 ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
5 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
7 ;;; This file is part of GNU Guix.
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22 (define-module (gnu system file-systems)
23 #:use-module (ice-9 match)
24 #:use-module (rnrs bytevectors)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-2)
27 #:use-module (srfi srfi-9)
28 #:use-module (srfi srfi-26)
29 #:use-module (srfi srfi-35)
30 #:use-module (srfi srfi-9 gnu)
31 #:use-module (guix records)
32 #:use-module ((guix diagnostics) #:select (&fix-hint))
33 #:use-module (guix i18n)
34 #:use-module (gnu system uuid)
35 #:re-export (uuid ;backward compatibility
41 file-system-device->string
42 file-system-title ;deprecated
43 file-system-mount-point
45 file-system-needed-for-boot?
48 file-system-options->alist
49 alist->file-system-options
52 file-system-mount-may-fail?
54 file-system-create-mount-point?
55 file-system-dependencies
58 file-system-type-predicate
60 btrfs-store-subvolume-file-name
64 file-system-label->string
68 specification->file-system-mapping
70 %pseudo-file-system-types
71 %fuse-control-file-system
72 %binary-format-file-system
75 %shared-memory-file-system
76 %pseudo-terminal-file-system
83 %container-file-systems
88 file-system-mapping-source
89 file-system-mapping-target
90 file-system-mapping-writable?
92 file-system-mapping->bind-mount
95 %network-configuration-files
96 %network-file-mappings))
100 ;;; Declaring file systems to be mounted.
102 ;;; Note: this file system is used both in the Shepherd and on the "host
103 ;;; side", so it must not include (gnu packages …) modules.
107 ;; File system declaration.
108 (define-record-type* <file-system> %file-system
111 (device file-system-device) ; string | <uuid> | <file-system-label>
112 (mount-point file-system-mount-point) ; string
113 (type file-system-type) ; string
114 (flags file-system-flags ; list of symbols
116 (options file-system-options ; string or #f
118 (mount? file-system-mount? ; Boolean
120 (mount-may-fail? file-system-mount-may-fail? ; Boolean
122 (needed-for-boot? %file-system-needed-for-boot? ; Boolean
124 (check? file-system-check? ; Boolean
126 (create-mount-point? file-system-create-mount-point? ; Boolean
128 (dependencies file-system-dependencies ; list of <file-system>
129 (default '())) ; or <mapped-device>
130 (location file-system-location
131 (default (current-source-location))
134 ;; A file system label for use in the 'device' field.
135 (define-record-type <file-system-label>
136 (file-system-label label)
138 (label file-system-label->string))
140 (set-record-type-printer! <file-system-label>
142 (format port "#<file-system-label ~s>"
143 (file-system-label->string obj))))
145 (define-syntax report-deprecation
147 "Report the use of the now-deprecated 'title' field."
150 (let* ((source (syntax-source #'field))
151 (file (and source (assq-ref source 'filename)))
153 (and=> (assq-ref source 'line) 1+)))
154 (column (and source (assq-ref source 'column))))
155 (format (current-error-port)
156 "~a:~a:~a: warning: 'title' field is deprecated~%"
160 ;; Helper for 'process-file-system-declaration'.
161 (define-syntax device-expression
162 (syntax-rules (quote label uuid device)
163 ((_ (quote label) dev)
164 (file-system-label dev))
165 ((_ (quote uuid) dev)
166 (if (uuid? dev) dev (uuid dev)))
167 ((_ (quote device) dev)
171 ((label) (file-system-label dev))
175 ;; Helper to interpret the now-deprecated 'title' field. Detect forms like
176 ;; (title 'label), remove them, and adjust the 'device' field accordingly.
177 ;; TODO: Remove this once 'title' has been deprecated long enough.
178 (define-syntax process-file-system-declaration
179 (syntax-rules (device title)
180 ((_ () (rest ...) #f #f) ;no 'title' and no 'device' field
181 (%file-system rest ...))
182 ((_ () (rest ...) dev #f) ;no 'title' field
183 (%file-system rest ... (device dev)))
184 ((_ () (rest ...) dev titl) ;got a 'title' field
185 (%file-system rest ...
186 (device (device-expression titl dev))))
187 ((_ ((title titl) rest ...) (previous ...) dev _)
189 (report-deprecation (title titl))
190 (process-file-system-declaration (rest ...)
193 ((_ ((device dev) rest ...) (previous ...) _ titl)
194 (process-file-system-declaration (rest ...)
197 ((_ (field rest ...) (previous ...) dev titl)
198 (process-file-system-declaration (rest ...)
202 (define-syntax-rule (file-system fields ...)
203 (process-file-system-declaration (fields ...) () #f #f))
205 (define (file-system-title fs) ;deprecated
206 (match (file-system-device fs)
207 ((? file-system-label?) 'label)
209 ((? string?) 'device)))
211 ;; Note: This module is used both on the build side and on the host side.
212 ;; Arrange not to pull (guix store) and (guix config) because the latter
213 ;; differs from user to user.
214 (define (%store-prefix)
215 "Return the store prefix."
216 ;; Note: If we have (guix store database) in the search path and we do *not*
217 ;; have (guix store) proper, 'resolve-module' returns an empty (guix store)
218 ;; with one sub-module.
219 (cond ((and=> (resolve-module '(guix store) #:ensure #f)
221 (module-variable store '%store-prefix)))
224 ((variable-ref variable))))
225 ((getenv "NIX_STORE")
231 (char-set-complement (char-set #\/)))
233 (define (file-prefix? file1 file2)
234 "Return #t if FILE1 denotes the name of a file that is a parent of FILE2,
235 where both FILE1 and FILE2 are absolute file name. For example:
237 (file-prefix? \"/gnu\" \"/gnu/store\")
240 (file-prefix? \"/gn\" \"/gnu/store\")
243 (and (string-prefix? "/" file1)
244 (string-prefix? "/" file2)
245 (let loop ((file1 (string-tokenize file1 %not-slash))
246 (file2 (string-tokenize file2 %not-slash)))
253 (and (string=? head1 head2) (loop tail1 tail2)))
257 (define* (file-system-device->string device #:key uuid-type)
258 "Return the string representations of the DEVICE field of a <file-system>
259 record. When the device is a UUID, its representation is chosen depending on
260 UUID-TYPE, a symbol such as 'dce or 'iso9660."
262 ((? file-system-label?)
263 (file-system-label->string device))
266 (uuid->string (uuid-bytevector device) uuid-type)
267 (uuid->string device)))
271 (define (file-system-options->alist string)
272 "Translate the option string format of a <file-system> record into an
273 association list of options or option/value pairs."
275 (let ((options (string-split string #\,)))
277 (let ((=index (string-index param #\=)))
279 (cons (string-take param =index)
280 (string-drop param (1+ =index)))
285 (define (alist->file-system-options options)
286 "Return the string representation of OPTIONS, an association list. The
287 string obtained can be used as the option field of a <file-system> record."
290 (string-join (map (match-lambda
292 (string-append key "=" value))
298 (define (file-system-needed-for-boot? fs)
299 "Return true if FS has the 'needed-for-boot?' flag set, or if it holds the
300 store--e.g., if FS is the root file system."
301 (or (%file-system-needed-for-boot? fs)
302 (and (file-prefix? (file-system-mount-point fs) (%store-prefix))
303 (not (memq 'bind-mount (file-system-flags fs))))))
305 (define (file-system->spec fs)
306 "Return a list corresponding to file-system FS that can be passed to the
309 (($ <file-system> device mount-point type flags options mount?
310 mount-may-fail? needed-for-boot? check?)
311 ;; Note: Add new fields towards the end for compatibility.
312 (list (cond ((uuid? device)
313 `(uuid ,(uuid-type device) ,(uuid-bytevector device)))
314 ((file-system-label? device)
315 `(file-system-label ,(file-system-label->string device)))
317 mount-point type flags options mount-may-fail? check?))))
319 (define (spec->file-system sexp)
320 "Deserialize SEXP, a list, to the corresponding <file-system> object."
322 ((device mount-point type flags options mount-may-fail? check?
323 _ ...) ;placeholder for new fields
325 (device (match device
326 (('uuid (? symbol? type) (? bytevector? bv))
327 (bytevector->uuid bv type))
328 (('file-system-label (? string? label))
329 (file-system-label label))
332 (mount-point mount-point) (type type)
333 (flags flags) (options options)
334 (mount-may-fail? mount-may-fail?)
337 (define (specification->file-system-mapping spec writable?)
338 "Read the SPEC and return the corresponding <file-system-mapping>. SPEC is
339 a string of the form \"SOURCE\" or \"SOURCE=TARGET\". The former specifies
340 that SOURCE from the host should be mounted at SOURCE in the other system.
341 The latter format specifies that SOURCE from the host should be mounted at
342 TARGET in the other system."
343 (let ((index (string-index spec #\=)))
346 (source (substring spec 0 index))
347 (target (substring spec (+ 1 index)))
348 (writable? writable?))
352 (writable? writable?)))))
356 ;;; Common file systems.
359 (define %pseudo-file-system-types
360 ;; List of know pseudo file system types. This is used when validating file
361 ;; system definitions.
362 '("binfmt_misc" "cgroup" "debugfs" "devpts" "devtmpfs" "efivarfs" "fusectl"
363 "hugetlbfs" "overlay" "proc" "securityfs" "sysfs" "tmpfs"))
365 (define %fuse-control-file-system
366 ;; Control file system for Linux' file systems in user-space (FUSE).
369 (mount-point "/sys/fs/fuse/connections")
373 (define %binary-format-file-system
374 ;; Support for arbitrary executable binary format.
376 (device "binfmt_misc")
377 (mount-point "/proc/sys/fs/binfmt_misc")
381 (define %debug-file-system
385 (mount-point "/sys/kernel/debug")
387 (create-mount-point? #t)))
389 (define %efivars-file-system
390 ;; Support for EFI variables file system.
393 (mount-point "/sys/firmware/efi/efivars")
396 (needed-for-boot? #f)
400 ;; ID of the 'tty' group. Allocate it statically to make it easy to refer
401 ;; to it from here and from the 'tty' group definitions.
404 (define %pseudo-terminal-file-system
405 ;; The pseudo-terminal file system. It needs to be mounted so that
406 ;; statfs(2) returns DEVPTS_SUPER_MAGIC like libc's getpt(3) expects (and
407 ;; thus openpty(3) and its users, such as xterm.)
410 (mount-point "/dev/pts")
413 (needed-for-boot? #f)
414 (create-mount-point? #t)
415 (options (string-append "gid=" (number->string %tty-gid) ",mode=620"))))
417 (define %shared-memory-file-system
421 (mount-point "/dev/shm")
424 (flags '(no-suid no-dev))
425 (options "size=50%") ;TODO: make size configurable
426 (create-mount-point? #t)))
428 (define %immutable-store
429 ;; Read-only store to avoid users or daemons accidentally modifying it.
430 ;; 'guix-daemon' has provisions to remount it read-write in its own name
433 (device (%store-prefix))
434 (mount-point (%store-prefix))
437 (flags '(read-only bind-mount no-atime))))
439 (define %control-groups
440 (let ((parent (file-system
442 (mount-point "/sys/fs/cgroup")
446 (map (lambda (subsystem)
449 (mount-point (string-append "/sys/fs/cgroup/" subsystem))
453 (create-mount-point? #t)
455 ;; This must be mounted after, and unmounted before the
457 (dependencies (list parent))))
458 '("cpuset" "cpu" "cpuacct" "memory" "devices" "freezer"
459 "blkio" "perf_event" "pids")))))
461 (define %elogind-file-systems
462 ;; We don't use systemd, but these file systems are needed for elogind,
463 ;; which was extracted from systemd.
467 (mount-point "/run/systemd")
470 (flags '(no-suid no-dev no-exec))
471 (options "mode=0755")
472 (create-mount-point? #t))
475 (mount-point "/run/user")
478 (flags '(no-suid no-dev no-exec))
479 (options "mode=0755")
480 (create-mount-point? #t))
481 ;; Elogind uses cgroups to organize processes, allowing it to map PIDs
482 ;; to sessions. Elogind's cgroup hierarchy isn't associated with any
483 ;; resource controller ("subsystem").
486 (mount-point "/sys/fs/cgroup/elogind")
489 (options "none,name=elogind")
490 (create-mount-point? #t)
491 (dependencies (list (car %control-groups)))))
494 (define %base-file-systems
495 ;; List of basic file systems to be mounted. Note that /proc and /sys are
496 ;; currently mounted by the initrd.
497 (list %pseudo-terminal-file-system
499 %shared-memory-file-system
503 ;; File systems for Linux containers differ from %base-file-systems in that
504 ;; they impose additional restrictions such as no-exec or need different
505 ;; options to function properly.
507 ;; The file system flags and options conform to the libcontainer
509 ;; https://github.com/docker/libcontainer/blob/master/SPEC.md#filesystem
510 (define %container-file-systems
512 ;; Pseudo-terminal file system.
515 (mount-point "/dev/pts")
517 (flags '(no-exec no-suid))
518 (needed-for-boot? #t)
519 (create-mount-point? #t)
521 (options "newinstance,ptmxmode=0666,mode=620"))
522 ;; Shared memory file system.
525 (mount-point "/dev/shm")
527 (flags '(no-exec no-suid no-dev))
528 (options "mode=1777,size=65536k")
529 (needed-for-boot? #t)
530 (create-mount-point? #t)
532 ;; Message queue file system.
535 (mount-point "/dev/mqueue")
537 (flags '(no-exec no-suid no-dev))
538 (needed-for-boot? #t)
539 (create-mount-point? #t)
544 ;;; Shared file systems, for VMs/containers.
547 ;; Mapping of host file system SOURCE to mount point TARGET in the guest.
548 (define-record-type* <file-system-mapping> file-system-mapping
549 make-file-system-mapping
551 (source file-system-mapping-source) ;string
552 (target file-system-mapping-target) ;string
553 (writable? file-system-mapping-writable? ;Boolean
556 (define (file-system-mapping->bind-mount mapping)
557 "Return a file system that realizes MAPPING, a <file-system-mapping>, using
560 (($ <file-system-mapping> source target writable?)
567 '(bind-mount read-only)))
569 (create-mount-point? #t)))))
571 (define %store-mapping
572 ;; Mapping of the host's store into the guest.
574 (source (%store-prefix))
575 (target (%store-prefix))
578 (define %network-configuration-files
579 ;; List of essential network configuration files.
585 (define %network-file-mappings
586 ;; List of file mappings for essential network files.
587 (filter-map (lambda (file)
591 ;; XXX: On some GNU/Linux systems, /etc/resolv.conf is a
592 ;; symlink to a file in a tmpfs which, for an unknown reason,
593 ;; cannot be bind mounted read-only within the container.
594 (writable? (string=? file "/etc/resolv.conf"))))
595 %network-configuration-files))
597 (define (file-system-type-predicate type)
598 "Return a predicate that, when passed a file system, returns #t if that file
599 system has the given TYPE."
601 (string=? (file-system-type fs) type)))
605 ;;; Btrfs specific helpers.
608 (define (btrfs-subvolume? fs)
609 "Predicate to check if FS, a file-system object, is a Btrfs subvolume."
610 (and-let* ((btrfs-file-system? (string= "btrfs" (file-system-type fs)))
611 (option-keys (map (match-lambda
614 (file-system-options->alist
615 (file-system-options fs)))))
616 (find (cut string-prefix? "subvol" <>) option-keys)))
618 (define (btrfs-store-subvolume-file-name file-systems)
619 "Return the subvolume file name within the Btrfs top level onto which the
620 store is located, else #f."
622 (define (prepend-slash/maybe s)
623 (if (string=? "/" (string-take s 1))
625 (string-append "/" s)))
627 (define (file-name-depth file-name)
628 (length (string-tokenize file-name %not-slash)))
630 (and-let* ((btrfs-subvolume-fs (filter btrfs-subvolume? file-systems))
632 (sort btrfs-subvolume-fs
634 (> (file-name-depth (file-system-mount-point fs1))
635 (file-name-depth (file-system-mount-point fs2))))))
637 (find (lambda (fs) (file-prefix? (file-system-mount-point fs)
639 btrfs-subvolume-fs*))
640 (options (file-system-options->alist
641 (file-system-options store-subvolume-fs))))
642 ;; XXX: Deriving the subvolume name based from a subvolume ID is not
643 ;; supported, as we'd need to query the actual file system.
644 (or (and=> (assoc-ref options "subvol") prepend-slash/maybe)
647 (message "The store is on a Btrfs subvolume, but the \
648 subvolume name is unknown."))
651 (G_ "Use the @code{subvol} Btrfs file system option."))))))))
654 ;;; file-systems.scm ends here