Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@ func Mount(device, target, mType, options string) error {
return mount(device, target, mType, uintptr(flag), data)
}

// Unmount lazily unmounts a filesystem on supported platforms, otherwise
// does a normal unmount.
// 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 {
return unmount(target, mntDetach)
}

// RecursiveUnmount unmounts the target and all mounts underneath, starting with
// the deepsest mount first.
// 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 := unmountBare(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
Expand Down
6 changes: 5 additions & 1 deletion mount/unmount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ package mount

import "golang.org/x/sys/unix"

func unmountBare(target string, flags int) error {
return unix.Unmount(target, flags)
}

func unmount(target string, flags int) error {
err := unix.Unmount(target, flags)
err := unmountBare(target, flags)
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
Expand Down
6 changes: 5 additions & 1 deletion mount/unmount_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

package mount

func unmount(target string, flag int) error {
func unmountBare(_ string, _ int) error {
panic("Not implemented")
}

func unmount(_ string, _ int) error {
panic("Not implemented")
}