File fix-restart-paravirt-xen.dif of Package cobbler.3314

Index: cobbler-2.6.6/koan/app.py
===================================================================
--- cobbler-2.6.6.orig/koan/app.py
+++ cobbler-2.6.6/koan/app.py
@@ -62,6 +62,10 @@ import socket
 import utils
 import time
 import configurator
+try:
+    import xml.etree.ElementTree as etree
+except ImportError:
+    etree = None
 
 COBBLER_REQUIRED = 1.300
 
@@ -1485,6 +1489,8 @@ class Koan:
             ct = 0
             while True:
                time.sleep(3)
+               if self.fix_restart_config(conn, virtname):
+                   break
                state = utils.get_vm_state(conn, virtname)
                if state == "running":
                    print "- install is still running, sleeping for 1 minute (%s)" % ct
@@ -1495,7 +1501,7 @@ class Koan:
                    return "failed"
                elif state == "shutdown":
                    print "- shutdown VM detected, is the install done?  Restarting!"
-                   utils.find_vm(conn, virtname).create()    
+                   utils.find_vm(conn, virtname).create()
                    return results
                else:
                    raise InfoException("internal error, bad virt state")
@@ -1515,6 +1521,63 @@ class Koan:
 
     #---------------------------------------------------
 
+    def fix_restart_config(self, conn, virtname):
+        if self.virt_type not in [ "xenpv", "xenfv" ]:
+            return False
+        if not etree:
+            return False
+        dom = utils.find_vm(conn, virtname)
+        # we need to fix XEN only
+        xml = dom.XMLDesc(0)
+        root = etree.fromstring(xml)
+        #print "OLD: %s" % etree.tostring(root)
+        on = root.findall('on_poweroff')
+        if on and len(on) == 1:
+            #print "set on_oweroff to destroy"
+            on[0].text = "destroy"
+        on = root.findall('on_reboot')
+        if on and len(on) == 1:
+            #print "set on_reboot to restart"
+            on[0].text = "restart"
+        on = root.findall('on_crash')
+        if on and len(on) == 1:
+            #print "set on_crash to restart"
+            on[0].text = "restart"
+
+        if self.virt_type == "xenpv":
+            bootloader = root.findall('bootloader')
+            ose = root.findall('./os')
+            if bootloader and len(bootloader) == 1:
+                #print "set bootloader to pygrub"
+                bootloader[0].text = '/usr/bin/pygrub'
+                if ose and len(ose) == 1:
+                    #print "remove os"
+                    root.remove(ose[0])
+            elif ose and len(ose) == 1:
+                os = ose[0]
+                for item in os:
+                    if item.tag == "kernel":
+                        item.text = "/usr/lib/grub2/x86_64-xen/grub.xen"
+                    elif item.tag == "initrd" or item.tag == "cmdline":
+                        item.text = ""
+        elif self.virt_type == "xenfv":
+            oses = root.findall("./os")
+            for os in oses:
+                for item in os:
+                    if item.tag == 'boot' and item.get('dev', '') == 'network':
+                        #print "remove network boot"
+                        os.remove(item)
+
+        try:
+            conn.defineXML(etree.tostring(root))
+        except:
+            import libvirt
+            conn = libvirt.open("xen:///")
+            conn.defineXML(etree.tostring(root))
+        return True
+
+    #---------------------------------------------------
+
     def load_virt_modules(self):
         try:
             import xencreate
openSUSE Build Service is sponsored by