- Stop the container even if the instance root dir doesn't exist.
- Branching procedures by force parameter.
- Change raise HypervisorError(...) to logging.warn to not abort retry
  loop which is performed in backend in soft shutdown.
- No need to sleep for waiting instance shutdown. The method will be
  called until the timeout.
- Raise HypervisorError if lxc-stop failed in hard shutdown.
- Use "lxc-stop --nokill" for soft shutdown.
- In LXC >= 1.0.0, killing container by lxc-stop needs explicit "--kill"
  switch.

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

diff --git a/lib/hypervisor/hv_lxc.py b/lib/hypervisor/hv_lxc.py
index 21b947f..483394a 100644
--- a/lib/hypervisor/hv_lxc.py
+++ b/lib/hypervisor/hv_lxc.py
@@ -25,7 +25,6 @@
 
 import os
 import os.path
-import time
 import logging
 
 from ganeti import constants
@@ -334,17 +333,29 @@ class LXCHypervisor(hv_base.BaseHypervisor):
                    timeout=None):
     """Stop an instance.
 
-    This method has complicated cleanup tests, as we must:
-      - try to kill all leftover processes
-      - try to unmount any additional sub-mountpoints
-      - finally unmount the instance dir
-
     """
     assert(timeout is None or force is not None)
 
     if name is None:
       name = instance.name
 
+    if name in self.ListInstances():
+      lxc_stop_cmd = ["lxc-stop", "-n", name]
+
+      if force:
+        lxc_stop_cmd.append("--kill")
+        result = utils.RunCmd(lxc_stop_cmd, timeout=timeout)
+        if result.failed:
+          raise HypervisorError("Failed to kill instance %s: %s" %
+                                (name, result.output))
+      else:
+        # The --timeout=-1 option is needed to prevent lxc-stop performs
+        # hard-stop(kill) for the container after the default timing out.
+        lxc_stop_cmd.extend(["--nokill", "--timeout", "-1"])
+        result = utils.RunCmd(lxc_stop_cmd, timeout=timeout)
+        if result.failed:
+          logging.error("Failed to stop instance %s: %s", name, result.output)
+
     timeout_cmd = []
     if timeout is not None:
       timeout_cmd.extend(["timeout", str(timeout)])
@@ -353,19 +364,6 @@ class LXCHypervisor(hv_base.BaseHypervisor):
     if not os.path.exists(root_dir):
       return
 
-    if name in self.ListInstances():
-      # Signal init to shutdown; this is a hack
-      if not retry and not force:
-        result = utils.RunCmd(["chroot", root_dir, "poweroff"])
-        if result.failed:
-          raise HypervisorError("Running 'poweroff' on the instance"
-                                " failed: %s" % result.output)
-      time.sleep(2)
-      result = utils.RunCmd(timeout_cmd.extend(["lxc-stop", "-n", name]))
-      if result.failed:
-        logging.warning("Error while doing lxc-stop for %s: %s", name,
-                        result.output)
-
     if not os.path.ismount(root_dir):
       return
 
-- 
1.8.5.5

Reply via email to