1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (gnu system file-systems)
20 #:use-module (ice-9 match)
21 #:use-module (srfi srfi-1)
22 #:use-module (guix records)
23 #:use-module ((gnu build file-systems)
24 #:select (string->uuid uuid->string))
25 #:re-export (string->uuid
27 #:export (<file-system>
32 file-system-mount-point
34 file-system-needed-for-boot?
39 file-system-create-mount-point?
40 file-system-dependencies
44 specification->file-system-mapping
47 %fuse-control-file-system
48 %binary-format-file-system
49 %shared-memory-file-system
50 %pseudo-terminal-file-system
57 %container-file-systems
62 file-system-mapping-source
63 file-system-mapping-target
64 file-system-mapping-writable?
66 file-system-mapping->bind-mount
69 %network-configuration-files
70 %network-file-mappings))
74 ;;; Declaring file systems to be mounted.
78 ;; File system declaration.
79 (define-record-type* <file-system> file-system
82 (device file-system-device) ; string
83 (title file-system-title ; 'device | 'label | 'uuid
85 (mount-point file-system-mount-point) ; string
86 (type file-system-type) ; string
87 (flags file-system-flags ; list of symbols
89 (options file-system-options ; string or #f
91 (mount? file-system-mount? ; Boolean
93 (needed-for-boot? %file-system-needed-for-boot? ; Boolean
95 (check? file-system-check? ; Boolean
97 (create-mount-point? file-system-create-mount-point? ; Boolean
99 (dependencies file-system-dependencies ; list of <file-system>
100 (default '()))) ; or <mapped-device>
102 ;; Note: This module is used both on the build side and on the host side.
103 ;; Arrange not to pull (guix store) and (guix config) because the latter
104 ;; differs from user to user.
105 (define (%store-prefix)
106 "Return the store prefix."
107 (cond ((resolve-module '(guix store) #:ensure #f)
110 ((module-ref store '%store-prefix))))
111 ((getenv "NIX_STORE")
117 (char-set-complement (char-set #\/)))
119 (define (file-prefix? file1 file2)
120 "Return #t if FILE1 denotes the name of a file that is a parent of FILE2,
121 where both FILE1 and FILE2 are absolute file name. For example:
123 (file-prefix? \"/gnu\" \"/gnu/store\")
126 (file-prefix? \"/gn\" \"/gnu/store\")
129 (and (string-prefix? "/" file1)
130 (string-prefix? "/" file2)
131 (let loop ((file1 (string-tokenize file1 %not-slash))
132 (file2 (string-tokenize file2 %not-slash)))
139 (and (string=? head1 head2) (loop tail1 tail2)))
143 (define (file-system-needed-for-boot? fs)
144 "Return true if FS has the 'needed-for-boot?' flag set, or if it holds the
145 store--e.g., if FS is the root file system."
146 (or (%file-system-needed-for-boot? fs)
147 (and (file-prefix? (file-system-mount-point fs) (%store-prefix))
148 (not (memq 'bind-mount (file-system-flags fs))))))
150 (define (file-system->spec fs)
151 "Return a list corresponding to file-system FS that can be passed to the
154 (($ <file-system> device title mount-point type flags options _ _ check?)
155 (list device title mount-point type flags options check?))))
157 (define (spec->file-system sexp)
158 "Deserialize SEXP, a list, to the corresponding <file-system> object."
160 ((device title mount-point type flags options check?)
162 (device device) (title title)
163 (mount-point mount-point) (type type)
164 (flags flags) (options options)
167 (define (specification->file-system-mapping spec writable?)
168 "Read the SPEC and return the corresponding <file-system-mapping>. SPEC is
169 a string of the form \"SOURCE\" or \"SOURCE=TARGET\". The former specifies
170 that SOURCE from the host should be mounted at SOURCE in the other system.
171 The latter format specifies that SOURCE from the host should be mounted at
172 TARGET in the other system."
173 (let ((index (string-index spec #\=)))
176 (source (substring spec 0 index))
177 (target (substring spec (+ 1 index)))
178 (writable? writable?))
182 (writable? writable?)))))
186 "Return the bytevector corresponding to the given UUID representation."
189 (string? (syntax->datum #'str))
190 ;; A literal string: do the conversion at expansion time.
191 (let ((bv (string->uuid (syntax->datum #'str))))
193 (syntax-violation 'uuid "invalid UUID" s))
194 (datum->syntax #'str bv)))
196 #'(string->uuid str)))))
200 ;;; Common file systems.
203 (define %fuse-control-file-system
204 ;; Control file system for Linux' file systems in user-space (FUSE).
207 (mount-point "/sys/fs/fuse/connections")
211 (define %binary-format-file-system
212 ;; Support for arbitrary executable binary format.
214 (device "binfmt_misc")
215 (mount-point "/proc/sys/fs/binfmt_misc")
220 ;; ID of the 'tty' group. Allocate it statically to make it easy to refer
221 ;; to it from here and from the 'tty' group definitions.
224 (define %pseudo-terminal-file-system
225 ;; The pseudo-terminal file system. It needs to be mounted so that
226 ;; statfs(2) returns DEVPTS_SUPER_MAGIC like libc's getpt(3) expects (and
227 ;; thus openpty(3) and its users, such as xterm.)
230 (mount-point "/dev/pts")
233 (needed-for-boot? #f)
234 (create-mount-point? #t)
235 (options (string-append "gid=" (number->string %tty-gid) ",mode=620"))))
237 (define %shared-memory-file-system
241 (mount-point "/dev/shm")
244 (flags '(no-suid no-dev))
245 (options "size=50%") ;TODO: make size configurable
246 (create-mount-point? #t)))
248 (define %immutable-store
249 ;; Read-only store to avoid users or daemons accidentally modifying it.
250 ;; 'guix-daemon' has provisions to remount it read-write in its own name
253 (device (%store-prefix))
254 (mount-point (%store-prefix))
257 (flags '(read-only bind-mount))))
259 (define %control-groups
260 (let ((parent (file-system
262 (mount-point "/sys/fs/cgroup")
266 (map (lambda (subsystem)
269 (mount-point (string-append "/sys/fs/cgroup/" subsystem))
273 (create-mount-point? #t)
275 ;; This must be mounted after, and unmounted before the
277 (dependencies (list parent))))
278 '("cpuset" "cpu" "cpuacct" "memory" "devices" "freezer"
279 "blkio" "perf_event" "hugetlb")))))
281 (define %elogind-file-systems
282 ;; We don't use systemd, but these file systems are needed for elogind,
283 ;; which was extracted from systemd.
286 (mount-point "/run/systemd")
289 (flags '(no-suid no-dev no-exec))
290 (options "mode=0755")
291 (create-mount-point? #t))
294 (mount-point "/run/user")
297 (flags '(no-suid no-dev no-exec))
298 (options "mode=0755")
299 (create-mount-point? #t))
300 ;; Elogind uses cgroups to organize processes, allowing it to map PIDs
301 ;; to sessions. Elogind's cgroup hierarchy isn't associated with any
302 ;; resource controller ("subsystem").
305 (mount-point "/sys/fs/cgroup/elogind")
308 (options "none,name=elogind")
309 (create-mount-point? #t)
310 (dependencies (list (car %control-groups))))))
312 (define %base-file-systems
313 ;; List of basic file systems to be mounted. Note that /proc and /sys are
314 ;; currently mounted by the initrd.
315 (append (list %pseudo-terminal-file-system
316 %shared-memory-file-system
320 ;; File systems for Linux containers differ from %base-file-systems in that
321 ;; they impose additional restrictions such as no-exec or need different
322 ;; options to function properly.
324 ;; The file system flags and options conform to the libcontainer
326 ;; https://github.com/docker/libcontainer/blob/master/SPEC.md#filesystem
327 (define %container-file-systems
329 ;; Pseudo-terminal file system.
332 (mount-point "/dev/pts")
334 (flags '(no-exec no-suid))
335 (needed-for-boot? #t)
336 (create-mount-point? #t)
338 (options "newinstance,ptmxmode=0666,mode=620"))
339 ;; Shared memory file system.
342 (mount-point "/dev/shm")
344 (flags '(no-exec no-suid no-dev))
345 (options "mode=1777,size=65536k")
346 (needed-for-boot? #t)
347 (create-mount-point? #t)
349 ;; Message queue file system.
352 (mount-point "/dev/mqueue")
354 (flags '(no-exec no-suid no-dev))
355 (needed-for-boot? #t)
356 (create-mount-point? #t)
361 ;;; Shared file systems, for VMs/containers.
364 ;; Mapping of host file system SOURCE to mount point TARGET in the guest.
365 (define-record-type* <file-system-mapping> file-system-mapping
366 make-file-system-mapping
368 (source file-system-mapping-source) ;string
369 (target file-system-mapping-target) ;string
370 (writable? file-system-mapping-writable? ;Boolean
373 (define (file-system-mapping->bind-mount mapping)
374 "Return a file system that realizes MAPPING, a <file-system-mapping>, using
377 (($ <file-system-mapping> source target writable?)
384 '(bind-mount read-only)))
386 (create-mount-point? #t)))))
388 (define %store-mapping
389 ;; Mapping of the host's store into the guest.
391 (source (%store-prefix))
392 (target (%store-prefix))
395 (define %network-configuration-files
396 ;; List of essential network configuration files.
402 (define %network-file-mappings
403 ;; List of file mappings for essential network files.
404 (filter-map (lambda (file)
408 ;; XXX: On some GNU/Linux systems, /etc/resolv.conf is a
409 ;; symlink to a file in a tmpfs which, for an unknown reason,
410 ;; cannot be bind mounted read-only within the container.
411 (writable? (string=? file "/etc/resolv.conf"))))
412 %network-configuration-files))
414 ;;; file-systems.scm ends here