1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu system file-systems)
21 #:use-module (ice-9 match)
22 #:use-module (rnrs bytevectors)
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-9)
25 #:use-module (srfi srfi-9 gnu)
26 #:use-module (guix records)
27 #:use-module (gnu system uuid)
28 #:re-export (uuid ;backward compatibility
34 file-system-device->string
35 file-system-title ;deprecated
36 file-system-mount-point
38 file-system-needed-for-boot?
43 file-system-create-mount-point?
44 file-system-dependencies
47 file-system-type-predicate
51 file-system-label->string
55 specification->file-system-mapping
57 %pseudo-file-system-types
58 %fuse-control-file-system
59 %binary-format-file-system
60 %shared-memory-file-system
61 %pseudo-terminal-file-system
68 %container-file-systems
73 file-system-mapping-source
74 file-system-mapping-target
75 file-system-mapping-writable?
77 file-system-mapping->bind-mount
80 %network-configuration-files
81 %network-file-mappings))
85 ;;; Declaring file systems to be mounted.
87 ;;; Note: this file system is used both in the Shepherd and on the "host
88 ;;; side", so it must not include (gnu packages …) modules.
92 ;; File system declaration.
93 (define-record-type* <file-system> %file-system
96 (device file-system-device) ; string | <uuid> | <file-system-label>
97 (mount-point file-system-mount-point) ; string
98 (type file-system-type) ; string
99 (flags file-system-flags ; list of symbols
101 (options file-system-options ; string or #f
103 (mount? file-system-mount? ; Boolean
105 (needed-for-boot? %file-system-needed-for-boot? ; Boolean
107 (check? file-system-check? ; Boolean
109 (create-mount-point? file-system-create-mount-point? ; Boolean
111 (dependencies file-system-dependencies ; list of <file-system>
112 (default '())) ; or <mapped-device>
113 (location file-system-location
114 (default (current-source-location))
117 ;; A file system label for use in the 'device' field.
118 (define-record-type <file-system-label>
119 (file-system-label label)
121 (label file-system-label->string))
123 (set-record-type-printer! <file-system-label>
125 (format port "#<file-system-label ~s>"
126 (file-system-label->string obj))))
128 (define-syntax report-deprecation
130 "Report the use of the now-deprecated 'title' field."
133 (let* ((source (syntax-source #'field))
134 (file (and source (assq-ref source 'filename)))
136 (and=> (assq-ref source 'line) 1+)))
137 (column (and source (assq-ref source 'column))))
138 (format (current-error-port)
139 "~a:~a:~a: warning: 'title' field is deprecated~%"
143 ;; Helper for 'process-file-system-declaration'.
144 (define-syntax device-expression
145 (syntax-rules (quote label uuid device)
146 ((_ (quote label) dev)
147 (file-system-label dev))
148 ((_ (quote uuid) dev)
149 (if (uuid? dev) dev (uuid dev)))
150 ((_ (quote device) dev)
154 ((label) (file-system-label dev))
158 ;; Helper to interpret the now-deprecated 'title' field. Detect forms like
159 ;; (title 'label), remove them, and adjust the 'device' field accordingly.
160 ;; TODO: Remove this once 'title' has been deprecated long enough.
161 (define-syntax process-file-system-declaration
162 (syntax-rules (device title)
163 ((_ () (rest ...) #f #f) ;no 'title' and no 'device' field
164 (%file-system rest ...))
165 ((_ () (rest ...) dev #f) ;no 'title' field
166 (%file-system rest ... (device dev)))
167 ((_ () (rest ...) dev titl) ;got a 'title' field
168 (%file-system rest ...
169 (device (device-expression titl dev))))
170 ((_ ((title titl) rest ...) (previous ...) dev _)
172 (report-deprecation (title titl))
173 (process-file-system-declaration (rest ...)
176 ((_ ((device dev) rest ...) (previous ...) _ titl)
177 (process-file-system-declaration (rest ...)
180 ((_ (field rest ...) (previous ...) dev titl)
181 (process-file-system-declaration (rest ...)
185 (define-syntax-rule (file-system fields ...)
186 (process-file-system-declaration (fields ...) () #f #f))
188 (define (file-system-title fs) ;deprecated
189 (match (file-system-device fs)
190 ((? file-system-label?) 'label)
192 ((? string?) 'device)))
194 ;; Note: This module is used both on the build side and on the host side.
195 ;; Arrange not to pull (guix store) and (guix config) because the latter
196 ;; differs from user to user.
197 (define (%store-prefix)
198 "Return the store prefix."
199 ;; Note: If we have (guix store database) in the search path and we do *not*
200 ;; have (guix store) proper, 'resolve-module' returns an empty (guix store)
201 ;; with one sub-module.
202 (cond ((and=> (resolve-module '(guix store) #:ensure #f)
204 (module-variable store '%store-prefix)))
207 ((variable-ref variable))))
208 ((getenv "NIX_STORE")
214 (char-set-complement (char-set #\/)))
216 (define (file-prefix? file1 file2)
217 "Return #t if FILE1 denotes the name of a file that is a parent of FILE2,
218 where both FILE1 and FILE2 are absolute file name. For example:
220 (file-prefix? \"/gnu\" \"/gnu/store\")
223 (file-prefix? \"/gn\" \"/gnu/store\")
226 (and (string-prefix? "/" file1)
227 (string-prefix? "/" file2)
228 (let loop ((file1 (string-tokenize file1 %not-slash))
229 (file2 (string-tokenize file2 %not-slash)))
236 (and (string=? head1 head2) (loop tail1 tail2)))
240 (define* (file-system-device->string device #:key uuid-type)
241 "Return the string representations of the DEVICE field of a <file-system>
242 record. When the device is a UUID, its representation is chosen depending on
243 UUID-TYPE, a symbol such as 'dce or 'iso9660."
245 ((? file-system-label?)
246 (file-system-label->string device))
249 (uuid->string (uuid-bytevector device) uuid-type)
250 (uuid->string device)))
254 (define (file-system-needed-for-boot? fs)
255 "Return true if FS has the 'needed-for-boot?' flag set, or if it holds the
256 store--e.g., if FS is the root file system."
257 (or (%file-system-needed-for-boot? fs)
258 (and (file-prefix? (file-system-mount-point fs) (%store-prefix))
259 (not (memq 'bind-mount (file-system-flags fs))))))
261 (define (file-system->spec fs)
262 "Return a list corresponding to file-system FS that can be passed to the
265 (($ <file-system> device mount-point type flags options _ _ check?)
266 (list (cond ((uuid? device)
267 `(uuid ,(uuid-type device) ,(uuid-bytevector device)))
268 ((file-system-label? device)
269 `(file-system-label ,(file-system-label->string device)))
271 mount-point type flags options check?))))
273 (define (spec->file-system sexp)
274 "Deserialize SEXP, a list, to the corresponding <file-system> object."
276 ((device mount-point type flags options check?)
278 (device (match device
279 (('uuid (? symbol? type) (? bytevector? bv))
280 (bytevector->uuid bv type))
281 (('file-system-label (? string? label))
282 (file-system-label label))
285 (mount-point mount-point) (type type)
286 (flags flags) (options options)
289 (define (specification->file-system-mapping spec writable?)
290 "Read the SPEC and return the corresponding <file-system-mapping>. SPEC is
291 a string of the form \"SOURCE\" or \"SOURCE=TARGET\". The former specifies
292 that SOURCE from the host should be mounted at SOURCE in the other system.
293 The latter format specifies that SOURCE from the host should be mounted at
294 TARGET in the other system."
295 (let ((index (string-index spec #\=)))
298 (source (substring spec 0 index))
299 (target (substring spec (+ 1 index)))
300 (writable? writable?))
304 (writable? writable?)))))
308 ;;; Common file systems.
311 (define %pseudo-file-system-types
312 ;; List of know pseudo file system types. This is used when validating file
313 ;; system definitions.
314 '("binfmt_misc" "cgroup" "debugfs" "devpts" "devtmpfs" "efivarfs" "fusectl"
315 "hugetlbfs" "overlay" "proc" "securityfs" "sysfs" "tmpfs"))
317 (define %fuse-control-file-system
318 ;; Control file system for Linux' file systems in user-space (FUSE).
321 (mount-point "/sys/fs/fuse/connections")
325 (define %binary-format-file-system
326 ;; Support for arbitrary executable binary format.
328 (device "binfmt_misc")
329 (mount-point "/proc/sys/fs/binfmt_misc")
334 ;; ID of the 'tty' group. Allocate it statically to make it easy to refer
335 ;; to it from here and from the 'tty' group definitions.
338 (define %pseudo-terminal-file-system
339 ;; The pseudo-terminal file system. It needs to be mounted so that
340 ;; statfs(2) returns DEVPTS_SUPER_MAGIC like libc's getpt(3) expects (and
341 ;; thus openpty(3) and its users, such as xterm.)
344 (mount-point "/dev/pts")
347 (needed-for-boot? #f)
348 (create-mount-point? #t)
349 (options (string-append "gid=" (number->string %tty-gid) ",mode=620"))))
351 (define %shared-memory-file-system
355 (mount-point "/dev/shm")
358 (flags '(no-suid no-dev))
359 (options "size=50%") ;TODO: make size configurable
360 (create-mount-point? #t)))
362 (define %immutable-store
363 ;; Read-only store to avoid users or daemons accidentally modifying it.
364 ;; 'guix-daemon' has provisions to remount it read-write in its own name
367 (device (%store-prefix))
368 (mount-point (%store-prefix))
371 (flags '(read-only bind-mount no-atime))))
373 (define %control-groups
374 (let ((parent (file-system
376 (mount-point "/sys/fs/cgroup")
380 (map (lambda (subsystem)
383 (mount-point (string-append "/sys/fs/cgroup/" subsystem))
387 (create-mount-point? #t)
389 ;; This must be mounted after, and unmounted before the
391 (dependencies (list parent))))
392 '("cpuset" "cpu" "cpuacct" "memory" "devices" "freezer"
393 "blkio" "perf_event" "pids")))))
395 (define %elogind-file-systems
396 ;; We don't use systemd, but these file systems are needed for elogind,
397 ;; which was extracted from systemd.
401 (mount-point "/run/systemd")
404 (flags '(no-suid no-dev no-exec))
405 (options "mode=0755")
406 (create-mount-point? #t))
409 (mount-point "/run/user")
412 (flags '(no-suid no-dev no-exec))
413 (options "mode=0755")
414 (create-mount-point? #t))
415 ;; Elogind uses cgroups to organize processes, allowing it to map PIDs
416 ;; to sessions. Elogind's cgroup hierarchy isn't associated with any
417 ;; resource controller ("subsystem").
420 (mount-point "/sys/fs/cgroup/elogind")
423 (options "none,name=elogind")
424 (create-mount-point? #t)
425 (dependencies (list (car %control-groups)))))
428 (define %base-file-systems
429 ;; List of basic file systems to be mounted. Note that /proc and /sys are
430 ;; currently mounted by the initrd.
431 (list %pseudo-terminal-file-system
432 %shared-memory-file-system
435 ;; File systems for Linux containers differ from %base-file-systems in that
436 ;; they impose additional restrictions such as no-exec or need different
437 ;; options to function properly.
439 ;; The file system flags and options conform to the libcontainer
441 ;; https://github.com/docker/libcontainer/blob/master/SPEC.md#filesystem
442 (define %container-file-systems
444 ;; Pseudo-terminal file system.
447 (mount-point "/dev/pts")
449 (flags '(no-exec no-suid))
450 (needed-for-boot? #t)
451 (create-mount-point? #t)
453 (options "newinstance,ptmxmode=0666,mode=620"))
454 ;; Shared memory file system.
457 (mount-point "/dev/shm")
459 (flags '(no-exec no-suid no-dev))
460 (options "mode=1777,size=65536k")
461 (needed-for-boot? #t)
462 (create-mount-point? #t)
464 ;; Message queue file system.
467 (mount-point "/dev/mqueue")
469 (flags '(no-exec no-suid no-dev))
470 (needed-for-boot? #t)
471 (create-mount-point? #t)
476 ;;; Shared file systems, for VMs/containers.
479 ;; Mapping of host file system SOURCE to mount point TARGET in the guest.
480 (define-record-type* <file-system-mapping> file-system-mapping
481 make-file-system-mapping
483 (source file-system-mapping-source) ;string
484 (target file-system-mapping-target) ;string
485 (writable? file-system-mapping-writable? ;Boolean
488 (define (file-system-mapping->bind-mount mapping)
489 "Return a file system that realizes MAPPING, a <file-system-mapping>, using
492 (($ <file-system-mapping> source target writable?)
499 '(bind-mount read-only)))
501 (create-mount-point? #t)))))
503 (define %store-mapping
504 ;; Mapping of the host's store into the guest.
506 (source (%store-prefix))
507 (target (%store-prefix))
510 (define %network-configuration-files
511 ;; List of essential network configuration files.
517 (define %network-file-mappings
518 ;; List of file mappings for essential network files.
519 (filter-map (lambda (file)
523 ;; XXX: On some GNU/Linux systems, /etc/resolv.conf is a
524 ;; symlink to a file in a tmpfs which, for an unknown reason,
525 ;; cannot be bind mounted read-only within the container.
526 ;; The same goes with /var/run/nscd, as discussed in
527 ;; <https://bugs.gnu.org/37967>.
528 (writable? (or (string=? file "/etc/resolv.conf")
529 (string=? file "/var/run/nscd")))))
530 (cons "/var/run/nscd" %network-configuration-files)))
532 (define (file-system-type-predicate type)
533 "Return a predicate that, when passed a file system, returns #t if that file
534 system has the given TYPE."
536 (string=? (file-system-type fs) type)))
538 ;;; file-systems.scm ends here