Skip to content

Commit

Permalink
Merge pull request #1584 from miaoyq/fix-mount-lookup-err
Browse files Browse the repository at this point in the history
Fixes looking up mountinfo corresponds to path
  • Loading branch information
mlaventure committed Oct 9, 2017
2 parents a8426ed + d7c4611 commit b2e3482
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
31 changes: 29 additions & 2 deletions mount/lookup_test/lookup_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

Expand All @@ -22,6 +23,15 @@ import (
)

func testLookup(t *testing.T, fsType string) {
checkLookup := func(mntPoint, dir string) {
info, err := mount.Lookup(dir)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, fsType, info.FSType)
assert.Equal(t, mntPoint, info.Mountpoint)
}

testutil.RequiresRoot(t)
mnt, err := ioutil.TempDir("", "containerd-mountinfo-test-lookup")
if err != nil {
Expand All @@ -46,11 +56,28 @@ func testLookup(t *testing.T, fsType string) {
cleanupDevice()
}()
assert.True(t, strings.HasPrefix(deviceName, "/dev/loop"))
info, err := mount.Lookup(mnt)
checkLookup(mnt, mnt)

newMnt, err := ioutil.TempDir("", "containerd-mountinfo-test-newMnt")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(newMnt)

if out, err := exec.Command("mount", "--bind", mnt, newMnt).CombinedOutput(); err != nil {
t.Fatalf("could not mount %s to %s: %v (out: %q)", mnt, newMnt, err, string(out))
}
defer func() {
testutil.Unmount(t, newMnt)
}()
checkLookup(newMnt, newMnt)

subDir := filepath.Join(newMnt, "subDir")
err = os.MkdirAll(subDir, 0700)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, fsType, info.FSType)
checkLookup(newMnt, subDir)
}

func TestLookupWithExt4(t *testing.T) {
Expand Down
11 changes: 10 additions & 1 deletion mount/lookup_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package mount

import (
"fmt"
"path/filepath"
"sort"
"strings"
"syscall"

"github.com/pkg/errors"
Expand All @@ -12,6 +15,7 @@ import (
// Lookup returns the mount info corresponds to the path.
func Lookup(dir string) (Info, error) {
var dirStat syscall.Stat_t
dir = filepath.Clean(dir)
if err := syscall.Stat(dir, &dirStat); err != nil {
return Info{}, errors.Wrapf(err, "failed to access %q", dir)
}
Expand All @@ -20,6 +24,11 @@ func Lookup(dir string) (Info, error) {
if err != nil {
return Info{}, err
}

// Sort descending order by Info.Mountpoint
sort.Slice(mounts, func(i, j int) bool {
return mounts[j].Mountpoint < mounts[i].Mountpoint
})
for _, m := range mounts {
// Note that m.{Major, Minor} are generally unreliable for our purpose here
// https://www.spinics.net/lists/linux-btrfs/msg58908.html
Expand All @@ -28,7 +37,7 @@ func Lookup(dir string) (Info, error) {
// may fail; ignore err
continue
}
if st.Dev == dirStat.Dev {
if st.Dev == dirStat.Dev && strings.HasPrefix(dir, m.Mountpoint) {
return m, nil
}
}
Expand Down

0 comments on commit b2e3482

Please sign in to comment.