mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
32d506b394
full diff: https://github.com/moby/sys/compare/mountinfo/v0.1.3...mountinfo/v0.4.0 > Note that this dependency uses submodules, providing "github.com/moby/sys/mount" > and "github.com/moby/sys/mountinfo". Our vendoring tool (vndr) currently doesn't > support submodules, so we vendor the top-level moby/sys repository (which contains > both) and pick the most recent tag, which could be either `mountinfo/vXXX` or > `mount/vXXX`. github.com/moby/sys/mountinfo v0.4.0 -------------------------------------------------------------------------------- Breaking changes: - `PidMountInfo` is now deprecated and will be removed before v1.0; users should switch to `GetMountsFromReader` Fixes and improvements: - run filter after all fields are parsed - correct handling errors from bufio.Scan - documentation formatting fixes github.com/moby/sys/mountinfo v0.3.1 -------------------------------------------------------------------------------- - mount: use MNT_* flags from golang.org/x/sys/unix on freebsd - various godoc and CI fixes - mountinfo: make GetMountinfoFromReader Linux-specific - Add support for OpenBSD in addition to FreeBSD - mountinfo: use idiomatic naming for fields github.com/moby/sys/mountinfo v0.2.0 -------------------------------------------------------------------------------- Bug fixes: - Fix path unescaping for paths with double quotes Improvements: - Mounted: speed up by adding fast paths using openat2 (Linux-only) and stat - Mounted: relax path requirements (allow relative, non-cleaned paths, symlinks) - Unescape fstype and source fields - Documentation improvements Testing/CI: - Unit tests: exclude darwin - CI: run tests under Fedora 32 to test openat2 - TestGetMounts: fix for Ubuntu build system - Makefile: fix ignoring test failures - CI: add cross build github.com/moby/sys/mount v0.1.1 -------------------------------------------------------------------------------- https://github.com/moby/sys/releases/tag/mount%2Fv0.1.1 Improvements: - RecursiveUnmount: add a fast path (#26) - Unmount: improve doc - fix CI linter warning on Windows Testing/CI: - Unit tests: exclude darwin - Makefile: fix ignoring test failures - CI: add cross build Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
// +build !darwin,!windows
|
|
|
|
package mount
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
|
|
"github.com/moby/sys/mountinfo"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// Mount will mount filesystem according to the specified configuration.
|
|
// Options must be specified like the mount or fstab unix commands:
|
|
// "opt1=val1,opt2=val2". See flags.go for supported option flags.
|
|
func Mount(device, target, mType, options string) error {
|
|
flag, data := parseOptions(options)
|
|
return mount(device, target, mType, uintptr(flag), data)
|
|
}
|
|
|
|
// Unmount lazily unmounts a filesystem on supported platforms, otherwise does
|
|
// a normal unmount. If target is not a mount point, no error is returned.
|
|
func Unmount(target string) error {
|
|
err := unix.Unmount(target, mntDetach)
|
|
if err == nil || 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.
|
|
return nil
|
|
}
|
|
|
|
return &mountError{
|
|
op: "umount",
|
|
target: target,
|
|
flags: uintptr(mntDetach),
|
|
err: err,
|
|
}
|
|
}
|
|
|
|
// RecursiveUnmount unmounts the target and all mounts underneath, starting
|
|
// with the deepest mount first. The argument does not have to be a mount
|
|
// point itself.
|
|
func RecursiveUnmount(target string) error {
|
|
// Fast path, works if target is a mount point that can be unmounted.
|
|
// On Linux, mntDetach flag ensures a recursive unmount. For other
|
|
// platforms, if there are submounts, we'll get EBUSY (and fall back
|
|
// to the slow path). NOTE we do not ignore EINVAL here as target might
|
|
// not be a mount point itself (but there can be mounts underneath).
|
|
if err := unix.Unmount(target, mntDetach); err == nil {
|
|
return nil
|
|
}
|
|
|
|
// Slow path: get all submounts, sort, unmount one by one.
|
|
mounts, err := mountinfo.GetMounts(mountinfo.PrefixFilter(target))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Make the deepest mount be first
|
|
sort.Slice(mounts, func(i, j int) bool {
|
|
return len(mounts[i].Mountpoint) > len(mounts[j].Mountpoint)
|
|
})
|
|
|
|
var (
|
|
suberr error
|
|
lastMount = len(mounts) - 1
|
|
)
|
|
for i, m := range mounts {
|
|
err = Unmount(m.Mountpoint)
|
|
if err != nil {
|
|
if i == lastMount {
|
|
if suberr != nil {
|
|
return fmt.Errorf("%w (possible cause: %s)", err, suberr)
|
|
}
|
|
return err
|
|
}
|
|
// This is a submount, we can ignore the error for now,
|
|
// the final unmount will fail if this is a real problem.
|
|
// With that in mind, the _first_ failed unmount error
|
|
// might be the real error cause, so let's keep it.
|
|
if suberr == nil {
|
|
suberr = err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|