37a053cdc383eb38ba722ffefc30b8265d64a6aa
[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 services shepherd)
33 #:use-module (gnu system)
34 #:use-module (gnu system file-systems)
35 #:export (system-container
36 containerized-operating-system
37 container-script))
38
39 (define (container-essential-services os)
40 "Return a list of essential services corresponding to OS, a
41 non-containerized OS. This procedure essentially strips essential services
42 from OS that are needed on the bare metal and not in a container."
43 (define base
44 (remove (lambda (service)
45 (memq (service-kind service)
46 (list (service-kind %linux-bare-metal-service)
47 firmware-service-type
48 system-service-type)))
49 (operating-system-essential-services os)))
50
51 (cons (service system-service-type
52 (let ((locale (operating-system-locale-directory os)))
53 (with-monad %store-monad
54 (return `(("locale" ,locale))))))
55 (append base (list %containerized-shepherd-service))))
56
57 (define (containerized-operating-system os mappings)
58 "Return an operating system based on OS for use in a Linux container
59 environment. MAPPINGS is a list of <file-system-mapping> to realize in the
60 containerized OS."
61 (define user-file-systems
62 (remove (lambda (fs)
63 (let ((target (file-system-mount-point fs))
64 (source (file-system-device fs)))
65 (or (string=? target (%store-prefix))
66 (string=? target "/")
67 (and (string? source)
68 (string-prefix? "/dev/" source))
69 (string-prefix? "/dev" target)
70 (string-prefix? "/sys" target))))
71 (operating-system-file-systems os)))
72
73 (define (mapping->fs fs)
74 (file-system (inherit (file-system-mapping->bind-mount fs))
75 (needed-for-boot? #t)))
76
77 (define useless-services
78 ;; Services that make no sense in a container. Those that attempt to
79 ;; access /dev/tty[0-9] in particular cannot work in a container.
80 (list console-font-service-type
81 mingetty-service-type
82 agetty-service-type))
83
84 (operating-system
85 (inherit os)
86 (swap-devices '()) ; disable swap
87 (essential-services (container-essential-services os))
88 (services (remove (lambda (service)
89 (memq (service-kind service)
90 useless-services))
91 (operating-system-user-services os)))
92 (file-systems (append (map mapping->fs (cons %store-mapping mappings))
93 %container-file-systems
94 user-file-systems))))
95
96 (define* (container-script os #:key (mappings '()))
97 "Return a derivation of a script that runs OS as a Linux container.
98 MAPPINGS is a list of <file-system> objects that specify the files/directories
99 that will be shared with the host system."
100 (let* ((os (containerized-operating-system os mappings))
101 (file-systems (filter file-system-needed-for-boot?
102 (operating-system-file-systems os)))
103 (specs (map file-system->spec file-systems)))
104
105 (define script
106 (with-imported-modules (source-module-closure
107 '((guix build utils)
108 (gnu build linux-container)))
109 #~(begin
110 (use-modules (gnu build linux-container)
111 (gnu system file-systems) ;spec->file-system
112 (guix build utils))
113
114 (call-with-container (map spec->file-system '#$specs)
115 (lambda ()
116 (setenv "HOME" "/root")
117 (setenv "TMPDIR" "/tmp")
118 (setenv "GUIX_NEW_SYSTEM" #$os)
119 (for-each mkdir-p '("/run" "/bin" "/etc" "/home" "/var"))
120 (primitive-load (string-append #$os "/boot")))
121 ;; A range of 65536 uid/gids is used to cover 16 bits worth of
122 ;; users and groups, which is sufficient for most cases.
123 ;;
124 ;; See: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html#--private-users=
125 #:host-uids 65536))))
126
127 (gexp->script "run-container" script)))