Call temp mounts and unmount in containerd server
Fixes #2004 Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
bc974a7a32
commit
b2ec177bb2
@ -12,6 +12,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/containerd/containerd/log"
|
"github.com/containerd/containerd/log"
|
||||||
|
"github.com/containerd/containerd/mount"
|
||||||
"github.com/containerd/containerd/server"
|
"github.com/containerd/containerd/server"
|
||||||
"github.com/containerd/containerd/sys"
|
"github.com/containerd/containerd/sys"
|
||||||
"github.com/containerd/containerd/version"
|
"github.com/containerd/containerd/version"
|
||||||
@ -95,6 +96,14 @@ func App() *cli.App {
|
|||||||
if err := applyFlags(context, config); err != nil {
|
if err := applyFlags(context, config); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// cleanup temp mounts
|
||||||
|
if err := mount.SetTempMountLocation(filepath.Join(config.Root, "tmpmounts")); err != nil {
|
||||||
|
return errors.Wrap(err, "creating temp mount location")
|
||||||
|
}
|
||||||
|
// unmount all temp mounts on boot for the server
|
||||||
|
if err := mount.CleanupTempMounts(0); err != nil {
|
||||||
|
return errors.Wrap(err, "unmounting temp mounts")
|
||||||
|
}
|
||||||
address := config.GRPC.Address
|
address := config.GRPC.Address
|
||||||
if address == "" {
|
if address == "" {
|
||||||
return errors.New("grpc address cannot be empty")
|
return errors.New("grpc address cannot be empty")
|
||||||
|
@ -188,7 +188,7 @@ func withRemappedSnapshotBase(id string, i Image, uid, gid uint32, readonly bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
func remapRootFS(ctx context.Context, mounts []mount.Mount, uid, gid uint32) error {
|
func remapRootFS(ctx context.Context, mounts []mount.Mount, uid, gid uint32) error {
|
||||||
return mount.DefaultTempLocation.Mount(ctx, mounts, func(root string) error {
|
return mount.WithTempMount(ctx, mounts, func(root string) error {
|
||||||
return filepath.Walk(root, incrementFS(root, uid, gid))
|
return filepath.Walk(root, incrementFS(root, uid, gid))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ func (s *fsApplier) Apply(ctx context.Context, desc ocispec.Descriptor, mounts [
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ocidesc ocispec.Descriptor
|
var ocidesc ocispec.Descriptor
|
||||||
if err := mount.DefaultTempLocation.Mount(ctx, mounts, func(root string) error {
|
if err := mount.WithTempMount(ctx, mounts, func(root string) error {
|
||||||
ra, err := s.store.ReaderAt(ctx, desc.Digest)
|
ra, err := s.store.ReaderAt(ctx, desc.Digest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to get reader from content store")
|
return errors.Wrap(err, "failed to get reader from content store")
|
||||||
|
@ -62,8 +62,8 @@ func (s *walkingDiff) Compare(ctx context.Context, lower, upper []mount.Mount, o
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ocidesc ocispec.Descriptor
|
var ocidesc ocispec.Descriptor
|
||||||
if err := mount.DefaultTempLocation.Mount(ctx, lower, func(lowerRoot string) error {
|
if err := mount.WithTempMount(ctx, lower, func(lowerRoot string) error {
|
||||||
return mount.DefaultTempLocation.Mount(ctx, upper, func(upperRoot string) error {
|
return mount.WithTempMount(ctx, upper, func(upperRoot string) error {
|
||||||
var newReference bool
|
var newReference bool
|
||||||
if config.Reference == "" {
|
if config.Reference == "" {
|
||||||
newReference = true
|
newReference = true
|
||||||
|
@ -4,46 +4,18 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/containerd/containerd/log"
|
"github.com/containerd/containerd/log"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
var tempMountLocation = os.TempDir()
|
||||||
t, err := TempLocation("/tmp")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
DefaultTempLocation = t
|
|
||||||
}
|
|
||||||
|
|
||||||
var DefaultTempLocation TempMounts
|
|
||||||
|
|
||||||
func TempLocation(root string) (TempMounts, error) {
|
|
||||||
root, err := filepath.Abs(root)
|
|
||||||
if err != nil {
|
|
||||||
return DefaultTempLocation, err
|
|
||||||
}
|
|
||||||
if err := os.MkdirAll(root, 0700); err != nil {
|
|
||||||
return DefaultTempLocation, err
|
|
||||||
}
|
|
||||||
return TempMounts{
|
|
||||||
root: root,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type TempMounts struct {
|
|
||||||
root string
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithTempMount mounts the provided mounts to a temp dir, and pass the temp dir to f.
|
// WithTempMount mounts the provided mounts to a temp dir, and pass the temp dir to f.
|
||||||
// The mounts are valid during the call to the f.
|
// The mounts are valid during the call to the f.
|
||||||
// Finally we will unmount and remove the temp dir regardless of the result of f.
|
// Finally we will unmount and remove the temp dir regardless of the result of f.
|
||||||
func (t TempMounts) Mount(ctx context.Context, mounts []Mount, f func(root string) error) (err error) {
|
func WithTempMount(ctx context.Context, mounts []Mount, f func(root string) error) (err error) {
|
||||||
root, uerr := ioutil.TempDir(t.root, "containerd-WithTempMount")
|
root, uerr := ioutil.TempDir(tempMountLocation, "containerd-mount")
|
||||||
if uerr != nil {
|
if uerr != nil {
|
||||||
return errors.Wrapf(uerr, "failed to create temp dir")
|
return errors.Wrapf(uerr, "failed to create temp dir")
|
||||||
}
|
}
|
||||||
@ -76,43 +48,3 @@ func (t TempMounts) Mount(ctx context.Context, mounts []Mount, f func(root strin
|
|||||||
}
|
}
|
||||||
return errors.Wrapf(f(root), "mount callback failed on %s", root)
|
return errors.Wrapf(f(root), "mount callback failed on %s", root)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmount all temp mounts and remove the directories
|
|
||||||
func (t TempMounts) Unmount(flags int) error {
|
|
||||||
mounts, err := PID(os.Getpid())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
var toUnmount []string
|
|
||||||
for _, m := range mounts {
|
|
||||||
if strings.HasPrefix(m.Mountpoint, t.root) {
|
|
||||||
toUnmount = append(toUnmount, m.Mountpoint)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Sort(sort.Reverse(mountSorter(toUnmount)))
|
|
||||||
for _, path := range toUnmount {
|
|
||||||
if err := UnmountAll(path, flags); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := os.Remove(path); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type mountSorter []string
|
|
||||||
|
|
||||||
func (by mountSorter) Len() int {
|
|
||||||
return len(by)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (by mountSorter) Less(i, j int) bool {
|
|
||||||
is := strings.Split(by[i], string(os.PathSeparator))
|
|
||||||
js := strings.Split(by[j], string(os.PathSeparator))
|
|
||||||
return len(is) < len(js)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (by mountSorter) Swap(i, j int) {
|
|
||||||
by[i], by[j] = by[j], by[i]
|
|
||||||
}
|
|
||||||
|
47
mount/temp_unix.go
Normal file
47
mount/temp_unix.go
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package mount
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SetTempMountLocation sets the temporary mount location
|
||||||
|
func SetTempMountLocation(root string) error {
|
||||||
|
root, err := filepath.Abs(root)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := os.MkdirAll(root, 0700); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tempMountLocation = root
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CleanupTempMounts all temp mounts and remove the directories
|
||||||
|
func CleanupTempMounts(flags int) error {
|
||||||
|
mounts, err := Self()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var toUnmount []string
|
||||||
|
for _, m := range mounts {
|
||||||
|
if strings.HasPrefix(m.Mountpoint, tempMountLocation) {
|
||||||
|
toUnmount = append(toUnmount, m.Mountpoint)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Sort(sort.Reverse(sort.StringSlice(toUnmount)))
|
||||||
|
for _, path := range toUnmount {
|
||||||
|
if err := UnmountAll(path, flags); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := os.Remove(path); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
13
mount/temp_unsupported.go
Normal file
13
mount/temp_unsupported.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// +build windows
|
||||||
|
|
||||||
|
package mount
|
||||||
|
|
||||||
|
// SetTempMountLocation sets the temporary mount location
|
||||||
|
func SetTempMountLocation(root string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CleanupTempMounts all temp mounts and remove the directories
|
||||||
|
func CleanupTempMounts(flags int) error {
|
||||||
|
return nil
|
||||||
|
}
|
@ -287,7 +287,7 @@ func WithUserID(uid uint32) SpecOpts {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return mount.DefaultTempLocation.Mount(ctx, mounts, func(root string) error {
|
return mount.WithTempMount(ctx, mounts, func(root string) error {
|
||||||
uuid, ugid, err := getUIDGIDFromPath(root, func(u user.User) bool {
|
uuid, ugid, err := getUIDGIDFromPath(root, func(u user.User) bool {
|
||||||
return u.Uid == int(uid)
|
return u.Uid == int(uid)
|
||||||
})
|
})
|
||||||
@ -334,7 +334,7 @@ func WithUsername(username string) SpecOpts {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return mount.DefaultTempLocation.Mount(ctx, mounts, func(root string) error {
|
return mount.WithTempMount(ctx, mounts, func(root string) error {
|
||||||
uid, gid, err := getUIDGIDFromPath(root, func(u user.User) bool {
|
uid, gid, err := getUIDGIDFromPath(root, func(u user.User) bool {
|
||||||
return u.Name == username
|
return u.Name == username
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user