2013-12-18 19:42:49 -05:00
|
|
|
package mount
|
|
|
|
|
2017-02-14 13:35:20 -05:00
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-03-28 09:29:33 -04:00
|
|
|
// GetMounts retrieves a list of mounts for the current running process.
|
2015-07-21 13:49:42 -04:00
|
|
|
func GetMounts() ([]*Info, error) {
|
2013-12-21 11:02:06 -05:00
|
|
|
return parseMountTable()
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:38:00 -04:00
|
|
|
// Mounted determines if a specified mountpoint has been mounted.
|
|
|
|
// On Linux it looks at /proc/self/mountinfo and on Solaris at mnttab.
|
2013-12-18 19:42:49 -05:00
|
|
|
func Mounted(mountpoint string) (bool, error) {
|
|
|
|
entries, err := parseMountTable()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search the table for the mountpoint
|
|
|
|
for _, e := range entries {
|
2013-12-21 11:02:06 -05:00
|
|
|
if e.Mountpoint == mountpoint {
|
2013-12-18 19:42:49 -05:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-03-28 09:29:33 -04:00
|
|
|
// Mount will mount filesystem according to the specified configuration, on the
|
|
|
|
// condition that the target path is *not* already mounted. Options must be
|
|
|
|
// specified like the mount or fstab unix commands: "opt1=val1,opt2=val2". See
|
|
|
|
// flags.go for supported option flags.
|
2013-12-18 19:42:49 -05:00
|
|
|
func Mount(device, target, mType, options string) error {
|
2014-06-25 04:15:08 -04:00
|
|
|
flag, _ := parseOptions(options)
|
|
|
|
if flag&REMOUNT != REMOUNT {
|
|
|
|
if mounted, err := Mounted(target); err != nil || mounted {
|
|
|
|
return err
|
|
|
|
}
|
2013-12-18 19:42:49 -05:00
|
|
|
}
|
2014-01-13 14:13:49 -05:00
|
|
|
return ForceMount(device, target, mType, options)
|
|
|
|
}
|
2013-12-18 19:42:49 -05:00
|
|
|
|
2015-03-28 09:29:33 -04:00
|
|
|
// ForceMount will mount a filesystem according to the specified configuration,
|
|
|
|
// *regardless* if the target path is not already mounted. Options must be
|
|
|
|
// specified like the mount or fstab unix commands: "opt1=val1,opt2=val2". See
|
|
|
|
// flags.go for supported option flags.
|
2014-01-13 14:13:49 -05:00
|
|
|
func ForceMount(device, target, mType, options string) error {
|
2013-12-18 19:42:49 -05:00
|
|
|
flag, data := parseOptions(options)
|
2017-03-30 05:26:16 -04:00
|
|
|
return mount(device, target, mType, uintptr(flag), data)
|
2013-12-18 19:42:49 -05:00
|
|
|
}
|
|
|
|
|
2017-02-27 11:32:49 -05:00
|
|
|
// Unmount lazily unmounts a filesystem on supported platforms, otherwise
|
|
|
|
// does a normal unmount.
|
2014-01-13 14:13:49 -05:00
|
|
|
func Unmount(target string) error {
|
2013-12-18 19:42:49 -05:00
|
|
|
if mounted, err := Mounted(target); err != nil || !mounted {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-27 11:32:49 -05:00
|
|
|
return unmount(target, mntDetach)
|
2013-12-18 19:42:49 -05:00
|
|
|
}
|
2017-02-14 13:35:20 -05:00
|
|
|
|
|
|
|
// RecursiveUnmount unmounts the target and all mounts underneath, starting with
|
|
|
|
// the deepsest mount first.
|
|
|
|
func RecursiveUnmount(target string) error {
|
|
|
|
mounts, err := GetMounts()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the deepest mount be first
|
|
|
|
sort.Sort(sort.Reverse(byMountpoint(mounts)))
|
|
|
|
|
|
|
|
for i, m := range mounts {
|
|
|
|
if !strings.HasPrefix(m.Mountpoint, target) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := Unmount(m.Mountpoint); err != nil && i == len(mounts)-1 {
|
|
|
|
if mounted, err := Mounted(m.Mountpoint); err != nil || mounted {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Ignore errors for submounts and continue trying to unmount others
|
|
|
|
// The final unmount should fail if there ane any submounts remaining
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|