Add a method - Cgroup.get_cgroup_mounts() - that can parse
/proc/mounts and return a list of CgroupMount instances.
This method will be useful for validating results from lssubsys.

Signed-off-by: Tom Hromatka <tom.hroma...@oracle.com>
---
 ftests/cgroup.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/ftests/cgroup.py b/ftests/cgroup.py
index a3f394e2ac80..0e2dd3a2179a 100644
--- a/ftests/cgroup.py
+++ b/ftests/cgroup.py
@@ -20,6 +20,7 @@
 #
 
 import consts
+import copy
 from controller import Controller
 from enum import Enum
 import multiprocessing as mp
@@ -28,6 +29,37 @@ from run import Run, RunError
 import time
 import utils
 
+class CgroupMount(object):
+    def __init__(self, mount_line):
+        entries = mount_line.split()
+
+        if entries[2] == "cgroup":
+            self.version = CgroupVersion.CGROUP_V1
+        elif entries[2] == "cgroup2":
+            self.version = CgroupVersion.CGROUP_V2
+        else:
+            raise ValueError("Unknown cgroup version")
+
+        self.mount_point = entries[1]
+
+        self.controller = None
+        if self.version == CgroupVersion.CGROUP_V1:
+            self.controller = entries[3].split(',')[-1]
+
+            if self.controller == "clone_children":
+                # the cpuset controller may append this option to the end
+                # rather than the controller name like all other controllers
+                self.controller = "cpuset"
+
+    def __str__(self):
+        out_str = "CgroupMount"
+        out_str += "\n\tMount Point = {}".format(self.mount_point)
+        out_str += "\n\tCgroup Version = {}".format(self.version)
+        if self.controller is not None:
+            out_str += "\n\tController = {}".format(self.controller)
+
+        return out_str
+
 class CgroupVersion(Enum):
     CGROUP_UNK = 0
     CGROUP_V1 = 1
@@ -690,3 +722,31 @@ class Cgroup(object):
                     raise re
 
         return ret
+
+    @staticmethod
+    def get_cgroup_mounts(config, expand_v2_mounts=True):
+        mount_list = list()
+
+        with open('/proc/mounts') as mntf:
+            for line in mntf.readlines():
+                entry = line.split()
+
+                if entry[0] != "cgroup" and entry[0] != "cgroup2":
+                    continue
+
+                mount = CgroupMount(line)
+
+                if mount.version == CgroupVersion.CGROUP_V1 or \
+                   expand_v2_mounts == False:
+                    mount_list.append(mount)
+                    continue
+
+                with open(os.path.join(mount.mount_point,
+                                       "cgroup.controllers")) as ctrlf:
+                    for line in ctrlf.readlines():
+                        for ctrl in line.split():
+                            mount_copy = copy.deepcopy(mount)
+                            mount_copy.controller = ctrl
+                            mount_list.append(mount_copy)
+
+        return mount_list
-- 
2.26.2



_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to