-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.go
414 lines (355 loc) · 12.3 KB
/
start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package main
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
eventsapi "github.com/containerd/containerd/api/events"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/namespaces"
taskapi "github.com/containerd/containerd/runtime/v2/task"
"github.com/coreos/go-systemd/unit"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sys/unix"
)
// Start the primary user process inside the container
func (s *Service) Start(ctx context.Context, r *taskapi.StartRequest) (_ *taskapi.StartResponse, retErr error) {
ns, err := namespaces.NamespaceRequired(ctx)
if err != nil {
return nil, errdefs.ToGRPC(err)
}
ctx, span := StartSpan(ctx, "service.Start", trace.WithAttributes(attribute.String(nsAttr, ns), attribute.String(cIDAttr, r.ID), attribute.String(eIDAttr, r.ExecID)))
defer func() {
if retErr != nil {
retErr = errdefs.ToGRPCf(retErr, "start")
span.SetStatus(codes.Error, retErr.Error())
}
span.End()
}()
ctx = log.WithLogger(ctx, log.G(ctx).WithField("id", r.ID).WithField("ns", ns).WithField("execID", r.ExecID))
p := s.processes.Get(path.Join(ns, r.ID))
if p == nil {
return nil, fmt.Errorf("%w: %s", errdefs.ErrNotFound, r.ID)
}
ctx = WithShimLog(ctx, p.LogWriter())
var pid uint32
if r.ExecID != "" {
ep := p.(*initProcess).execs.Get(r.ExecID)
if ep == nil {
return nil, fmt.Errorf("exec %s: %w", r.ExecID, errdefs.ErrNotFound)
}
pid, err = ep.Start(ctx)
if err != nil {
s.units.Delete(ep)
return nil, err
}
s.send(ctx, ns, &eventsapi.TaskExecStarted{
ContainerID: r.ID,
ExecID: r.ExecID,
Pid: pid,
})
} else {
pid, err = p.Start(ctx)
if err != nil {
return nil, err
}
s.send(ctx, ns, &eventsapi.TaskStart{
ContainerID: r.ID,
Pid: pid,
})
}
return &taskapi.StartResponse{Pid: pid}, nil
}
func (p *process) runcCmd(cmd []string) ([]string, error) {
root := []string{p.runc.Command, "--debug=" + strconv.FormatBool(p.runc.Debug), "--systemd-cgroup=" + strconv.FormatBool(p.opts.SystemdCgroup), "--root", p.runc.Root}
if p.runc.Debug {
root = append(root, "--log="+p.runc.Log)
}
return append(root, cmd...), nil
}
func writeUnit(name string, opts []*unit.UnitOption) error {
rdr := unit.Serialize(opts)
f, err := os.Create(filepath.Join("/run/systemd/system", name))
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, rdr); err != nil {
return err
}
return nil
}
func (p *initProcess) startOptions(rcmd []string) ([]*unit.UnitOption, error) {
const svc = "Service"
sysctl, err := exec.LookPath("systemctl")
if err != nil {
return nil, err
}
opts := []*unit.UnitOption{
unit.NewUnitOption(svc, "Type", p.unitType()),
unit.NewUnitOption(svc, "RemainAfterExit", "no"),
unit.NewUnitOption(svc, "PIDFile", p.pidFile()),
unit.NewUnitOption(svc, "Delegate", "yes"),
unit.NewUnitOption(svc, "ExecStopPost", "-"+p.exe+" --bundle="+p.Bundle+" exit "+os.Getenv("UNIT_NAME")),
// Set this as env vars here because we only want these fifos to be used for the container stdio, not the other commands we run.
// Otherwise we can run into interesting cases like the client has closeed the fifo and our Pre/Post commands hang
// We already had to open these fifos in process to prevent such hangs with `ExecStart`, now instead it'll open them just before
// executing runc.
unit.NewUnitOption(svc, "Environment", "STDIN_FIFO="+p.Stdin),
unit.NewUnitOption(svc, "Environment", "STDOUT_FIFO="+p.Stdout),
unit.NewUnitOption(svc, "Environment", "STDERR_FIFO="+p.Stderr),
unit.NewUnitOption(svc, "Environment", "DAEMON_UNIT_NAME="+os.Getenv("UNIT_NAME")),
unit.NewUnitOption(svc, "Environment", "UNIT_NAME=%n"), // %n is replaced with the unit name by systemd
unit.NewUnitOption(svc, "Environment", "EXIT_STATE_PATH="+p.exitStatePath()),
}
if p.shimCgroup != "" {
opts = append(opts, unit.NewUnitOption(svc, "Environment", "SHIM_CGROUP="+p.shimCgroup))
}
prefix := []string{p.exe, "--debug=" + strconv.FormatBool(p.runc.Debug), "--bundle=" + p.Bundle, "create"}
if len(p.Rootfs) > 0 {
if p.noNewNamespace {
opts = append(opts, unit.NewUnitOption(svc, "ExecStartPre", p.exe+" mount "+p.mountConfigPath()))
opts = append(opts, unit.NewUnitOption(svc, "ExecStopPost", "-"+p.exe+" unmount "+filepath.Join(p.Bundle, "rootfs")))
} else {
// Unfortunately with PrivateMounts we can't use `ExecStartPre` to mount the rootfs b/c it does not share a mount namespace
// with the main process. Instead we re-exec with `create` subcommand which will mount and exec the main process.
opts = append(opts, unit.NewUnitOption(svc, "PrivateMounts", "yes"))
prefix = append(prefix, "--mounts="+p.mountConfigPath())
}
}
if p.Terminal || p.opts.Terminal {
opts = append(opts, unit.NewUnitOption("Service", "ExecStopPost", "-"+sysctl+" stop "+p.ttyUnitName()))
prefix = append(prefix, "--tty")
}
execStart, err := p.runcCmd(append(rcmd, p.id))
if err != nil {
return nil, err
}
opts = append(opts, unit.NewUnitOption(svc, "ExecStart", strings.Join(append(prefix, execStart...), " ")))
return opts, nil
}
func (p *execProcess) startOptions() ([]*unit.UnitOption, error) {
const svc = "Service"
sysctl, err := exec.LookPath("systemctl")
if err != nil {
return nil, err
}
opts := []*unit.UnitOption{
unit.NewUnitOption(svc, "Type", "notify"),
unit.NewUnitOption(svc, "PIDFile", p.pidFile()),
unit.NewUnitOption(svc, "GuessMainPID", "yes"),
unit.NewUnitOption(svc, "Delegate", "yes"),
unit.NewUnitOption(svc, "RemainAfterExit", "no"),
unit.NewUnitOption(svc, "ExecStopPost", "-"+p.exe+" --debug="+strconv.FormatBool(p.runc.Debug)+" --id="+p.id+" --bundle="+p.parent.Bundle+" exit"),
// Set this as env vars here because we only want these fifos to be used for the container stdio, not the other commands we run.
// Otherwise we can run into interesting cases like the client has closeed the fifo and our Pre/Post commands hang
// We already had to open these fifos in process to prevent such hangs with `ExecStart`, now instead it'll open them just before
// executing runc.
unit.NewUnitOption(svc, "Environment", "STDIN_FIFO="+p.Stdin),
unit.NewUnitOption(svc, "Environment", "STDOUT_FIFO="+p.Stdout),
unit.NewUnitOption(svc, "Environment", "STDERR_FIFO="+p.Stderr),
unit.NewUnitOption(svc, "Environment", "DAEMON_UNIT_NAME="+os.Getenv("UNIT_NAME")),
unit.NewUnitOption(svc, "Environment", "UNIT_NAME=%n"), // %n is replaced with the unit name by systemd
unit.NewUnitOption(svc, "Environment", "EXIT_STATE_PATH="+p.exitStatePath()),
}
if p.shimCgroup != "" {
opts = append(opts, unit.NewUnitOption(svc, "Environment", "SHIM_CGROUP="+p.shimCgroup))
}
prefix := []string{p.exe, "--debug=" + strconv.FormatBool(p.runc.Debug), "--bundle=" + p.parent.Bundle, "create"}
cmd := []string{"exec", "--process=" + p.processFilePath(), "--pid-file=" + p.pidFile(), "--detach"}
if p.Terminal || p.opts.Terminal {
s, err := p.ttySockPath()
if err != nil {
return nil, err
}
cmd = append(cmd, "-t")
cmd = append(cmd, "--console-socket="+s)
opts = append(opts, unit.NewUnitOption(svc, "ExecStopPost", "-"+sysctl+" stop "+p.ttyUnitName()))
prefix = append(prefix, "--tty")
}
execStart, err := p.runcCmd(append(cmd, p.parent.id))
if err != nil {
return nil, err
}
execStart = append(prefix, execStart...)
opts = append(opts, unit.NewUnitOption(svc, "ExecStart", strings.Join(execStart, " ")))
return opts, nil
}
func (p *process) unitType() string {
if p.opts.SdNotifyEnable {
return "notify"
}
return "forking"
}
func (p *initProcess) Start(ctx context.Context) (pid uint32, retErr error) {
ctx, span := StartSpan(ctx, "InitProcess.Start")
defer func() {
if retErr != nil {
span.SetStatus(codes.Error, retErr.Error())
}
span.SetAttributes(attribute.Int("pid", int(pid)))
span.End()
}()
if p.checkpoint != "" {
return p.restore(ctx)
}
if p.ProcessState().Exited() {
return 0, fmt.Errorf("process has already exited: %s: %w", p.ProcessState(), errdefs.ErrFailedPrecondition)
}
if err := p.runc.Start(ctx, p.id); err != nil {
log.G(ctx).WithError(err).Error("Error calling runc start")
ret := fmt.Errorf("failed runc start: %w", err)
if err := p.LoadState(ctx); err != nil {
log.G(ctx).WithError(err).Warn("Error loading process state")
}
if !p.ProcessState().Exited() {
log.G(ctx).Debug("runc start failed but process is still running, sending sigkill")
p.systemd.KillUnitContext(ctx, p.Name(), int32(unix.SIGKILL))
if err := p.LoadState(ctx); err != nil {
log.G(ctx).WithError(err).Debug("Error loading process state")
}
if !p.ProcessState().Exited() {
p.SetState(ctx, pState{ExitCode: 255, ExitedAt: time.Now()})
}
}
p.cond.Broadcast()
if p.runc.Debug {
unitData, err := os.ReadFile("/run/systemd/system/" + p.Name())
if err == nil {
ret = fmt.Errorf("%w:\n%s\n%s", ret, p.Name(), unitData)
}
processData, err := os.ReadFile(filepath.Join(p.Bundle, "config.json"))
if err == nil {
ret = fmt.Errorf("%w:\nprocess.json:\n%s", ret, string(processData))
}
debug, err := os.ReadFile(p.runc.Log)
if err == nil {
ret = fmt.Errorf("%w:\nrunc debug:\n%s", ret, string(debug))
} else {
log.G(ctx).WithError(err).Warn("Error opening runc debug log")
}
}
return 0, ret
}
for p.Pid() == 0 && !p.ProcessState().Exited() {
select {
case <-ctx.Done():
default:
}
if err := p.LoadState(ctx); err != nil {
log.G(ctx).WithError(err).Warn("Error loading process state")
}
}
return pid, nil
}
func (p *initProcess) restore(ctx context.Context) (pid uint32, retErr error) {
if p.Terminal || p.opts.Terminal {
sockPath, err := p.ttySockPath()
if err != nil {
return 0, err
}
u, _, err := p.makePty(ctx, sockPath)
if err != nil {
return 0, err
}
defer func() {
if retErr != nil {
p.systemd.KillUnitContext(ctx, u, int32(syscall.SIGKILL))
}
}()
}
return p.startUnit(ctx)
}
func (p *execProcess) Start(ctx context.Context) (_ uint32, retErr error) {
if !p.parent.ProcessState().Started() {
p.parent.LoadState(ctx)
if !p.parent.ProcessState().Started() {
return 0, fmt.Errorf("%w: container is not started", errdefs.ErrFailedPrecondition)
}
}
if p.Terminal || p.opts.Terminal {
sockPath, err := p.ttySockPath()
if err != nil {
return 0, err
}
u, _, err := p.makePty(ctx, sockPath)
if err != nil {
return 0, err
}
defer func() {
if retErr != nil {
p.systemd.KillUnitContext(ctx, u, int32(syscall.SIGKILL))
}
}()
}
ch := make(chan string, 1)
if _, err := p.systemd.StartUnitContext(ctx, p.Name(), "replace", ch); err != nil {
return 0, err
}
select {
case <-ctx.Done():
log.G(ctx).WithError(ctx.Err()).Warn("start: context cancelled, killing exec unit")
p.systemd.KillUnitContext(context.TODO(), p.Name(), int32(syscall.SIGKILL))
case status := <-ch:
if status != "done" {
if err := p.LoadState(ctx); err != nil {
log.G(ctx).WithError(err).Warn("Error loading process state")
}
if !p.ProcessState().Exited() {
log.G(ctx).Error("Start failed but process is not in exited state")
break
}
if p.ProcessState().ExitCode != 255 {
break
}
ret := fmt.Errorf("error starting exec process")
if p.runc.Debug {
ret = fmt.Errorf("%w:\n%s", ret, p.Name())
unitData, err := os.ReadFile("/run/systemd/system/" + p.Name())
if err == nil {
ret = fmt.Errorf("%w:\n%s\n%s", ret, p.Name(), unitData)
}
processData, err := os.ReadFile(p.processFilePath())
if err == nil {
ret = fmt.Errorf("%w:\nprocess.json:\n%s", ret, string(processData))
}
debug, err := os.ReadFile(p.runc.Log)
if err == nil {
ret = fmt.Errorf("%w:\nrunc debug:\n%s", ret, string(debug))
} else {
log.G(ctx).WithError(err).Warn("Error opening runc debug log")
}
}
return 0, ret
}
}
p.LoadState(ctx)
if p.ProcessState().Status == exitedInit || p.ProcessState().Status == "exit-code" {
ret := fmt.Errorf("error starting exec process")
if p.runc.Debug {
debug, err := os.ReadFile(p.runc.Log)
if err == nil {
ret = fmt.Errorf("%w:\nrunc debug:\n%s", ret, string(debug))
}
}
return 0, ret
}
pid, err := p.getPid(ctx)
if err != nil {
return 0, err
}
p.mu.Lock()
p.state.Pid = pid
p.mu.Unlock()
return pid, nil
}