-
Notifications
You must be signed in to change notification settings - Fork 19
/
create.go
403 lines (353 loc) · 11.8 KB
/
create.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
package lxcri
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/lxc/lxcri/pkg/specki"
"github.com/opencontainers/runtime-spec/specs-go"
)
// Create creates a single container instance from the given ContainerConfig.
// Create is the first runtime method to call within the lifecycle of a container.
// A created Container must be released with Container.Release after use.
// You should call Runtime.Delete to cleanup container runtime state, even
// if the Create returned with an error.
func (rt *Runtime) Create(ctx context.Context, cfg *ContainerConfig) (*Container, error) {
if err := rt.checkConfig(cfg); err != nil {
return nil, err
}
c := &Container{ContainerConfig: cfg}
c.runtimeDir = filepath.Join(rt.Root, c.ContainerID)
if cfg.Spec.Annotations == nil {
cfg.Spec.Annotations = make(map[string]string)
}
cfg.Spec.Annotations["org.linuxcontainers.lxc.ConfigFile"] = c.RuntimePath("config")
if err := c.create(); err != nil {
return c, errorf("failed to create container: %w", err)
}
if err := configureContainer(rt, c); err != nil {
return c, errorf("failed to configure container: %w", err)
}
cleanenv(c, true)
// Seralize the modified spec.Spec separately, to make it available for
// runtime hooks.
specPath := c.RuntimePath(BundleConfigFile)
err := specki.EncodeJSONFile(specPath, cfg.Spec, os.O_EXCL|os.O_CREATE, 0444)
if err != nil {
return c, err
}
err = specki.EncodeJSONFile(c.RuntimePath("hooks.json"), cfg.Spec.Hooks, os.O_EXCL|os.O_CREATE, 0444)
if err != nil {
return c, err
}
state, err := c.State()
if err != nil {
return c, err
}
err = specki.EncodeJSONFile(c.RuntimePath("state.json"), state.SpecState, os.O_EXCL|os.O_CREATE, 0444)
if err != nil {
return c, err
}
if err := rt.runStartCmd(ctx, c); err != nil {
return c, errorf("failed to run container process: %w", err)
}
return c, nil
}
func configureContainer(rt *Runtime, c *Container) error {
if err := c.SetLog(c.LogFile, c.LogLevel); err != nil {
return errorf("failed to configure container log: %w", err)
}
if err := configureHostname(rt, c); err != nil {
return err
}
if err := configureRootfs(rt, c); err != nil {
return fmt.Errorf("failed to configure rootfs: %w", err)
}
if err := configureInit(rt, c); err != nil {
return fmt.Errorf("failed to configure init: %w", err)
}
if os.Getuid() != 0 {
// ensure user namespace is enabled
if !isNamespaceEnabled(c.Spec, specs.UserNamespace) {
rt.Log.Warn().Msg("unprivileged runtime - enabling user namespace")
c.Spec.Linux.Namespaces = append(c.Spec.Linux.Namespaces,
specs.LinuxNamespace{Type: specs.UserNamespace},
)
}
}
if err := configureNamespaces(c); err != nil {
return fmt.Errorf("failed to configure namespaces: %w", err)
}
if c.Spec.Process.OOMScoreAdj != nil {
if err := c.setConfigItem("lxc.proc.oom_score_adj", fmt.Sprintf("%d", *c.Spec.Process.OOMScoreAdj)); err != nil {
return err
}
}
if c.Spec.Process.NoNewPrivileges {
if err := c.setConfigItem("lxc.no_new_privs", "1"); err != nil {
return err
}
}
if rt.Features.Apparmor {
if err := configureApparmor(c); err != nil {
return fmt.Errorf("failed to configure apparmor: %w", err)
}
} else {
rt.Log.Warn().Msg("apparmor feature is disabled - profile is set to unconfined")
}
if rt.Features.Seccomp {
if c.Spec.Linux.Seccomp != nil && len(c.Spec.Linux.Seccomp.Syscalls) > 0 {
profilePath := c.RuntimePath("seccomp.conf")
if err := writeSeccompProfile(profilePath, c.Spec.Linux.Seccomp); err != nil {
return err
}
if err := c.setConfigItem("lxc.seccomp.profile", profilePath); err != nil {
return err
}
}
} else {
rt.Log.Warn().Msg("seccomp feature is disabled - all system calls are allowed")
}
if rt.Features.Capabilities {
if err := configureCapabilities(c); err != nil {
return fmt.Errorf("failed to configure capabilities: %w", err)
}
} else {
rt.Log.Warn().Msg("capabilities feature is disabled - running with runtime privileges")
}
// make sure autodev is disabled
if err := c.setConfigItem("lxc.autodev", "0"); err != nil {
return err
}
// NOTE crio can add devices (through the config) but this does not work for privileged containers.
// See https://github.com/cri-o/cri-o/blob/a705db4c6d04d7c14a4d59170a0ebb4b30850675/server/container_create_linux.go#L45
// File an issue on cri-o (at least for support)
if err := specki.AllowEssentialDevices(c.Spec); err != nil {
return err
}
if !rt.hasCapability("mknod") {
rt.Log.Info().Msg("runtime does not have capability CAP_MKNOD")
// CAP_MKNOD is not granted `man capabilities`
// Bind mount devices instead.
newMounts := make([]specs.Mount, 0, len(c.Spec.Mounts)+len(c.Spec.Linux.Devices))
for _, m := range c.Spec.Mounts {
if m.Destination == "/dev" {
rt.Log.Info().Msg("removing old /dev mount")
continue
}
newMounts = append(newMounts, m)
}
newMounts = append(newMounts,
specs.Mount{
Destination: "/dev", Source: "tmpfs", Type: "tmpfs",
Options: []string{"rw", "nosuid", "noexec", "relatime"},
},
)
rt.Log.Info().Msg("bind mount devices")
for _, device := range c.Spec.Linux.Devices {
newMounts = append(newMounts,
specs.Mount{
Destination: device.Path, Source: device.Path, Type: "bind",
Options: []string{"bind", "create=file"},
},
)
}
c.Spec.Mounts = newMounts
c.Spec.Linux.Devices = nil
}
if err := configureHooks(rt, c); err != nil {
return err
}
if err := configureCgroup(rt, c); err != nil {
return fmt.Errorf("failed to configure cgroups: %w", err)
}
for key, val := range c.Spec.Linux.Sysctl {
if err := c.setConfigItem("lxc.sysctl."+key, val); err != nil {
return err
}
}
// `man lxc.container.conf`: "A resource with no explicitly configured limitation will be inherited
// from the process starting up the container"
seenLimits := make([]string, 0, len(c.Spec.Process.Rlimits))
for _, limit := range c.Spec.Process.Rlimits {
name := strings.TrimPrefix(strings.ToLower(limit.Type), "rlimit_")
for _, seen := range seenLimits {
if seen == name {
return fmt.Errorf("duplicate resource limit %q", limit.Type)
}
}
seenLimits = append(seenLimits, name)
val := fmt.Sprintf("%d:%d", limit.Soft, limit.Hard)
if err := c.setConfigItem("lxc.prlimit."+name, val); err != nil {
return err
}
}
if err := configureMounts(rt, c); err != nil {
return fmt.Errorf("failed to configure mounts: %w", err)
}
if err := configureReadonlyPaths(c); err != nil {
return fmt.Errorf("failed to configure read-only paths: %w", err)
}
return nil
}
func configureHostname(rt *Runtime, c *Container) error {
if c.Spec.Hostname == "" {
return nil
}
if err := c.setConfigItem("lxc.uts.name", c.Spec.Hostname); err != nil {
return err
}
// Check if UTS namespace is shared, but not with the host.
uts := getNamespace(c.Spec, specs.UTSNamespace)
if uts == nil {
return nil
}
yes, err := isNamespaceSharedWithRuntime(uts)
if err != nil {
return errorf("failed to check if uts namespace is shared with host: %w", err)
}
if yes {
return nil
}
// Set the hostname on shared UTS namespace, since liblxc doesn't do it.
if err := setHostname(uts.Path, c.Spec.Hostname); err != nil {
return fmt.Errorf("failed to set hostname: %w", err)
}
return nil
}
func configureRootfs(rt *Runtime, c *Container) error {
rootfs := c.Spec.Root.Path
if !filepath.IsAbs(rootfs) {
rootfs = filepath.Join(c.BundlePath, rootfs)
}
if err := c.setConfigItem("lxc.rootfs.path", rootfs); err != nil {
return err
}
if err := c.setConfigItem("lxc.rootfs.mount", rootfs); err != nil {
return err
}
if err := c.setConfigItem("lxc.rootfs.managed", "0"); err != nil {
return err
}
// Resources not created by the container runtime MUST NOT be deleted by it.
if err := c.setConfigItem("lxc.ephemeral", "0"); err != nil {
return err
}
rootfsOptions := []string{}
if c.Spec.Linux.RootfsPropagation != "" {
rootfsOptions = append(rootfsOptions, c.Spec.Linux.RootfsPropagation)
}
if c.Spec.Root.Readonly {
rootfsOptions = append(rootfsOptions, "ro")
}
if err := c.setConfigItem("lxc.rootfs.options", strings.Join(rootfsOptions, ",")); err != nil {
return err
}
return nil
}
func configureReadonlyPaths(c *Container) error {
rootmnt := c.getConfigItem("lxc.rootfs.mount")
if rootmnt == "" {
return fmt.Errorf("lxc.rootfs.mount unavailable")
}
for _, p := range c.Spec.Linux.ReadonlyPaths {
mnt := fmt.Sprintf("%s %s %s %s", filepath.Join(rootmnt, p), strings.TrimPrefix(p, "/"), "bind", "bind,ro,optional")
if err := c.setConfigItem("lxc.mount.entry", mnt); err != nil {
return fmt.Errorf("failed to make path readonly: %w", err)
}
}
return nil
}
func configureApparmor(c *Container) error {
// The value *apparmor_profile* from crio.conf is used if no profile is defined by the container.
aaprofile := c.Spec.Process.ApparmorProfile
if aaprofile == "" {
aaprofile = "unconfined"
}
return c.setConfigItem("lxc.apparmor.profile", aaprofile)
}
// configureCapabilities configures the linux capabilities / privileges granted to the container processes.
// See `man lxc.container.conf` lxc.cap.drop and lxc.cap.keep for details.
// https://blog.container-solutions.com/linux-capabilities-in-practice
// https://blog.container-solutions.com/linux-capabilities-why-they-exist-and-how-they-work
func configureCapabilities(c *Container) error {
keepCaps := "none"
if c.Spec.Process.Capabilities != nil {
var caps []string
for _, c := range c.Spec.Process.Capabilities.Permitted {
lcCapName := strings.TrimPrefix(strings.ToLower(c), "cap_")
caps = append(caps, lcCapName)
}
if len(caps) > 0 {
keepCaps = strings.Join(caps, " ")
}
}
return c.setConfigItem("lxc.cap.keep", keepCaps)
}
// NOTE keep in sync with cmd/lxcri-hook#ociHooksAndState
func configureHooks(rt *Runtime, c *Container) error {
// prepend runtime OCI hooks to container hooks
hooks := rt.Hooks
if c.Spec.Hooks != nil {
if len(c.Spec.Hooks.Prestart) > 0 {
hooks.Prestart = append(hooks.Prestart, c.Spec.Hooks.Prestart...)
}
if len(c.Spec.Hooks.CreateRuntime) > 0 {
hooks.CreateRuntime = append(hooks.CreateRuntime, c.Spec.Hooks.CreateRuntime...)
}
if len(c.Spec.Hooks.CreateContainer) > 0 {
hooks.CreateContainer = append(hooks.CreateContainer, c.Spec.Hooks.CreateContainer...)
}
if len(c.Spec.Hooks.StartContainer) > 0 {
hooks.StartContainer = append(hooks.StartContainer, c.Spec.Hooks.StartContainer...)
}
if len(c.Spec.Hooks.Poststart) > 0 {
hooks.Poststart = append(hooks.Poststart, c.Spec.Hooks.Poststart...)
}
if len(c.Spec.Hooks.Poststop) > 0 {
hooks.Poststop = append(hooks.Poststop, c.Spec.Hooks.Poststop...)
}
}
c.Spec.Hooks = &hooks
// pass context information as environment variables to hook scripts
if err := c.setConfigItem("lxc.hook.version", "1"); err != nil {
return err
}
if len(c.Spec.Hooks.Prestart) > 0 || len(c.Spec.Hooks.CreateRuntime) > 0 {
if err := c.setConfigItem("lxc.hook.pre-mount", rt.libexec(ExecHook)); err != nil {
return err
}
}
if len(c.Spec.Hooks.CreateContainer) > 0 {
if err := c.setConfigItem("lxc.hook.mount", rt.libexec(ExecHook)); err != nil {
return err
}
}
if len(c.Spec.Hooks.StartContainer) > 0 {
if err := c.setConfigItem("lxc.hook.start", rt.libexec(ExecHook)); err != nil {
return err
}
}
return nil
}
// cleanenv removes duplicates from spec.Process.Env.
// If overwrite is false the first defined value takes precedence,
// if overwrite is true, the last defined value overwrites previously
// defined values.
func cleanenv(c *Container, overwrite bool) {
env := c.Spec.Process.Env
if len(env) < 2 {
return
}
newEnv := make([]string, 0, len(env))
var exist bool
for _, kv := range env {
newEnv, exist = specki.Setenv(newEnv, kv, overwrite)
if exist {
vals := strings.Split(kv, "=")
c.Log.Warn().Msgf("duplicate environment variable %s (overwrite=%t)", vals[0], overwrite)
}
}
c.Spec.Process.Env = newEnv
}