2013-12-18 19:42:49 -05:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-03-28 09:29:33 -04:00
|
|
|
// GetMounts retrieves a list of mounts for the current running process.
|
2013-12-21 11:02:06 -05:00
|
|
|
func GetMounts() ([]*MountInfo, error) {
|
|
|
|
return parseMountTable()
|
|
|
|
}
|
|
|
|
|
2015-03-28 09:29:33 -04:00
|
|
|
// Mounted looks at /proc/self/mountinfo to determine of the specified
|
2013-12-18 19:42:49 -05:00
|
|
|
// mountpoint has been mounted
|
|
|
|
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)
|
|
|
|
if err := mount(device, target, mType, uintptr(flag), data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-28 09:29:33 -04:00
|
|
|
// Unmount will unmount the target filesystem, so long as it is mounted.
|
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
|
|
|
|
}
|
2014-01-13 14:13:49 -05:00
|
|
|
return ForceUnmount(target)
|
|
|
|
}
|
2013-12-18 19:42:49 -05:00
|
|
|
|
2015-03-28 09:29:33 -04:00
|
|
|
// ForceUnmount will force an unmount of the target filesystem, regardless if
|
|
|
|
// it is mounted or not.
|
2014-01-13 14:13:49 -05:00
|
|
|
func ForceUnmount(target string) (err error) {
|
2013-12-18 19:42:49 -05:00
|
|
|
// Simple retry logic for unmount
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if err = unmount(target, 0); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|