1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015 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 (ice-9 regex)
22 #:use-module (guix gexp)
23 #:use-module (guix records)
24 #:use-module (guix store)
25 #:use-module (rnrs bytevectors)
26 #:use-module ((gnu build file-systems) #:select (uuid->string))
27 #:re-export (uuid->string)
28 #:export (<file-system>
33 file-system-mount-point
35 file-system-needed-for-boot?
39 file-system-create-mount-point?
40 file-system-dependencies
46 %fuse-control-file-system
47 %binary-format-file-system
48 %shared-memory-file-system
49 %pseudo-terminal-file-system
55 %container-file-systems
65 mapped-device-kind-open
66 mapped-device-kind-close
71 file-system-mapping-source
72 file-system-mapping-target
73 file-system-mapping-writable?
79 ;;; Declaring file systems to be mounted.
83 ;; File system declaration.
84 (define-record-type* <file-system> file-system
87 (device file-system-device) ; string
88 (title file-system-title ; 'device | 'label | 'uuid
90 (mount-point file-system-mount-point) ; string
91 (type file-system-type) ; string
92 (flags file-system-flags ; list of symbols
94 (options file-system-options ; string or #f
96 (needed-for-boot? %file-system-needed-for-boot? ; Boolean
98 (check? file-system-check? ; Boolean
100 (create-mount-point? file-system-create-mount-point? ; Boolean
102 (dependencies file-system-dependencies ; list of <file-system>
103 (default '()))) ; or <mapped-device>
105 (define-inlinable (file-system-needed-for-boot? fs)
106 "Return true if FS has the 'needed-for-boot?' flag set, or if it's the root
108 (or (%file-system-needed-for-boot? fs)
109 (string=? "/" (file-system-mount-point fs))))
111 (define (file-system->spec fs)
112 "Return a list corresponding to file-system FS that can be passed to the
115 (($ <file-system> device title mount-point type flags options _ check?)
116 (list device title mount-point type flags options check?))))
119 ;; The regexp of a UUID.
120 (make-regexp "^([[:xdigit:]]{8})-([[:xdigit:]]{4})-([[:xdigit:]]{4})-([[:xdigit:]]{4})-([[:xdigit:]]{12})$"))
122 (define (string->uuid str)
123 "Parse STR as a DCE UUID (see <https://tools.ietf.org/html/rfc4122>) and
124 return its contents as a 16-byte bytevector. Return #f if STR is not a valid
125 UUID representation."
126 (and=> (regexp-exec %uuid-rx str)
128 (letrec-syntax ((hex->number
131 (string->number (match:substring match index)
135 ((_ bv index (number len) rest ...)
137 (bytevector-uint-set! bv index number
138 (endianness big) len)
139 (put! bv (+ index len) rest ...)))
142 (let ((time-low (hex->number 1))
143 (time-mid (hex->number 2))
144 (time-hi (hex->number 3))
145 (clock-seq (hex->number 4))
146 (node (hex->number 5))
147 (uuid (make-bytevector 16)))
149 (time-low 4) (time-mid 2) (time-hi 2)
150 (clock-seq 2) (node 6)))))))
154 "Return the bytevector corresponding to the given UUID representation."
157 (string? (syntax->datum #'str))
158 ;; A literal string: do the conversion at expansion time.
159 (let ((bv (string->uuid (syntax->datum #'str))))
161 (syntax-violation 'uuid "invalid UUID" s))
162 (datum->syntax #'str bv)))
164 #'(string->uuid str)))))
168 ;;; Common file systems.
171 (define %fuse-control-file-system
172 ;; Control file system for Linux' file systems in user-space (FUSE).
175 (mount-point "/sys/fs/fuse/connections")
179 (define %binary-format-file-system
180 ;; Support for arbitrary executable binary format.
182 (device "binfmt_misc")
183 (mount-point "/proc/sys/fs/binfmt_misc")
188 ;; ID of the 'tty' group. Allocate it statically to make it easy to refer
189 ;; to it from here and from the 'tty' group definitions.
192 (define %pseudo-terminal-file-system
193 ;; The pseudo-terminal file system. It needs to be mounted so that
194 ;; statfs(2) returns DEVPTS_SUPER_MAGIC like libc's getpt(3) expects (and
195 ;; thus openpty(3) and its users, such as xterm.)
198 (mount-point "/dev/pts")
201 (needed-for-boot? #f)
202 (create-mount-point? #t)
203 (options (string-append "gid=" (number->string %tty-gid) ",mode=620"))))
205 (define %shared-memory-file-system
209 (mount-point "/dev/shm")
212 (flags '(no-suid no-dev))
213 (options "size=50%") ;TODO: make size configurable
214 (create-mount-point? #t)))
216 (define %immutable-store
217 ;; Read-only store to avoid users or daemons accidentally modifying it.
218 ;; 'guix-daemon' has provisions to remount it read-write in its own name
221 (device (%store-prefix))
222 (mount-point (%store-prefix))
225 (flags '(read-only bind-mount))))
227 (define %control-groups
228 (let ((parent (file-system
230 (mount-point "/sys/fs/cgroup")
234 (map (lambda (subsystem)
237 (mount-point (string-append "/sys/fs/cgroup/" subsystem))
241 (create-mount-point? #t)
243 ;; This must be mounted after, and unmounted before the
245 (dependencies (list parent))))
246 '("cpuset" "cpu" "cpuacct" "memory" "devices" "freezer"
247 "blkio" "perf_event" "hugetlb")))))
249 (define %elogind-file-systems
250 ;; We don't use systemd, but these file systems are needed for elogind,
251 ;; which was extracted from systemd.
254 (mount-point "/run/systemd")
257 (flags '(no-suid no-dev no-exec))
258 (options "mode=0755")
259 (create-mount-point? #t))
262 (mount-point "/run/user")
265 (flags '(no-suid no-dev no-exec))
266 (options "mode=0755")
267 (create-mount-point? #t))))
269 (define %base-file-systems
270 ;; List of basic file systems to be mounted. Note that /proc and /sys are
271 ;; currently mounted by the initrd.
272 (append (list %pseudo-terminal-file-system
273 %shared-memory-file-system
275 %elogind-file-systems
278 ;; File systems for Linux containers differ from %base-file-systems in that
279 ;; they impose additional restrictions such as no-exec or need different
280 ;; options to function properly.
282 ;; The file system flags and options conform to the libcontainer
284 ;; https://github.com/docker/libcontainer/blob/master/SPEC.md#filesystem
285 (define %container-file-systems
287 ;; Pseudo-terminal file system.
290 (mount-point "/dev/pts")
292 (flags '(no-exec no-suid))
293 (needed-for-boot? #t)
294 (create-mount-point? #t)
296 (options "newinstance,ptmxmode=0666,mode=620"))
297 ;; Shared memory file system.
300 (mount-point "/dev/shm")
302 (flags '(no-exec no-suid no-dev))
303 (options "mode=1777,size=65536k")
304 (needed-for-boot? #t)
305 (create-mount-point? #t)
307 ;; Message queue file system.
310 (mount-point "/dev/mqueue")
312 (flags '(no-exec no-suid no-dev))
313 (needed-for-boot? #t)
314 (create-mount-point? #t)
320 ;;; Mapped devices, for Linux's device-mapper.
323 (define-record-type* <mapped-device> mapped-device
326 (source mapped-device-source) ;string
327 (target mapped-device-target) ;string
328 (type mapped-device-type)) ;<mapped-device-kind>
330 (define-record-type* <mapped-device-type> mapped-device-kind
331 make-mapped-device-kind
333 (open mapped-device-kind-open) ;source target -> gexp
334 (close mapped-device-kind-close ;source target -> gexp
335 (default (const #~(const #f)))))
339 ;;; Shared file systems, for VMs/containers.
342 ;; Mapping of host file system SOURCE to mount point TARGET in the guest.
343 (define-record-type* <file-system-mapping> file-system-mapping
344 make-file-system-mapping
346 (source file-system-mapping-source) ;string
347 (target file-system-mapping-target) ;string
348 (writable? file-system-mapping-writable? ;Boolean
351 (define %store-mapping
352 ;; Mapping of the host's store into the guest.
354 (source (%store-prefix))
355 (target (%store-prefix))
358 ;;; file-systems.scm ends here