From 809dae19784a13d8fd0b4b12ee2c3d4da4466572 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Wed, 8 Jun 2016 10:05:47 -0700 Subject: [PATCH] Fix bug in isLikelyNotMountPoint function In nsenter_mount.go/isLikelyNotMountPoint function, the returned output from findmnt command misses the last letter. Modify the code to make sure that output has the full target path. fix #26421 #25056 #22911 --- pkg/util/mount/nsenter_mount.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/util/mount/nsenter_mount.go b/pkg/util/mount/nsenter_mount.go index 9be0e551d35..a5ccd89929b 100644 --- a/pkg/util/mount/nsenter_mount.go +++ b/pkg/util/mount/nsenter_mount.go @@ -175,8 +175,11 @@ func (n *NsenterMounter) IsLikelyNotMountPoint(file string) (bool, error) { glog.V(5).Infof("findmnt: directory %s does not exist", file) return true, err } - - args := []string{"--mount=/rootfs/proc/1/ns/mnt", "--", n.absHostPath("findmnt"), "-o", "target", "--noheadings", "--target", file} + // Add --first-only option: since we are testing for the absense of a mountpoint, it is sufficient to get only + // the first of multiple possible mountpoints using --first-only. + // Also add fstype output to make sure that the output of target file will give the full path + // TODO: Need more refactoring for this function. Track the solution with issue #26996 + args := []string{"--mount=/rootfs/proc/1/ns/mnt", "--", n.absHostPath("findmnt"), "-o", "target,fstype", "--noheadings", "--first-only", "--target", file} glog.V(5).Infof("findmnt command: %v %v", nsenterPath, args) exec := exec.New() @@ -188,13 +191,15 @@ func (n *NsenterMounter) IsLikelyNotMountPoint(file string) (bool, error) { // It's safer to assume that it's not a mount point. return true, nil } - strOut := strings.TrimSuffix(string(out), "\n") + mountTarget := strings.Split(string(out), " ")[0] + mountTarget = strings.TrimSuffix(mountTarget, "\n") + glog.V(5).Infof("IsLikelyNotMountPoint findmnt output for path %s: %v:", file, mountTarget) - glog.V(5).Infof("IsLikelyNotMountPoint findmnt output for path %s: %v", file, strOut) - if strOut == file { + if mountTarget == file { + glog.V(5).Infof("IsLikelyNotMountPoint: %s is a mount point", file) return false, nil } - + glog.V(5).Infof("IsLikelyNotMountPoint: %s is not a mount point", file) return true, nil }