2013-12-18 19:42:49 -05:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2013-12-20 13:34:05 -05:00
|
|
|
"io"
|
2013-12-18 19:42:49 -05:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2013-12-20 13:34:05 -05:00
|
|
|
// We only parse upto the mountinfo because that is all we
|
|
|
|
// care about right now
|
|
|
|
mountinfoFormat = "%d %d %d:%d %s %s %s"
|
2013-12-18 19:42:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Represents one line from /proc/self/mountinfo
|
|
|
|
type procEntry struct {
|
2013-12-20 13:34:05 -05:00
|
|
|
id, parent, major, minor int
|
|
|
|
source, mountpoint, opts string
|
2013-12-18 19:42:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse /proc/self/mountinfo because comparing Dev and ino does not work from bind mounts
|
|
|
|
func parseMountTable() ([]*procEntry, error) {
|
|
|
|
f, err := os.Open("/proc/self/mountinfo")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2013-12-20 13:34:05 -05:00
|
|
|
return parseInfoFile(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseInfoFile(r io.Reader) ([]*procEntry, error) {
|
|
|
|
var (
|
|
|
|
s = bufio.NewScanner(r)
|
|
|
|
out = []*procEntry{}
|
|
|
|
)
|
|
|
|
|
2013-12-18 19:42:49 -05:00
|
|
|
for s.Scan() {
|
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-12-20 13:34:05 -05:00
|
|
|
var (
|
|
|
|
p = &procEntry{}
|
|
|
|
text = s.Text()
|
|
|
|
)
|
|
|
|
if _, err := fmt.Sscanf(text, mountinfoFormat,
|
2013-12-18 19:42:49 -05:00
|
|
|
&p.id, &p.parent, &p.major, &p.minor,
|
2013-12-20 13:34:05 -05:00
|
|
|
&p.source, &p.mountpoint, &p.opts); err != nil {
|
|
|
|
return nil, fmt.Errorf("Scanning '%s' failed: %s", text, err)
|
2013-12-18 19:42:49 -05:00
|
|
|
}
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|