Skip to content

Commit 8cd2182

Browse files
committed
mountinfo: refactored
Signed-off-by: Akihiro Suda <[email protected]>
1 parent d5707d3 commit 8cd2182

9 files changed

Lines changed: 71 additions & 67 deletions

mountinfo/mountinfo.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,18 @@ type Info struct {
2323
// Mountpoint indicates the mount point relative to the process's root.
2424
Mountpoint string
2525

26-
// Opts represents mount-specific options.
27-
Opts string
26+
// Options represents mount-specific options.
27+
Options string
2828

2929
// Optional represents optional fields.
3030
Optional string
3131

32-
// Fstype indicates the type of filesystem, such as EXT3.
33-
Fstype string
32+
// FSType indicates the type of filesystem, such as EXT3.
33+
FSType string
3434

3535
// Source indicates filesystem specific information or "none".
3636
Source string
3737

38-
// VfsOpts represents per super block options.
39-
VfsOpts string
40-
}
41-
42-
// GetMounts retrieves a list of mounts for the current running process.
43-
func GetMounts() ([]*Info, error) {
44-
return parseMountTable()
38+
// VFSOptions represents per super block options.
39+
VFSOptions string
4540
}

mountinfo/mountinfo_freebsd.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ import (
1313
"unsafe"
1414
)
1515

16-
// Parse /proc/self/mountinfo because comparing Dev and ino does not work from
17-
// bind mounts.
18-
func parseMountTable() ([]*Info, error) {
16+
// Self retrieves a list of mounts for the current running process.
17+
func Self() ([]Info, error) {
1918
var rawEntries *C.struct_statfs
2019

2120
count := int(C.getmntinfo(&rawEntries, C.MNT_WAIT))
@@ -29,13 +28,18 @@ func parseMountTable() ([]*Info, error) {
2928
header.Len = count
3029
header.Data = uintptr(unsafe.Pointer(rawEntries))
3130

32-
var out []*Info
31+
var out []Info
3332
for _, entry := range entries {
3433
var mountinfo Info
3534
mountinfo.Mountpoint = C.GoString(&entry.f_mntonname[0])
3635
mountinfo.Source = C.GoString(&entry.f_mntfromname[0])
37-
mountinfo.Fstype = C.GoString(&entry.f_fstypename[0])
38-
out = append(out, &mountinfo)
36+
mountinfo.FSType = C.GoString(&entry.f_fstypename[0])
37+
out = append(out, mountinfo)
3938
}
4039
return out, nil
4140
}
41+
42+
// PID collects the mounts for a specific process ID.
43+
func PID(pid int) ([]Info, error) {
44+
return nil, fmt.Errorf("mountinfo.PID is not implemented on freebsd")
45+
}

mountinfo/mountinfo_linux.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ const (
2828
mountinfoFormat = "%d %d %d:%d %s %s %s %s"
2929
)
3030

31-
// Parse /proc/self/mountinfo because comparing Dev and ino does not work from
32-
// bind mounts
33-
func parseMountTable() ([]*Info, error) {
31+
// Self retrieves a list of mounts for the current running process.
32+
func Self() ([]Info, error) {
3433
f, err := os.Open("/proc/self/mountinfo")
3534
if err != nil {
3635
return nil, err
@@ -40,10 +39,10 @@ func parseMountTable() ([]*Info, error) {
4039
return parseInfoFile(f)
4140
}
4241

43-
func parseInfoFile(r io.Reader) ([]*Info, error) {
42+
func parseInfoFile(r io.Reader) ([]Info, error) {
4443
var (
4544
s = bufio.NewScanner(r)
46-
out = []*Info{}
45+
out = []Info{}
4746
)
4847

4948
for s.Scan() {
@@ -52,14 +51,14 @@ func parseInfoFile(r io.Reader) ([]*Info, error) {
5251
}
5352

5453
var (
55-
p = &Info{}
54+
p = Info{}
5655
text = s.Text()
5756
optionalFields string
5857
)
5958

6059
if _, err := fmt.Sscanf(text, mountinfoFormat,
6160
&p.ID, &p.Parent, &p.Major, &p.Minor,
62-
&p.Root, &p.Mountpoint, &p.Opts, &optionalFields); err != nil {
61+
&p.Root, &p.Mountpoint, &p.Options, &optionalFields); err != nil {
6362
return nil, fmt.Errorf("Scanning '%s' failed: %s", text, err)
6463
}
6564
// Safe as mountinfo encodes mountpoints with spaces as \040.
@@ -73,18 +72,18 @@ func parseInfoFile(r io.Reader) ([]*Info, error) {
7372
p.Optional = optionalFields
7473
}
7574

76-
p.Fstype = postSeparatorFields[0]
75+
p.FSType = postSeparatorFields[0]
7776
p.Source = postSeparatorFields[1]
78-
p.VfsOpts = strings.Join(postSeparatorFields[2:], " ")
77+
p.VFSOptions = strings.Join(postSeparatorFields[2:], " ")
7978
out = append(out, p)
8079
}
8180
return out, nil
8281
}
8382

84-
// PidMountInfo collects the mounts for a specific process ID. If the process
85-
// ID is unknown, it is better to use `GetMounts` which will inspect
83+
// PID collects the mounts for a specific process ID. If the process
84+
// ID is unknown, it is better to use `Self` which will inspect
8685
// "/proc/self/mountinfo" instead.
87-
func PidMountInfo(pid int) ([]*Info, error) {
86+
func PID(pid int) ([]Info, error) {
8887
f, err := os.Open(fmt.Sprintf("/proc/%d/mountinfo", pid))
8988
if err != nil {
9089
return nil, err

mountinfo/mountinfo_linux_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,14 @@ func TestParseFedoraMountinfoFields(t *testing.T) {
463463
Minor: 3,
464464
Root: "/",
465465
Mountpoint: "/proc",
466-
Opts: "rw,nosuid,nodev,noexec,relatime",
466+
Options: "rw,nosuid,nodev,noexec,relatime",
467467
Optional: "shared:5",
468-
Fstype: "proc",
468+
FSType: "proc",
469469
Source: "proc",
470-
VfsOpts: "rw",
470+
VFSOptions: "rw",
471471
}
472472

473-
if *infos[0] != mi {
473+
if infos[0] != mi {
474474
t.Fatalf("expected %#v, got %#v", mi, infos[0])
475475
}
476476
}

mountinfo/mountinfo_solaris.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,32 @@ import (
1212
"fmt"
1313
)
1414

15-
func parseMountTable() ([]*Info, error) {
15+
// Self retrieves a list of mounts for the current running process.
16+
func Self() ([]Info, error) {
1617
mnttab := C.fopen(C.CString(C.MNTTAB), C.CString("r"))
1718
if mnttab == nil {
1819
return nil, fmt.Errorf("Failed to open %s", C.MNTTAB)
1920
}
2021

21-
var out []*Info
22+
var out []Info
2223
var mp C.struct_mnttab
2324

2425
ret := C.getmntent(mnttab, &mp)
2526
for ret == 0 {
2627
var mountinfo Info
2728
mountinfo.Mountpoint = C.GoString(mp.mnt_mountp)
2829
mountinfo.Source = C.GoString(mp.mnt_special)
29-
mountinfo.Fstype = C.GoString(mp.mnt_fstype)
30-
mountinfo.Opts = C.GoString(mp.mnt_mntopts)
31-
out = append(out, &mountinfo)
30+
mountinfo.FSType = C.GoString(mp.mnt_fstype)
31+
mountinfo.Options = C.GoString(mp.mnt_mntopts)
32+
out = append(out, mountinfo)
3233
ret = C.getmntent(mnttab, &mp)
3334
}
3435

3536
C.fclose(mnttab)
3637
return out, nil
3738
}
39+
40+
// PID collects the mounts for a specific process ID.
41+
func PID(pid int) ([]Info, error) {
42+
return nil, fmt.Errorf("mountinfo.PID is not implemented on solaris")
43+
}

mountinfo/mountinfo_unsupported.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build !windows,!linux,!freebsd,!solaris freebsd,!cgo solaris,!cgo
1+
// +build !linux,!freebsd,!solaris freebsd,!cgo solaris,!cgo
22

33
package mountinfo
44

@@ -7,6 +7,12 @@ import (
77
"runtime"
88
)
99

10-
func parseMountTable() ([]*Info, error) {
11-
return nil, fmt.Errorf("mount.parseMountTable is not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
10+
// Self retrieves a list of mounts for the current running process.
11+
func Self() ([]Info, error) {
12+
return nil, fmt.Errorf("mountinfo.Self is not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
13+
}
14+
15+
// PID collects the mounts for a specific process ID.
16+
func PID(pid int) ([]Info, error) {
17+
return nil, fmt.Errorf("mountinfo.PID is not implemented on %s/%s", runtime.GOOS, runtime.GOARCH)
1218
}

mountinfo/mountinfo_windows.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

snapshot/btrfs/btrfs.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ type snapshotter struct {
3636
ms *storage.MetaStore
3737
}
3838

39-
func getBtrfsDevice(root string, mounts []*mountinfo.Info) (string, error) {
39+
func getBtrfsDevice(root string, mounts []mountinfo.Info) (string, error) {
4040
device := ""
4141
deviceMountpoint := ""
4242
for _, info := range mounts {
4343
if (info.Root == "/" || info.Root == "") && strings.HasPrefix(root, info.Mountpoint) {
44-
if info.Fstype == "btrfs" && len(info.Mountpoint) > len(deviceMountpoint) {
44+
if info.FSType == "btrfs" && len(info.Mountpoint) > len(deviceMountpoint) {
4545
device = info.Source
4646
deviceMountpoint = info.Mountpoint
4747
}
48-
if root == info.Mountpoint && info.Fstype != "btrfs" {
49-
return "", fmt.Errorf("%s needs to be btrfs, but seems %s", root, info.Fstype)
48+
if root == info.Mountpoint && info.FSType != "btrfs" {
49+
return "", fmt.Errorf("%s needs to be btrfs, but seems %s", root, info.FSType)
5050
}
5151
}
5252
}
@@ -62,7 +62,7 @@ func getBtrfsDevice(root string, mounts []*mountinfo.Info) (string, error) {
6262
// a file in the provided root.
6363
// root needs to be a mount point of btrfs.
6464
func NewSnapshotter(root string) (snapshot.Snapshotter, error) {
65-
mounts, err := mountinfo.GetMounts()
65+
mounts, err := mountinfo.Self()
6666
if err != nil {
6767
return nil, err
6868
}

snapshot/btrfs/btrfs_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -222,45 +222,45 @@ func TestGetBtrfsDevice(t *testing.T) {
222222
expectedDevice string
223223
expectedError string
224224
root string
225-
mounts []*mountinfo.Info
225+
mounts []mountinfo.Info
226226
}{
227227
{
228228
expectedDevice: "/dev/loop0",
229229
root: "/var/lib/containerd/snapshot/btrfs",
230-
mounts: []*mountinfo.Info{
231-
{Root: "/", Mountpoint: "/", Fstype: "ext4", Source: "/dev/sda1"},
232-
{Root: "/", Mountpoint: "/var/lib/containerd/snapshot/btrfs", Fstype: "btrfs", Source: "/dev/loop0"},
230+
mounts: []mountinfo.Info{
231+
{Root: "/", Mountpoint: "/", FSType: "ext4", Source: "/dev/sda1"},
232+
{Root: "/", Mountpoint: "/var/lib/containerd/snapshot/btrfs", FSType: "btrfs", Source: "/dev/loop0"},
233233
},
234234
},
235235
{
236236
expectedError: "/var/lib/containerd/snapshot/btrfs is not mounted as btrfs",
237237
root: "/var/lib/containerd/snapshot/btrfs",
238-
mounts: []*mountinfo.Info{
239-
{Root: "/", Mountpoint: "/", Fstype: "ext4", Source: "/dev/sda1"},
238+
mounts: []mountinfo.Info{
239+
{Root: "/", Mountpoint: "/", FSType: "ext4", Source: "/dev/sda1"},
240240
},
241241
},
242242
{
243243
expectedDevice: "/dev/sda1",
244244
root: "/var/lib/containerd/snapshot/btrfs",
245-
mounts: []*mountinfo.Info{
246-
{Root: "/", Mountpoint: "/", Fstype: "btrfs", Source: "/dev/sda1"},
245+
mounts: []mountinfo.Info{
246+
{Root: "/", Mountpoint: "/", FSType: "btrfs", Source: "/dev/sda1"},
247247
},
248248
},
249249
{
250250
expectedDevice: "/dev/sda2",
251251
root: "/var/lib/containerd/snapshot/btrfs",
252-
mounts: []*mountinfo.Info{
253-
{Root: "/", Mountpoint: "/", Fstype: "btrfs", Source: "/dev/sda1"},
254-
{Root: "/", Mountpoint: "/var/lib/containerd/snapshot/btrfs", Fstype: "btrfs", Source: "/dev/sda2"},
252+
mounts: []mountinfo.Info{
253+
{Root: "/", Mountpoint: "/", FSType: "btrfs", Source: "/dev/sda1"},
254+
{Root: "/", Mountpoint: "/var/lib/containerd/snapshot/btrfs", FSType: "btrfs", Source: "/dev/sda2"},
255255
},
256256
},
257257
{
258258
expectedDevice: "/dev/sda2",
259259
root: "/var/lib/containerd/snapshot/btrfs",
260-
mounts: []*mountinfo.Info{
261-
{Root: "/", Mountpoint: "/var/lib/containerd/snapshot/btrfs", Fstype: "btrfs", Source: "/dev/sda2"},
262-
{Root: "/", Mountpoint: "/var/lib/foooooooooooooooooooo/baaaaaaaaaaaaaaaaaaaar", Fstype: "btrfs", Source: "/dev/sda3"}, // mountpoint length longer than /var/lib/containerd/snapshot/btrfs
263-
{Root: "/", Mountpoint: "/", Fstype: "btrfs", Source: "/dev/sda1"},
260+
mounts: []mountinfo.Info{
261+
{Root: "/", Mountpoint: "/var/lib/containerd/snapshot/btrfs", FSType: "btrfs", Source: "/dev/sda2"},
262+
{Root: "/", Mountpoint: "/var/lib/foooooooooooooooooooo/baaaaaaaaaaaaaaaaaaaar", FSType: "btrfs", Source: "/dev/sda3"}, // mountpoint length longer than /var/lib/containerd/snapshot/btrfs
263+
{Root: "/", Mountpoint: "/", FSType: "btrfs", Source: "/dev/sda1"},
264264
},
265265
},
266266
}

0 commit comments

Comments
 (0)