1// SPDX-License-Identifier: MIT
2
3package guest
4
5import (
6 "context"
7 "errors"
8 "fmt"
9 "io"
10 "net"
11 "net/http"
12 "os"
13 "os/signal"
14 "strings"
15 "syscall"
16 "time"
17
18 "github.com/bnema/zerowrap"
19 "golang.org/x/sys/unix"
20
21 "bnema.dev/ermite-runner/internal/sandbox"
22)
23
24// InitConfig configures Init. The zero value reads the real system; tests
25// replace the fields.
26type InitConfig struct {
27 Version string
28 // Cmdline is the kernel command line; empty reads /proc/cmdline.
29 Cmdline string
30 // Getpid reports the process id; Init only acts as a VM's PID 1 when it is 1.
31 Getpid func() int
32 // Signals delivers the termination signals to forward to the executor;
33 // nil means "subscribe to SIGTERM and SIGINT". Tests inject their own.
34 Signals <-chan os.Signal
35 // SkipDocker does not start dockerd (tests, images without Docker).
36 SkipDocker bool
37 // ExecArgs is the executor command line; default
38 // [GuestExecDir/GuestExecName exec].
39 ExecArgs []string
40 // DockerdArgs replaces the dockerd command line, and the check that
41 // /usr/bin/dockerd exists (tests); SkipDocker still wins.
42 DockerdArgs []string
43 // DockerSocket is the Docker API socket dockerd listens on and init waits
44 // for; default dockerSocket.
45 DockerSocket string
46 // DockerReadyTimeout bounds the wait for the Docker API before the executor
47 // is started anyway; default dockerReadyTimeout.
48 DockerReadyTimeout time.Duration
49}
50
51// Init is `ermite-runner init`: the entrypoint of every sandbox.
52//
53// In a micro-VM it is the kernel's init (PID 1, root = the read-only exec disk,
54// /dev/vdb = shared base image, /dev/vdc = per-job scratch disk): it mounts the
55// pseudo filesystems, sets the clock and the DNS server, builds an overlayfs
56// root (lower = base, upper = scratch) and pivots into it, then does what a
57// container does.
58//
59// In a container the root and the pseudo filesystems are ready: it skips all of
60// that and only prepares cgroups. Then, in both: start dockerd with its data
61// root on the scratch space, wait (bounded) for its API to answer, run the executor
62// (`ermite-runner exec`) as a child, reap orphans, stop dockerd, and finish by
63// powering the VM off, or exiting with the executor's code in a container.
64func Init(ctx context.Context, cfg InitConfig) int {
65 log := zerowrap.FromCtx(ctx)
66 if cfg.Getpid == nil {
67 cfg.Getpid = os.Getpid
68 }
69 if cfg.ExecArgs == nil {
70 cfg.ExecArgs = []string{sandbox.GuestExecDir + "/" + sandbox.GuestExecName, "exec"}
71 }
72 cmdline := cfg.Cmdline
73 if cmdline == "" {
74 b, err := os.ReadFile("/proc/cmdline")
75 if err != nil && cfg.Getpid() == 1 {
76 // A VM's init starts with nothing mounted: /proc is needed to read
77 // the kernel command line that says what we are. setupVM reuses it.
78 if merr := unix.Mount("proc", "/proc", "proc", 0, ""); merr == nil {
79 b, err = os.ReadFile("/proc/cmdline")
80 }
81 }
82 if err == nil {
83 cmdline = string(b)
84 }
85 }
86 args := ParseCmdline(cmdline)
87 inVM := args[sandbox.CmdlineSandbox] == string(sandbox.VM) && cfg.Getpid() == 1
88
89 mode := string(sandbox.Container)
90 if inVM {
91 mode = string(sandbox.VM)
92 if err := setupVM(log, args); err != nil {
93 log.Error().Err(err).Msg("init: vm setup failed")
94 powerOff()
95 return 1
96 }
97 }
98 code := runPayload(ctx, cfg, mode, args)
99 if inVM {
100 powerOff()
101 }
102 return code
103}
104
105// defaultPath is the PATH of a sandbox whose init was started by the kernel
106// with an empty environment.
107const defaultPath = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
108
109func runPayload(ctx context.Context, cfg InitConfig, mode string, args map[string]string) int {
110 log := zerowrap.FromCtx(ctx)
111 if os.Getenv("PATH") == "" {
112 _ = os.Setenv("PATH", defaultPath)
113 }
114 if os.Getenv("HOME") == "" {
115 _ = os.Setenv("HOME", "/root")
116 }
117 if mode == string(sandbox.Container) {
118 if err := nestCgroups(); err != nil {
119 log.Warn().Err(err).Msg("init: cgroup setup (docker inside may not start)")
120 }
121 }
122 target := args[sandbox.CmdlineConnect]
123 if target == "" {
124 target = os.Getenv("ERMITE_CONNECT")
125 }
126 if _, err := ParseTarget(target); err != nil {
127 log.Error().Err(err).Msg("init: no executor target")
128 return 1
129 }
130
131 var dockerPid int
132 _, statErr := os.Stat("/usr/bin/dockerd")
133 if !cfg.SkipDocker && (statErr == nil || cfg.DockerdArgs != nil) {
134 _ = os.MkdirAll(sandbox.GuestScratch+"/docker", 0o711)
135 argv := cfg.DockerdArgs
136 if argv == nil {
137 argv = []string{"/usr/bin/dockerd", "--data-root", sandbox.GuestScratch + "/docker",
138 "--storage-driver", "overlay2", "--host", "unix:///var/run/docker.sock"}
139 }
140 pid, err := spawn(argv, nil)
141 if err != nil {
142 log.Error().Err(err).Msg("init: start dockerd")
143 } else {
144 dockerPid = pid
145 }
146 }
147
148 // SIGTERM/SIGINT (docker stop, or the host stopping the VM) go to the
149 // executor, which cancels its script with the usual escalation and reports
150 // its Exit; init then finishes normally instead of dying with children.
151 sigs := cfg.Signals
152 if sigs == nil {
153 ch := make(chan os.Signal, 4)
154 signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT)
155 defer signal.Stop(ch)
156 sigs = ch
157 }
158
159 // The first job-container step needs the Docker API, so the executor must
160 // not start before dockerd answers. If it never does (or dies), start the
161 // executor anyway: jobs that do not use Docker still run. A shutdown signal
162 // during the wait aborts the boot instead: see abortBootError.
163 if dockerPid != 0 {
164 sock := cfg.DockerSocket
165 if sock == "" {
166 sock = dockerSocket
167 }
168 timeout := cfg.DockerReadyTimeout
169 if timeout <= 0 {
170 timeout = dockerReadyTimeout
171 }
172 err := waitDockerReady(ctx, log, sock, dockerPid, timeout, sigs)
173 var exited *dockerdExitError
174 var abort *abortBootError
175 switch {
176 case errors.As(err, &abort):
177 stopDockerd(dockerPid)
178 return abort.code
179 case errors.As(err, &exited):
180 dockerPid = 0 // already reaped
181 }
182 }
183
184 execEnv := append(os.Environ(), "ERMITE_CONNECT="+target, "ERMITE_SANDBOX="+mode, "ERMITE_VERSION="+cfg.Version)
185 execPid, err := spawn(cfg.ExecArgs, execEnv)
186 if err != nil {
187 log.Error().Err(err).Msg("init: start executor")
188 stopDockerd(dockerPid)
189 return 1
190 }
191 forward := func(sig os.Signal) {
192 if s, ok := sig.(syscall.Signal); ok {
193 log.Info().Str("signal", s.String()).Msg("init: forwarding the signal to the executor")
194 _ = syscall.Kill(-execPid, s) // the executor leads its own process group
195 _ = syscall.Kill(execPid, s)
196 }
197 }
198 stopFwd := make(chan struct{})
199 defer close(stopFwd)
200 go func() {
201 for {
202 select {
203 case sig := <-sigs:
204 forward(sig)
205 case <-stopFwd:
206 return
207 }
208 }
209 }()
210 code := reapUntil(execPid)
211 stopDockerd(dockerPid)
212 return code
213}
214
215// stopDockerd stops dockerd (SIGTERM, then SIGKILL after 5 s) and reaps it (at
216// most 5 s more after the SIGKILL); a zero pid is a no-op.
217func stopDockerd(pid int) {
218 if pid == 0 {
219 return
220 }
221 _ = syscall.Kill(pid, syscall.SIGTERM)
222 deadline := time.Now().Add(5 * time.Second)
223 for time.Now().Before(deadline) {
224 if got, _ := wait4NoHang(pid); got == pid {
225 return
226 }
227 time.Sleep(100 * time.Millisecond)
228 }
229 _ = syscall.Kill(pid, syscall.SIGKILL)
230 // SIGKILL cannot be ignored, so the exit is prompt; poll rather than block
231 // for ever on a process stuck in the kernel (uninterruptible sleep).
232 for end := time.Now().Add(5 * time.Second); time.Now().Before(end); time.Sleep(10 * time.Millisecond) {
233 got, err := wait4NoHang(pid)
234 if got == pid || (err != nil && err != syscall.EINTR) { // ECHILD: already reaped
235 return
236 }
237 }
238}
239
240const (
241 // dockerSocket is the Docker API socket of the nested dockerd.
242 dockerSocket = "/var/run/docker.sock"
243 // dockerReadyTimeout bounds the wait for the Docker API at boot.
244 dockerReadyTimeout = 60 * time.Second
245 dockerPollInterval = 100 * time.Millisecond
246 dockerPingTimeout = 2 * time.Second
247)
248
249// dockerdExitError reports that dockerd exited (and was reaped) before its API
250// answered.
251type dockerdExitError struct{ status syscall.WaitStatus }
252
253func (e *dockerdExitError) Error() string {
254 if e.status.Signaled() {
255 return "dockerd killed by signal " + e.status.Signal().String()
256 }
257 return fmt.Sprintf("dockerd exited with code %d", e.status.ExitStatus())
258}
259
260// abortBootError means a shutdown signal (or the cancelled context) arrived
261// while waiting for the Docker API: init must not start the executor, stop
262// dockerd and exit with code (128+signal, as for a killed executor).
263//
264// Not starting the executor is safe for the host: the sandbox ends before the
265// executor connected ("container exited ... before the executor connected" /
266// "firecracker exited before the executor connected"), and the host reports the
267// job cancelled (not failed) when a cancel or shutdown was requested
268// (jobRun.execute: startSandbox error with cancelRequested). A stop that the
269// host did not ask for is reported as "the sandbox did not start".
270type abortBootError struct{ code int }
271
272func (e *abortBootError) Error() string {
273 return fmt.Sprintf("boot aborted by a shutdown signal (exit %d)", e.code)
274}
275
276// reapChildren reaps every child that has exited (orphans adopted by init
277// included) without blocking, and reports whether pid was among them, with its
278// wait status.
279func reapChildren(pid int) (bool, syscall.WaitStatus) {
280 var found bool
281 var status syscall.WaitStatus
282 for {
283 var ws syscall.WaitStatus
284 got, err := syscall.Wait4(-1, &ws, syscall.WNOHANG, nil)
285 if err == syscall.EINTR {
286 continue
287 }
288 if err != nil || got <= 0 {
289 return found, status
290 }
291 if got == pid {
292 found, status = true, ws
293 }
294 }
295}
296
297// waitDockerReady blocks until the Docker API on sock answers GET /_ping with
298// "OK", for at most timeout. It returns nil when ready, and otherwise:
299// - *dockerdExitError: dockerd (pid, a child of init) exited and was reaped;
300// - a plain error: the deadline passed;
301// - *abortBootError: a signal arrived on sigs or ctx was cancelled.
302//
303// The orphans that init adopts are reaped on every tick, and a ping in flight
304// never delays the deadline, the signals or the exit check. Every outcome is
305// logged; for the first two the caller starts the executor anyway.
306func waitDockerReady(ctx context.Context, log zerowrap.Logger, sock string, pid int, timeout time.Duration, sigs <-chan os.Signal) error {
307 start := time.Now()
308 log.Info().Str("socket", sock).Dur("timeout", timeout).Msg("init: waiting for the Docker API")
309 wctx, cancel := context.WithTimeout(ctx, timeout)
310 defer cancel() // also aborts a ping in flight
311 client := &http.Client{Transport: &http.Transport{
312 DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
313 var d net.Dialer
314 return d.DialContext(ctx, "unix", sock)
315 },
316 DisableKeepAlives: true,
317 }}
318 defer client.CloseIdleConnections()
319 ticker := time.NewTicker(dockerPollInterval)
320 defer ticker.Stop()
321
322 var pinging <-chan bool // result of the ping in flight, nil if none
323 startPing := func() {
324 ch := make(chan bool, 1)
325 pinging = ch
326 go func() { ch <- dockerPing(wctx, client) }()
327 }
328 checkExit := func() error {
329 if ok, ws := reapChildren(pid); ok {
330 err := &dockerdExitError{status: ws}
331 log.Warn().Err(err).Msg("init: dockerd died before its API was ready; starting the executor anyway (jobs using Docker will fail)")
332 return err
333 }
334 return nil
335 }
336 if err := checkExit(); err != nil {
337 return err
338 }
339 startPing()
340 for {
341 select {
342 case ok := <-pinging:
343 pinging = nil
344 if ok {
345 log.Info().Dur("after", time.Since(start).Round(time.Millisecond)).Msg("init: the Docker API is ready")
346 return nil
347 }
348 case <-ticker.C:
349 if err := checkExit(); err != nil {
350 return err
351 }
352 if pinging == nil {
353 startPing()
354 }
355 case sig := <-sigs:
356 return abortBoot(log, sig)
357 case <-wctx.Done():
358 if ctx.Err() != nil {
359 return abortBoot(log, syscall.SIGTERM)
360 }
361 log.Warn().Dur("timeout", timeout).Msg("init: the Docker API did not answer in time; starting the executor anyway (jobs using Docker will fail)")
362 return fmt.Errorf("docker API not ready after %s", timeout)
363 }
364 }
365}
366
367func abortBoot(log zerowrap.Logger, sig os.Signal) error {
368 code := 128 + int(syscall.SIGTERM)
369 if s, ok := sig.(syscall.Signal); ok {
370 code = 128 + int(s)
371 }
372 log.Warn().Str("signal", fmt.Sprint(sig)).Msg("init: shutdown signal while waiting for the Docker API; not starting the executor, stopping dockerd")
373 return &abortBootError{code: code}
374}
375
376// dockerPing reports whether GET /_ping answers 200 "OK".
377func dockerPing(ctx context.Context, client *http.Client) bool {
378 // One ping is bounded on its own so that a daemon that accepts and stalls
379 // does not hold the others back; ctx (the readiness deadline) bounds it too.
380 ctx, cancel := context.WithTimeout(ctx, dockerPingTimeout)
381 defer cancel()
382 req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://docker/_ping", nil)
383 if err != nil {
384 return false
385 }
386 resp, err := client.Do(req)
387 if err != nil {
388 return false
389 }
390 defer func() { _ = resp.Body.Close() }()
391 body, _ := io.ReadAll(io.LimitReader(resp.Body, 16))
392 return resp.StatusCode == http.StatusOK && strings.TrimSpace(string(body)) == "OK"
393}
394
395// spawn starts argv[0] as a child (own process group) without os/exec, so that
396// reapUntil is the only waiter and orphans can be reaped safely.
397func spawn(argv, env []string) (int, error) {
398 if env == nil {
399 env = os.Environ()
400 }
401 return syscall.ForkExec(argv[0], argv, &syscall.ProcAttr{
402 Env: env,
403 Files: []uintptr{0, 1, 2},
404 Sys: &syscall.SysProcAttr{Setpgid: true},
405 })
406}
407
408// reapUntil waits for every child (orphans included) and returns when pid
409// exits, with its exit code (128+signal when killed).
410func reapUntil(pid int) int {
411 for {
412 var ws syscall.WaitStatus
413 got, err := syscall.Wait4(-1, &ws, 0, nil)
414 if err == syscall.EINTR {
415 continue
416 }
417 if err != nil { // ECHILD: no child left, the executor included
418 return 1
419 }
420 if got == pid {
421 switch {
422 case ws.Exited():
423 return ws.ExitStatus()
424 case ws.Signaled():
425 return 128 + int(ws.Signal())
426 }
427 return 1
428 }
429 }
430}
431
432func wait4NoHang(pid int) (int, error) {
433 var ws syscall.WaitStatus
434 return syscall.Wait4(pid, &ws, syscall.WNOHANG, nil)
435}
436
437func powerOff() {
438 unix.Sync()
439 // On x86 Firecracker has no ACPI power off: a reboot makes the VMM exit
440 // (kernel cmdline reboot=k). On arm64 PSCI handles power off.
441 _ = unix.Reboot(unix.LINUX_REBOOT_CMD_RESTART)
442 _ = unix.Reboot(unix.LINUX_REBOOT_CMD_POWER_OFF)
443}
444
445// nestCgroups prepares cgroup v2 for a nested dockerd in a container: a cgroup
446// with member processes cannot delegate controllers to children, so move every
447// process to an "init" child and enable the controllers (like the upstream
448// docker-in-docker entrypoint). No-op without cgroup v2.
449func nestCgroups() error {
450 const root = "/sys/fs/cgroup"
451 ctl, err := os.ReadFile(root + "/cgroup.controllers")
452 if err != nil {
453 return nil
454 }
455 if err := os.MkdirAll(root+"/init", 0o755); err != nil {
456 return err
457 }
458 procs, err := os.ReadFile(root + "/cgroup.procs")
459 if err != nil {
460 return err
461 }
462 for _, p := range strings.Fields(string(procs)) {
463 // Processes may exit meanwhile: ignore per-process errors.
464 _ = os.WriteFile(root+"/init/cgroup.procs", []byte(p), 0o644)
465 }
466 var enable []string
467 for _, c := range strings.Fields(string(ctl)) {
468 enable = append(enable, "+"+c)
469 }
470 if len(enable) == 0 {
471 return nil
472 }
473 if err := os.WriteFile(root+"/cgroup.subtree_control", []byte(strings.Join(enable, " ")), 0o644); err != nil {
474 return fmt.Errorf("enable controllers: %w", err)
475 }
476 return nil
477}