diff --git a/lib/constants.py b/lib/constants.py index 32aa2acf71bf3b1f4c25e22b343e6823191ea99e..a7570c93f8193f0b7aeeaa384d40455221b7c46a 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 8972b2e5304e77d1c5a6fded9d641a41ce109ebd..0976c40cee5bf492d88bcaadf5b183ba3f27d141 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 bdd96d863d8dbdbb5ef5c50237000f0e4291db64..5979fafe99c0187900de185e602962abaaab0fc4 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"""