image: Add rock64 support.
[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 Google LLC
4 ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
5 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
6 ;;;
7 ;;; This file is part of GNU Guix.
8 ;;;
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.
13 ;;;
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.
18 ;;;
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/>.
21
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
36 string->uuid
37 uuid->string)
38 #:export (file-system
39 file-system?
40 file-system-device
41 file-system-device->string
42 file-system-title ;deprecated
43 file-system-mount-point
44 file-system-type
45 file-system-needed-for-boot?
46 file-system-flags
47 file-system-options
48 file-system-options->alist
49 alist->file-system-options
50
51 file-system-mount?
52 file-system-mount-may-fail?
53 file-system-check?
54 file-system-create-mount-point?
55 file-system-dependencies
56 file-system-location
57
58 file-system-type-predicate
59 btrfs-subvolume?
60 btrfs-store-subvolume-file-name
61
62 file-system-label
63 file-system-label?
64 file-system-label->string
65
66 file-system->spec
67 spec->file-system
68 specification->file-system-mapping
69
70 %pseudo-file-system-types
71 %fuse-control-file-system
72 %binary-format-file-system
73 %debug-file-system
74 %efivars-file-system
75 %shared-memory-file-system
76 %pseudo-terminal-file-system
77 %tty-gid
78 %immutable-store
79 %control-groups
80 %elogind-file-systems
81
82 %base-file-systems
83 %container-file-systems
84
85 <file-system-mapping>
86 file-system-mapping
87 file-system-mapping?
88 file-system-mapping-source
89 file-system-mapping-target
90 file-system-mapping-writable?
91
92 file-system-mapping->bind-mount
93
94 %store-mapping
95 %network-configuration-files
96 %network-file-mappings))
97
98 ;;; Commentary:
99 ;;;
100 ;;; Declaring file systems to be mounted.
101 ;;;
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.
104 ;;;
105 ;;; Code:
106
107 ;; File system declaration.
108 (define-record-type* <file-system> %file-system
109 make-file-system
110 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
115 (default '()))
116 (options file-system-options ; string or #f
117 (default #f))
118 (mount? file-system-mount? ; Boolean
119 (default #t))
120 (mount-may-fail? file-system-mount-may-fail? ; Boolean
121 (default #f))
122 (needed-for-boot? %file-system-needed-for-boot? ; Boolean
123 (default #f))
124 (check? file-system-check? ; Boolean
125 (default #t))
126 (create-mount-point? file-system-create-mount-point? ; Boolean
127 (default #f))
128 (dependencies file-system-dependencies ; list of <file-system>
129 (default '())) ; or <mapped-device>
130 (location file-system-location
131 (default (current-source-location))
132 (innate)))
133
134 ;; A file system label for use in the 'device' field.
135 (define-record-type <file-system-label>
136 (file-system-label label)
137 file-system-label?
138 (label file-system-label->string))
139
140 (set-record-type-printer! <file-system-label>
141 (lambda (obj port)
142 (format port "#<file-system-label ~s>"
143 (file-system-label->string obj))))
144
145 (define-syntax report-deprecation
146 (lambda (s)
147 "Report the use of the now-deprecated 'title' field."
148 (syntax-case s ()
149 ((_ field)
150 (let* ((source (syntax-source #'field))
151 (file (and source (assq-ref source 'filename)))
152 (line (and source
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~%"
157 file line column)
158 #t)))))
159
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)
168 dev)
169 ((_ title dev)
170 (case title
171 ((label) (file-system-label dev))
172 ((uuid) (uuid dev))
173 (else dev)))))
174
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 _)
188 (begin
189 (report-deprecation (title titl))
190 (process-file-system-declaration (rest ...)
191 (previous ...)
192 dev titl)))
193 ((_ ((device dev) rest ...) (previous ...) _ titl)
194 (process-file-system-declaration (rest ...)
195 (previous ...)
196 dev titl))
197 ((_ (field rest ...) (previous ...) dev titl)
198 (process-file-system-declaration (rest ...)
199 (previous ... field)
200 dev titl))))
201
202 (define-syntax-rule (file-system fields ...)
203 (process-file-system-declaration (fields ...) () #f #f))
204
205 (define (file-system-title fs) ;deprecated
206 (match (file-system-device fs)
207 ((? file-system-label?) 'label)
208 ((? uuid?) 'uuid)
209 ((? string?) 'device)))
210
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)
220 (lambda (store)
221 (module-variable store '%store-prefix)))
222 =>
223 (lambda (variable)
224 ((variable-ref variable))))
225 ((getenv "NIX_STORE")
226 => identity)
227 (else
228 "/gnu/store")))
229
230 (define %not-slash
231 (char-set-complement (char-set #\/)))
232
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:
236
237 (file-prefix? \"/gnu\" \"/gnu/store\")
238 => #t
239
240 (file-prefix? \"/gn\" \"/gnu/store\")
241 => #f
242 "
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)))
247 (match file1
248 (()
249 #t)
250 ((head1 tail1 ...)
251 (match file2
252 ((head2 tail2 ...)
253 (and (string=? head1 head2) (loop tail1 tail2)))
254 (()
255 #f)))))))
256
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."
261 (match device
262 ((? file-system-label?)
263 (file-system-label->string device))
264 ((? uuid?)
265 (if uuid-type
266 (uuid->string (uuid-bytevector device) uuid-type)
267 (uuid->string device)))
268 ((? string?)
269 device)))
270
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."
274 (if string
275 (let ((options (string-split string #\,)))
276 (map (lambda (param)
277 (let ((=index (string-index param #\=)))
278 (if =index
279 (cons (string-take param =index)
280 (string-drop param (1+ =index)))
281 param)))
282 options))
283 '()))
284
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."
288 (if (null? options)
289 #f
290 (string-join (map (match-lambda
291 ((key . value)
292 (string-append key "=" value))
293 (key
294 key))
295 options)
296 ",")))
297
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))))))
304
305 (define (file-system->spec fs)
306 "Return a list corresponding to file-system FS that can be passed to the
307 initrd code."
308 (match fs
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)))
316 (else device))
317 mount-point type flags options mount-may-fail? check?))))
318
319 (define (spec->file-system sexp)
320 "Deserialize SEXP, a list, to the corresponding <file-system> object."
321 (match sexp
322 ((device mount-point type flags options mount-may-fail? check?
323 _ ...) ;placeholder for new fields
324 (file-system
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))
330 (_
331 device)))
332 (mount-point mount-point) (type type)
333 (flags flags) (options options)
334 (mount-may-fail? mount-may-fail?)
335 (check? check?)))))
336
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 #\=)))
344 (if index
345 (file-system-mapping
346 (source (substring spec 0 index))
347 (target (substring spec (+ 1 index)))
348 (writable? writable?))
349 (file-system-mapping
350 (source spec)
351 (target spec)
352 (writable? writable?)))))
353
354 \f
355 ;;;
356 ;;; Common file systems.
357 ;;;
358
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"))
364
365 (define %fuse-control-file-system
366 ;; Control file system for Linux' file systems in user-space (FUSE).
367 (file-system
368 (device "fusectl")
369 (mount-point "/sys/fs/fuse/connections")
370 (type "fusectl")
371 (check? #f)))
372
373 (define %binary-format-file-system
374 ;; Support for arbitrary executable binary format.
375 (file-system
376 (device "binfmt_misc")
377 (mount-point "/proc/sys/fs/binfmt_misc")
378 (type "binfmt_misc")
379 (check? #f)))
380
381 (define %debug-file-system
382 (file-system
383 (type "debugfs")
384 (device "none")
385 (mount-point "/sys/kernel/debug")
386 (check? #f)
387 (create-mount-point? #t)))
388
389 (define %efivars-file-system
390 ;; Support for EFI variables file system.
391 (file-system
392 (device "efivarfs")
393 (mount-point "/sys/firmware/efi/efivars")
394 (type "efivarfs")
395 (mount-may-fail? #t)
396 (needed-for-boot? #f)
397 (check? #f)))
398
399 (define %tty-gid
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.
402 996)
403
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.)
408 (file-system
409 (device "none")
410 (mount-point "/dev/pts")
411 (type "devpts")
412 (check? #f)
413 (needed-for-boot? #f)
414 (create-mount-point? #t)
415 (options (string-append "gid=" (number->string %tty-gid) ",mode=620"))))
416
417 (define %shared-memory-file-system
418 ;; Shared memory.
419 (file-system
420 (device "tmpfs")
421 (mount-point "/dev/shm")
422 (type "tmpfs")
423 (check? #f)
424 (flags '(no-suid no-dev))
425 (options "size=50%") ;TODO: make size configurable
426 (create-mount-point? #t)))
427
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
431 ;; space.
432 (file-system
433 (device (%store-prefix))
434 (mount-point (%store-prefix))
435 (type "none")
436 (check? #f)
437 (flags '(read-only bind-mount no-atime))))
438
439 (define %control-groups
440 (let ((parent (file-system
441 (device "cgroup")
442 (mount-point "/sys/fs/cgroup")
443 (type "tmpfs")
444 (check? #f))))
445 (cons parent
446 (map (lambda (subsystem)
447 (file-system
448 (device "cgroup")
449 (mount-point (string-append "/sys/fs/cgroup/" subsystem))
450 (type "cgroup")
451 (check? #f)
452 (options subsystem)
453 (create-mount-point? #t)
454
455 ;; This must be mounted after, and unmounted before the
456 ;; parent directory.
457 (dependencies (list parent))))
458 '("cpuset" "cpu" "cpuacct" "memory" "devices" "freezer"
459 "blkio" "perf_event" "pids")))))
460
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.
464 (append
465 (list (file-system
466 (device "none")
467 (mount-point "/run/systemd")
468 (type "tmpfs")
469 (check? #f)
470 (flags '(no-suid no-dev no-exec))
471 (options "mode=0755")
472 (create-mount-point? #t))
473 (file-system
474 (device "none")
475 (mount-point "/run/user")
476 (type "tmpfs")
477 (check? #f)
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").
484 (file-system
485 (device "cgroup")
486 (mount-point "/sys/fs/cgroup/elogind")
487 (type "cgroup")
488 (check? #f)
489 (options "none,name=elogind")
490 (create-mount-point? #t)
491 (dependencies (list (car %control-groups)))))
492 %control-groups))
493
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
498 %debug-file-system
499 %shared-memory-file-system
500 %efivars-file-system
501 %immutable-store))
502
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.
506 ;;
507 ;; The file system flags and options conform to the libcontainer
508 ;; specification:
509 ;; https://github.com/docker/libcontainer/blob/master/SPEC.md#filesystem
510 (define %container-file-systems
511 (list
512 ;; Pseudo-terminal file system.
513 (file-system
514 (device "none")
515 (mount-point "/dev/pts")
516 (type "devpts")
517 (flags '(no-exec no-suid))
518 (needed-for-boot? #t)
519 (create-mount-point? #t)
520 (check? #f)
521 (options "newinstance,ptmxmode=0666,mode=620"))
522 ;; Shared memory file system.
523 (file-system
524 (device "tmpfs")
525 (mount-point "/dev/shm")
526 (type "tmpfs")
527 (flags '(no-exec no-suid no-dev))
528 (options "mode=1777,size=65536k")
529 (needed-for-boot? #t)
530 (create-mount-point? #t)
531 (check? #f))
532 ;; Message queue file system.
533 (file-system
534 (device "mqueue")
535 (mount-point "/dev/mqueue")
536 (type "mqueue")
537 (flags '(no-exec no-suid no-dev))
538 (needed-for-boot? #t)
539 (create-mount-point? #t)
540 (check? #f))))
541
542 \f
543 ;;;
544 ;;; Shared file systems, for VMs/containers.
545 ;;;
546
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
550 file-system-mapping?
551 (source file-system-mapping-source) ;string
552 (target file-system-mapping-target) ;string
553 (writable? file-system-mapping-writable? ;Boolean
554 (default #f)))
555
556 (define (file-system-mapping->bind-mount mapping)
557 "Return a file system that realizes MAPPING, a <file-system-mapping>, using
558 a bind mount."
559 (match mapping
560 (($ <file-system-mapping> source target writable?)
561 (file-system
562 (mount-point target)
563 (device source)
564 (type "none")
565 (flags (if writable?
566 '(bind-mount)
567 '(bind-mount read-only)))
568 (check? #f)
569 (create-mount-point? #t)))))
570
571 (define %store-mapping
572 ;; Mapping of the host's store into the guest.
573 (file-system-mapping
574 (source (%store-prefix))
575 (target (%store-prefix))
576 (writable? #f)))
577
578 (define %network-configuration-files
579 ;; List of essential network configuration files.
580 '("/etc/resolv.conf"
581 "/etc/nsswitch.conf"
582 "/etc/services"
583 "/etc/hosts"))
584
585 (define %network-file-mappings
586 ;; List of file mappings for essential network files.
587 (filter-map (lambda (file)
588 (file-system-mapping
589 (source file)
590 (target 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))
596
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."
600 (lambda (fs)
601 (string=? (file-system-type fs) type)))
602
603 \f
604 ;;;
605 ;;; Btrfs specific helpers.
606 ;;;
607
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
612 ((key . value) key)
613 (key key))
614 (file-system-options->alist
615 (file-system-options fs)))))
616 (find (cut string-prefix? "subvol" <>) option-keys)))
617
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."
621
622 (define (prepend-slash/maybe s)
623 (if (string=? "/" (string-take s 1))
624 s
625 (string-append "/" s)))
626
627 (define (file-name-depth file-name)
628 (length (string-tokenize file-name %not-slash)))
629
630 (and-let* ((btrfs-subvolume-fs (filter btrfs-subvolume? file-systems))
631 (btrfs-subvolume-fs*
632 (sort btrfs-subvolume-fs
633 (lambda (fs1 fs2)
634 (> (file-name-depth (file-system-mount-point fs1))
635 (file-name-depth (file-system-mount-point fs2))))))
636 (store-subvolume-fs
637 (find (lambda (fs) (file-prefix? (file-system-mount-point fs)
638 (%store-prefix)))
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)
645 (raise (condition
646 (&message
647 (message "The store is on a Btrfs subvolume, but the \
648 subvolume name is unknown."))
649 (&fix-hint
650 (hint
651 (G_ "Use the @code{subvol} Btrfs file system option."))))))))
652
653
654 ;;; file-systems.scm ends here