2018-02-05 16:05:59 -05:00
|
|
|
package mount // import "github.com/docker/docker/pkg/mount"
|
2014-03-20 11:52:12 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2018-01-23 03:41:22 -05:00
|
|
|
"strconv"
|
2014-03-20 11:52:12 -04:00
|
|
|
"strings"
|
2019-03-29 11:31:27 -04:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2014-03-20 11:52:12 -04:00
|
|
|
)
|
|
|
|
|
2018-01-19 14:56:32 -05:00
|
|
|
func parseInfoFile(r io.Reader, filter FilterFunc) ([]*Info, error) {
|
2018-01-23 03:41:22 -05:00
|
|
|
s := bufio.NewScanner(r)
|
|
|
|
out := []*Info{}
|
2019-03-29 11:31:27 -04:00
|
|
|
var err error
|
2014-03-20 11:52:12 -04:00
|
|
|
for s.Scan() {
|
2019-03-29 11:31:27 -04:00
|
|
|
if err = s.Err(); err != nil {
|
2014-03-20 11:52:12 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-23 03:41:22 -05:00
|
|
|
/*
|
2019-03-29 11:31:27 -04:00
|
|
|
See http://man7.org/linux/man-pages/man5/proc.5.html
|
|
|
|
|
2018-01-23 03:41:22 -05:00
|
|
|
36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
|
|
|
|
(1)(2)(3) (4) (5) (6) (7) (8) (9) (10) (11)
|
2014-03-20 11:52:12 -04:00
|
|
|
|
2018-01-23 03:41:22 -05:00
|
|
|
(1) mount ID: unique identifier of the mount (may be reused after umount)
|
|
|
|
(2) parent ID: ID of parent (or of self for the top of the mount tree)
|
|
|
|
(3) major:minor: value of st_dev for files on filesystem
|
|
|
|
(4) root: root of the mount within the filesystem
|
|
|
|
(5) mount point: mount point relative to the process's root
|
|
|
|
(6) mount options: per mount options
|
|
|
|
(7) optional fields: zero or more fields of the form "tag[:value]"
|
|
|
|
(8) separator: marks the end of the optional fields
|
|
|
|
(9) filesystem type: name of filesystem of the form "type[.subtype]"
|
|
|
|
(10) mount source: filesystem specific information or "none"
|
|
|
|
(11) super options: per super block options
|
|
|
|
*/
|
|
|
|
|
|
|
|
text := s.Text()
|
|
|
|
fields := strings.Split(text, " ")
|
|
|
|
numFields := len(fields)
|
|
|
|
if numFields < 10 {
|
|
|
|
// should be at least 10 fields
|
|
|
|
return nil, fmt.Errorf("Parsing '%s' failed: not enough fields (%d)", text, numFields)
|
|
|
|
}
|
2014-03-20 11:52:12 -04:00
|
|
|
|
2018-01-23 03:41:22 -05:00
|
|
|
p := &Info{}
|
|
|
|
// ignore any numbers parsing errors, as there should not be any
|
|
|
|
p.ID, _ = strconv.Atoi(fields[0])
|
|
|
|
p.Parent, _ = strconv.Atoi(fields[1])
|
|
|
|
mm := strings.Split(fields[2], ":")
|
|
|
|
if len(mm) != 2 {
|
|
|
|
return nil, fmt.Errorf("Parsing '%s' failed: unexpected minor:major pair %s", text, mm)
|
2014-03-20 11:52:12 -04:00
|
|
|
}
|
2018-01-23 03:41:22 -05:00
|
|
|
p.Major, _ = strconv.Atoi(mm[0])
|
|
|
|
p.Minor, _ = strconv.Atoi(mm[1])
|
|
|
|
|
2019-03-29 11:31:27 -04:00
|
|
|
p.Root, err = strconv.Unquote(`"` + fields[3] + `"`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "Parsing '%s' failed: unable to unquote root field", fields[3])
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Mountpoint, err = strconv.Unquote(`"` + fields[4] + `"`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "Parsing '%s' failed: unable to unquote mount point field", fields[4])
|
|
|
|
}
|
2018-01-23 03:41:22 -05:00
|
|
|
p.Opts = fields[5]
|
|
|
|
|
|
|
|
var skip, stop bool
|
2018-01-19 14:56:32 -05:00
|
|
|
if filter != nil {
|
|
|
|
// filter out entries we're not interested in
|
|
|
|
skip, stop = filter(p)
|
|
|
|
if skip {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 03:41:22 -05:00
|
|
|
|
|
|
|
// one or more optional fields, when a separator (-)
|
|
|
|
i := 6
|
|
|
|
for ; i < numFields && fields[i] != "-"; i++ {
|
|
|
|
switch i {
|
|
|
|
case 6:
|
|
|
|
p.Optional = fields[6]
|
|
|
|
default:
|
|
|
|
/* NOTE there might be more optional fields before the such as
|
|
|
|
fields[7]...fields[N] (where N < sepIndex), although
|
|
|
|
as of Linux kernel 4.15 the only known ones are
|
|
|
|
mount propagation flags in fields[6]. The correct
|
|
|
|
behavior is to ignore any unknown optional fields.
|
|
|
|
*/
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i == numFields {
|
|
|
|
return nil, fmt.Errorf("Parsing '%s' failed: missing separator ('-')", text)
|
2014-03-20 11:52:12 -04:00
|
|
|
}
|
2014-09-15 05:04:05 -04:00
|
|
|
|
2018-01-23 03:41:22 -05:00
|
|
|
// There should be 3 fields after the separator...
|
|
|
|
if i+4 > numFields {
|
|
|
|
return nil, fmt.Errorf("Parsing '%s' failed: not enough fields after a separator", text)
|
2014-11-03 22:05:04 -05:00
|
|
|
}
|
2018-01-23 03:41:22 -05:00
|
|
|
// ... but in Linux <= 3.9 mounting a cifs with spaces in a share name
|
|
|
|
// (like "//serv/My Documents") _may_ end up having a space in the last field
|
|
|
|
// of mountinfo (like "unc=//serv/My Documents"). Since kernel 3.10-rc1, cifs
|
|
|
|
// option unc= is ignored, so a space should not appear. In here we ignore
|
|
|
|
// those "extra" fields caused by extra spaces.
|
|
|
|
p.Fstype = fields[i+1]
|
|
|
|
p.Source = fields[i+2]
|
|
|
|
p.VfsOpts = fields[i+3]
|
2014-11-03 22:05:04 -05:00
|
|
|
|
2014-03-20 11:52:12 -04:00
|
|
|
out = append(out, p)
|
2018-01-19 14:56:32 -05:00
|
|
|
if stop {
|
|
|
|
break
|
|
|
|
}
|
2014-03-20 11:52:12 -04:00
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2014-10-31 16:28:20 -04:00
|
|
|
|
2018-01-23 03:41:22 -05:00
|
|
|
// Parse /proc/self/mountinfo because comparing Dev and ino does not work from
|
|
|
|
// bind mounts
|
|
|
|
func parseMountTable(filter FilterFunc) ([]*Info, error) {
|
|
|
|
f, err := os.Open("/proc/self/mountinfo")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return parseInfoFile(f, filter)
|
|
|
|
}
|
|
|
|
|
2015-03-28 09:29:33 -04:00
|
|
|
// PidMountInfo collects the mounts for a specific process ID. If the process
|
|
|
|
// ID is unknown, it is better to use `GetMounts` which will inspect
|
|
|
|
// "/proc/self/mountinfo" instead.
|
2015-07-21 13:49:42 -04:00
|
|
|
func PidMountInfo(pid int) ([]*Info, error) {
|
2014-10-31 16:28:20 -04:00
|
|
|
f, err := os.Open(fmt.Sprintf("/proc/%d/mountinfo", pid))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2018-01-19 14:56:32 -05:00
|
|
|
return parseInfoFile(f, nil)
|
2014-10-31 16:28:20 -04:00
|
|
|
}
|