From 302367a2e78f2c0e72d1ba373b82bf5254d0412f Mon Sep 17 00:00:00 2001 From: Tom Hromatka Date: Mon, 26 Oct 2020 11:53:06 -0600 Subject: [PATCH] ftests: Add a method to get a controller's cgroup version MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Add a static method to the Cgroup class to get a controller's cgroup version. Signed-off-by: Tom Hromatka Reviewed-by: Michal Koutný --- ftests/cgroup.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/ftests/cgroup.py b/ftests/cgroup.py index c8bffa54..6f7042d6 100644 --- a/ftests/cgroup.py +++ b/ftests/cgroup.py @@ -20,10 +20,15 @@ # import consts +from enum import Enum import os from run import Run -class Cgroup(object): +class Cgroup(Enum): + CGROUP_UNK = 0 + CGROUP_V1 = 1 + CGROUP_V2 = 2 + @staticmethod def build_cmd_path(in_container, cmd): if in_container: @@ -170,3 +175,22 @@ class Cgroup(object): ret = Run.run(cmd) return ret + + @staticmethod + def version(controller): + with open('/proc/mounts', 'r') as mntf: + for line in mntf.readlines(): + mnt_path = line.split()[1] + + if line.split()[0] == 'cgroup': + for option in line.split()[3].split(','): + if option == controller: + return Cgroup.CGROUP_V1 + elif line.split()[0] == 'cgroup2': + with open(os.path.join(mnt_path, 'cgroup.controllers'), 'r') as ctrlf: + controllers = ctrlf.readline() + for ctrl in controllers.split(): + if ctrl == controller: + return Cgroup.CGROUP_V2 + + return Cgroup.CGROUP_UNK -- 2.47.2