2016-11-22 19:32:10 +00:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
2017-09-12 23:29:24 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-11-22 19:32:10 +00:00
|
|
|
"time"
|
2017-09-12 23:29:24 +00:00
|
|
|
|
|
|
|
"github.com/containers/storage/pkg/fileutils"
|
2016-11-22 19:32:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetMounts retrieves a list of mounts for the current running process.
|
|
|
|
func GetMounts() ([]*Info, error) {
|
|
|
|
return parseMountTable()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mounted determines if a specified mountpoint has been mounted.
|
|
|
|
// On Linux it looks at /proc/self/mountinfo and on Solaris at mnttab.
|
|
|
|
func Mounted(mountpoint string) (bool, error) {
|
|
|
|
entries, err := parseMountTable()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
mountpoint, err = fileutils.ReadSymlinkedDirectory(mountpoint)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2016-11-22 19:32:10 +00:00
|
|
|
// Search the table for the mountpoint
|
|
|
|
for _, e := range entries {
|
|
|
|
if e.Mountpoint == mountpoint {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func Mount(device, target, mType, options string) error {
|
|
|
|
flag, _ := parseOptions(options)
|
|
|
|
if flag&REMOUNT != REMOUNT {
|
|
|
|
if mounted, err := Mounted(target); err != nil || mounted {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ForceMount(device, target, mType, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func ForceMount(device, target, mType, options string) error {
|
|
|
|
flag, data := parseOptions(options)
|
2017-09-12 23:29:24 +00:00
|
|
|
return mount(device, target, mType, uintptr(flag), data)
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00:00
|
|
|
// Unmount lazily unmounts a filesystem on supported platforms, otherwise
|
|
|
|
// does a normal unmount.
|
2016-11-22 19:32:10 +00:00
|
|
|
func Unmount(target string) error {
|
|
|
|
if mounted, err := Mounted(target); err != nil || !mounted {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ForceUnmount(target)
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:29:24 +00: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
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:32:10 +00:00
|
|
|
// ForceUnmount will force an unmount of the target filesystem, regardless if
|
|
|
|
// it is mounted or not.
|
|
|
|
func ForceUnmount(target string) (err error) {
|
|
|
|
// 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)
|
|
|
|
}
|
2017-09-12 23:29:24 +00:00
|
|
|
return nil
|
2016-11-22 19:32:10 +00:00
|
|
|
}
|