Simplify mount.Lookup.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2018-02-01 02:28:26 +00:00
parent eed3b1c804
commit d0779a6145
2 changed files with 54 additions and 25 deletions

View File

@ -10,6 +10,7 @@
package lookuptest package lookuptest
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
@ -20,10 +21,10 @@ import (
"github.com/containerd/containerd/mount" "github.com/containerd/containerd/mount"
"github.com/containerd/containerd/testutil" "github.com/containerd/containerd/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func testLookup(t *testing.T, fsType string) { func checkLookup(t *testing.T, fsType, mntPoint, dir string) {
checkLookup := func(mntPoint, dir string) {
info, err := mount.Lookup(dir) info, err := mount.Lookup(dir)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -32,6 +33,7 @@ func testLookup(t *testing.T, fsType string) {
assert.Equal(t, mntPoint, info.Mountpoint) assert.Equal(t, mntPoint, info.Mountpoint)
} }
func testLookup(t *testing.T, fsType string) {
testutil.RequiresRoot(t) testutil.RequiresRoot(t)
mnt, err := ioutil.TempDir("", "containerd-mountinfo-test-lookup") mnt, err := ioutil.TempDir("", "containerd-mountinfo-test-lookup")
if err != nil { if err != nil {
@ -56,7 +58,7 @@ func testLookup(t *testing.T, fsType string) {
cleanupDevice() cleanupDevice()
}() }()
assert.True(t, strings.HasPrefix(deviceName, "/dev/loop")) assert.True(t, strings.HasPrefix(deviceName, "/dev/loop"))
checkLookup(mnt, mnt) checkLookup(t, fsType, mnt, mnt)
newMnt, err := ioutil.TempDir("", "containerd-mountinfo-test-newMnt") newMnt, err := ioutil.TempDir("", "containerd-mountinfo-test-newMnt")
if err != nil { if err != nil {
@ -70,14 +72,14 @@ func testLookup(t *testing.T, fsType string) {
defer func() { defer func() {
testutil.Unmount(t, newMnt) testutil.Unmount(t, newMnt)
}() }()
checkLookup(newMnt, newMnt) checkLookup(t, fsType, newMnt, newMnt)
subDir := filepath.Join(newMnt, "subDir") subDir := filepath.Join(newMnt, "subDir")
err = os.MkdirAll(subDir, 0700) err = os.MkdirAll(subDir, 0700)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
checkLookup(newMnt, subDir) checkLookup(t, fsType, newMnt, subDir)
} }
func TestLookupWithExt4(t *testing.T) { func TestLookupWithExt4(t *testing.T) {
@ -87,3 +89,39 @@ func TestLookupWithExt4(t *testing.T) {
func TestLookupWithXFS(t *testing.T) { func TestLookupWithXFS(t *testing.T) {
testLookup(t, "xfs") testLookup(t, "xfs")
} }
func TestLookupWithOverlay(t *testing.T) {
lower, err := ioutil.TempDir("", "containerd-mountinfo-test-lower")
require.NoError(t, err)
defer os.RemoveAll(lower)
upper, err := ioutil.TempDir("", "containerd-mountinfo-test-upper")
require.NoError(t, err)
defer os.RemoveAll(upper)
work, err := ioutil.TempDir("", "containerd-mountinfo-test-work")
require.NoError(t, err)
defer os.RemoveAll(work)
overlay, err := ioutil.TempDir("", "containerd-mountinfo-test-overlay")
require.NoError(t, err)
defer os.RemoveAll(overlay)
if out, err := exec.Command("mount", "-t", "overlay", "overlay", "-o", fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s",
lower, upper, work), overlay).CombinedOutput(); err != nil {
// not fatal
t.Skipf("could not mount overlay: %v (out: %q)", err, string(out))
}
defer testutil.Unmount(t, overlay)
testdir := filepath.Join(overlay, "testdir")
err = os.Mkdir(testdir, 0777)
require.NoError(t, err)
testfile := filepath.Join(overlay, "testfile")
_, err = os.Create(testfile)
require.NoError(t, err)
checkLookup(t, "overlay", overlay, testdir)
checkLookup(t, "overlay", overlay, testfile)
}

View File

@ -3,22 +3,16 @@
package mount package mount
import ( import (
"fmt"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
"syscall"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// Lookup returns the mount info corresponds to the path. // Lookup returns the mount info corresponds to the path.
func Lookup(dir string) (Info, error) { func Lookup(dir string) (Info, error) {
var dirStat syscall.Stat_t
dir = filepath.Clean(dir) dir = filepath.Clean(dir)
if err := syscall.Stat(dir, &dirStat); err != nil {
return Info{}, errors.Wrapf(err, "failed to access %q", dir)
}
mounts, err := Self() mounts, err := Self()
if err != nil { if err != nil {
@ -26,21 +20,18 @@ func Lookup(dir string) (Info, error) {
} }
// Sort descending order by Info.Mountpoint // Sort descending order by Info.Mountpoint
sort.Slice(mounts, func(i, j int) bool { sort.SliceStable(mounts, func(i, j int) bool {
return mounts[j].Mountpoint < mounts[i].Mountpoint return mounts[j].Mountpoint < mounts[i].Mountpoint
}) })
for _, m := range mounts { for _, m := range mounts {
// Note that m.{Major, Minor} are generally unreliable for our purpose here // Note that m.{Major, Minor} are generally unreliable for our purpose here
// https://www.spinics.net/lists/linux-btrfs/msg58908.html // https://www.spinics.net/lists/linux-btrfs/msg58908.html
var st syscall.Stat_t // Note that device number is not checked here, because for overlayfs files
if err := syscall.Stat(m.Mountpoint, &st); err != nil { // may have different device number with the mountpoint.
// may fail; ignore err if strings.HasPrefix(dir, m.Mountpoint) {
continue
}
if st.Dev == dirStat.Dev && strings.HasPrefix(dir, m.Mountpoint) {
return m, nil return m, nil
} }
} }
return Info{}, fmt.Errorf("failed to find the mount info for %q", dir) return Info{}, errors.Errorf("failed to find the mount info for %q", dir)
} }