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 (guix records)
22 #:use-module (guix store)
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?
70 ;;; Declaring file systems to be mounted.
74 ;; File system declaration.
75 (define-record-type* <file-system> file-system
78 (device file-system-device) ; string
79 (title file-system-title ; 'device | 'label | 'uuid
81 (mount-point file-system-mount-point) ; string
82 (type file-system-type) ; string
83 (flags file-system-flags ; list of symbols
85 (options file-system-options ; string or #f
87 (mount? file-system-mount? ; Boolean
89 (needed-for-boot? %file-system-needed-for-boot? ; Boolean
91 (check? file-system-check? ; Boolean
93 (create-mount-point? file-system-create-mount-point? ; Boolean
95 (dependencies file-system-dependencies ; list of <file-system>
96 (default '()))) ; or <mapped-device>
99 (char-set-complement (char-set #\/)))
101 (define (file-prefix? file1 file2)
102 "Return #t if FILE1 denotes the name of a file that is a parent of FILE2,
103 where both FILE1 and FILE2 are absolute file name. For example:
105 (file-prefix? \"/gnu\" \"/gnu/store\")
108 (file-prefix? \"/gn\" \"/gnu/store\")
111 (and (string-prefix? "/" file1)
112 (string-prefix? "/" file2)
113 (let loop ((file1 (string-tokenize file1 %not-slash))
114 (file2 (string-tokenize file2 %not-slash)))
121 (and (string=? head1 head2) (loop tail1 tail2)))
125 (define (file-system-needed-for-boot? fs)
126 "Return true if FS has the 'needed-for-boot?' flag set, or if it holds the
127 store--e.g., if FS is the root file system."
128 (or (%file-system-needed-for-boot? fs)
129 (and (file-prefix? (file-system-mount-point fs) (%store-prefix))
130 (not (memq 'bind-mount (file-system-flags fs))))))
132 (define (file-system->spec fs)
133 "Return a list corresponding to file-system FS that can be passed to the
136 (($ <file-system> device title mount-point type flags options _ _ check?)
137 (list device title mount-point type flags options check?))))
139 (define (spec->file-system sexp)
140 "Deserialize SEXP, a list, to the corresponding <file-system> object."
142 ((device title mount-point type flags options check?)
144 (device device) (title title)
145 (mount-point mount-point) (type type)
146 (flags flags) (options options)
149 (define (specification->file-system-mapping spec writable?)
150 "Read the SPEC and return the corresponding <file-system-mapping>. SPEC is
151 a string of the form \"SOURCE\" or \"SOURCE=TARGET\". The former specifies
152 that SOURCE from the host should be mounted at SOURCE in the other system.
153 The latter format specifies that SOURCE from the host should be mounted at
154 TARGET in the other system."
155 (let ((index (string-index spec #\=)))
158 (source (substring spec 0 index))
159 (target (substring spec (+ 1 index)))
160 (writable? writable?))
164 (writable? writable?)))))
168 "Return the bytevector corresponding to the given UUID representation."
171 (string? (syntax->datum #'str))
172 ;; A literal string: do the conversion at expansion time.
173 (let ((bv (string->uuid (syntax->datum #'str))))
175 (syntax-violation 'uuid "invalid UUID" s))
176 (datum->syntax #'str bv)))
178 #'(string->uuid str)))))
182 ;;; Common file systems.
185 (define %fuse-control-file-system
186 ;; Control file system for Linux' file systems in user-space (FUSE).
189 (mount-point "/sys/fs/fuse/connections")
193 (define %binary-format-file-system
194 ;; Support for arbitrary executable binary format.
196 (device "binfmt_misc")
197 (mount-point "/proc/sys/fs/binfmt_misc")
202 ;; ID of the 'tty' group. Allocate it statically to make it easy to refer
203 ;; to it from here and from the 'tty' group definitions.
206 (define %pseudo-terminal-file-system
207 ;; The pseudo-terminal file system. It needs to be mounted so that
208 ;; statfs(2) returns DEVPTS_SUPER_MAGIC like libc's getpt(3) expects (and
209 ;; thus openpty(3) and its users, such as xterm.)
212 (mount-point "/dev/pts")
215 (needed-for-boot? #f)
216 (create-mount-point? #t)
217 (options (string-append "gid=" (number->string %tty-gid) ",mode=620"))))
219 (define %shared-memory-file-system
223 (mount-point "/dev/shm")
226 (flags '(no-suid no-dev))
227 (options "size=50%") ;TODO: make size configurable
228 (create-mount-point? #t)))
230 (define %immutable-store
231 ;; Read-only store to avoid users or daemons accidentally modifying it.
232 ;; 'guix-daemon' has provisions to remount it read-write in its own name
235 (device (%store-prefix))
236 (mount-point (%store-prefix))
239 (flags '(read-only bind-mount))))
241 (define %control-groups
242 (let ((parent (file-system
244 (mount-point "/sys/fs/cgroup")
248 (map (lambda (subsystem)
251 (mount-point (string-append "/sys/fs/cgroup/" subsystem))
255 (create-mount-point? #t)
257 ;; This must be mounted after, and unmounted before the
259 (dependencies (list parent))))
260 '("cpuset" "cpu" "cpuacct" "memory" "devices" "freezer"
261 "blkio" "perf_event" "hugetlb")))))
263 (define %elogind-file-systems
264 ;; We don't use systemd, but these file systems are needed for elogind,
265 ;; which was extracted from systemd.
268 (mount-point "/run/systemd")
271 (flags '(no-suid no-dev no-exec))
272 (options "mode=0755")
273 (create-mount-point? #t))
276 (mount-point "/run/user")
279 (flags '(no-suid no-dev no-exec))
280 (options "mode=0755")
281 (create-mount-point? #t))
282 ;; Elogind uses cgroups to organize processes, allowing it to map PIDs
283 ;; to sessions. Elogind's cgroup hierarchy isn't associated with any
284 ;; resource controller ("subsystem").
287 (mount-point "/sys/fs/cgroup/elogind")
290 (options "none,name=elogind")
291 (create-mount-point? #t)
292 (dependencies (list (car %control-groups))))))
294 (define %base-file-systems
295 ;; List of basic file systems to be mounted. Note that /proc and /sys are
296 ;; currently mounted by the initrd.
297 (append (list %pseudo-terminal-file-system
298 %shared-memory-file-system
302 ;; File systems for Linux containers differ from %base-file-systems in that
303 ;; they impose additional restrictions such as no-exec or need different
304 ;; options to function properly.
306 ;; The file system flags and options conform to the libcontainer
308 ;; https://github.com/docker/libcontainer/blob/master/SPEC.md#filesystem
309 (define %container-file-systems
311 ;; Pseudo-terminal file system.
314 (mount-point "/dev/pts")
316 (flags '(no-exec no-suid))
317 (needed-for-boot? #t)
318 (create-mount-point? #t)
320 (options "newinstance,ptmxmode=0666,mode=620"))
321 ;; Shared memory file system.
324 (mount-point "/dev/shm")
326 (flags '(no-exec no-suid no-dev))
327 (options "mode=1777,size=65536k")
328 (needed-for-boot? #t)
329 (create-mount-point? #t)
331 ;; Message queue file system.
334 (mount-point "/dev/mqueue")
336 (flags '(no-exec no-suid no-dev))
337 (needed-for-boot? #t)
338 (create-mount-point? #t)
343 ;;; Shared file systems, for VMs/containers.
346 ;; Mapping of host file system SOURCE to mount point TARGET in the guest.
347 (define-record-type* <file-system-mapping> file-system-mapping
348 make-file-system-mapping
350 (source file-system-mapping-source) ;string
351 (target file-system-mapping-target) ;string
352 (writable? file-system-mapping-writable? ;Boolean
355 (define %store-mapping
356 ;; Mapping of the host's store into the guest.
358 (source (%store-prefix))
359 (target (%store-prefix))
362 ;;; file-systems.scm ends here