Skip to content

Commit c0f0b21

Browse files
Apply PR feedback
* Rootfs dir is created during container creation not during bundle creation * Add support for v2 * UnmountAll is a no-op when the path to unmount (i.e. the rootfs dir) does not exist or is invalid Co-authored-by: Danail Branekov <[email protected]> Signed-off-by: Georgi Sabev <[email protected]>
1 parent 2a5e4c4 commit c0f0b21

6 files changed

Lines changed: 29 additions & 23 deletions

File tree

mount/mount_linux.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ func unmount(target string, flags int) error {
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.
114114
func UnmountAll(mount string, flags int) error {
115+
if _, err := os.Stat(mount); err != nil {
116+
return nil
117+
}
115118
for {
116119
if err := unmount(mount, flags); err != nil {
117120
// 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/linux/proc/init.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,10 @@ func (p *Init) delete(ctx context.Context) error {
304304
}
305305
p.io.Close()
306306
}
307-
if p.Rootfs != "" {
308-
if err2 := mount.UnmountAll(p.Rootfs, 0); err2 != nil {
309-
log.G(ctx).WithError(err2).Warn("failed to cleanup rootfs mount")
310-
if err == nil {
311-
err = errors.Wrap(err2, "failed rootfs umount")
312-
}
307+
if err2 := mount.UnmountAll(p.Rootfs, 0); err2 != nil {
308+
log.G(ctx).WithError(err2).Warn("failed to cleanup rootfs mount")
309+
if err == nil {
310+
err = errors.Wrap(err2, "failed rootfs umount")
313311
}
314312
}
315313
return err

runtime/v1/shim/service.go

Lines changed: 10 additions & 7 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,14 +145,13 @@ 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")
141-
defer func(rootfs string) {
148+
defer func() {
142149
if err != nil {
143150
if err2 := mount.UnmountAll(rootfs, 0); err2 != nil {
144151
log.G(ctx).WithError(err2).Warn("Failed to cleanup rootfs mount")
145152
}
146153
}
147-
}(rootfs)
154+
}()
148155
for _, rm := range mounts {
149156
m := &mount.Mount{
150157
Type: rm.Type,
@@ -159,10 +166,6 @@ func (s *Service) Create(ctx context.Context, r *shimapi.CreateTaskRequest) (_ *
159166
s.mu.Lock()
160167
defer s.mu.Unlock()
161168

162-
if len(mounts) == 0 {
163-
rootfs = ""
164-
}
165-
166169
process, err := newInit(
167170
ctx,
168171
s.config.Path,

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)