2018-02-05 16:05:59 -05:00
|
|
|
package mount // import "github.com/docker/docker/pkg/mount"
|
2013-12-18 19:42:49 -05:00
|
|
|
|
2017-02-14 13:35:20 -05:00
|
|
|
import (
|
|
|
|
"sort"
|
2018-10-22 21:30:34 -04:00
|
|
|
"strconv"
|
2017-02-14 13:35:20 -05:00
|
|
|
"strings"
|
2017-08-02 21:29:43 -04:00
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-02-14 13:35:20 -05:00
|
|
|
)
|
|
|
|
|
2018-10-22 21:30:34 -04:00
|
|
|
// mountError records an error from mount or unmount operation
|
|
|
|
type mountError struct {
|
|
|
|
op string
|
|
|
|
source, target string
|
|
|
|
flags uintptr
|
|
|
|
data string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *mountError) Error() string {
|
|
|
|
out := e.op + " "
|
|
|
|
|
|
|
|
if e.source != "" {
|
|
|
|
out += e.source + ":" + e.target
|
|
|
|
} else {
|
|
|
|
out += e.target
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.flags != uintptr(0) {
|
|
|
|
out += ", flags: 0x" + strconv.FormatUint(uint64(e.flags), 16)
|
|
|
|
}
|
|
|
|
if e.data != "" {
|
|
|
|
out += ", data: " + e.data
|
|
|
|
}
|
|
|
|
|
|
|
|
out += ": " + e.err.Error()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cause returns the underlying cause of the error
|
|
|
|
func (e *mountError) Cause() error {
|
|
|
|
return e.err
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:56:32 -05:00
|
|
|
// FilterFunc is a type defining a callback function
|
|
|
|
// to filter out unwanted entries. It takes a pointer
|
|
|
|
// to an Info struct (not fully populated, currently
|
|
|
|
// only Mountpoint is filled in), and returns two booleans:
|
|
|
|
// - skip: true if the entry should be skipped
|
|
|
|
// - stop: true if parsing should be stopped after the entry
|
|
|
|
type FilterFunc func(*Info) (skip, stop bool)
|
|
|
|
|
|
|
|
// PrefixFilter discards all entries whose mount points
|
|
|
|
// do not start with a prefix specified
|
|
|
|
func PrefixFilter(prefix string) FilterFunc {
|
|
|
|
return func(m *Info) (bool, bool) {
|
|
|
|
skip := !strings.HasPrefix(m.Mountpoint, prefix)
|
|
|
|
return skip, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SingleEntryFilter looks for a specific entry
|
|
|
|
func SingleEntryFilter(mp string) FilterFunc {
|
|
|
|
return func(m *Info) (bool, bool) {
|
|
|
|
if m.Mountpoint == mp {
|
|
|
|
return false, true // don't skip, stop now
|
|
|
|
}
|
|
|
|
return true, false // skip, keep going
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
getSourceMount(): simplify
The flow of getSourceMount was:
1 get all entries from /proc/self/mountinfo
2 do a linear search for the `source` directory
3 if found, return its data
4 get the parent directory of `source`, goto 2
The repeated linear search through the whole mountinfo (which can have
thousands of records) is inefficient. Instead, let's just
1 collect all the relevant records (only those mount points
that can be a parent of `source`)
2 find the record with the longest mountpath, return its data
This was tested manually with something like
```go
func TestGetSourceMount(t *testing.T) {
mnt, flags, err := getSourceMount("/sys/devices/msr/")
assert.NoError(t, err)
t.Logf("mnt: %v, flags: %v", mnt, flags)
}
```
...but it relies on having a specific mount points on the system
being used for testing.
[v2: add unit tests for ParentsFilter]
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-01-25 23:13:46 -05:00
|
|
|
// ParentsFilter returns all entries whose mount points
|
|
|
|
// can be parents of a path specified, discarding others.
|
|
|
|
// For example, given `/var/lib/docker/something`, entries
|
|
|
|
// like `/var/lib/docker`, `/var` and `/` are returned.
|
|
|
|
func ParentsFilter(path string) FilterFunc {
|
|
|
|
return func(m *Info) (bool, bool) {
|
|
|
|
skip := !strings.HasPrefix(path, m.Mountpoint)
|
|
|
|
return skip, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:56:32 -05:00
|
|
|
// GetMounts retrieves a list of mounts for the current running process,
|
|
|
|
// with an optional filter applied (use nil for no filter).
|
|
|
|
func GetMounts(f FilterFunc) ([]*Info, error) {
|
|
|
|
return parseMountTable(f)
|
2013-12-21 11:02:06 -05:00
|
|
|
}
|
|
|
|
|
2016-03-25 19:38:00 -04:00
|
|
|
// Mounted determines if a specified mountpoint has been mounted.
|
2017-11-01 19:37:53 -04:00
|
|
|
// On Linux it looks at /proc/self/mountinfo.
|
2013-12-18 19:42:49 -05:00
|
|
|
func Mounted(mountpoint string) (bool, error) {
|
2018-01-19 14:56:32 -05:00
|
|
|
entries, err := GetMounts(SingleEntryFilter(mountpoint))
|
2013-12-18 19:42:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:56:32 -05:00
|
|
|
return len(entries) > 0, nil
|
2013-12-18 19:42:49 -05:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-10-25 13:44:28 -04: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 {
|
2018-01-19 14:56:32 -05:00
|
|
|
mounts, err := parseMountTable(PrefixFilter(target))
|
2017-02-14 13:35:20 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the deepest mount be first
|
2018-01-24 23:02:23 -05:00
|
|
|
sort.Slice(mounts, func(i, j int) bool {
|
|
|
|
return len(mounts[i].Mountpoint) > len(mounts[j].Mountpoint)
|
|
|
|
})
|
2017-02-14 13:35:20 -05:00
|
|
|
|
|
|
|
for i, m := range mounts {
|
2017-09-22 09:52:41 -04:00
|
|
|
logrus.Debugf("Trying to unmount %s", m.Mountpoint)
|
2017-08-02 21:29:43 -04:00
|
|
|
err = unmount(m.Mountpoint, mntDetach)
|
|
|
|
if err != nil {
|
2018-10-25 13:44:28 -04:00
|
|
|
if i == len(mounts)-1 { // last mount
|
2017-08-02 21:29:43 -04:00
|
|
|
if mounted, e := Mounted(m.Mountpoint); e != nil || mounted {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-25 13:44:28 -04:00
|
|
|
} else {
|
|
|
|
// This is some submount, we can ignore this error for now, the final unmount will fail if this is a real problem
|
|
|
|
logrus.WithError(err).Warnf("Failed to unmount submount %s", m.Mountpoint)
|
2017-02-14 13:35:20 -05:00
|
|
|
}
|
|
|
|
}
|
2017-08-02 21:29:43 -04:00
|
|
|
|
|
|
|
logrus.Debugf("Unmounted %s", m.Mountpoint)
|
2017-02-14 13:35:20 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|