Signed-off-by: Stéphane Graber <stgra...@ubuntu.com>
---
 src/lxc/lxc-ls | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)

diff --git a/src/lxc/lxc-ls b/src/lxc/lxc-ls
index cc0a1e7..9d45400 100644
--- a/src/lxc/lxc-ls
+++ b/src/lxc/lxc-ls
@@ -35,7 +35,7 @@ import json
 import lxc
 import os
 import re
-import subprocess
+import tempfile
 import sys
 
 _ = gettext.gettext
@@ -89,17 +89,23 @@ def getTerminalSize():
 
 
 def getSubContainers(container, lxcpath):
-    attach = ['lxc-attach', '-P', lxcpath, '-R', '-s', 'NETWORK|PID',
-              '-n', container,
-              '--', sys.argv[0], "--nesting"]
-
     with open(os.devnull, "w") as fd:
-        newenv = dict(os.environ)
-        newenv['NESTED'] = "/proc/1/root/%s" % lxc.default_config_path
-        sp = subprocess.Popen(attach, stderr=fd, stdout=subprocess.PIPE,
-                              env=newenv, universal_newlines=True)
-        sp.wait()
-        out = sp.stdout.read()
+        fdnum, path = tempfile.mkstemp()
+        os.remove(path)
+
+        fd = os.fdopen(fdnum)
+
+        container.attach_wait(
+            lxc.attach_run_command, [sys.argv[0], "--nesting"],
+            attach_flags=(lxc.LXC_ATTACH_REMOUNT_PROC_SYS),
+            namespaces=(lxc.CLONE_NEWNET + lxc.CLONE_NEWPID),
+            extra_env_vars=["NESTED=/proc/1/root/%s" %
+                            lxc.default_config_path],
+            stdout=fd)
+
+        fd.seek(0)
+        out = fd.read()
+        fd.close()
         if out:
             return json.loads(out)
     return None
@@ -213,8 +219,8 @@ for container_name in 
lxc.list_containers(config_path=lxcpath):
     containers.append(entry)
 
     # Nested containers
-    if args.nesting:
-        sub = getSubContainers(container_name, args.lxcpath)
+    if args.nesting and container.state == "RUNNING":
+        sub = getSubContainers(container, args.lxcpath)
         if sub:
             for entry in sub:
                 if 'nesting_parent' not in entry:
-- 
1.8.3.2


------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60135991&iu=/4140/ostg.clktrk
_______________________________________________
Lxc-devel mailing list
Lxc-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/lxc-devel

Reply via email to