Skip to content

Commit 19af235

Browse files
Merge pull request #3148 from masters-of-cats/wip-rootless-containerd
Skip rootfs unmount when no mounts are provided
2 parents d71c7ad + ae5ca81 commit 19af235

5 files changed

Lines changed: 33 additions & 13 deletions

File tree

mount/mount_linux.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,18 @@ func unmount(target string, flags int) error {
111111
// UnmountAll repeatedly unmounts the given mount point until there
112112
// are no mounts remaining (EINVAL is returned by mount), which is
113113
// useful for undoing a stack of mounts on the same mount point.
114+
// UnmountAll all is noop when the first argument is an empty string.
115+
// This is done when the containerd client did not specify any rootfs
116+
// mounts (e.g. because the rootfs is managed outside containerd)
117+
// UnmountAll is noop when the mount path does not exist.
114118
func UnmountAll(mount string, flags int) error {
119+
if mount == "" {
120+
return nil
121+
}
122+
if _, err := os.Stat(mount); os.IsNotExist(err) {
123+
return nil
124+
}
125+
115126
for {
116127
if err := unmount(mount, flags); err != nil {
117128
// EINVAL is returned if the target is not a

runtime/v1/linux/bundle.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ func newBundle(id, path, workDir string, spec []byte) (b *bundle, err error) {
6565
os.RemoveAll(workDir)
6666
}
6767
}()
68-
if err := os.Mkdir(filepath.Join(path, "rootfs"), 0711); err != nil {
69-
return nil, err
70-
}
7168
err = ioutil.WriteFile(filepath.Join(path, configFilename), spec, 0666)
7269
return &bundle{
7370
id: id,

runtime/v1/shim/service.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ func (s *Service) Create(ctx context.Context, r *shimapi.CreateTaskRequest) (_ *
124124
})
125125
}
126126

127+
rootfs := ""
128+
if len(mounts) > 0 {
129+
rootfs = filepath.Join(r.Bundle, "rootfs")
130+
if err := os.Mkdir(rootfs, 0711); err != nil {
131+
return nil, err
132+
}
133+
}
134+
127135
config := &proc.CreateConfig{
128136
ID: r.ID,
129137
Bundle: r.Bundle,
@@ -137,7 +145,6 @@ func (s *Service) Create(ctx context.Context, r *shimapi.CreateTaskRequest) (_ *
137145
ParentCheckpoint: r.ParentCheckpoint,
138146
Options: r.Options,
139147
}
140-
rootfs := filepath.Join(r.Bundle, "rootfs")
141148
defer func() {
142149
if err != nil {
143150
if err2 := mount.UnmountAll(rootfs, 0); err2 != nil {
@@ -169,6 +176,7 @@ func (s *Service) Create(ctx context.Context, r *shimapi.CreateTaskRequest) (_ *
169176
s.config.SystemdCgroup,
170177
s.platform,
171178
config,
179+
rootfs,
172180
)
173181
if err != nil {
174182
return nil, errdefs.ToGRPC(err)
@@ -632,7 +640,7 @@ func getTopic(ctx context.Context, e interface{}) string {
632640
return runtime.TaskUnknownTopic
633641
}
634642

635-
func newInit(ctx context.Context, path, workDir, runtimeRoot, namespace, criu string, systemdCgroup bool, platform rproc.Platform, r *proc.CreateConfig) (*proc.Init, error) {
643+
func newInit(ctx context.Context, path, workDir, runtimeRoot, namespace, criu string, systemdCgroup bool, platform rproc.Platform, r *proc.CreateConfig, rootfs string) (*proc.Init, error) {
636644
var options runctypes.CreateOptions
637645
if r.Options != nil {
638646
v, err := typeurl.UnmarshalAny(r.Options)
@@ -642,7 +650,6 @@ func newInit(ctx context.Context, path, workDir, runtimeRoot, namespace, criu st
642650
options = *v.(*runctypes.CreateOptions)
643651
}
644652

645-
rootfs := filepath.Join(path, "rootfs")
646653
runtime := proc.NewRunc(runtimeRoot, path, namespace, r.Runtime, criu, systemdCgroup)
647654
p := proc.New(r.ID, runtime, rproc.Stdio{
648655
Stdin: r.Stdin,

runtime/v2/bundle.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ func NewBundle(ctx context.Context, root, state, id string, spec []byte) (b *Bun
8989
}
9090
}
9191
paths = append(paths, work)
92-
// create rootfs dir
93-
if err := os.Mkdir(filepath.Join(b.Path, "rootfs"), 0711); err != nil {
94-
return nil, err
95-
}
9692
// symlink workdir
9793
if err := os.Symlink(work, filepath.Join(b.Path, "work")); err != nil {
9894
return nil, err

runtime/v2/runc/container.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package runc
2121
import (
2222
"context"
2323
"io/ioutil"
24+
"os"
2425
"path/filepath"
2526
"sync"
2627

@@ -63,6 +64,15 @@ func NewContainer(ctx context.Context, platform rproc.Platform, r *task.CreateTa
6364
Options: m.Options,
6465
})
6566
}
67+
68+
rootfs := ""
69+
if len(mounts) > 0 {
70+
rootfs = filepath.Join(r.Bundle, "rootfs")
71+
if err := os.Mkdir(rootfs, 0711); err != nil {
72+
return nil, err
73+
}
74+
}
75+
6676
config := &proc.CreateConfig{
6777
ID: r.ID,
6878
Bundle: r.Bundle,
@@ -80,7 +90,6 @@ func NewContainer(ctx context.Context, platform rproc.Platform, r *task.CreateTa
8090
if err := WriteRuntime(r.Bundle, opts.BinaryName); err != nil {
8191
return nil, err
8292
}
83-
rootfs := filepath.Join(r.Bundle, "rootfs")
8493
defer func() {
8594
if err != nil {
8695
if err2 := mount.UnmountAll(rootfs, 0); err2 != nil {
@@ -107,6 +116,7 @@ func NewContainer(ctx context.Context, platform rproc.Platform, r *task.CreateTa
107116
platform,
108117
config,
109118
&opts,
119+
rootfs,
110120
)
111121
if err != nil {
112122
return nil, errdefs.ToGRPC(err)
@@ -146,8 +156,7 @@ func WriteRuntime(path, runtime string) error {
146156
}
147157

148158
func newInit(ctx context.Context, path, workDir, namespace string, platform rproc.Platform,
149-
r *proc.CreateConfig, options *options.Options) (*proc.Init, error) {
150-
rootfs := filepath.Join(path, "rootfs")
159+
r *proc.CreateConfig, options *options.Options, rootfs string) (*proc.Init, error) {
151160
runtime := proc.NewRunc(options.Root, path, namespace, options.BinaryName, options.CriuPath, options.SystemdCgroup)
152161
p := proc.New(r.ID, runtime, rproc.Stdio{
153162
Stdin: r.Stdin,

0 commit comments

Comments
 (0)