diff --git a/mount/doc.go b/mount/doc.go new file mode 100644 index 00000000..b8108d85 --- /dev/null +++ b/mount/doc.go @@ -0,0 +1,5 @@ +// Package mount provides a set of functions to mount and unmount mounts. +// +// Currently it supports Linux. For historical reasons, there is also some support for FreeBSD. + +package mount diff --git a/mount/flags.go b/mount/flags_unix.go similarity index 99% rename from mount/flags.go rename to mount/flags_unix.go index d514a9d8..995d7280 100644 --- a/mount/flags.go +++ b/mount/flags_unix.go @@ -1,3 +1,5 @@ +// +build !darwin,!windows + package mount import ( diff --git a/mount/flags_unsupported.go b/mount/flags_unsupported.go deleted file mode 100644 index 3ec4a11e..00000000 --- a/mount/flags_unsupported.go +++ /dev/null @@ -1,30 +0,0 @@ -// +build !linux,!freebsd - -package mount - -// These flags are unsupported. -const ( - BIND = 0 - DIRSYNC = 0 - MANDLOCK = 0 - NOATIME = 0 - NODEV = 0 - NODIRATIME = 0 - NOEXEC = 0 - NOSUID = 0 - UNBINDABLE = 0 - RUNBINDABLE = 0 - PRIVATE = 0 - RPRIVATE = 0 - SHARED = 0 - RSHARED = 0 - SLAVE = 0 - RSLAVE = 0 - RBIND = 0 - RELATIME = 0 - REMOUNT = 0 - STRICTATIME = 0 - SYNCHRONOUS = 0 - RDONLY = 0 - mntDetach = 0 -) diff --git a/mount/mount.go b/mount/mount_unix.go similarity index 76% rename from mount/mount.go rename to mount/mount_unix.go index 4a7bb27b..b47ab875 100644 --- a/mount/mount.go +++ b/mount/mount_unix.go @@ -1,4 +1,4 @@ -// +build go1.13 +// +build !darwin,!windows package mount @@ -7,6 +7,7 @@ import ( "sort" "github.com/moby/sys/mountinfo" + "golang.org/x/sys/unix" ) // Mount will mount filesystem according to the specified configuration. @@ -18,9 +19,22 @@ func Mount(device, target, mType, options string) error { } // Unmount lazily unmounts a filesystem on supported platforms, otherwise does -// a normal unmount. If target is not a mount point, no error is returned. +// a normal unmount. If target is not a mount point, no error is returned. func Unmount(target string) error { - return unmount(target, mntDetach) + 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 @@ -32,7 +46,7 @@ func RecursiveUnmount(target string) error { // 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 { + if err := unix.Unmount(target, mntDetach); err == nil { return nil } @@ -49,7 +63,7 @@ func RecursiveUnmount(target string) error { var suberr error for i, m := range mounts { - err = unmount(m.Mountpoint, mntDetach) + err = Unmount(m.Mountpoint) if err != nil { if i == len(mounts)-1 { // last mount return fmt.Errorf("%w (possible cause: %s)", err, suberr) diff --git a/mount/mounter_freebsd.go b/mount/mounter_freebsd.go index 3964af4f..f4e2e0bb 100644 --- a/mount/mounter_freebsd.go +++ b/mount/mounter_freebsd.go @@ -1,3 +1,5 @@ +// +build freebsd cgo + package mount /* diff --git a/mount/mounter_unsupported.go b/mount/mounter_unsupported.go index 15380671..41ac018a 100644 --- a/mount/mounter_unsupported.go +++ b/mount/mounter_unsupported.go @@ -1,7 +1,7 @@ -// +build !linux,!freebsd freebsd,!cgo +// +build freebsd,!cgo package mount func mount(device, target, mType string, flag uintptr, data string) error { - panic("Not implemented") + panic("cgo required on freebsd") } diff --git a/mount/sharedsubtree_linux_test.go b/mount/sharedsubtree_linux_test.go index e950230a..1e2a053e 100644 --- a/mount/sharedsubtree_linux_test.go +++ b/mount/sharedsubtree_linux_test.go @@ -1,4 +1,4 @@ -// +build linux,go1.13 +// +build linux package mount diff --git a/mount/unmount_unix.go b/mount/unmount_unix.go deleted file mode 100644 index 924d059a..00000000 --- a/mount/unmount_unix.go +++ /dev/null @@ -1,26 +0,0 @@ -// +build !windows - -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 := 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 - // assumes that the flags value is always correct. - return nil - } - - return &mountError{ - op: "umount", - target: target, - flags: uintptr(flags), - err: err, - } -} diff --git a/mount/unmount_unsupported.go b/mount/unmount_unsupported.go deleted file mode 100644 index 4d6073ec..00000000 --- a/mount/unmount_unsupported.go +++ /dev/null @@ -1,11 +0,0 @@ -// +build windows - -package mount - -func unmountBare(_ string, _ int) error { - panic("Not implemented") -} - -func unmount(_ string, _ int) error { - panic("Not implemented") -}