Browse Source

Add code and test for migrating vm.hvm into vm.virt_mode

This will allow to load old qubes.xml - otherwise previous change render
the system unusable (until manual qubes.xml edit).

QubesOS/qubes-issues#2912
Marek Marczykowski-Górecki 7 years ago
parent
commit
9ba84ecdad
2 changed files with 34 additions and 0 deletions
  1. 24 0
      qubes/tests/vm/qubesvm.py
  2. 10 0
      qubes/vm/qubesvm.py

+ 24 - 0
qubes/tests/vm/qubesvm.py

@@ -26,6 +26,7 @@ import os
 import unittest
 import uuid
 import datetime
+import lxml.etree
 
 import qubes
 import qubes.exc
@@ -476,3 +477,26 @@ class TC_90_QubesVM(QubesVMTestsMixin, qubes.tests.QubesTestCase):
         vm = self.get_vm()
         self.assertPropertyInvalidValue(vm, 'backup_timestamp', 'xxx')
         self.assertPropertyInvalidValue(vm, 'backup_timestamp', None)
+
+    def test_500_property_migrate_virt_mode(self):
+        xml_template = '''
+        <domain class="QubesVM" id="domain-1">
+            <properties>
+                <property name="qid">1</property>
+                <property name="name">testvm</property>
+                <property name="label" ref="label-1" />
+                <property name="hvm">{hvm_value}</property>
+            </properties>
+        </domain>
+        '''
+        xml = lxml.etree.XML(xml_template.format(hvm_value='True'))
+        vm = qubes.vm.qubesvm.QubesVM(self.app, xml)
+        self.assertEqual(vm.virt_mode, 'hvm')
+        with self.assertRaises(AttributeError):
+            vm.hvm
+
+        xml = lxml.etree.XML(xml_template.format(hvm_value='False'))
+        vm = qubes.vm.qubesvm.QubesVM(self.app, xml)
+        self.assertEqual(vm.virt_mode, 'pv')
+        with self.assertRaises(AttributeError):
+            vm.hvm

+ 10 - 0
qubes/vm/qubesvm.py

@@ -621,6 +621,16 @@ class QubesVM(qubes.vm.mix.net.NetVMMixin, qubes.vm.BaseVM):
     #
 
     def __init__(self, app, xml, volume_config=None, **kwargs):
+        # migrate renamed properties
+        if xml is not None:
+            node_hvm = xml.find('./properties/property[@name=\'hvm\']')
+            if node_hvm is not None:
+                if qubes.property.bool(None, None, node_hvm.text):
+                    kwargs['virt_mode'] = 'hvm'
+                else:
+                    kwargs['virt_mode'] = 'pv'
+                node_hvm.getparent().remove(node_hvm)
+
         super(QubesVM, self).__init__(app, xml, **kwargs)
 
         if volume_config is None: