forked from lxc/lxcri
/
container.go
519 lines (459 loc) · 13.5 KB
/
container.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package lxcri
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/rs/zerolog"
"golang.org/x/sys/unix"
"gopkg.in/lxc/go-lxc.v2"
)
// ContainerConfig is the configuration for a single Container instance.
type ContainerConfig struct {
*specs.Spec
RuntimeDir string
ContainerID string
BundlePath string
ConsoleSocket string `json:",omitempty"`
// PidFile is the absolute PID file path
// for the container monitor process (ExecStart)
MonitorCgroupDir string
CgroupDir string
// LogFile is the liblxc log file path
LogFile string
// LogLevel is the liblxc log level
LogLevel string
// Log is the container Logger
Log zerolog.Logger `json:"-"`
Hooks `json:"-"`
}
func (cfg ContainerConfig) ConfigFilePath() string {
return cfg.RuntimePath("config")
}
func (cfg ContainerConfig) syncFifoPath() string {
return cfg.RuntimePath(initDir, "syncfifo")
}
// RuntimePath returns the absolute path within the container root.
func (cfg ContainerConfig) RuntimePath(subPath ...string) string {
return filepath.Join(cfg.RuntimeDir, filepath.Join(subPath...))
}
func (cfg ContainerConfig) runtimeDirExists() bool {
if _, err := os.Stat(cfg.RuntimeDir); err == nil {
return true
}
return false
}
func (c *ContainerConfig) LoadSpecJson(p string) error {
spec := &specs.Spec{}
if err := decodeFileJSON(spec, p); err != nil {
return err
}
c.Spec = spec
return nil
}
// Container is the runtime state of a container instance.
type Container struct {
LinuxContainer *lxc.Container `json:"-"`
*ContainerConfig
CreatedAt time.Time
Pid int
}
func (c *Container) create() error {
if c.runtimeDirExists() {
return ErrExist
}
if err := os.MkdirAll(c.RuntimeDir, 0700); err != nil {
return fmt.Errorf("failed to create container dir: %w", err)
}
// An empty tmpfile is created to ensure that createContainer can only succeed once.
// The config file is atomically activated in SaveConfig.
// #nosec
f, err := os.OpenFile(c.RuntimePath(".config"), os.O_EXCL|os.O_CREATE|os.O_RDWR, 0640)
if err != nil {
return err
}
if err := f.Close(); err != nil {
return fmt.Errorf("failed to close empty config tmpfile: %w", err)
}
c.LinuxContainer, err = lxc.NewContainer(c.ContainerID, filepath.Dir(c.RuntimeDir))
if err != nil {
return err
}
return nil
}
func (c *Container) load() error {
if !c.runtimeDirExists() {
return ErrNotExist
}
err := decodeFileJSON(c, c.RuntimePath("container.json"))
if err != nil {
return fmt.Errorf("failed to load container config: %w", err)
}
_, err = os.Stat(c.ConfigFilePath())
if err != nil {
return fmt.Errorf("failed to load lxc config file: %w", err)
}
c.LinuxContainer, err = lxc.NewContainer(c.ContainerID, filepath.Dir(c.RuntimeDir))
if err != nil {
return fmt.Errorf("failed to create lxc container: %w", err)
}
err = c.LinuxContainer.LoadConfigFile(c.ConfigFilePath())
if err != nil {
return fmt.Errorf("failed to load config file: %w", err)
}
return nil
}
func (c *Container) waitCreated(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
state := c.LinuxContainer.State()
if !(state == lxc.RUNNING) {
c.Log.Debug().Stringer("state", state).Msg("wait for state lxc.RUNNING")
time.Sleep(time.Millisecond * 100)
continue
}
initState, err := c.getContainerInitState()
if err != nil {
return err
}
if initState == specs.StateCreated {
return nil
}
return fmt.Errorf("unexpected init state %q", initState)
}
}
}
func (c *Container) waitNot(ctx context.Context, state specs.ContainerState) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
initState, _ := c.getContainerInitState()
if initState != state {
return nil
}
time.Sleep(time.Millisecond * 10)
}
}
}
func (c *Container) wait(ctx context.Context, state lxc.State) bool {
for {
select {
case <-ctx.Done():
return false
default:
if c.LinuxContainer.State() == state {
return true
}
time.Sleep(time.Millisecond * 100)
}
}
}
func (c *Container) State() (*specs.State, error) {
status, err := c.ContainerState()
if err != nil {
return nil, errorf("failed go get container status: %w", err)
}
state := &specs.State{
Version: specs.Version,
ID: c.ContainerID,
Bundle: c.BundlePath,
Pid: c.Pid,
Annotations: c.Annotations,
Status: status,
}
return state, nil
}
func (c *Container) ContainerState() (specs.ContainerState, error) {
state := c.LinuxContainer.State()
switch state {
case lxc.STOPPED:
return specs.StateStopped, nil
case lxc.STARTING:
return specs.StateCreating, nil
case lxc.RUNNING, lxc.STOPPING, lxc.ABORTING, lxc.FREEZING, lxc.FROZEN, lxc.THAWED:
return c.getContainerInitState()
default:
return specs.StateStopped, fmt.Errorf("unsupported lxc container state %q", state)
}
}
// getContainerInitState returns the detailed state of the container init process.
// This should be called if the container is in state lxc.RUNNING.
// On error the caller should call getContainerState() again
func (c *Container) getContainerInitState() (specs.ContainerState, error) {
initPid := c.LinuxContainer.InitPid()
if initPid < 1 {
return specs.StateStopped, nil
}
cmdlinePath := fmt.Sprintf("/proc/%d/cmdline", initPid)
cmdline, err := ioutil.ReadFile(cmdlinePath)
if os.IsNotExist(err) {
// init process died or returned
return specs.StateStopped, nil
}
if err != nil {
// it's a serious error if cmdlinePath exists but can't be read
return specs.StateStopped, err
}
initCmdline := fmt.Sprintf("/.lxcri/init\000%s\000", c.ContainerID)
if string(cmdline) == initCmdline {
return specs.StateCreated, nil
}
return specs.StateRunning, nil
}
func (c *Container) kill(ctx context.Context, signum unix.Signal) error {
c.Log.Info().Int("signum", int(signum)).Msg("killing container process")
if signum == unix.SIGKILL || signum == unix.SIGTERM {
return c.stop(ctx, signum)
}
// Send non-terminating signals to monitor process.
// The monitor process propagates the signal to all container process.
// FIXME revise this, it may be prone to race-conditions,
/// the monitor process PID could have been recycled
// Maybe use c.lxccontainer.InitPidFd() ?
if c.Pid > 1 {
c.Log.Info().Int("pid", c.Pid).Int("signal", int(signum)).Msg("sending signal")
if err := unix.Kill(c.Pid, 0); err == nil {
err := unix.Kill(c.Pid, signum)
if err != unix.ESRCH {
return fmt.Errorf("failed to send signal %d to container process %d: %w", signum, c.Pid, err)
}
}
}
return nil
}
func (c *Container) stop(ctx context.Context, signum unix.Signal) error {
if err := c.SetConfigItem("lxc.signal.stop", strconv.Itoa(int(signum))); err != nil {
return err
}
if err := c.LinuxContainer.Stop(); err != nil {
return err
}
if !c.wait(ctx, lxc.STOPPED) {
c.Log.Warn().Msg("failed to stop lxc container")
}
// draining the cgroup is required to catch processes that escaped from
// 'kill' e.g a bash for loop that spawns a new child immediately.
start := time.Now()
err := drainCgroup(ctx, c.CgroupDir, signum)
if err != nil && !os.IsNotExist(err) {
c.Log.Warn().Err(err).Str("file", c.CgroupDir).Msg("failed to drain cgroup")
} else {
c.Log.Info().Dur("duration", time.Since(start)).Str("file", c.CgroupDir).Msg("cgroup drained")
}
return err
}
// SaveConfig creates and atomically enables the lxc config file.
// It must be called only once. It is automatically called by Runtime#Create.
// Any config changes via clxc.setConfigItem must be done before calling SaveConfig.
// FIXME revise the config file mechanism
func (c *Container) saveConfig() error {
// createContainer creates the tmpfile
tmpFile := c.RuntimePath(".config")
if _, err := os.Stat(tmpFile); err != nil {
return fmt.Errorf("failed to stat config tmpfile: %w", err)
}
// Don't overwrite an existing config.
cfgFile := c.ConfigFilePath()
if _, err := os.Stat(cfgFile); err == nil {
return fmt.Errorf("config file %s already exists", cfgFile)
}
err := c.LinuxContainer.SaveConfigFile(tmpFile)
if err != nil {
return fmt.Errorf("failed to save config file to %q: %w", tmpFile, err)
}
if err := os.Rename(tmpFile, cfgFile); err != nil {
return fmt.Errorf("failed to rename config file: %w", err)
}
return nil
}
func (c *Container) GetConfigItem(key string) string {
vals := c.LinuxContainer.ConfigItem(key)
if len(vals) > 0 {
first := vals[0]
// some lxc config values are set to '(null)' if unset
// eg. lxc.cgroup.dir
if first != "(null)" {
return first
}
}
return ""
}
func (c *Container) SetConfigItem(key, value string) error {
err := c.LinuxContainer.SetConfigItem(key, value)
if err != nil {
return fmt.Errorf("failed to set config item '%s=%s': %w", key, value, err)
}
c.Log.Debug().Str("lxc.config", key).Str("val", value).Msg("set config item")
return nil
}
func (c *Container) SupportsConfigItem(keys ...string) bool {
canCheck := lxc.VersionAtLeast(4, 0, 6)
if !canCheck {
c.Log.Warn().Msg("lxc.IsSupportedConfigItem is broken in liblxc < 4.0.6")
}
for _, key := range keys {
if canCheck && lxc.IsSupportedConfigItem(key) {
continue
}
c.Log.Info().Str("lxc.config", key).Msg("unsupported config item")
return false
}
return true
}
func (c *Container) Release() error {
return c.LinuxContainer.Release()
}
// "Note that resources associated with the container,
// but not created by this container, MUST NOT be deleted."
// TODO - because we set rootfs.managed=0, Destroy() doesn't
// delete the /var/lib/lxc/$containerID/config file:
func (c *Container) destroy() error {
if err := c.LinuxContainer.Destroy(); err != nil {
return fmt.Errorf("failed to destroy container: %w", err)
}
if c.CgroupDir != "" {
err := deleteCgroup(c.CgroupDir)
if err != nil && !os.IsNotExist(err) {
c.Log.Warn().Err(err).Str("file", c.CgroupDir).Msg("failed to remove cgroup dir")
}
}
return os.RemoveAll(c.RuntimePath())
}
func (c *Container) start(ctx context.Context) error {
done := make(chan error)
go func() {
// FIXME fifo must be unblocked otherwise
// this may be a goroutine leak
done <- c.readFifo()
}()
select {
case <-ctx.Done():
return errorf("syncfifo timeout: %w", ctx.Err())
// TODO write to fifo here and fallthrough ?
case err := <-done:
if err != nil {
return errorf("failed to read from syncfifo: %w", err)
}
}
// wait for container state to change
return c.waitNot(ctx, specs.StateCreated)
}
func (c *Container) readFifo() error {
// #nosec
f, err := os.OpenFile(c.syncFifoPath(), os.O_RDONLY, 0)
if err != nil {
return err
}
// NOTE it's not possible to set an IO deadline on a fifo
// #nosec
defer f.Close()
data := make([]byte, len(c.ContainerID))
_, err = f.Read(data)
if err != nil {
return fmt.Errorf("problem reading from fifo: %w", err)
}
if c.ContainerID != string(data) {
return fmt.Errorf("bad fifo content: %s", string(data))
}
return nil
}
func (c *Container) ExecDetached(args []string, proc *specs.Process) (pid int, err error) {
opts, err := attachOptions(proc, c.Linux.Namespaces)
if err != nil {
return 0, errorf("failed to create attach options: %w", err)
}
c.Log.Info().Strs("args", args).
Int("uid", opts.UID).Int("gid", opts.GID).
Ints("groups", opts.Groups).Msg("execute cmd")
pid, err = c.LinuxContainer.RunCommandNoWait(args, opts)
if err != nil {
return pid, errorf("failed to run exec cmd detached: %w", err)
}
return pid, nil
}
func (c *Container) Exec(args []string, proc *specs.Process) (exitStatus int, err error) {
opts, err := attachOptions(proc, c.Linux.Namespaces)
if err != nil {
return 0, errorf("failed to create attach options: %w", err)
}
exitStatus, err = c.LinuxContainer.RunCommandStatus(args, opts)
if err != nil {
return exitStatus, errorf("failed to run exec cmd: %w", err)
}
return exitStatus, nil
}
func attachOptions(procSpec *specs.Process, ns []specs.LinuxNamespace) (lxc.AttachOptions, error) {
opts := lxc.AttachOptions{
StdinFd: 0,
StdoutFd: 1,
StderrFd: 2,
}
clone, err := cloneFlags(ns)
if err != nil {
return opts, err
}
opts.Namespaces = clone
if procSpec != nil {
opts.Cwd = procSpec.Cwd
// Use the environment defined by the process spec.
opts.ClearEnv = true
opts.Env = procSpec.Env
opts.UID = int(procSpec.User.UID)
opts.GID = int(procSpec.User.GID)
if n := len(procSpec.User.AdditionalGids); n > 0 {
opts.Groups = make([]int, n)
for i, g := range procSpec.User.AdditionalGids {
opts.Groups[i] = int(g)
}
}
}
return opts, nil
}
func setLog(c *Container) error {
// Never let lxc write to stdout, stdout belongs to the container init process.
// Explicitly disable it - allthough it is currently the default.
c.LinuxContainer.SetVerbosity(lxc.Quiet)
// The log level for a running container is set, and may change, per runtime call.
err := c.LinuxContainer.SetLogLevel(parseContainerLogLevel(c.LogLevel))
if err != nil {
return fmt.Errorf("failed to set container loglevel: %w", err)
}
if err := c.LinuxContainer.SetLogFile(c.LogFile); err != nil {
return fmt.Errorf("failed to set container log file: %w", err)
}
return nil
}
func parseContainerLogLevel(level string) lxc.LogLevel {
switch strings.ToLower(level) {
case "trace":
return lxc.TRACE
case "debug":
return lxc.DEBUG
case "info":
return lxc.INFO
case "notice":
return lxc.NOTICE
case "warn":
return lxc.WARN
case "error":
return lxc.ERROR
case "crit":
return lxc.CRIT
case "alert":
return lxc.ALERT
case "fatal":
return lxc.FATAL
default:
return lxc.WARN
}
}