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