[elbe-devel] [PATCH 2/2] elbepack: init: use actual CPU count from libvirt domain

Thomas Weißschuh thomas.weissschuh at linutronix.de
Mon Aug 5 14:50:19 CEST 2024


The clamping to the CPUs of the local machine will be wrong when the VM
is running on another host.
Ask libvirt for a more correct limit.
If the VM is running on the local machine this will be the same value as
before.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/init/libvirt.xml.mako | 2 --
 elbepack/initvm.py             | 7 +++++++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/elbepack/init/libvirt.xml.mako b/elbepack/init/libvirt.xml.mako
index 8308a1f42245..32d2c0378be5 100644
--- a/elbepack/init/libvirt.xml.mako
+++ b/elbepack/init/libvirt.xml.mako
@@ -7,7 +7,6 @@
 # TODO: Relativ file path for diskimage
 
 
-import multiprocessing
 import os
 import shutil
 from elbepack.filesystem import size_to_int
@@ -17,7 +16,6 @@ from elbepack.uuid7 import uuid7
 uid = uuid7()
 
 cpus = int(prj.text('max-cpus', default=defs, key='max-cpus'))
-cpus = min(multiprocessing.cpu_count(), cpus)
 memory = size_to_int(prj.text('mem', default=defs, key='mem')) // 1024
 
 imagetype = prj.text('img', default=defs, key='img')
diff --git a/elbepack/initvm.py b/elbepack/initvm.py
index 3870b7b97319..f9fc7b6baf20 100644
--- a/elbepack/initvm.py
+++ b/elbepack/initvm.py
@@ -169,6 +169,12 @@ class LibvirtInitVM(_InitVM):
 
         xml.root.et.attrib['type'] = 'kvm'
 
+    def _libvirt_set_vcpus(self, xml):
+        vcpu_elem = xml.et.find('.//vcpu')
+        cpus = min(int(vcpu_elem.text),
+                   self._conn.getMaxVcpus(xml.root.et.attrib['type']))
+        vcpu_elem.text = str(cpus)
+
     def _build(self):
         domain = self._get_domain()
         if domain is not None:
@@ -193,6 +199,7 @@ class LibvirtInitVM(_InitVM):
             xml = etree(f)
 
         self._libvirt_enable_kvm(xml)
+        self._libvirt_set_vcpus(xml)
 
         # Register initvm in libvirt.
         try:

-- 
2.45.2



More information about the elbe-devel mailing list