[lxc-devel] [PATCH] python: Update add_device_node to use the new API

Stéphane Graber stgraber at ubuntu.com
Mon Dec 10 04:36:10 UTC 2012


Update add_device_node to use the new set_cgroup_item call instead
of having to figure out the cgroup paths and update the entries manually.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 src/python-lxc/lxc/__init__.py.in | 40 ++++++++++-----------------------------
 1 file changed, 10 insertions(+), 30 deletions(-)

diff --git a/src/python-lxc/lxc/__init__.py.in b/src/python-lxc/lxc/__init__.py.in
index 91a59ed..07c956b 100644
--- a/src/python-lxc/lxc/__init__.py.in
+++ b/src/python-lxc/lxc/__init__.py.in
@@ -172,37 +172,17 @@ class Container(_lxc.Container):
         path_stat = os.stat(path)
         mode = stat.S_IMODE(path_stat.st_mode)
 
-        # Lookup the cgroup
-        cgroup_path = None
-        with open("/proc/%s/cgroup" % self.init_pid, "r") as fd:
-            for line in fd:
-                if ":devices:" in line:
-                    cgroup_path = line.split(":")[-1].strip()
-                    break
-            else:
-                return False
-
-        # Lookup the cgroup mount point
-        cgroup = None
-        with open("/proc/mounts", "r") as fd:
-            for line in fd:
-                mount = line.split()
-                if (mount[2] == "cgroup" and "devices" in mount[3]
-                        and os.path.exists("%s/%s" % (mount[1], cgroup_path))):
-                    cgroup = "%s/%s" % (mount[1], cgroup_path)
-                    break
-
-        if not os.path.exists(cgroup):
-            return False
-
         # Allow the target
-        with open("%s/devices.allow" % cgroup, "a") as fd:
-            if stat.S_ISBLK(path_stat.st_mode):
-                fd.write("b %s:%s rwm" % (int(path_stat.st_rdev / 256),
-                                          int(path_stat.st_rdev % 256)))
-            elif stat.S_ISCHR(path_stat.st_mode):
-                fd.write("c %s:%s rwm" % (int(path_stat.st_rdev / 256),
-                                          int(path_stat.st_rdev % 256)))
+        if stat.S_ISBLK(path_stat.st_mode):
+            self.set_cgroup_item("devices.allow",
+                                 "b %s:%s rwm" %
+                                 (int(path_stat.st_rdev / 256),
+                                  int(path_stat.st_rdev % 256)))
+        elif stat.S_ISCHR(path_stat.st_mode):
+            self.set_cgroup_item("devices.allow",
+                                 "c %s:%s rwm" %
+                                 (int(path_stat.st_rdev / 256),
+                                  int(path_stat.st_rdev % 256)))
 
         # Create the target
         rootfs = "/proc/%s/root/" % self.init_pid
-- 
1.8.0





More information about the lxc-devel mailing list