Merge branch 'master' into core-updates
[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 ;;; Copyright © 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (gnu system linux-container)
21 #:use-module (ice-9 match)
22 #:use-module (srfi srfi-1)
23 #:use-module (guix config)
24 #:use-module (guix store)
25 #:use-module (guix gexp)
26 #:use-module (guix derivations)
27 #:use-module (guix monads)
28 #:use-module (guix modules)
29 #:use-module (gnu build linux-container)
30 #:use-module (gnu services)
31 #:use-module (gnu services base)
32 #:use-module (gnu system)
33 #:use-module (gnu system file-systems)
34 #:export (system-container
35 containerized-operating-system
36 container-script))
37
38 (define (container-essential-services os)
39 "Return a list of essential services corresponding to OS, a
40 non-containerized OS. This procedure essentially strips essential services
41 from OS that are needed on the bare metal and not in a container."
42 (define base
43 (remove (lambda (service)
44 (memq (service-kind service)
45 (list (service-kind %linux-bare-metal-service)
46 firmware-service-type
47 system-service-type)))
48 (operating-system-essential-services os)))
49
50 (cons (service system-service-type
51 (let ((locale (operating-system-locale-directory os)))
52 (with-monad %store-monad
53 (return `(("locale" ,locale))))))
54 base))
55
56 (define (containerized-operating-system os mappings)
57 "Return an operating system based on OS for use in a Linux container
58 environment. MAPPINGS is a list of <file-system-mapping> to realize in the
59 containerized OS."
60 (define user-file-systems
61 (remove (lambda (fs)
62 (let ((target (file-system-mount-point fs))
63 (source (file-system-device fs)))
64 (or (string=? target (%store-prefix))
65 (string=? target "/")
66 (and (string? source)
67 (string-prefix? "/dev/" source))
68 (string-prefix? "/dev" target)
69 (string-prefix? "/sys" target))))
70 (operating-system-file-systems os)))
71
72 (define (mapping->fs fs)
73 (file-system (inherit (file-system-mapping->bind-mount fs))
74 (needed-for-boot? #t)))
75
76 (define useless-services
77 ;; Services that make no sense in a container. Those that attempt to
78 ;; access /dev/tty[0-9] in particular cannot work in a container.
79 (list console-font-service-type
80 mingetty-service-type
81 agetty-service-type))
82
83 (operating-system
84 (inherit os)
85 (swap-devices '()) ; disable swap
86 (essential-services (container-essential-services os))
87 (services (remove (lambda (service)
88 (memq (service-kind service)
89 useless-services))
90 (operating-system-user-services os)))
91 (file-systems (append (map mapping->fs (cons %store-mapping mappings))
92 %container-file-systems
93 user-file-systems))))
94
95 (define* (container-script os #:key (mappings '()))
96 "Return a derivation of a script that runs OS as a Linux container.
97 MAPPINGS is a list of <file-system> objects that specify the files/directories
98 that will be shared with the host system."
99 (let* ((os (containerized-operating-system os mappings))
100 (file-systems (filter file-system-needed-for-boot?
101 (operating-system-file-systems os)))
102 (specs (map file-system->spec file-systems)))
103
104 (define script
105 (with-imported-modules (source-module-closure
106 '((guix build utils)
107 (gnu build linux-container)))
108 #~(begin
109 (use-modules (gnu build linux-container)
110 (gnu system file-systems) ;spec->file-system
111 (guix build utils))
112
113 (call-with-container (map spec->file-system '#$specs)
114 (lambda ()
115 (setenv "HOME" "/root")
116 (setenv "TMPDIR" "/tmp")
117 (setenv "GUIX_NEW_SYSTEM" #$os)
118 (for-each mkdir-p '("/run" "/bin" "/etc" "/home" "/var"))
119 (primitive-load (string-append #$os "/boot")))
120 ;; A range of 65536 uid/gids is used to cover 16 bits worth of
121 ;; users and groups, which is sufficient for most cases.
122 ;;
123 ;; See: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html#--private-users=
124 #:host-uids 65536))))
125
126 (gexp->script "run-container" script)))