From 1b045f5dd1bb35ca5cd0ebd73c522a714723c852 Mon Sep 17 00:00:00 2001
From: Balazs Lecz <leczb@google.com>
Date: Mon, 5 Jul 2010 16:20:17 +0000
Subject: [PATCH] Add utils.GetMounts()

Signed-off-by: Balazs Lecz <leczb@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/constants.py              |  2 ++
 lib/utils.py                  | 20 ++++++++++++++++++++
 test/ganeti.utils_unittest.py | 21 +++++++++++++++++++++
 3 files changed, 43 insertions(+)

diff --git a/lib/constants.py b/lib/constants.py
index 32aa2acf7..a7570c93f 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -176,6 +176,8 @@ LOG_BURNIN = LOG_DIR + "burnin.log"
 
 DEV_CONSOLE = "/dev/console"
 
+PROC_MOUNTS = "/proc/mounts"
+
 # luxi related constants
 LUXI_EOM = "\3"
 
diff --git a/lib/utils.py b/lib/utils.py
index 8972b2e53..0976c40ce 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -3156,6 +3156,26 @@ def CalculateDirectorySize(path):
   return BytesToMebibyte(size)
 
 
+def GetMounts(filename=constants.PROC_MOUNTS):
+  """Returns the list of mounted filesystems.
+
+  This function is Linux-specific.
+
+  @param filename: path of mounts file (/proc/mounts by default)
+  @rtype: list of tuples
+  @return: list of mount entries (device, mountpoint, fstype, options)
+
+  """
+  # TODO(iustin): investigate non-Linux options (e.g. via mount output)
+  data = []
+  mountlines = ReadFile(filename).splitlines()
+  for line in mountlines:
+    device, mountpoint, fstype, options, _ = line.split(None, 4)
+    data.append((device, mountpoint, fstype, options))
+
+  return data
+
+
 def GetFilesystemStats(path):
   """Returns the total and free space on a filesystem.
 
diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py
index bdd96d863..5979fafe9 100755
--- a/test/ganeti.utils_unittest.py
+++ b/test/ganeti.utils_unittest.py
@@ -1148,6 +1148,27 @@ class TestEtcHosts(testutils.GanetiTestCase):
     self.assertFileMode(self.tmpname, 0644)
 
 
+class TestGetMounts(unittest.TestCase):
+  """Test case for GetMounts()."""
+
+  TESTDATA = (
+    "rootfs /     rootfs rw 0 0\n"
+    "none   /sys  sysfs  rw,nosuid,nodev,noexec,relatime 0 0\n"
+    "none   /proc proc   rw,nosuid,nodev,noexec,relatime 0 0\n")
+
+  def setUp(self):
+    self.tmpfile = tempfile.NamedTemporaryFile()
+    utils.WriteFile(self.tmpfile.name, data=self.TESTDATA)
+
+  def testGetMounts(self):
+    self.assertEqual(utils.GetMounts(filename=self.tmpfile.name),
+      [
+        ("rootfs", "/", "rootfs", "rw"),
+        ("none", "/sys", "sysfs", "rw,nosuid,nodev,noexec,relatime"),
+        ("none", "/proc", "proc", "rw,nosuid,nodev,noexec,relatime"),
+      ])
+
+
 class TestShellQuoting(unittest.TestCase):
   """Test case for shell quoting functions"""
 
-- 
GitLab