Commit | Line | Data |
---|---|---|
63302a4e | 1 | ;;; GNU Guix --- Functional package management for GNU |
9ceeca08 | 2 | ;;; Copyright © 2017, 2018 Ludovic Courtès <ludo@gnu.org> |
63302a4e LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
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. | |
10 | ;;; | |
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. | |
15 | ;;; | |
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/>. | |
18 | ||
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)) | |
26 | ||
27 | ;;; Commentary: | |
28 | ;;; | |
29 | ;;; This module provides extensions to the GNU Shepherd. In particular, it | |
30 | ;;; provides a helper to start services in a container. | |
31 | ;;; | |
32 | ;;; Code: | |
33 | ||
34 | (define (clean-up file) | |
35 | (when file | |
36 | (catch 'system-error | |
37 | (lambda () | |
38 | (delete-file file)) | |
39 | (lambda args | |
40 | (unless (= ENOENT (system-error-errno args)) | |
41 | (apply throw args)))))) | |
42 | ||
43 | (define-syntax-rule (catch-system-error exp) | |
44 | (catch 'system-error | |
45 | (lambda () | |
46 | exp) | |
47 | (const #f))) | |
48 | ||
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))) | |
53 | ||
54 | (define* (default-mounts #:key (namespaces (default-namespaces '()))) | |
55 | (define (tmpfs directory) | |
56 | (file-system | |
57 | (device "none") | |
63302a4e LC |
58 | (mount-point directory) |
59 | (type "tmpfs") | |
60 | (check? #f))) | |
61 | ||
0cb9c9d1 | 62 | (define accounts |
63302a4e LC |
63 | ;; This is for processes in the default user namespace but living in a |
64 | ;; different mount namespace, so that they can lookup users. | |
0cb9c9d1 LC |
65 | (list (file-system-mapping |
66 | (source "/etc/passwd") (target source)) | |
67 | (file-system-mapping | |
68 | (source "/etc/group") (target source)))) | |
63302a4e LC |
69 | |
70 | (define nscd-socket | |
71 | (file-system-mapping | |
72 | (source "/var/run/nscd") (target source) | |
73 | (writable? #t))) | |
74 | ||
75 | (append (cons (tmpfs "/tmp") %container-file-systems) | |
76 | (let ((mappings `(,@(if (memq 'net namespaces) | |
77 | '() | |
78 | (cons nscd-socket | |
79 | %network-file-mappings)) | |
80 | ,@(if (and (memq 'mnt namespaces) | |
81 | (not (memq 'user namespaces))) | |
0cb9c9d1 | 82 | accounts |
63302a4e LC |
83 | '()) |
84 | ,%store-mapping))) ;XXX: coarse-grain | |
85 | (map file-system-mapping->bind-mount | |
86 | (filter (lambda (mapping) | |
87 | (file-exists? (file-system-mapping-source mapping))) | |
88 | mappings))))) | |
89 | ||
90 | ;; XXX: Lazy-bind the Shepherd to avoid a compile-time dependency. | |
91 | (module-autoload! (current-module) | |
92 | '(shepherd service) '(read-pid-file exec-command)) | |
93 | ||
94 | (define* (read-pid-file/container pid pid-file #:key (max-delay 5)) | |
95 | "Read PID-FILE in the container namespaces of PID, which exists in a | |
96 | separate mount and PID name space. Return the \"outer\" PID. " | |
97 | (match (container-excursion* pid | |
98 | (lambda () | |
99 | (read-pid-file pid-file | |
100 | #:max-delay max-delay))) | |
101 | (#f | |
102 | (catch-system-error (kill pid SIGTERM)) | |
103 | #f) | |
104 | ((? integer? container-pid) | |
105 | ;; XXX: When COMMAND is started in a separate PID namespace, its | |
106 | ;; PID is always 1, but that's not what Shepherd needs to know. | |
107 | pid))) | |
108 | ||
109 | (define* (make-forkexec-constructor/container command | |
110 | #:key | |
111 | (namespaces | |
112 | (default-namespaces args)) | |
113 | (mappings '()) | |
114 | (user #f) | |
115 | (group #f) | |
116 | (log-file #f) | |
117 | pid-file | |
118 | (pid-file-timeout 5) | |
119 | (directory "/") | |
120 | (environment-variables | |
121 | (environ)) | |
122 | #:rest args) | |
123 | "This is a variant of 'make-forkexec-constructor' that starts COMMAND in | |
124 | NAMESPACES, a list of Linux namespaces such as '(mnt ipc). MAPPINGS is the | |
125 | list of <file-system-mapping> to make in the case of a separate mount | |
126 | namespace, in addition to essential bind-mounts such /proc." | |
127 | (define container-directory | |
128 | (match command | |
129 | ((program _ ...) | |
130 | (string-append "/var/run/containers/" (basename program))))) | |
131 | ||
132 | (define auto-mappings | |
133 | `(,@(if log-file | |
134 | (list (file-system-mapping | |
135 | (source log-file) | |
136 | (target source) | |
137 | (writable? #t))) | |
138 | '()))) | |
139 | ||
140 | (define mounts | |
141 | (append (map file-system-mapping->bind-mount | |
142 | (append auto-mappings mappings)) | |
143 | (default-mounts #:namespaces namespaces))) | |
144 | ||
145 | (lambda args | |
146 | (mkdir-p container-directory) | |
147 | ||
148 | (when log-file | |
149 | ;; Create LOG-FILE so we can map it in the container. | |
150 | (unless (file-exists? log-file) | |
151 | (call-with-output-file log-file (const #t)))) | |
152 | ||
153 | (let ((pid (run-container container-directory | |
154 | mounts namespaces 1 | |
155 | (lambda () | |
156 | (mkdir-p "/var/run") | |
157 | (clean-up pid-file) | |
158 | (clean-up log-file) | |
159 | ||
160 | (exec-command command | |
161 | #:user user | |
162 | #:group group | |
163 | #:log-file log-file | |
164 | #:directory directory | |
165 | #:environment-variables | |
166 | environment-variables))))) | |
167 | (if pid-file | |
168 | (if (or (memq 'mnt namespaces) (memq 'pid namespaces)) | |
169 | (read-pid-file/container pid pid-file | |
170 | #:max-delay pid-file-timeout) | |
171 | (read-pid-file pid-file #:max-delay pid-file-timeout)) | |
172 | pid)))) | |
173 | ||
174 | ;; Local Variables: | |
175 | ;; eval: (put 'container-excursion* 'scheme-indent-function 1) | |
176 | ;; End: | |
177 | ||
178 | ;;; shepherd.scm ends here |