From: Tom Hromatka Date: Wed, 24 Mar 2021 19:43:42 +0000 (+0000) Subject: cgroup.py: Add method to parse /proc/mounts for cgroup mounts X-Git-Tag: v2.0.3~11^2^2~19^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4bd3ff56288f7a12dd852b3664902beab25277b9;p=thirdparty%2Flibcgroup.git cgroup.py: Add method to parse /proc/mounts for cgroup mounts 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 --- diff --git a/ftests/cgroup.py b/ftests/cgroup.py index a3f394e2..0e2dd3a2 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