From dce0de897561b5d62d32b1d5e2152df609fce77b Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 12 Mar 2020 17:32:25 -0700 Subject: [PATCH] getParentMount: benefit from GetMounts filter Signed-off-by: Kir Kolyshkin --- libcontainer/rootfs_linux.go | 40 +++++++++--------------------------- 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/libcontainer/rootfs_linux.go b/libcontainer/rootfs_linux.go index e02c4ace..77418159 100644 --- a/libcontainer/rootfs_linux.go +++ b/libcontainer/rootfs_linux.go @@ -661,46 +661,26 @@ func mknodDevice(dest string, node *configs.Device) error { return unix.Chown(dest, int(node.Uid), int(node.Gid)) } -func getMountInfo(mountinfo []*mountinfo.Info, dir string) *mountinfo.Info { - for _, m := range mountinfo { - if m.Mountpoint == dir { - return m - } - } - return nil -} - // Get the parent mount point of directory passed in as argument. Also return // optional fields. func getParentMount(rootfs string) (string, string, error) { - var path string - - mountinfos, err := mountinfo.GetMounts(nil) + mi, err := mountinfo.GetMounts(mountinfo.ParentsFilter(rootfs)) if err != nil { return "", "", err } - - mountinfo := getMountInfo(mountinfos, rootfs) - if mountinfo != nil { - return rootfs, mountinfo.Optional, nil + if len(mi) < 1 { + return "", "", fmt.Errorf("could not find parent mount of %s", rootfs) } - path = rootfs - for { - path = filepath.Dir(path) - - mountinfo = getMountInfo(mountinfos, path) - if mountinfo != nil { - return path, mountinfo.Optional, nil - } - - if path == "/" { - break + // find the longest mount point + var idx, maxlen int + for i := range mi { + if len(mi[i].Mountpoint) > maxlen { + maxlen = len(mi[i].Mountpoint) + idx = i } } - - // If we are here, we did not find parent mount. Something is wrong. - return "", "", fmt.Errorf("Could not find parent mount of %s", rootfs) + return mi[idx].Mountpoint, mi[idx].Optional, nil } // Make parent mount private if it was shared