Commit | Line | Data |
---|---|---|
c5df1839 | 1 | ;;; GNU Guix --- Functional package management for GNU |
3392ce5d | 2 | ;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org> |
c5df1839 LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
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. | |
10 | ;;; | |
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. | |
15 | ;;; | |
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/>. | |
18 | ||
19 | (define-module (gnu system file-systems) | |
575b4b09 | 20 | #:use-module (ice-9 match) |
661a1d79 | 21 | #:use-module (ice-9 regex) |
722554a3 | 22 | #:use-module (guix gexp) |
c5df1839 | 23 | #:use-module (guix records) |
3392ce5d | 24 | #:use-module (guix store) |
661a1d79 LC |
25 | #:use-module (rnrs bytevectors) |
26 | #:use-module ((gnu build file-systems) #:select (uuid->string)) | |
27 | #:re-export (uuid->string) | |
c5df1839 LC |
28 | #:export (<file-system> |
29 | file-system | |
30 | file-system? | |
31 | file-system-device | |
d4c87617 | 32 | file-system-title |
c5df1839 LC |
33 | file-system-mount-point |
34 | file-system-type | |
35 | file-system-needed-for-boot? | |
36 | file-system-flags | |
37 | file-system-options | |
4e469051 LC |
38 | file-system-check? |
39 | file-system-create-mount-point? | |
e51710d1 | 40 | file-system-dependencies |
c5df1839 | 41 | |
575b4b09 | 42 | file-system->spec |
661a1d79 LC |
43 | string->uuid |
44 | uuid | |
575b4b09 | 45 | |
c5df1839 | 46 | %fuse-control-file-system |
a69576ea | 47 | %binary-format-file-system |
705f8b68 MW |
48 | %shared-memory-file-system |
49 | %pseudo-terminal-file-system | |
3392ce5d | 50 | %immutable-store |
727636aa | 51 | %control-groups |
14454f0b | 52 | %elogind-file-systems |
a69576ea | 53 | |
5dae0186 | 54 | %base-file-systems |
c829bc80 | 55 | %container-file-systems |
5dae0186 LC |
56 | |
57 | mapped-device | |
58 | mapped-device? | |
59 | mapped-device-source | |
60 | mapped-device-target | |
722554a3 LC |
61 | mapped-device-type |
62 | ||
63 | mapped-device-kind | |
64 | mapped-device-kind? | |
65 | mapped-device-kind-open | |
9110c2e9 DT |
66 | mapped-device-kind-close |
67 | ||
68 | <file-system-mapping> | |
69 | file-system-mapping | |
70 | file-system-mapping? | |
71 | file-system-mapping-source | |
72 | file-system-mapping-target | |
73 | file-system-mapping-writable? | |
74 | ||
75 | %store-mapping)) | |
c5df1839 LC |
76 | |
77 | ;;; Commentary: | |
78 | ;;; | |
79 | ;;; Declaring file systems to be mounted. | |
80 | ;;; | |
81 | ;;; Code: | |
82 | ||
83 | ;; File system declaration. | |
84 | (define-record-type* <file-system> file-system | |
85 | make-file-system | |
86 | file-system? | |
87 | (device file-system-device) ; string | |
d4c87617 LC |
88 | (title file-system-title ; 'device | 'label | 'uuid |
89 | (default 'device)) | |
c5df1839 LC |
90 | (mount-point file-system-mount-point) ; string |
91 | (type file-system-type) ; string | |
92 | (flags file-system-flags ; list of symbols | |
93 | (default '())) | |
94 | (options file-system-options ; string or #f | |
95 | (default #f)) | |
4d6b879c | 96 | (needed-for-boot? %file-system-needed-for-boot? ; Boolean |
c5df1839 LC |
97 | (default #f)) |
98 | (check? file-system-check? ; Boolean | |
4e469051 LC |
99 | (default #t)) |
100 | (create-mount-point? file-system-create-mount-point? ; Boolean | |
e51710d1 LC |
101 | (default #f)) |
102 | (dependencies file-system-dependencies ; list of strings (mount | |
103 | ; points depended on) | |
104 | (default '()))) | |
c5df1839 | 105 | |
4d6b879c LC |
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 | |
108 | file system." | |
109 | (or (%file-system-needed-for-boot? fs) | |
110 | (string=? "/" (file-system-mount-point fs)))) | |
111 | ||
575b4b09 DT |
112 | (define (file-system->spec fs) |
113 | "Return a list corresponding to file-system FS that can be passed to the | |
114 | initrd code." | |
115 | (match fs | |
116 | (($ <file-system> device title mount-point type flags options _ check?) | |
117 | (list device title mount-point type flags options check?)))) | |
118 | ||
661a1d79 LC |
119 | (define %uuid-rx |
120 | ;; The regexp of a UUID. | |
121 | (make-regexp "^([[:xdigit:]]{8})-([[:xdigit:]]{4})-([[:xdigit:]]{4})-([[:xdigit:]]{4})-([[:xdigit:]]{12})$")) | |
122 | ||
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) | |
128 | (lambda (match) | |
129 | (letrec-syntax ((hex->number | |
130 | (syntax-rules () | |
131 | ((_ index) | |
132 | (string->number (match:substring match index) | |
133 | 16)))) | |
134 | (put! | |
135 | (syntax-rules () | |
136 | ((_ bv index (number len) rest ...) | |
137 | (begin | |
138 | (bytevector-uint-set! bv index number | |
139 | (endianness big) len) | |
140 | (put! bv (+ index len) rest ...))) | |
141 | ((_ bv index) | |
142 | bv)))) | |
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))) | |
149 | (put! uuid 0 | |
150 | (time-low 4) (time-mid 2) (time-hi 2) | |
151 | (clock-seq 2) (node 6))))))) | |
152 | ||
153 | (define-syntax uuid | |
154 | (lambda (s) | |
155 | "Return the bytevector corresponding to the given UUID representation." | |
156 | (syntax-case s () | |
157 | ((_ str) | |
158 | (string? (syntax->datum #'str)) | |
159 | ;; A literal string: do the conversion at expansion time. | |
1cab9e81 LC |
160 | (let ((bv (string->uuid (syntax->datum #'str)))) |
161 | (unless bv | |
162 | (syntax-violation 'uuid "invalid UUID" s)) | |
163 | (datum->syntax #'str bv))) | |
661a1d79 LC |
164 | ((_ str) |
165 | #'(string->uuid str))))) | |
166 | ||
167 | \f | |
168 | ;;; | |
169 | ;;; Common file systems. | |
170 | ;;; | |
171 | ||
c5df1839 LC |
172 | (define %fuse-control-file-system |
173 | ;; Control file system for Linux' file systems in user-space (FUSE). | |
174 | (file-system | |
175 | (device "fusectl") | |
176 | (mount-point "/sys/fs/fuse/connections") | |
177 | (type "fusectl") | |
178 | (check? #f))) | |
179 | ||
180 | (define %binary-format-file-system | |
181 | ;; Support for arbitrary executable binary format. | |
182 | (file-system | |
183 | (device "binfmt_misc") | |
184 | (mount-point "/proc/sys/fs/binfmt_misc") | |
185 | (type "binfmt_misc") | |
186 | (check? #f))) | |
187 | ||
7f239fd3 LC |
188 | (define %tty-gid |
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. | |
c8fa3426 | 191 | 996) |
7f239fd3 LC |
192 | |
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.) | |
197 | (file-system | |
198 | (device "none") | |
199 | (mount-point "/dev/pts") | |
200 | (type "devpts") | |
201 | (check? #f) | |
202 | (needed-for-boot? #f) | |
203 | (create-mount-point? #t) | |
204 | (options (string-append "gid=" (number->string %tty-gid) ",mode=620")))) | |
a69576ea | 205 | |
db17ae5c LC |
206 | (define %shared-memory-file-system |
207 | ;; Shared memory. | |
208 | (file-system | |
209 | (device "tmpfs") | |
210 | (mount-point "/dev/shm") | |
211 | (type "tmpfs") | |
212 | (check? #f) | |
213 | (flags '(no-suid no-dev)) | |
214 | (options "size=50%") ;TODO: make size configurable | |
215 | (create-mount-point? #t))) | |
216 | ||
3392ce5d LC |
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 | |
220 | ;; space. | |
221 | (file-system | |
222 | (device (%store-prefix)) | |
223 | (mount-point (%store-prefix)) | |
224 | (type "none") | |
225 | (check? #f) | |
226 | (flags '(read-only bind-mount)))) | |
227 | ||
727636aa | 228 | (define %control-groups |
b78cad85 LC |
229 | (let ((parent (file-system |
230 | (device "cgroup") | |
231 | (mount-point "/sys/fs/cgroup") | |
232 | (type "tmpfs") | |
233 | (check? #f)))) | |
234 | (cons parent | |
235 | (map (lambda (subsystem) | |
236 | (file-system | |
237 | (device "cgroup") | |
238 | (mount-point (string-append "/sys/fs/cgroup/" subsystem)) | |
239 | (type "cgroup") | |
240 | (check? #f) | |
241 | (options subsystem) | |
242 | (create-mount-point? #t) | |
243 | ||
244 | ;; This must be mounted after, and unmounted before the | |
245 | ;; parent directory. | |
246 | (dependencies (list parent)))) | |
247 | '("cpuset" "cpu" "cpuacct" "memory" "devices" "freezer" | |
248 | "blkio" "perf_event" "hugetlb"))))) | |
727636aa | 249 | |
14454f0b MW |
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. | |
253 | (list (file-system | |
254 | (device "none") | |
255 | (mount-point "/run/systemd") | |
256 | (type "tmpfs") | |
257 | (check? #f) | |
258 | (flags '(no-suid no-dev no-exec)) | |
259 | (options "mode=0755") | |
260 | (create-mount-point? #t)) | |
261 | (file-system | |
262 | (device "none") | |
263 | (mount-point "/run/user") | |
264 | (type "tmpfs") | |
265 | (check? #f) | |
266 | (flags '(no-suid no-dev no-exec)) | |
267 | (options "mode=0755") | |
268 | (create-mount-point? #t)))) | |
269 | ||
a69576ea LC |
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. | |
cc0e575a | 273 | (append (list %pseudo-terminal-file-system |
727636aa DT |
274 | %shared-memory-file-system |
275 | %immutable-store) | |
14454f0b | 276 | %elogind-file-systems |
727636aa | 277 | %control-groups)) |
a69576ea | 278 | |
c829bc80 DT |
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. | |
282 | ;; | |
283 | ;; The file system flags and options conform to the libcontainer | |
284 | ;; specification: | |
285 | ;; https://github.com/docker/libcontainer/blob/master/SPEC.md#filesystem | |
286 | (define %container-file-systems | |
287 | (list | |
b57ec5f6 | 288 | ;; Pseudo-terminal file system. |
c829bc80 DT |
289 | (file-system |
290 | (device "none") | |
291 | (mount-point "/dev/pts") | |
292 | (type "devpts") | |
293 | (flags '(no-exec no-suid)) | |
294 | (needed-for-boot? #t) | |
295 | (create-mount-point? #t) | |
296 | (check? #f) | |
297 | (options "newinstance,ptmxmode=0666,mode=620")) | |
298 | ;; Shared memory file system. | |
299 | (file-system | |
300 | (device "tmpfs") | |
301 | (mount-point "/dev/shm") | |
302 | (type "tmpfs") | |
303 | (flags '(no-exec no-suid no-dev)) | |
304 | (options "mode=1777,size=65536k") | |
305 | (needed-for-boot? #t) | |
306 | (create-mount-point? #t) | |
307 | (check? #f)) | |
308 | ;; Message queue file system. | |
309 | (file-system | |
310 | (device "mqueue") | |
311 | (mount-point "/dev/mqueue") | |
312 | (type "mqueue") | |
313 | (flags '(no-exec no-suid no-dev)) | |
314 | (needed-for-boot? #t) | |
315 | (create-mount-point? #t) | |
316 | (check? #f)))) | |
317 | ||
5dae0186 LC |
318 | |
319 | \f | |
320 | ;;; | |
321 | ;;; Mapped devices, for Linux's device-mapper. | |
322 | ;;; | |
323 | ||
324 | (define-record-type* <mapped-device> mapped-device | |
325 | make-mapped-device | |
326 | mapped-device? | |
327 | (source mapped-device-source) ;string | |
328 | (target mapped-device-target) ;string | |
9cb426b8 | 329 | (type mapped-device-type)) ;<mapped-device-kind> |
722554a3 LC |
330 | |
331 | (define-record-type* <mapped-device-type> mapped-device-kind | |
332 | make-mapped-device-kind | |
333 | 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))))) | |
5dae0186 | 337 | |
9110c2e9 DT |
338 | \f |
339 | ;;; | |
340 | ;;; Shared file systems, for VMs/containers. | |
341 | ;;; | |
342 | ||
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 | |
346 | file-system-mapping? | |
347 | (source file-system-mapping-source) ;string | |
348 | (target file-system-mapping-target) ;string | |
349 | (writable? file-system-mapping-writable? ;Boolean | |
350 | (default #f))) | |
351 | ||
352 | (define %store-mapping | |
353 | ;; Mapping of the host's store into the guest. | |
354 | (file-system-mapping | |
355 | (source (%store-prefix)) | |
356 | (target (%store-prefix)) | |
357 | (writable? #f))) | |
358 | ||
c5df1839 | 359 | ;;; file-systems.scm ends here |