2013-12-18 19:42:49 -05:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-12-21 11:02:06 -05:00
|
|
|
func GetMounts() ([]*MountInfo, error) {
|
|
|
|
return parseMountTable()
|
|
|
|
}
|
|
|
|
|
2013-12-18 19:42:49 -05:00
|
|
|
// Looks at /proc/self/mountinfo to determine of the specified
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2014-01-13 14:13:49 -05:00
|
|
|
// Mount the specified options at the target path only if
|
|
|
|
// the target is not mounted
|
2013-12-18 19:42:49 -05:00
|
|
|
// Options must be specified as fstab style
|
|
|
|
func Mount(device, target, mType, options string) error {
|
|
|
|
if mounted, err := Mounted(target); err != nil || mounted {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-13 14:13:49 -05:00
|
|
|
return ForceMount(device, target, mType, options)
|
|
|
|
}
|
2013-12-18 19:42:49 -05:00
|
|
|
|
2014-01-13 14:13:49 -05:00
|
|
|
// Mount the specified options at the target path
|
|
|
|
// reguardless if the target is mounted or not
|
|
|
|
// Options must be specified as fstab style
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmount the target only if 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
|
|
|
|
2014-01-13 14:13:49 -05:00
|
|
|
// Unmount the target reguardless if it is mounted or not
|
|
|
|
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
|
|
|
|
}
|