/
container_create.go
504 lines (445 loc) · 15.3 KB
/
container_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
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
package server
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/containers/storage/pkg/idtools"
"github.com/containers/storage/pkg/mount"
"github.com/containers/storage/pkg/stringid"
"github.com/cri-o/cri-o/internal/factory/container"
"github.com/cri-o/cri-o/internal/lib/sandbox"
"github.com/cri-o/cri-o/internal/log"
"github.com/cri-o/cri-o/internal/resourcestore"
"github.com/cri-o/cri-o/internal/storage"
"github.com/cri-o/cri-o/pkg/config"
"github.com/cri-o/cri-o/utils"
securejoin "github.com/cyphar/filepath-securejoin"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
types "k8s.io/cri-api/pkg/apis/runtime/v1"
)
// sync with https://github.com/containers/storage/blob/7fe03f6c765f2adbc75a5691a1fb4f19e56e7071/pkg/truncindex/truncindex.go#L92
const noSuchID = "no such id"
type orderedMounts []rspec.Mount
// Len returns the number of mounts. Used in sorting.
func (m orderedMounts) Len() int {
return len(m)
}
// Less returns true if the number of parts (a/b/c would be 3 parts) in the
// mount indexed by parameter 1 is less than that of the mount indexed by
// parameter 2. Used in sorting.
func (m orderedMounts) Less(i, j int) bool {
return m.parts(i) < m.parts(j)
}
// Swap swaps two items in an array of mounts. Used in sorting
func (m orderedMounts) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
// parts returns the number of parts in the destination of a mount. Used in sorting.
func (m orderedMounts) parts(i int) int {
return strings.Count(filepath.Clean(m[i].Destination), string(os.PathSeparator))
}
// mounts defines how to sort runtime.Mount.
// This is the same with the Docker implementation:
//
// https://github.com/moby/moby/blob/17.05.x/daemon/volumes.go#L26
type criOrderedMounts []*types.Mount
// Len returns the number of mounts. Used in sorting.
func (m criOrderedMounts) Len() int {
return len(m)
}
// Less returns true if the number of parts (a/b/c would be 3 parts) in the
// mount indexed by parameter 1 is less than that of the mount indexed by
// parameter 2. Used in sorting.
func (m criOrderedMounts) Less(i, j int) bool {
return m.parts(i) < m.parts(j)
}
// Swap swaps two items in an array of mounts. Used in sorting
func (m criOrderedMounts) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
// parts returns the number of parts in the destination of a mount. Used in sorting.
func (m criOrderedMounts) parts(i int) int {
return strings.Count(filepath.Clean(m[i].ContainerPath), string(os.PathSeparator))
}
// Ensure mount point on which path is mounted, is shared.
func ensureShared(path string, mountInfos []*mount.Info) error {
sourceMount, optionalOpts, err := getSourceMount(path, mountInfos)
if err != nil {
return err
}
// Make sure source mount point is shared.
optsSplit := strings.Split(optionalOpts, " ")
for _, opt := range optsSplit {
if strings.HasPrefix(opt, "shared:") {
return nil
}
}
return fmt.Errorf("path %q is mounted on %q but it is not a shared mount", path, sourceMount)
}
// Ensure mount point on which path is mounted, is either shared or slave.
func ensureSharedOrSlave(path string, mountInfos []*mount.Info) error {
sourceMount, optionalOpts, err := getSourceMount(path, mountInfos)
if err != nil {
return err
}
// Make sure source mount point is shared.
optsSplit := strings.Split(optionalOpts, " ")
for _, opt := range optsSplit {
if strings.HasPrefix(opt, "shared:") {
return nil
} else if strings.HasPrefix(opt, "master:") {
return nil
}
}
return fmt.Errorf("path %q is mounted on %q but it is not a shared or slave mount", path, sourceMount)
}
func addImageVolumes(ctx context.Context, rootfs string, s *Server, containerInfo *storage.ContainerInfo, mountLabel string, specgen *generate.Generator) ([]rspec.Mount, error) {
ctx, span := log.StartSpan(ctx)
defer span.End()
mounts := []rspec.Mount{}
for dest := range containerInfo.Config.Config.Volumes {
fp, err := securejoin.SecureJoin(rootfs, dest)
if err != nil {
return nil, err
}
switch s.config.ImageVolumes {
case config.ImageVolumesMkdir:
IDs := idtools.IDPair{UID: int(specgen.Config.Process.User.UID), GID: int(specgen.Config.Process.User.GID)}
if err1 := idtools.MkdirAllAndChownNew(fp, 0o755, IDs); err1 != nil {
return nil, err1
}
if mountLabel != "" {
if err1 := securityLabel(fp, mountLabel, true, false); err1 != nil {
return nil, err1
}
}
case config.ImageVolumesBind:
volumeDirName := stringid.GenerateNonCryptoID()
src := filepath.Join(containerInfo.RunDir, "mounts", volumeDirName)
if err1 := os.MkdirAll(src, 0o755); err1 != nil {
return nil, err1
}
// Label the source with the sandbox selinux mount label
if mountLabel != "" {
if err1 := securityLabel(src, mountLabel, true, false); err1 != nil {
return nil, err1
}
}
log.Debugf(ctx, "Adding bind mounted volume: %s to %s", src, dest)
mounts = append(mounts, rspec.Mount{
Source: src,
Destination: dest,
Type: "bind",
Options: []string{"private", "bind", "rw"},
})
case config.ImageVolumesIgnore:
log.Debugf(ctx, "Ignoring volume %v", dest)
default:
log.Errorf(ctx, "Unrecognized image volumes setting")
}
}
return mounts, nil
}
// resolveSymbolicLink resolves a possible symlink path. If the path is a symlink, returns resolved
// path; if not, returns the original path.
// note: strictly SecureJoin is not sufficient, as it does not error when a part of the path doesn't exist
// but simply moves on. If the last part of the path doesn't exist, it may need to be created.
func resolveSymbolicLink(scope, path string) (string, error) {
info, err := os.Lstat(path)
if err != nil {
return "", err
}
if info.Mode()&os.ModeSymlink != os.ModeSymlink {
return path, nil
}
if scope == "" {
scope = "/"
}
return securejoin.SecureJoin(scope, path)
}
// setupContainerUser sets the UID, GID and supplemental groups in OCI runtime config
func setupContainerUser(ctx context.Context, specgen *generate.Generator, rootfs, mountLabel, ctrRunDir string, sc *types.LinuxContainerSecurityContext, imageConfig *v1.Image) error {
ctx, span := log.StartSpan(ctx)
defer span.End()
if sc == nil {
return nil
}
if sc.RunAsGroup != nil && sc.RunAsUser == nil && sc.RunAsUsername == "" {
return fmt.Errorf("user group is specified without user or username")
}
imageUser := ""
homedir := ""
for _, env := range specgen.Config.Process.Env {
if strings.HasPrefix(env, "HOME=") {
homedir = strings.TrimPrefix(env, "HOME=")
if idx := strings.Index(homedir, `\n`); idx > -1 {
return fmt.Errorf("invalid HOME environment; newline not allowed")
}
break
}
}
if homedir == "" {
homedir = specgen.Config.Process.Cwd
}
if imageConfig != nil {
imageUser = imageConfig.Config.User
}
containerUser := generateUserString(
sc.RunAsUsername,
imageUser,
sc.RunAsUser,
)
log.Debugf(ctx, "CONTAINER USER: %+v", containerUser)
// Add uid, gid and groups from user
uid, gid, addGroups, err := utils.GetUserInfo(rootfs, containerUser)
if err != nil {
return err
}
genPasswd := true
for _, mount := range specgen.Config.Mounts {
if mount.Destination == "/etc" ||
mount.Destination == "/etc/" ||
mount.Destination == "/etc/passwd" {
genPasswd = false
break
}
}
if genPasswd {
// verify uid exists in containers /etc/passwd, else generate a passwd with the user entry
passwdPath, err := utils.GeneratePasswd(containerUser, uid, gid, homedir, rootfs, ctrRunDir)
if err != nil {
return err
}
if passwdPath != "" {
if err := securityLabel(passwdPath, mountLabel, false, false); err != nil {
return err
}
mnt := rspec.Mount{
Type: "bind",
Source: passwdPath,
Destination: "/etc/passwd",
Options: []string{"rw", "bind", "nodev", "nosuid", "noexec"},
}
specgen.AddMount(mnt)
}
}
specgen.SetProcessUID(uid)
if sc.RunAsGroup != nil {
gid = uint32(sc.RunAsGroup.Value)
}
specgen.SetProcessGID(gid)
specgen.AddProcessAdditionalGid(gid)
for _, group := range addGroups {
specgen.AddProcessAdditionalGid(group)
}
// Add groups from CRI
groups := sc.SupplementalGroups
for _, group := range groups {
specgen.AddProcessAdditionalGid(uint32(group))
}
return nil
}
// generateUserString generates valid user string based on OCI Image Spec v1.0.0.
func generateUserString(username, imageUser string, uid *types.Int64Value) string {
var userstr string
if uid != nil {
userstr = strconv.FormatInt(uid.Value, 10)
}
if username != "" {
userstr = username
}
// We use the user from the image config if nothing is provided
if userstr == "" {
userstr = imageUser
}
if userstr == "" {
return ""
}
return userstr
}
// CreateContainer creates a new container in specified PodSandbox
func (s *Server) CreateContainer(ctx context.Context, req *types.CreateContainerRequest) (res *types.CreateContainerResponse, retErr error) {
if req.Config == nil {
return nil, errors.New("config is nil")
}
if req.Config.Image == nil {
return nil, errors.New("config image is nil")
}
if req.SandboxConfig == nil {
return nil, errors.New("sandbox config is nil")
}
if req.SandboxConfig.Metadata == nil {
return nil, errors.New("sandbox config metadata is nil")
}
log.Infof(ctx, "Creating container: %s", translateLabelsToDescription(req.GetConfig().GetLabels()))
// Check if image is a file. If it is a file it might be a checkpoint archive.
checkpointImage, err := func() (bool, error) {
if !s.config.CheckpointRestore() {
// If CRIU support is not enabled return from
// this check as early as possible.
return false, nil
}
if _, err := os.Stat(req.Config.Image.Image); err == nil {
log.Debugf(
ctx,
"%q is a file. Assuming it is a checkpoint archive",
req.Config.Image.Image,
)
return true, nil
}
// Check if this is an OCI checkpoint image
imageID, err := s.checkIfCheckpointOCIImage(ctx, req.Config.Image.Image)
if err != nil {
return false, fmt.Errorf("failed to check if this is a checkpoint image: %w", err)
}
return imageID != nil, nil
}()
if err != nil {
return nil, err
}
if checkpointImage {
// This might be a checkpoint image. Let's pass
// it to the checkpoint code.
ctrID, err := s.CRImportCheckpoint(
ctx,
req.Config,
req.PodSandboxId,
req.SandboxConfig.Metadata.Uid,
)
if err != nil {
return nil, err
}
log.Debugf(ctx, "Prepared %s for restore\n", ctrID)
return &types.CreateContainerResponse{
ContainerId: ctrID,
}, nil
}
sb, err := s.getPodSandboxFromRequest(ctx, req.PodSandboxId)
if err != nil {
if err == sandbox.ErrIDEmpty {
return nil, err
}
return nil, fmt.Errorf("specified sandbox not found: %s: %w", req.PodSandboxId, err)
}
stopMutex := sb.StopMutex()
stopMutex.RLock()
defer stopMutex.RUnlock()
if sb.Stopped() {
return nil, fmt.Errorf("CreateContainer failed as the sandbox was stopped: %s", sb.ID())
}
ctr, err := container.New()
if err != nil {
return nil, fmt.Errorf("failed to create container: %w", err)
}
if err := ctr.SetConfig(req.Config, req.SandboxConfig); err != nil {
return nil, fmt.Errorf("setting container config: %w", err)
}
if err := ctr.SetNameAndID(""); err != nil {
return nil, fmt.Errorf("setting container name and ID: %w", err)
}
resourceCleaner := resourcestore.NewResourceCleaner()
defer func() {
// no error, no need to cleanup
if retErr == nil || isContextError(retErr) {
return
}
if err := resourceCleaner.Cleanup(); err != nil {
log.Errorf(ctx, "Unable to cleanup: %v", err)
}
}()
if _, err = s.ReserveContainerName(ctr.ID(), ctr.Name()); err != nil {
reservedID, getErr := s.ContainerIDForName(ctr.Name())
if getErr != nil {
return nil, fmt.Errorf("failed to get ID of container with reserved name (%s), after failing to reserve name with %v: %w", ctr.Name(), getErr, getErr)
}
// if we're able to find the container, and it's created, this is actually a duplicate request
// Just return that container
if reservedCtr := s.GetContainer(ctx, reservedID); reservedCtr != nil && reservedCtr.Created() {
return &types.CreateContainerResponse{ContainerId: reservedID}, nil
}
cachedID, resourceErr := s.getResourceOrWait(ctx, ctr.Name(), "container")
if resourceErr == nil {
return &types.CreateContainerResponse{ContainerId: cachedID}, nil
}
return nil, fmt.Errorf("%v: %w", resourceErr, err)
}
s.resourceStore.SetStageForResource(ctx, ctr.Name(), "container creating")
resourceCleaner.Add(ctx, "createCtr: releasing container name "+ctr.Name(), func() error {
s.ReleaseContainerName(ctx, ctr.Name())
return nil
})
newContainer, err := s.createSandboxContainer(ctx, ctr, sb)
if err != nil {
return nil, err
}
resourceCleaner.Add(ctx, "createCtr: deleting container "+ctr.ID()+" from storage", func() error {
if err := s.StorageRuntimeServer().DeleteContainer(ctx, ctr.ID()); err != nil {
return fmt.Errorf("failed to cleanup container storage: %w", err)
}
return nil
})
s.addContainer(ctx, newContainer)
resourceCleaner.Add(ctx, "createCtr: removing container "+newContainer.ID(), func() error {
s.removeContainer(ctx, newContainer)
return nil
})
if err := s.CtrIDIndex().Add(ctr.ID()); err != nil {
return nil, err
}
resourceCleaner.Add(ctx, "createCtr: deleting container ID "+ctr.ID()+" from idIndex", func() error {
if err := s.CtrIDIndex().Delete(ctr.ID()); err != nil && !strings.Contains(err.Error(), noSuchID) {
return err
}
return nil
})
mappings, err := s.getSandboxIDMappings(ctx, sb)
if err != nil {
return nil, err
}
s.resourceStore.SetStageForResource(ctx, ctr.Name(), "container runtime creation")
if err := s.createContainerPlatform(ctx, newContainer, sb.CgroupParent(), mappings); err != nil {
return nil, err
}
resourceCleaner.Add(ctx, "createCtr: removing container ID "+ctr.ID()+" from runtime", func() error {
if err := s.Runtime().DeleteContainer(ctx, newContainer); err != nil {
return fmt.Errorf("failed to delete container in runtime %s: %w", ctr.ID(), err)
}
return nil
})
if err := s.ContainerStateToDisk(ctx, newContainer); err != nil {
log.Warnf(ctx, "Unable to write containers %s state to disk: %v", newContainer.ID(), err)
}
if isContextError(ctx.Err()) {
if err := s.resourceStore.Put(ctr.Name(), newContainer, resourceCleaner); err != nil {
log.Errorf(ctx, "CreateCtr: failed to save progress of container %s: %v", newContainer.ID(), err)
}
log.Infof(ctx, "CreateCtr: context was either canceled or the deadline was exceeded: %v", ctx.Err())
return nil, ctx.Err()
}
// Since it's not a context error, we can delete the resource from the store, it will be tracked in the server from now on.
s.resourceStore.Delete(ctr.Name())
newContainer.SetCreated()
if err := s.nri.postCreateContainer(ctx, sb, newContainer); err != nil {
log.Warnf(ctx, "NRI post-create event failed for container %q: %v",
newContainer.ID(), err)
}
s.generateCRIEvent(ctx, newContainer, types.ContainerEventType_CONTAINER_CREATED_EVENT)
log.Infof(ctx, "Created container %s: %s", newContainer.ID(), newContainer.Description())
return &types.CreateContainerResponse{
ContainerId: ctr.ID(),
}, nil
}
func isInCRIMounts(dst string, mounts []*types.Mount) bool {
for _, m := range mounts {
if m.ContainerPath == dst {
return true
}
}
return false
}