Skip to content

Commit b2ec177

Browse files
committed
Call temp mounts and unmount in containerd server
Fixes containerd#2004 Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
1 parent bc974a7 commit b2ec177

File tree

8 files changed

+78
-77
lines changed

8 files changed

+78
-77
lines changed

cmd/containerd/command/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/containerd/containerd/log"
15+
"github.com/containerd/containerd/mount"
1516
"github.com/containerd/containerd/server"
1617
"github.com/containerd/containerd/sys"
1718
"github.com/containerd/containerd/version"
@@ -95,6 +96,14 @@ func App() *cli.App {
9596
if err := applyFlags(context, config); err != nil {
9697
return err
9798
}
99+
// cleanup temp mounts
100+
if err := mount.SetTempMountLocation(filepath.Join(config.Root, "tmpmounts")); err != nil {
101+
return errors.Wrap(err, "creating temp mount location")
102+
}
103+
// unmount all temp mounts on boot for the server
104+
if err := mount.CleanupTempMounts(0); err != nil {
105+
return errors.Wrap(err, "unmounting temp mounts")
106+
}
98107
address := config.GRPC.Address
99108
if address == "" {
100109
return errors.New("grpc address cannot be empty")

container_opts_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func withRemappedSnapshotBase(id string, i Image, uid, gid uint32, readonly bool
188188
}
189189

190190
func remapRootFS(ctx context.Context, mounts []mount.Mount, uid, gid uint32) error {
191-
return mount.DefaultTempLocation.Mount(ctx, mounts, func(root string) error {
191+
return mount.WithTempMount(ctx, mounts, func(root string) error {
192192
return filepath.Walk(root, incrementFS(root, uid, gid))
193193
})
194194
}

diff/apply/apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (s *fsApplier) Apply(ctx context.Context, desc ocispec.Descriptor, mounts [
5656
}
5757

5858
var ocidesc ocispec.Descriptor
59-
if err := mount.DefaultTempLocation.Mount(ctx, mounts, func(root string) error {
59+
if err := mount.WithTempMount(ctx, mounts, func(root string) error {
6060
ra, err := s.store.ReaderAt(ctx, desc.Digest)
6161
if err != nil {
6262
return errors.Wrap(err, "failed to get reader from content store")

diff/walking/differ.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func (s *walkingDiff) Compare(ctx context.Context, lower, upper []mount.Mount, o
6262
}
6363

6464
var ocidesc ocispec.Descriptor
65-
if err := mount.DefaultTempLocation.Mount(ctx, lower, func(lowerRoot string) error {
66-
return mount.DefaultTempLocation.Mount(ctx, upper, func(upperRoot string) error {
65+
if err := mount.WithTempMount(ctx, lower, func(lowerRoot string) error {
66+
return mount.WithTempMount(ctx, upper, func(upperRoot string) error {
6767
var newReference bool
6868
if config.Reference == "" {
6969
newReference = true

mount/temp.go

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,18 @@ import (
44
"context"
55
"io/ioutil"
66
"os"
7-
"path/filepath"
8-
"sort"
9-
"strings"
107

118
"github.com/containerd/containerd/log"
129
"github.com/pkg/errors"
1310
)
1411

15-
func init() {
16-
t, err := TempLocation("/tmp")
17-
if err != nil {
18-
panic(err)
19-
}
20-
DefaultTempLocation = t
21-
}
22-
23-
var DefaultTempLocation TempMounts
24-
25-
func TempLocation(root string) (TempMounts, error) {
26-
root, err := filepath.Abs(root)
27-
if err != nil {
28-
return DefaultTempLocation, err
29-
}
30-
if err := os.MkdirAll(root, 0700); err != nil {
31-
return DefaultTempLocation, err
32-
}
33-
return TempMounts{
34-
root: root,
35-
}, nil
36-
}
37-
38-
type TempMounts struct {
39-
root string
40-
}
12+
var tempMountLocation = os.TempDir()
4113

4214
// WithTempMount mounts the provided mounts to a temp dir, and pass the temp dir to f.
4315
// The mounts are valid during the call to the f.
4416
// Finally we will unmount and remove the temp dir regardless of the result of f.
45-
func (t TempMounts) Mount(ctx context.Context, mounts []Mount, f func(root string) error) (err error) {
46-
root, uerr := ioutil.TempDir(t.root, "containerd-WithTempMount")
17+
func WithTempMount(ctx context.Context, mounts []Mount, f func(root string) error) (err error) {
18+
root, uerr := ioutil.TempDir(tempMountLocation, "containerd-mount")
4719
if uerr != nil {
4820
return errors.Wrapf(uerr, "failed to create temp dir")
4921
}
@@ -76,43 +48,3 @@ func (t TempMounts) Mount(ctx context.Context, mounts []Mount, f func(root strin
7648
}
7749
return errors.Wrapf(f(root), "mount callback failed on %s", root)
7850
}
79-
80-
// Unmount all temp mounts and remove the directories
81-
func (t TempMounts) Unmount(flags int) error {
82-
mounts, err := PID(os.Getpid())
83-
if err != nil {
84-
return err
85-
}
86-
var toUnmount []string
87-
for _, m := range mounts {
88-
if strings.HasPrefix(m.Mountpoint, t.root) {
89-
toUnmount = append(toUnmount, m.Mountpoint)
90-
}
91-
}
92-
sort.Sort(sort.Reverse(mountSorter(toUnmount)))
93-
for _, path := range toUnmount {
94-
if err := UnmountAll(path, flags); err != nil {
95-
return err
96-
}
97-
if err := os.Remove(path); err != nil {
98-
return err
99-
}
100-
}
101-
return nil
102-
}
103-
104-
type mountSorter []string
105-
106-
func (by mountSorter) Len() int {
107-
return len(by)
108-
}
109-
110-
func (by mountSorter) Less(i, j int) bool {
111-
is := strings.Split(by[i], string(os.PathSeparator))
112-
js := strings.Split(by[j], string(os.PathSeparator))
113-
return len(is) < len(js)
114-
}
115-
116-
func (by mountSorter) Swap(i, j int) {
117-
by[i], by[j] = by[j], by[i]
118-
}

mount/temp_unix.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// +build !windows
2+
3+
package mount
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
"sort"
9+
"strings"
10+
)
11+
12+
// SetTempMountLocation sets the temporary mount location
13+
func SetTempMountLocation(root string) error {
14+
root, err := filepath.Abs(root)
15+
if err != nil {
16+
return err
17+
}
18+
if err := os.MkdirAll(root, 0700); err != nil {
19+
return err
20+
}
21+
tempMountLocation = root
22+
return nil
23+
}
24+
25+
// CleanupTempMounts all temp mounts and remove the directories
26+
func CleanupTempMounts(flags int) error {
27+
mounts, err := Self()
28+
if err != nil {
29+
return err
30+
}
31+
var toUnmount []string
32+
for _, m := range mounts {
33+
if strings.HasPrefix(m.Mountpoint, tempMountLocation) {
34+
toUnmount = append(toUnmount, m.Mountpoint)
35+
}
36+
}
37+
sort.Sort(sort.Reverse(sort.StringSlice(toUnmount)))
38+
for _, path := range toUnmount {
39+
if err := UnmountAll(path, flags); err != nil {
40+
return err
41+
}
42+
if err := os.Remove(path); err != nil {
43+
return err
44+
}
45+
}
46+
return nil
47+
}

mount/temp_unsupported.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// +build windows
2+
3+
package mount
4+
5+
// SetTempMountLocation sets the temporary mount location
6+
func SetTempMountLocation(root string) error {
7+
return nil
8+
}
9+
10+
// CleanupTempMounts all temp mounts and remove the directories
11+
func CleanupTempMounts(flags int) error {
12+
return nil
13+
}

oci/spec_opts_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func WithUserID(uid uint32) SpecOpts {
287287
if err != nil {
288288
return err
289289
}
290-
return mount.DefaultTempLocation.Mount(ctx, mounts, func(root string) error {
290+
return mount.WithTempMount(ctx, mounts, func(root string) error {
291291
uuid, ugid, err := getUIDGIDFromPath(root, func(u user.User) bool {
292292
return u.Uid == int(uid)
293293
})
@@ -334,7 +334,7 @@ func WithUsername(username string) SpecOpts {
334334
if err != nil {
335335
return err
336336
}
337-
return mount.DefaultTempLocation.Mount(ctx, mounts, func(root string) error {
337+
return mount.WithTempMount(ctx, mounts, func(root string) error {
338338
uid, gid, err := getUIDGIDFromPath(root, func(u user.User) bool {
339339
return u.Name == username
340340
})

0 commit comments

Comments
 (0)