Skip to content

Commit b2e3482

Browse files
authored
Merge pull request containerd#1584 from miaoyq/fix-mount-lookup-err
Fixes looking up mountinfo corresponds to path
2 parents a8426ed + d7c4611 commit b2e3482

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

mount/lookup_test/lookup_linux_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"io/ioutil"
1414
"os"
1515
"os/exec"
16+
"path/filepath"
1617
"strings"
1718
"testing"
1819

@@ -22,6 +23,15 @@ import (
2223
)
2324

2425
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)
33+
}
34+
2535
testutil.RequiresRoot(t)
2636
mnt, err := ioutil.TempDir("", "containerd-mountinfo-test-lookup")
2737
if err != nil {
@@ -46,11 +56,28 @@ func testLookup(t *testing.T, fsType string) {
4656
cleanupDevice()
4757
}()
4858
assert.True(t, strings.HasPrefix(deviceName, "/dev/loop"))
49-
info, err := mount.Lookup(mnt)
59+
checkLookup(mnt, mnt)
60+
61+
newMnt, err := ioutil.TempDir("", "containerd-mountinfo-test-newMnt")
62+
if err != nil {
63+
t.Fatal(err)
64+
}
65+
defer os.RemoveAll(newMnt)
66+
67+
if out, err := exec.Command("mount", "--bind", mnt, newMnt).CombinedOutput(); err != nil {
68+
t.Fatalf("could not mount %s to %s: %v (out: %q)", mnt, newMnt, err, string(out))
69+
}
70+
defer func() {
71+
testutil.Unmount(t, newMnt)
72+
}()
73+
checkLookup(newMnt, newMnt)
74+
75+
subDir := filepath.Join(newMnt, "subDir")
76+
err = os.MkdirAll(subDir, 0700)
5077
if err != nil {
5178
t.Fatal(err)
5279
}
53-
assert.Equal(t, fsType, info.FSType)
80+
checkLookup(newMnt, subDir)
5481
}
5582

5683
func TestLookupWithExt4(t *testing.T) {

mount/lookup_unix.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ package mount
44

55
import (
66
"fmt"
7+
"path/filepath"
8+
"sort"
9+
"strings"
710
"syscall"
811

912
"github.com/pkg/errors"
@@ -12,6 +15,7 @@ import (
1215
// Lookup returns the mount info corresponds to the path.
1316
func Lookup(dir string) (Info, error) {
1417
var dirStat syscall.Stat_t
18+
dir = filepath.Clean(dir)
1519
if err := syscall.Stat(dir, &dirStat); err != nil {
1620
return Info{}, errors.Wrapf(err, "failed to access %q", dir)
1721
}
@@ -20,6 +24,11 @@ func Lookup(dir string) (Info, error) {
2024
if err != nil {
2125
return Info{}, err
2226
}
27+
28+
// Sort descending order by Info.Mountpoint
29+
sort.Slice(mounts, func(i, j int) bool {
30+
return mounts[j].Mountpoint < mounts[i].Mountpoint
31+
})
2332
for _, m := range mounts {
2433
// Note that m.{Major, Minor} are generally unreliable for our purpose here
2534
// https://www.spinics.net/lists/linux-btrfs/msg58908.html
@@ -28,7 +37,7 @@ func Lookup(dir string) (Info, error) {
2837
// may fail; ignore err
2938
continue
3039
}
31-
if st.Dev == dirStat.Dev {
40+
if st.Dev == dirStat.Dev && strings.HasPrefix(dir, m.Mountpoint) {
3241
return m, nil
3342
}
3443
}

0 commit comments

Comments
 (0)