Blob Blame History Raw
From c4dc4f2a277a484cb2c610f4aa91c50651bb2a38 Mon Sep 17 00:00:00 2001
From: Pavel Hrdina <phrdina@redhat.com>
Date: Thu, 27 Apr 2017 13:11:13 -0400
Subject: guest: Only use define+start logic for vz

From: Cole Robinson <crobinso@redhat.com>

Handling this for qemu, which may need UNDEFINE_NVRAM flags to do
the cleanup, is a pain, so move this logic to only apply to vz
driver which doesn't support createXML

Mentioned on list: https://www.redhat.com/archives/virt-tools-list/2017-April/msg00037.html

(cherry picked from commit 8181e8609867fe6ed101e48212d42a1c38373539)

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1441902

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
---
 tests/clitest.py  |  2 ++
 virtinst/guest.py | 55 ++++++++++++++++++++++++++++++++++++-------------------
 2 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/tests/clitest.py b/tests/clitest.py
index 7f07c8ee..5bfa2799 100644
--- a/tests/clitest.py
+++ b/tests/clitest.py
@@ -794,6 +794,8 @@ c.add_compare("--disk %(BLOCKVOL)s --cdrom %(EXISTIMG1)s --livecd --hvm", "xen-h
 #####################
 
 c = vinst.add_category("vz", "--connect %(URI-VZ)s --noautoconsole")
+c.add_valid("--container")  # validate the special define+start logic
+c.add_invalid("--container --transient")  # doesn't support --transient
 c.add_compare(""" \
 --container \
 --filesystem type=template,source=centos-7-x86_64,target="/" \
diff --git a/virtinst/guest.py b/virtinst/guest.py
index 16934169..ec30e17b 100644
--- a/virtinst/guest.py
+++ b/virtinst/guest.py
@@ -385,6 +385,31 @@ class Guest(XMLBuilder):
 
         return install_xml, final_xml
 
+    def _manual_transient_create(self, install_xml, final_xml, needs_boot):
+        """
+        For hypervisors (like vz) that don't implement createXML,
+        we need to define+start, and undefine on start failure
+        """
+        domain = self.conn.defineXML(install_xml or final_xml)
+        if not needs_boot:
+            return domain
+
+        # Handle undefining the VM if the initial startup fails
+        try:
+            domain.create()
+        except:
+            import sys
+            exc_info = sys.exc_info()
+            try:
+                domain.undefine()
+            except:
+                pass
+            raise exc_info[0], exc_info[1], exc_info[2]
+
+        if install_xml and install_xml != final_xml:
+            domain = self.conn.defineXML(final_xml)
+        return domain
+
     def _create_guest(self, meter, install_xml, final_xml, doboot, transient):
         """
         Actually do the XML logging, guest defining/creating
@@ -394,27 +419,19 @@ class Guest(XMLBuilder):
         meter_label = _("Creating domain...")
         meter = util.ensure_meter(meter)
         meter.start(size=None, text=meter_label)
+        needs_boot = doboot or self.installer.has_install_phase()
+
+        if self.type == "vz":
+            if transient:
+                raise RuntimeError(_("Domain type 'vz' doesn't support "
+                    "transient installs."))
+            domain = self._manual_transient_create(
+                    install_xml, final_xml, needs_boot)
 
-        if transient:
-            domain = self.conn.createXML(install_xml or final_xml, 0)
         else:
-            # Not all hypervisors (vz) support createXML, so avoid it here
-            domain = self.conn.defineXML(install_xml or final_xml)
-
-            # Handle undefining the VM if the initial startup fails
-            if doboot or self.installer.has_install_phase():
-                try:
-                    domain.create()
-                except:
-                    import sys
-                    exc_info = sys.exc_info()
-                    try:
-                        domain.undefine()
-                    except:
-                        pass
-                    raise exc_info[0], exc_info[1], exc_info[2]
-
-            if install_xml and install_xml != final_xml:
+            if transient or needs_boot:
+                domain = self.conn.createXML(install_xml or final_xml, 0)
+            if not transient:
                 domain = self.conn.defineXML(final_xml)
 
         self.domain = domain
-- 
2.13.0