Skip to content

Commit d0779a6

Browse files
committed
Simplify mount.Lookup.
Signed-off-by: Lantao Liu <lantaol@google.com>
1 parent eed3b1c commit d0779a6

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

mount/lookup_test/lookup_linux_test.go

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package lookuptest
1111

1212
import (
13+
"fmt"
1314
"io/ioutil"
1415
"os"
1516
"os/exec"
@@ -20,18 +21,19 @@ import (
2021
"github.com/containerd/containerd/mount"
2122
"github.com/containerd/containerd/testutil"
2223
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
2325
)
2426

25-
func testLookup(t *testing.T, fsType string) {
26-
checkLookup := func(mntPoint, dir string) {
27-
info, err := mount.Lookup(dir)
28-
if err != nil {
29-
t.Fatal(err)
30-
}
31-
assert.Equal(t, fsType, info.FSType)
32-
assert.Equal(t, mntPoint, info.Mountpoint)
27+
func checkLookup(t *testing.T, fsType, mntPoint, dir string) {
28+
info, err := mount.Lookup(dir)
29+
if err != nil {
30+
t.Fatal(err)
3331
}
32+
assert.Equal(t, fsType, info.FSType)
33+
assert.Equal(t, mntPoint, info.Mountpoint)
34+
}
3435

36+
func testLookup(t *testing.T, fsType string) {
3537
testutil.RequiresRoot(t)
3638
mnt, err := ioutil.TempDir("", "containerd-mountinfo-test-lookup")
3739
if err != nil {
@@ -56,7 +58,7 @@ func testLookup(t *testing.T, fsType string) {
5658
cleanupDevice()
5759
}()
5860
assert.True(t, strings.HasPrefix(deviceName, "/dev/loop"))
59-
checkLookup(mnt, mnt)
61+
checkLookup(t, fsType, mnt, mnt)
6062

6163
newMnt, err := ioutil.TempDir("", "containerd-mountinfo-test-newMnt")
6264
if err != nil {
@@ -70,14 +72,14 @@ func testLookup(t *testing.T, fsType string) {
7072
defer func() {
7173
testutil.Unmount(t, newMnt)
7274
}()
73-
checkLookup(newMnt, newMnt)
75+
checkLookup(t, fsType, newMnt, newMnt)
7476

7577
subDir := filepath.Join(newMnt, "subDir")
7678
err = os.MkdirAll(subDir, 0700)
7779
if err != nil {
7880
t.Fatal(err)
7981
}
80-
checkLookup(newMnt, subDir)
82+
checkLookup(t, fsType, newMnt, subDir)
8183
}
8284

8385
func TestLookupWithExt4(t *testing.T) {
@@ -87,3 +89,39 @@ func TestLookupWithExt4(t *testing.T) {
8789
func TestLookupWithXFS(t *testing.T) {
8890
testLookup(t, "xfs")
8991
}
92+
93+
func TestLookupWithOverlay(t *testing.T) {
94+
lower, err := ioutil.TempDir("", "containerd-mountinfo-test-lower")
95+
require.NoError(t, err)
96+
defer os.RemoveAll(lower)
97+
98+
upper, err := ioutil.TempDir("", "containerd-mountinfo-test-upper")
99+
require.NoError(t, err)
100+
defer os.RemoveAll(upper)
101+
102+
work, err := ioutil.TempDir("", "containerd-mountinfo-test-work")
103+
require.NoError(t, err)
104+
defer os.RemoveAll(work)
105+
106+
overlay, err := ioutil.TempDir("", "containerd-mountinfo-test-overlay")
107+
require.NoError(t, err)
108+
defer os.RemoveAll(overlay)
109+
110+
if out, err := exec.Command("mount", "-t", "overlay", "overlay", "-o", fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s",
111+
lower, upper, work), overlay).CombinedOutput(); err != nil {
112+
// not fatal
113+
t.Skipf("could not mount overlay: %v (out: %q)", err, string(out))
114+
}
115+
defer testutil.Unmount(t, overlay)
116+
117+
testdir := filepath.Join(overlay, "testdir")
118+
err = os.Mkdir(testdir, 0777)
119+
require.NoError(t, err)
120+
121+
testfile := filepath.Join(overlay, "testfile")
122+
_, err = os.Create(testfile)
123+
require.NoError(t, err)
124+
125+
checkLookup(t, "overlay", overlay, testdir)
126+
checkLookup(t, "overlay", overlay, testfile)
127+
}

mount/lookup_unix.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,35 @@
33
package mount
44

55
import (
6-
"fmt"
76
"path/filepath"
87
"sort"
98
"strings"
10-
"syscall"
119

1210
"github.com/pkg/errors"
1311
)
1412

1513
// Lookup returns the mount info corresponds to the path.
1614
func Lookup(dir string) (Info, error) {
17-
var dirStat syscall.Stat_t
1815
dir = filepath.Clean(dir)
19-
if err := syscall.Stat(dir, &dirStat); err != nil {
20-
return Info{}, errors.Wrapf(err, "failed to access %q", dir)
21-
}
2216

2317
mounts, err := Self()
2418
if err != nil {
2519
return Info{}, err
2620
}
2721

2822
// Sort descending order by Info.Mountpoint
29-
sort.Slice(mounts, func(i, j int) bool {
23+
sort.SliceStable(mounts, func(i, j int) bool {
3024
return mounts[j].Mountpoint < mounts[i].Mountpoint
3125
})
3226
for _, m := range mounts {
3327
// Note that m.{Major, Minor} are generally unreliable for our purpose here
3428
// https://www.spinics.net/lists/linux-btrfs/msg58908.html
35-
var st syscall.Stat_t
36-
if err := syscall.Stat(m.Mountpoint, &st); err != nil {
37-
// may fail; ignore err
38-
continue
39-
}
40-
if st.Dev == dirStat.Dev && strings.HasPrefix(dir, m.Mountpoint) {
29+
// Note that device number is not checked here, because for overlayfs files
30+
// may have different device number with the mountpoint.
31+
if strings.HasPrefix(dir, m.Mountpoint) {
4132
return m, nil
4233
}
4334
}
4435

45-
return Info{}, fmt.Errorf("failed to find the mount info for %q", dir)
36+
return Info{}, errors.Errorf("failed to find the mount info for %q", dir)
4637
}

0 commit comments

Comments
 (0)