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 strings (mount
103 ; points depended on)
106 (define-inlinable (file-system-needed-for-boot? fs)
107 "Return true if FS has the 'needed-for-boot?' flag set, or if it's the root
109 (or (%file-system-needed-for-boot? fs)
110 (string=? "/" (file-system-mount-point fs))))
112 (define (file-system->spec fs)
113 "Return a list corresponding to file-system FS that can be passed to the
116 (($ <file-system> device title mount-point type flags options _ check?)
117 (list device title mount-point type flags options check?))))
120 ;; The regexp of a UUID.
121 (make-regexp "^([[:xdigit:]]{8})-([[:xdigit:]]{4})-([[:xdigit:]]{4})-([[:xdigit:]]{4})-([[:xdigit:]]{12})$"))
123 (define (string->uuid str)
124 "Parse STR as a DCE UUID (see <https://tools.ietf.org/html/rfc4122>) and
125 return its contents as a 16-byte bytevector. Return #f if STR is not a valid
126 UUID representation."
127 (and=> (regexp-exec %uuid-rx str)
129 (letrec-syntax ((hex->number
132 (string->number (match:substring match index)
136 ((_ bv index (number len) rest ...)
138 (bytevector-uint-set! bv index number
139 (endianness big) len)
140 (put! bv (+ index len) rest ...)))
143 (let ((time-low (hex->number 1))
144 (time-mid (hex->number 2))
145 (time-hi (hex->number 3))
146 (clock-seq (hex->number 4))
147 (node (hex->number 5))
148 (uuid (make-bytevector 16)))
150 (time-low 4) (time-mid 2) (time-hi 2)
151 (clock-seq 2) (node 6)))))))
155 "Return the bytevector corresponding to the given UUID representation."
158 (string? (syntax->datum #'str))
159 ;; A literal string: do the conversion at expansion time.
160 (let ((bv (string->uuid (syntax->datum #'str))))
162 (syntax-violation 'uuid "invalid UUID" s))
163 (datum->syntax #'str bv)))
165 #'(string->uuid str)))))
169 ;;; Common file systems.
172 (define %fuse-control-file-system
173 ;; Control file system for Linux' file systems in user-space (FUSE).
176 (mount-point "/sys/fs/fuse/connections")
180 (define %binary-format-file-system
181 ;; Support for arbitrary executable binary format.
183 (device "binfmt_misc")
184 (mount-point "/proc/sys/fs/binfmt_misc")
189 ;; ID of the 'tty' group. Allocate it statically to make it easy to refer
190 ;; to it from here and from the 'tty' group definitions.
193 (define %pseudo-terminal-file-system
194 ;; The pseudo-terminal file system. It needs to be mounted so that
195 ;; statfs(2) returns DEVPTS_SUPER_MAGIC like libc's getpt(3) expects (and
196 ;; thus openpty(3) and its users, such as xterm.)
199 (mount-point "/dev/pts")
202 (needed-for-boot? #f)
203 (create-mount-point? #t)
204 (options (string-append "gid=" (number->string %tty-gid) ",mode=620"))))
206 (define %shared-memory-file-system
210 (mount-point "/dev/shm")
213 (flags '(no-suid no-dev))
214 (options "size=50%") ;TODO: make size configurable
215 (create-mount-point? #t)))
217 (define %immutable-store
218 ;; Read-only store to avoid users or daemons accidentally modifying it.
219 ;; 'guix-daemon' has provisions to remount it read-write in its own name
222 (device (%store-prefix))
223 (mount-point (%store-prefix))
226 (flags '(read-only bind-mount))))
228 (define %control-groups
229 (let ((parent (file-system
231 (mount-point "/sys/fs/cgroup")
235 (map (lambda (subsystem)
238 (mount-point (string-append "/sys/fs/cgroup/" subsystem))
242 (create-mount-point? #t)
244 ;; This must be mounted after, and unmounted before the
246 (dependencies (list parent))))
247 '("cpuset" "cpu" "cpuacct" "memory" "devices" "freezer"
248 "blkio" "perf_event" "hugetlb")))))
250 (define %elogind-file-systems
251 ;; We don't use systemd, but these file systems are needed for elogind,
252 ;; which was extracted from systemd.
255 (mount-point "/run/systemd")
258 (flags '(no-suid no-dev no-exec))
259 (options "mode=0755")
260 (create-mount-point? #t))
263 (mount-point "/run/user")
266 (flags '(no-suid no-dev no-exec))
267 (options "mode=0755")
268 (create-mount-point? #t))))
270 (define %base-file-systems
271 ;; List of basic file systems to be mounted. Note that /proc and /sys are
272 ;; currently mounted by the initrd.
273 (append (list %pseudo-terminal-file-system
274 %shared-memory-file-system
276 %elogind-file-systems
279 ;; File systems for Linux containers differ from %base-file-systems in that
280 ;; they impose additional restrictions such as no-exec or need different
281 ;; options to function properly.
283 ;; The file system flags and options conform to the libcontainer
285 ;; https://github.com/docker/libcontainer/blob/master/SPEC.md#filesystem
286 (define %container-file-systems
288 ;; Pseudo-terminal file system.
291 (mount-point "/dev/pts")
293 (flags '(no-exec no-suid))
294 (needed-for-boot? #t)
295 (create-mount-point? #t)
297 (options "newinstance,ptmxmode=0666,mode=620"))
298 ;; Shared memory file system.
301 (mount-point "/dev/shm")
303 (flags '(no-exec no-suid no-dev))
304 (options "mode=1777,size=65536k")
305 (needed-for-boot? #t)
306 (create-mount-point? #t)
308 ;; Message queue file system.
311 (mount-point "/dev/mqueue")
313 (flags '(no-exec no-suid no-dev))
314 (needed-for-boot? #t)
315 (create-mount-point? #t)
321 ;;; Mapped devices, for Linux's device-mapper.
324 (define-record-type* <mapped-device> mapped-device
327 (source mapped-device-source) ;string
328 (target mapped-device-target) ;string
329 (type mapped-device-type)) ;<mapped-device-kind>
331 (define-record-type* <mapped-device-type> mapped-device-kind
332 make-mapped-device-kind
334 (open mapped-device-kind-open) ;source target -> gexp
335 (close mapped-device-kind-close ;source target -> gexp
336 (default (const #~(const #f)))))
340 ;;; Shared file systems, for VMs/containers.
343 ;; Mapping of host file system SOURCE to mount point TARGET in the guest.
344 (define-record-type* <file-system-mapping> file-system-mapping
345 make-file-system-mapping
347 (source file-system-mapping-source) ;string
348 (target file-system-mapping-target) ;string
349 (writable? file-system-mapping-writable? ;Boolean
352 (define %store-mapping
353 ;; Mapping of the host's store into the guest.
355 (source (%store-prefix))
356 (target (%store-prefix))
359 ;;; file-systems.scm ends here