1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (gnu build shepherd)
20 #:use-module (gnu system file-systems)
21 #:use-module (gnu build linux-container)
22 #:use-module (guix build utils)
23 #:use-module (guix utils)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-26)
26 #:use-module (ice-9 match)
27 #:export (make-forkexec-constructor/container
28 fork+exec-command/container))
32 ;;; This module provides extensions to the GNU Shepherd. In particular, it
33 ;;; provides a helper to start services in a container.
37 (define (clean-up file)
43 (unless (= ENOENT (system-error-errno args))
44 (apply throw args))))))
46 (define-syntax-rule (catch-system-error exp)
52 (define (default-namespaces args)
53 ;; Most daemons are here to talk to the network, and most of them expect to
54 ;; run under a non-zero UID.
55 (fold delq %namespaces '(net user)))
57 (define* (default-mounts #:key (namespaces (default-namespaces '())))
58 (define (tmpfs directory)
61 (mount-point directory)
66 ;; This is for processes in the default user namespace but living in a
67 ;; different mount namespace, so that they can lookup users.
68 (list (file-system-mapping
69 (source "/etc/passwd") (target source))
71 (source "/etc/group") (target source))))
73 (append (cons (tmpfs "/tmp") %container-file-systems)
74 (let ((mappings `(,@(if (memq 'net namespaces)
76 %network-file-mappings)
77 ,@(if (and (memq 'mnt namespaces)
78 (not (memq 'user namespaces)))
82 ;; Tell the process what timezone we're in. This
83 ;; makes sure that, for instance, its syslog
84 ;; messages have the correct timestamp.
86 (source "/etc/localtime")
89 ,%store-mapping))) ;XXX: coarse-grain
90 (map file-system-mapping->bind-mount
91 (filter (lambda (mapping)
92 (file-exists? (file-system-mapping-source mapping)))
95 ;; XXX: Lazy-bind the Shepherd to avoid a compile-time dependency.
96 (module-autoload! (current-module)
98 '(fork+exec-command read-pid-file exec-command
100 (module-autoload! (current-module)
101 '(shepherd system) '(unblock-signals))
103 (define* (read-pid-file/container pid pid-file #:key (max-delay 5))
104 "Read PID-FILE in the container namespaces of PID, which exists in a
105 separate mount and PID name space. Return the \"outer\" PID. "
106 (match (container-excursion* pid
108 (read-pid-file pid-file
109 #:max-delay max-delay)))
111 ;; Send SIGTERM to the whole process group.
112 (catch-system-error (kill (- pid) SIGTERM))
114 ((? integer? container-pid)
115 ;; XXX: When COMMAND is started in a separate PID namespace, its
116 ;; PID is always 1, but that's not what Shepherd needs to know.
119 (define* (make-forkexec-constructor/container command
122 (default-namespaces args))
130 (environment-variables
133 "This is a variant of 'make-forkexec-constructor' that starts COMMAND in
134 NAMESPACES, a list of Linux namespaces such as '(mnt ipc). MAPPINGS is the
135 list of <file-system-mapping> to make in the case of a separate mount
136 namespace, in addition to essential bind-mounts such /proc."
137 (define container-directory
140 (string-append "/var/run/containers/" (basename program)))))
142 (define auto-mappings
144 (list (file-system-mapping
151 (append (map file-system-mapping->bind-mount
152 (append auto-mappings mappings))
153 (default-mounts #:namespaces namespaces)))
156 (mkdir-p container-directory)
159 ;; Create LOG-FILE so we can map it in the container.
160 (unless (file-exists? log-file)
161 (call-with-output-file log-file (const #t))
163 (let ((pw (getpwnam user)))
164 (chown log-file (passwd:uid pw) (passwd:gid pw))))))
166 (let ((pid (run-container container-directory
169 ;; First restore the default handlers.
170 (for-each (cut sigaction <> SIG_DFL)
173 ;; Unblock any signals that have been blocked
174 ;; by the parent process.
175 (unblock-signals %precious-signals)
180 (exec-command command
184 #:directory directory
185 #:environment-variables
186 environment-variables)))))
188 (if (or (memq 'mnt namespaces) (memq 'pid namespaces))
189 (read-pid-file/container pid pid-file
190 #:max-delay pid-file-timeout)
191 (read-pid-file pid-file #:max-delay pid-file-timeout))
194 (define* (fork+exec-command/container command
198 "This is a variant of 'fork+exec-command' procedure, that joins the
199 namespaces of process PID beforehand."
200 (container-excursion* pid
202 (apply fork+exec-command command
203 (strip-keyword-arguments '(#:pid) args)))))
206 ;; eval: (put 'container-excursion* 'scheme-indent-function 1)
209 ;;; shepherd.scm ends here