[KVM] CPU Features for System VMs (#10964)

* CPU features for System VMs

* Apply guest.cpu.features for System VMs
This commit is contained in:
Bernardo De Marco Gonçalves 2025-08-15 11:32:50 -03:00 committed by GitHub
parent 2c34f5e495
commit ba2d70ab21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 16 deletions

View File

@ -213,8 +213,9 @@ hypervisor.type=kvm
# If null (default), defaults to the VM's OS architecture
#guest.cpu.arch=
# This param will require CPU features on the CPU section.
# The features listed in this property must be separated by a blank space (e.g.: vmx vme)
# Specifies required CPU features for end-user and system VMs.
# These features must be present on the host CPU for VM deployment.
# Multiple features should be separated by whitespace (e.g.: vmx vme).
#guest.cpu.features=
# Disables memory ballooning on VM guests for overcommit.

View File

@ -425,8 +425,9 @@ public class AgentProperties{
public static final Property<String> GUEST_CPU_ARCH = new Property<>("guest.cpu.arch", null, String.class);
/**
* This param will require CPU features on the CPU section.<br>
* The features listed in this property must be separated by a blank space (see example below).<br>
* Specifies required CPU features for end-user and system VMs.<br>
* These features must be present on the host CPU for VM deployment.<br>
* Multiple features should be separated by whitespace (see example below).<br>
* Possible values: vmx vme <br>
* Data type: String.<br>
* Default value: <code>null</code>

View File

@ -1316,15 +1316,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
params.put("guest.cpu.model", guestCpuModel);
}
final String cpuFeatures = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES);
if (cpuFeatures != null) {
this.cpuFeatures = new ArrayList<String>();
for (final String feature: cpuFeatures.split(" ")) {
if (!feature.isEmpty()) {
this.cpuFeatures.add(feature);
}
}
}
this.cpuFeatures = parseCpuFeatures(AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES));
final String[] info = NetUtils.getNetworkParams(privateNic);
@ -1430,6 +1422,22 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
return true;
}
/**
* Parses a string containing whitespace-separated CPU feature names and converts it into a list.
*
* @param features A string containing whitespace-separated CPU feature names to be parsed.
* @return A list of CPU feature strings. Returns an empty list if {@code features} is null.
*/
protected List<String> parseCpuFeatures(String features) {
if (features == null) {
return new ArrayList<>();
}
return Arrays.stream(features.split(" "))
.filter(feature -> !feature.isEmpty())
.collect(Collectors.toList());
}
/**
* Gets the ID list of the VMs to set memory balloon stats period.
* @param conn the Libvirt connection.
@ -3210,9 +3218,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
String cpuModel = MapUtils.isNotEmpty(details) && details.get(VmDetailConstants.GUEST_CPU_MODEL) != null ? details.get(VmDetailConstants.GUEST_CPU_MODEL) : guestCpuModel;
cmd.setMode(cpuMode);
cmd.setModel(cpuModel);
if (VirtualMachine.Type.User.equals(vmTO.getType())) {
cmd.setFeatures(cpuFeatures);
}
cmd.setFeatures(cpuFeatures);
int vCpusInDef = vmTO.getVcpuMaxLimit() == null ? vcpus : vmTO.getVcpuMaxLimit();
setCpuTopology(cmd, vCpusInDef, vmTO.getDetails());
return cmd;

View File

@ -7126,4 +7126,20 @@ public class LibvirtComputingResourceTest {
assertEquals("2g.10gb", vfInstance2.getModelName());
assertNull(vfInstance2.getVmName());
}
@Test
public void parseCpuFeaturesTestReturnEmptyListWhenFeaturesIsNull() {
List<String> cpuFeatures = libvirtComputingResourceSpy.parseCpuFeatures(null);
Assert.assertEquals(0, cpuFeatures.size());
}
@Test
public void parseCpuFeaturesTestReturnListOfCpuFeaturesAndIgnoreMultipleWhitespacesAlongsideEachOther() {
List<String> cpuFeatures = libvirtComputingResourceSpy.parseCpuFeatures(" -mca mce -mmx hle ");
Assert.assertEquals(4, cpuFeatures.size());
Assert.assertEquals("-mca", cpuFeatures.get(0));
Assert.assertEquals("mce", cpuFeatures.get(1));
Assert.assertEquals("-mmx", cpuFeatures.get(2));
Assert.assertEquals("hle", cpuFeatures.get(3));
}
}