Ensure getting the correct mountinfo corresponds to path
Signed-off-by: Yanqiang Miao <miao.yanqiang@zte.com.cn>
This commit is contained in:
parent
a8426ed9e7
commit
d7c4611848
@ -13,6 +13,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -22,6 +23,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func testLookup(t *testing.T, fsType string) {
|
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)
|
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 {
|
||||||
@ -46,11 +56,28 @@ 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"))
|
||||||
info, err := mount.Lookup(mnt)
|
checkLookup(mnt, mnt)
|
||||||
|
|
||||||
|
newMnt, err := ioutil.TempDir("", "containerd-mountinfo-test-newMnt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, fsType, info.FSType)
|
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)
|
||||||
|
}
|
||||||
|
checkLookup(newMnt, subDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLookupWithExt4(t *testing.T) {
|
func TestLookupWithExt4(t *testing.T) {
|
||||||
|
@ -4,6 +4,9 @@ package mount
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -12,6 +15,7 @@ import (
|
|||||||
// 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
|
var dirStat syscall.Stat_t
|
||||||
|
dir = filepath.Clean(dir)
|
||||||
if err := syscall.Stat(dir, &dirStat); err != nil {
|
if err := syscall.Stat(dir, &dirStat); err != nil {
|
||||||
return Info{}, errors.Wrapf(err, "failed to access %q", dir)
|
return Info{}, errors.Wrapf(err, "failed to access %q", dir)
|
||||||
}
|
}
|
||||||
@ -20,6 +24,11 @@ func Lookup(dir string) (Info, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return Info{}, err
|
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 {
|
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
|
||||||
@ -28,7 +37,7 @@ func Lookup(dir string) (Info, error) {
|
|||||||
// may fail; ignore err
|
// may fail; ignore err
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if st.Dev == dirStat.Dev {
|
if st.Dev == dirStat.Dev && strings.HasPrefix(dir, m.Mountpoint) {
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user