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