- Add the _GetCurrentCgroupSubsysGroups to read the current cgroup subsystem hierarchies from /proc/self/cgroup. - Add the _GetCgroupInstanceSubsysDir to return the path of instance cgroup subsystem directory.
Signed-off-by: Yuto KAWAMURA(kawamuray) <[email protected]> --- lib/hypervisor/hv_lxc.py | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/hypervisor/hv_lxc.py b/lib/hypervisor/hv_lxc.py index 38dee2a..b63342a 100644 --- a/lib/hypervisor/hv_lxc.py +++ b/lib/hypervisor/hv_lxc.py @@ -55,6 +55,8 @@ class LXCHypervisor(hv_base.BaseHypervisor): """ _ROOT_DIR = pathutils.RUN_DIR + "/lxc" _CGROUP_ROOT_DIR = _ROOT_DIR + "/cgroup" + _PROC_CGROUP_FILE = "/proc/self/cgroup" + _DEVS = [ "c 1:3", # /dev/null "c 1:5", # /dev/zero @@ -234,6 +236,46 @@ class LXCHypervisor(hv_base.BaseHypervisor): return cls._MountCgroupSubsystem(subsystem) @classmethod + def _GetCurrentCgroupSubsysGroups(cls): + """Return the dict of cgroup subsystem hierarchies this process belongs to. + + The dictionary has the cgroup subsystem as a key and its hierarchy as a + value. + Information is read from /proc/self/cgroup. + + """ + try: + cgroup_list = utils.ReadFile(cls._PROC_CGROUP_FILE) + except EnvironmentError, err: + raise HypervisorError("Failed to read %s : %s" % + (cls._PROC_CGROUP_FILE, err)) + + cgroups = {} + for line in filter(None, cgroup_list.split("\n")): + _, subsystems, hierarchy = line.split(":") + for subsys in subsystems.split(","): + cgroups[subsys] = hierarchy[1:] # discard first '/' + + return cgroups + + @classmethod + def _GetCgroupInstanceSubsysDir(cls, instance_name, subsystem): + """Return the directory of the cgroup subsystem for the instance. + + @type instance_name: string + @param instance_name: instance name + @type subsystem: string + @param subsystem: cgroup subsystem name + @rtype string + @return path of the instance hierarchy directory for the subsystem + + """ + subsys_dir = cls._GetOrPrepareCgroupSubsysMountPoint(subsystem) + base_group = cls._GetCurrentCgroupSubsysGroups().get(subsystem, "") + + return utils.PathJoin(subsys_dir, base_group, "lxc", instance_name) + + @classmethod def _GetCgroupInstanceValue(cls, instance_name, subsystem, param): """Return the value of the specified cgroup parameter. @@ -247,8 +289,8 @@ class LXCHypervisor(hv_base.BaseHypervisor): @return value read from cgroup subsystem fs """ - subsys_dir = cls._GetOrPrepareCgroupSubsysMountPoint(subsystem) - param_file = utils.PathJoin(subsys_dir, "lxc", instance_name, param) + subsys_dir = cls._GetCgroupInstanceSubsysDir(instance_name, subsystem) + param_file = utils.PathJoin(subsys_dir, param) return utils.ReadFile(param_file).rstrip("\n") @classmethod -- 1.8.5.5
