1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017, 2018, 2019 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 (srfi srfi-1)
24 #:use-module (ice-9 match)
25 #:export (make-forkexec-constructor/container))
29 ;;; This module provides extensions to the GNU Shepherd. In particular, it
30 ;;; provides a helper to start services in a container.
34 (define (clean-up file)
40 (unless (= ENOENT (system-error-errno args))
41 (apply throw args))))))
43 (define-syntax-rule (catch-system-error exp)
49 (define (default-namespaces args)
50 ;; Most daemons are here to talk to the network, and most of them expect to
51 ;; run under a non-zero UID.
52 (fold delq %namespaces '(net user)))
54 (define* (default-mounts #:key (namespaces (default-namespaces '())))
55 (define (tmpfs directory)
58 (mount-point directory)
63 ;; This is for processes in the default user namespace but living in a
64 ;; different mount namespace, so that they can lookup users.
65 (list (file-system-mapping
66 (source "/etc/passwd") (target source))
68 (source "/etc/group") (target source))))
70 (append (cons (tmpfs "/tmp") %container-file-systems)
71 (let ((mappings `(,@(if (memq 'net namespaces)
73 %network-file-mappings)
74 ,@(if (and (memq 'mnt namespaces)
75 (not (memq 'user namespaces)))
79 ;; Tell the process what timezone we're in. This
80 ;; makes sure that, for instance, its syslog
81 ;; messages have the correct timestamp.
83 (source "/etc/localtime")
86 ,%store-mapping))) ;XXX: coarse-grain
87 (map file-system-mapping->bind-mount
88 (filter (lambda (mapping)
89 (file-exists? (file-system-mapping-source mapping)))
92 ;; XXX: Lazy-bind the Shepherd to avoid a compile-time dependency.
93 (module-autoload! (current-module)
94 '(shepherd service) '(read-pid-file exec-command))
96 (define* (read-pid-file/container pid pid-file #:key (max-delay 5))
97 "Read PID-FILE in the container namespaces of PID, which exists in a
98 separate mount and PID name space. Return the \"outer\" PID. "
99 (match (container-excursion* pid
101 (read-pid-file pid-file
102 #:max-delay max-delay)))
104 (catch-system-error (kill pid SIGTERM))
106 ((? integer? container-pid)
107 ;; XXX: When COMMAND is started in a separate PID namespace, its
108 ;; PID is always 1, but that's not what Shepherd needs to know.
111 (define* (make-forkexec-constructor/container command
114 (default-namespaces args))
122 (environment-variables
125 "This is a variant of 'make-forkexec-constructor' that starts COMMAND in
126 NAMESPACES, a list of Linux namespaces such as '(mnt ipc). MAPPINGS is the
127 list of <file-system-mapping> to make in the case of a separate mount
128 namespace, in addition to essential bind-mounts such /proc."
129 (define container-directory
132 (string-append "/var/run/containers/" (basename program)))))
134 (define auto-mappings
136 (list (file-system-mapping
143 (append (map file-system-mapping->bind-mount
144 (append auto-mappings mappings))
145 (default-mounts #:namespaces namespaces)))
148 (mkdir-p container-directory)
151 ;; Create LOG-FILE so we can map it in the container.
152 (unless (file-exists? log-file)
153 (call-with-output-file log-file (const #t))
155 (let ((pw (getpwnam user)))
156 (chown log-file (passwd:uid pw) (passwd:gid pw))))))
158 (let ((pid (run-container container-directory
164 (exec-command command
168 #:directory directory
169 #:environment-variables
170 environment-variables)))))
172 (if (or (memq 'mnt namespaces) (memq 'pid namespaces))
173 (read-pid-file/container pid pid-file
174 #:max-delay pid-file-timeout)
175 (read-pid-file pid-file #:max-delay pid-file-timeout))
179 ;; eval: (put 'container-excursion* 'scheme-indent-function 1)
182 ;;; shepherd.scm ends here