From: Tom Hromatka Date: Mon, 26 Oct 2020 17:53:06 +0000 (-0600) Subject: ftests: Add a method to get a controller's cgroup version X-Git-Tag: v2.0.3~11^2^2~31^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=302367a2e78f2c0e72d1ba373b82bf5254d0412f;p=thirdparty%2Flibcgroup.git ftests: Add a method to get a controller's cgroup version Add a static method to the Cgroup class to get a controller's cgroup version. Signed-off-by: Tom Hromatka Reviewed-by: Michal Koutný --- 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