mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
pkg/mount: refactor Unmount()
It has been pointed out that we're ignoring EINVAL from umount(2) everywhere, so let's move it to a lower-level function. Also, its implementation should be the same for any UNIX incarnation, so let's consolidate it. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
parent
2f98b5f51f
commit
90be078fe5
4 changed files with 22 additions and 32 deletions
|
@ -3,7 +3,6 @@ package mount // import "github.com/docker/docker/pkg/mount"
|
||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
@ -89,12 +88,7 @@ func ForceMount(device, target, mType, options string) error {
|
||||||
// Unmount lazily unmounts a filesystem on supported platforms, otherwise
|
// Unmount lazily unmounts a filesystem on supported platforms, otherwise
|
||||||
// does a normal unmount.
|
// does a normal unmount.
|
||||||
func Unmount(target string) error {
|
func Unmount(target string) error {
|
||||||
err := unmount(target, mntDetach)
|
return unmount(target, mntDetach)
|
||||||
if err == syscall.EINVAL {
|
|
||||||
// ignore "not mounted" error
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RecursiveUnmount unmounts the target and all mounts underneath, starting with
|
// RecursiveUnmount unmounts the target and all mounts underneath, starting with
|
||||||
|
@ -114,25 +108,14 @@ func RecursiveUnmount(target string) error {
|
||||||
logrus.Debugf("Trying to unmount %s", m.Mountpoint)
|
logrus.Debugf("Trying to unmount %s", m.Mountpoint)
|
||||||
err = unmount(m.Mountpoint, mntDetach)
|
err = unmount(m.Mountpoint, mntDetach)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If the error is EINVAL either this whole package is wrong (invalid flags passed to unmount(2)) or this is
|
if i == len(mounts)-1 { // last mount
|
||||||
// not a mountpoint (which is ok in this case).
|
|
||||||
// Meanwhile calling `Mounted()` is very expensive.
|
|
||||||
//
|
|
||||||
// We've purposefully used `syscall.EINVAL` here instead of `unix.EINVAL` to avoid platform branching
|
|
||||||
// Since `EINVAL` is defined for both Windows and Linux in the `syscall` package (and other platforms),
|
|
||||||
// this is nicer than defining a custom value that we can refer to in each platform file.
|
|
||||||
if err == syscall.EINVAL {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if i == len(mounts)-1 {
|
|
||||||
if mounted, e := Mounted(m.Mountpoint); e != nil || mounted {
|
if mounted, e := Mounted(m.Mountpoint); e != nil || mounted {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
continue
|
} else {
|
||||||
|
// This is some submount, we can ignore this error for now, the final unmount will fail if this is a real problem
|
||||||
|
logrus.WithError(err).Warnf("Failed to unmount submount %s", m.Mountpoint)
|
||||||
}
|
}
|
||||||
// This is some submount, we can ignore this error for now, the final unmount will fail if this is a real problem
|
|
||||||
logrus.WithError(err).Warnf("Failed to unmount submount %s", m.Mountpoint)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Debugf("Unmounted %s", m.Mountpoint)
|
logrus.Debugf("Unmounted %s", m.Mountpoint)
|
||||||
|
|
|
@ -14,8 +14,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func allocateIOVecs(options []string) []C.struct_iovec {
|
func allocateIOVecs(options []string) []C.struct_iovec {
|
||||||
|
@ -54,7 +52,3 @@ func mount(device, target, mType string, flag uintptr, data string) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func unmount(target string, flag int) error {
|
|
||||||
return unix.Unmount(target, flag)
|
|
||||||
}
|
|
||||||
|
|
|
@ -51,7 +51,3 @@ func mount(device, target, mType string, flags uintptr, data string) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func unmount(target string, flag int) error {
|
|
||||||
return unix.Unmount(target, flag)
|
|
||||||
}
|
|
||||||
|
|
17
pkg/mount/unmount_unix.go
Normal file
17
pkg/mount/unmount_unix.go
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package mount // import "github.com/docker/docker/pkg/mount"
|
||||||
|
|
||||||
|
import "golang.org/x/sys/unix"
|
||||||
|
|
||||||
|
func unmount(target string, flags int) error {
|
||||||
|
err := unix.Unmount(target, flags)
|
||||||
|
if err == unix.EINVAL {
|
||||||
|
// Ignore "not mounted" error here. Note the same error
|
||||||
|
// can be returned if flags are invalid, so this code
|
||||||
|
// assumes that the flags value is always correct.
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue