[lxc-devel] [lxd/master] util linux: implement mountpoint checking

brauner on Github lxc-bot at linuxcontainers.org
Mon Oct 2 08:07:45 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 736 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20171002/19c700b2/attachment.bin>
-------------- next part --------------
From e9bbbc352be9fece1f9b6e848e6bd03538caa116 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Mon, 2 Oct 2017 10:05:17 +0200
Subject: [PATCH] util linux: implement mountpoint checking

The mountpoint -q tool simply looks at /proc/<pid>/mountinfo per default and
falls back to stat()ing the relevant path in case /proc/<pid>/mountinfo does
not exist. There's really no point in spawning a new subprocess for this. Let's
simply pars /proc/<pid>/mountinfo per default and fallback to stat()ing when
the file doesn't exist or is busted.

Closes #3877.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 shared/util_linux.go | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)

diff --git a/shared/util_linux.go b/shared/util_linux.go
index 7560e44ac..229fe2713 100644
--- a/shared/util_linux.go
+++ b/shared/util_linux.go
@@ -335,15 +335,34 @@ func GetFileStat(p string) (uid int, gid int, major int, minor int,
 	return
 }
 
-func IsMountPoint(name string) bool {
-	_, err := exec.LookPath("mountpoint")
-	if err == nil {
-		_, err = RunCommand("mountpoint", "-q", name)
-		if err != nil {
-			return false
+func parseMountinfo(name string) int {
+	f, err := os.Open("/proc/self/mountinfo")
+	if err != nil {
+		return -1
+	}
+	defer f.Close()
+
+	scanner := bufio.NewScanner(f)
+	for scanner.Scan() {
+		line := scanner.Text()
+		tokens := strings.Fields(line)
+		if len(tokens) < 5 {
+			return -1
 		}
+		cleanPath := filepath.Clean(tokens[4])
+		cleanName := filepath.Clean(name)
+		if cleanPath == cleanName {
+			return 1
+		}
+	}
 
-		return true
+	return 0
+}
+
+func IsMountPoint(name string) bool {
+	ret := parseMountinfo(name)
+	if ret >= 0 {
+		return (ret == 1)
 	}
 
 	stat, err := os.Stat(name)


More information about the lxc-devel mailing list