- Add _GetCurrentCgroupSubsysGroups to read current cgroup subsystem
  hierarchies from /proc/self/cgroup.
- Add _GetCgroupInstanceSubsysDir to return path of instance cgroup
  subsystem directory.

Signed-off-by: Yuto KAWAMURA(kawamuray) <[email protected]>
---
 lib/hypervisor/hv_lxc.py | 45 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 43 insertions(+), 2 deletions(-)

diff --git a/lib/hypervisor/hv_lxc.py b/lib/hypervisor/hv_lxc.py
index 5bcaa6f..8e4ddce 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
@@ -236,6 +238,45 @@ class LXCHypervisor(hv_base.BaseHypervisor):
     return cls._MountCgroupSubsystem(subsystem)
 
   @classmethod
+  def _GetCurrentCgroupSubsysGroups(cls):
+    """Return the dict of cgroup subsystem hierarchies currently belonging.
+
+    The dictionary has cgroup subsystem as its key and hierarchy as its 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 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 specified cgroup parameter.
 
@@ -249,8 +290,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

Reply via email to