From 63fb7defee298288a6690bb96c49e55b1aa963ed Mon Sep 17 00:00:00 2001 From: catborise Date: Wed, 11 Nov 2026 12:41:21 +0300 Subject: [PATCH] fix(instances): a refused vCPU resize keeps the VM's vCPU hotplug With vCPU hotplug on, a resize defined the VM three times: without its hotplug settings, with the new counts, with hotplug settings again. When libvirt refused the new counts (a maximum over the machine type's limit), the VM was left with hotplug switched off and an error. The counts and the hotplug settings for them now go in one definition. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- vrtManager/instance.py | 38 ++++++++++++++--------------- vrtManager/tests/test_resize_cpu.py | 10 +++++--- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/vrtManager/instance.py b/vrtManager/instance.py index 9886f2c8..05334fcb 100644 --- a/vrtManager/instance.py +++ b/vrtManager/instance.py @@ -1352,22 +1352,26 @@ class wvmInstance(wvmConnect): """Turns a hotpluggable vCPU of the active VM on or off, now and for its next start.""" self.instance.setVcpu(str(cpu_id), enabled, VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG) + @staticmethod + def _hotplug_vcpus(enabled): + """ with the first enabled vCPUs on at boot; libvirt adds the + rest up to the maximum as disabled hotpluggable vCPUs.""" + vcpus = etree.Element("vcpus") + etree.SubElement(vcpus, "vcpu", id="0", enabled="yes", hotpluggable="no", order="1") + for i in range(1, enabled): + etree.SubElement(vcpus, "vcpu", id=str(i), enabled="yes", hotpluggable="yes", order=str(i + 1)) + return vcpus + def set_vcpu_hotplug(self, status, vcpus_hotplug=0): """vcpus_hotplug: the vCPUs enabled at boot, 0 keeps the current ones; libvirt adds the rest up to the maximum as disabled hotpluggable vCPUs""" vcpus_hotplug = self.get_cur_vcpu() if vcpus_hotplug == 0 else vcpus_hotplug if self.get_status() == 5: # shutoff if status: - xml = """ """ - xml += """""" - for i in range(1, vcpus_hotplug): - xml += f"""""" - xml += """""" - tree = etree.fromstring(self._XMLDesc(PERSISTENT_XML)) vcpus = tree.xpath("/domain/vcpus") if not vcpus: - tree.append(etree.fromstring(xml)) + tree.append(self._hotplug_vcpus(vcpus_hotplug)) self._defineXML(etree.tostring(tree).decode()) else: tree = etree.fromstring(self._XMLDesc(PERSISTENT_XML)) @@ -1677,22 +1681,16 @@ class wvmInstance(wvmConnect): if count != int(vcpu): raise util.OperationError(gettext_noop("The VM's CPU topology has %(count)s vCPUs: change it in the " "XML editor to set another maximum"), count=count) - is_vcpus_enabled = self.get_vcpus() - if is_vcpus_enabled: - self.set_vcpu_hotplug(False) - - xml = self._XMLDesc(PERSISTENT_XML) - tree = etree.fromstring(xml) - + tree = etree.fromstring(self._XMLDesc(PERSISTENT_XML)) vcpu_elem = tree.find("vcpu") vcpu_elem.text = vcpu vcpu_elem.set("current", cur_vcpu) - - new_xml = etree.tostring(tree).decode() - self._defineXML(new_xml) - - if is_vcpus_enabled: - self.set_vcpu_hotplug(True, int(cur_vcpu)) + # hotplug settings are of the old count: made again for the new one, + # in the same definition, so a refused one leaves them as they were + hotplug = tree.find("vcpus") + if hotplug is not None: + tree.replace(hotplug, self._hotplug_vcpus(int(cur_vcpu))) + self._defineXML(etree.tostring(tree).decode()) def resize_mem(self, cur_memory, memory): """ diff --git a/vrtManager/tests/test_resize_cpu.py b/vrtManager/tests/test_resize_cpu.py index 8b108664..dd767ea6 100644 --- a/vrtManager/tests/test_resize_cpu.py +++ b/vrtManager/tests/test_resize_cpu.py @@ -35,6 +35,10 @@ class ResizeCpuTests(unittest.TestCase): for topology, count in (("", "4"), ("", "4")): with self.subTest(topology): inst = shut_off_vm(DOMAIN_XML.format(count=2, topology=topology)) - inst.resize_cpu("2", count) - vcpu = etree.fromstring(inst.wvm.defineXML.call_args_list[1].args[0]).find("vcpu") - self.assertEqual((vcpu.text, vcpu.get("current")), ("4", "2")) + inst.resize_cpu("3", count) + # one definition: libvirt refusing it leaves the hotplug settings as they were + tree = etree.fromstring(inst.wvm.defineXML.call_args.args[0]) + self.assertEqual(inst.wvm.defineXML.call_count, 1) + vcpu = tree.find("vcpu") + self.assertEqual((vcpu.text, vcpu.get("current")), ("4", "3")) + self.assertEqual(tree.xpath("vcpus/vcpu[@enabled='yes']/@id"), ["0", "1", "2"])