From 9d383e3fd96faf2557736b4c2008dc4ec9ed2407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Thu, 19 Mar 2020 23:32:44 +0100 Subject: [PATCH] Container: call shutdown() in parallel to speed up cleanup --- lxc_net/container.py | 11 +++++++++++ lxc_net/parse_network.py | 8 ++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lxc_net/container.py b/lxc_net/container.py index 12a9aa2..b2f60c0 100644 --- a/lxc_net/container.py +++ b/lxc_net/container.py @@ -201,6 +201,17 @@ class Container(util.LibvirtObject): self.lxc_container = self.conn.createXML(xml) + def notify_cleanup(self): + """ This method can be called before `cleanup` to notify the host that a + cleanup will be performed soon, speeding the process up. """ + if self.lxc_container: + try: + self.lxc_container.shutdown() + except libvirt.libvirtError as exn: + if not str(exn).startswith("Domain not found:"): + raise exn + # Else, the machine was already stopped: everything is fine + def cleanup(self): if self.lxc_container: try: diff --git a/lxc_net/parse_network.py b/lxc_net/parse_network.py index e4836a6..803f875 100644 --- a/lxc_net/parse_network.py +++ b/lxc_net/parse_network.py @@ -52,8 +52,8 @@ class Topology: self.conn = conn self.parsed = False - self.domains = None - self.links = None + self.domains = [] + self.links = [] @requires_parsed @requires_id_in_link_range @@ -159,6 +159,10 @@ class Topology: print("{} domains: ".format("Starting" if state else "Stopping"), end="\t") sys.stdout.flush() + if state is False: + for dom in self.domains: + dom.notify_cleanup() + for dom_id in range(len(self.domains)): self.dom_setstate_single(dom_id, state, verbose, verbose_inline=True)