summaryrefslogtreecommitdiff
path: root/gnu/system/linux-container.scm
diff options
context:
space:
mode:
authorArun Isaac <arunisaac@systemreboot.net>2019-05-10 16:56:16 +0530
committerArun Isaac <arunisaac@systemreboot.net>2019-05-14 02:54:58 +0530
commitb33454ae0b488e79faafef75a06090be6b2ac6a2 (patch)
tree61fc01c617f20a25c605aff4bc93cefa116b6a8f /gnu/system/linux-container.scm
parent1bba3e8ca89b42a2828a8553dd8b3613592960cf (diff)
linux-container: Support container network sharing.
* gnu/system/linux-container.scm (container-essential-services): If network is to be shared with the host, remove network configuration files from etc service. (containerized-operating-system): If network is to be shared with the host, remove nscd service and map host's /var/run/nscd if it exists. (container-script): If network is to be shared with the host, do not create network namespace. * guix/scripts/system.scm (system-derivation-for-action): Add #:container-shared-network? argument. (perform-action): Add #:container-shared-network? argument. (show-help): Add "-N, --network" help information. (%options): Add network option. (process-action): Call perform-action with #container-shared-network? argument. * doc/guix.texi (Invoking guix system): Document the "-N, --network" option. Co-authored-by: Christopher Baines <mail@cbaines.net>
Diffstat (limited to 'gnu/system/linux-container.scm')
-rw-r--r--gnu/system/linux-container.scm63
1 files changed, 52 insertions, 11 deletions
diff --git a/gnu/system/linux-container.scm b/gnu/system/linux-container.scm
index ded5f279fed..ce786e39b22 100644
--- a/gnu/system/linux-container.scm
+++ b/gnu/system/linux-container.scm
@@ -1,6 +1,7 @@
1;;; GNU Guix --- Functional package management for GNU 1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2015 David Thompson <davet@gnu.org> 2;;; Copyright © 2015 David Thompson <davet@gnu.org>
3;;; Copyright © 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org> 3;;; Copyright © 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org>
4;;; Copyright © 2019 Arun Isaac <arunisaac@systemreboot.net>
4;;; 5;;;
5;;; This file is part of GNU Guix. 6;;; This file is part of GNU Guix.
6;;; 7;;;
@@ -35,7 +36,7 @@
35 containerized-operating-system 36 containerized-operating-system
36 container-script)) 37 container-script))
37 38
38(define (container-essential-services os) 39(define* (container-essential-services os #:key shared-network?)
39 "Return a list of essential services corresponding to OS, a 40 "Return a list of essential services corresponding to OS, a
40non-containerized OS. This procedure essentially strips essential services 41non-containerized OS. This procedure essentially strips essential services
41from OS that are needed on the bare metal and not in a container." 42from OS that are needed on the bare metal and not in a container."
@@ -51,9 +52,20 @@ from OS that are needed on the bare metal and not in a container."
51 (let ((locale (operating-system-locale-directory os))) 52 (let ((locale (operating-system-locale-directory os)))
52 (with-monad %store-monad 53 (with-monad %store-monad
53 (return `(("locale" ,locale)))))) 54 (return `(("locale" ,locale))))))
54 base)) 55 ;; If network is to be shared with the host, remove network
56 ;; configuration files from etc-service.
57 (if shared-network?
58 (modify-services base
59 (etc-service-type
60 files => (remove
61 (match-lambda
62 ((filename _)
63 (member filename
64 (map basename %network-configuration-files))))
65 files)))
66 base)))
55 67
56(define (containerized-operating-system os mappings) 68(define* (containerized-operating-system os mappings #:key shared-network?)
57 "Return an operating system based on OS for use in a Linux container 69 "Return an operating system based on OS for use in a Linux container
58environment. MAPPINGS is a list of <file-system-mapping> to realize in the 70environment. MAPPINGS is a list of <file-system-mapping> to realize in the
59containerized OS." 71containerized OS."
@@ -76,27 +88,53 @@ containerized OS."
76 (define useless-services 88 (define useless-services
77 ;; Services that make no sense in a container. Those that attempt to 89 ;; 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. 90 ;; access /dev/tty[0-9] in particular cannot work in a container.
79 (list console-font-service-type 91 (append (list console-font-service-type
80 mingetty-service-type 92 mingetty-service-type
81 agetty-service-type)) 93 agetty-service-type)
94 ;; Remove nscd service if network is shared with the host.
95 (if shared-network?
96 (list nscd-service-type)
97 (list))))
98
99 (define shared-network-file-mappings
100 ;; Files to map if network is to be shared with the host
101 (append %network-file-mappings
102 (let ((nscd-run-directory "/var/run/nscd"))
103 (if (file-exists? nscd-run-directory)
104 (list (file-system-mapping
105 (source nscd-run-directory)
106 (target nscd-run-directory)))
107 (list)))))
108
109 ;; (write shared-network-file-mappings)
110 ;; (newline)
82 111
83 (operating-system 112 (operating-system
84 (inherit os) 113 (inherit os)
85 (swap-devices '()) ; disable swap 114 (swap-devices '()) ; disable swap
86 (essential-services (container-essential-services os)) 115 (essential-services (container-essential-services
116 os #:shared-network? shared-network?))
87 (services (remove (lambda (service) 117 (services (remove (lambda (service)
88 (memq (service-kind service) 118 (memq (service-kind service)
89 useless-services)) 119 useless-services))
90 (operating-system-user-services os))) 120 (operating-system-user-services os)))
91 (file-systems (append (map mapping->fs (cons %store-mapping mappings)) 121 (file-systems (append (map mapping->fs
122 (cons %store-mapping
123 (append mappings
124 (if shared-network?
125 shared-network-file-mappings
126 (list)))))
92 %container-file-systems 127 %container-file-systems
93 user-file-systems)))) 128 user-file-systems))))
94 129
95(define* (container-script os #:key (mappings '())) 130(define* (container-script os #:key (mappings '()) shared-network?)
96 "Return a derivation of a script that runs OS as a Linux container. 131 "Return a derivation of a script that runs OS as a Linux container.
97MAPPINGS is a list of <file-system> objects that specify the files/directories 132MAPPINGS is a list of <file-system> objects that specify the files/directories
98that will be shared with the host system." 133that will be shared with the host system."
99 (let* ((os (containerized-operating-system os mappings)) 134 (let* ((os (containerized-operating-system
135 os
136 mappings
137 #:shared-network? shared-network?))
100 (file-systems (filter file-system-needed-for-boot? 138 (file-systems (filter file-system-needed-for-boot?
101 (operating-system-file-systems os))) 139 (operating-system-file-systems os)))
102 (specs (map file-system->spec file-systems))) 140 (specs (map file-system->spec file-systems)))
@@ -121,6 +159,9 @@ that will be shared with the host system."
121 ;; users and groups, which is sufficient for most cases. 159 ;; users and groups, which is sufficient for most cases.
122 ;; 160 ;;
123 ;; See: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html#--private-users= 161 ;; See: http://www.freedesktop.org/software/systemd/man/systemd-nspawn.html#--private-users=
124 #:host-uids 65536)))) 162 #:host-uids 65536
163 #:namespaces (if #$shared-network?
164 (delq 'net %namespaces)
165 %namespaces)))))
125 166
126 (gexp->script "run-container" script))) 167 (gexp->script "run-container" script)))