Merge branch 'master' into dbus-update
[jackhill/guix/guix.git] / gnu / system / linux-container.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
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 linux-container)
20 #:use-module (ice-9 match)
21 #:use-module (srfi srfi-1)
22 #:use-module (guix config)
23 #:use-module (guix store)
24 #:use-module (guix gexp)
25 #:use-module (guix derivations)
26 #:use-module (guix monads)
27 #:use-module (gnu build linux-container)
28 #:use-module (gnu system)
29 #:use-module (gnu system file-systems)
30 #:export (mapping->file-system
31 system-container
32 containerized-operating-system
33 container-script))
34
35 (define (mapping->file-system mapping)
36 "Return a file system that realizes MAPPING."
37 (match mapping
38 (($ <file-system-mapping> source target writable?)
39 (file-system
40 (mount-point target)
41 (device source)
42 (type "none")
43 (flags (if writable?
44 '(bind-mount)
45 '(bind-mount read-only)))
46 (check? #f)
47 (create-mount-point? #t)))))
48
49 (define (system-container os)
50 "Return a derivation that builds OS as a Linux container."
51 (mlet* %store-monad
52 ((profile (operating-system-profile os))
53 (etc (operating-system-etc-directory os))
54 (boot (operating-system-boot-script os #:container? #t))
55 (locale (operating-system-locale-directory os)))
56 (file-union "system-container"
57 `(("boot" ,#~#$boot)
58 ("profile" ,#~#$profile)
59 ("locale" ,#~#$locale)
60 ("etc" ,#~#$etc)))))
61
62 (define (containerized-operating-system os mappings)
63 "Return an operating system based on OS for use in a Linux container
64 environment. MAPPINGS is a list of <file-system-mapping> to realize in the
65 containerized OS."
66 (define user-file-systems
67 (remove (lambda (fs)
68 (let ((target (file-system-mount-point fs))
69 (source (file-system-device fs)))
70 (or (string=? target (%store-prefix))
71 (string=? target "/")
72 (string-prefix? "/dev/" source)
73 (string-prefix? "/dev" target)
74 (string-prefix? "/sys" target))))
75 (operating-system-file-systems os)))
76
77 (define (mapping->fs fs)
78 (file-system (inherit (mapping->file-system fs))
79 (needed-for-boot? #t)))
80
81 (operating-system (inherit os)
82 (swap-devices '()) ; disable swap
83 (file-systems (append (map mapping->fs (cons %store-mapping mappings))
84 %container-file-systems
85 user-file-systems))))
86
87 (define* (container-script os #:key (mappings '()))
88 "Return a derivation of a script that runs OS as a Linux container.
89 MAPPINGS is a list of <file-system> objects that specify the files/directories
90 that will be shared with the host system."
91 (let* ((os (containerized-operating-system os mappings))
92 (file-systems (filter file-system-needed-for-boot?
93 (operating-system-file-systems os)))
94 (specs (map file-system->spec file-systems)))
95
96 (mlet* %store-monad ((os-drv (system-container os)))
97
98 (define script
99 #~(begin
100 (use-modules (gnu build linux-container)
101 (guix build utils))
102
103 (call-with-container '#$specs
104 (lambda ()
105 (setenv "HOME" "/root")
106 (setenv "TMPDIR" "/tmp")
107 (setenv "GUIX_NEW_SYSTEM" #$os-drv)
108 (for-each mkdir-p '("/run" "/bin" "/etc" "/home" "/var"))
109 (primitive-load (string-append #$os-drv "/boot"))))))
110
111 (gexp->script "run-container" script
112 #:modules '((ice-9 match)
113 (srfi srfi-98)
114 (guix config)
115 (guix utils)
116 (guix build utils)
117 (guix build syscalls)
118 (gnu build file-systems)
119 (gnu build linux-container))))))