Skip to content

Commit 62d9b91

Browse files
CPU features for system VMs
1 parent b57994e commit 62d9b91

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

agent/conf/agent.properties

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,16 @@ hypervisor.type=kvm
213213
# If null (default), defaults to the VM's OS architecture
214214
#guest.cpu.arch=
215215

216-
# This param will require CPU features on the CPU section.
217-
# The features listed in this property must be separated by a blank space (e.g.: vmx vme)
216+
# Specifies required CPU features for end-user VMs (non-system VMs).
217+
# These features must be present on the host CPU for VM deployment.
218+
# Multiple features should be separated by whitespace (e.g.: vmx vme).
218219
#guest.cpu.features=
219220

221+
# Specifies required CPU features for system VMs.
222+
# These features must be present on the host CPU for VM deployment.
223+
# Multiple features should be separated by whitespace (e.g.: vmx vme).
224+
#systemvm.guest.cpu.features=
225+
220226
# Disables memory ballooning on VM guests for overcommit.
221227
# By default overcommit feature enables balloon and sets currentMemory to a minimum value.
222228
#vm.memballoon.disable=false

agent/src/main/java/com/cloud/agent/properties/AgentProperties.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,14 +398,25 @@ public class AgentProperties{
398398
public static final Property<String> GUEST_CPU_ARCH = new Property<>("guest.cpu.arch", null, String.class);
399399

400400
/**
401-
* This param will require CPU features on the CPU section.<br>
402-
* The features listed in this property must be separated by a blank space (see example below).<br>
401+
* Specifies required CPU features for end-user VMs (non-system VMs).<br>
402+
* These features must be present on the host CPU for VM deployment.<br>
403+
* Multiple features should be separated by whitespace (see example below).<br>
403404
* Possible values: vmx vme <br>
404405
* Data type: String.<br>
405406
* Default value: <code>null</code>
406407
*/
407408
public static final Property<String> GUEST_CPU_FEATURES = new Property<>("guest.cpu.features", null, String.class);
408409

410+
/**
411+
* Specifies required CPU features for system VMs.<br>
412+
* These features must be present on the host CPU for VM deployment.<br>
413+
* Multiple features should be separated by whitespace (see example below).<br>
414+
* Possible values: vmx vme <br>
415+
* Data type: String.<br>
416+
* Default value: <code>null</code>
417+
*/
418+
public static final Property<String> SYSTEMVM_GUEST_CPU_FEATURES = new Property<>("systemvm.guest.cpu.features", null, String.class);
419+
409420
/**
410421
* Disables memory ballooning on VM guests for overcommit.<br>
411422
* By default overcommit feature enables balloon and sets currentMemory to a minimum value.<br>

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,8 @@ protected String getDefaultScriptsDir() {
860860

861861
protected List<String> cpuFeatures;
862862

863+
protected List<String> systemVmCpuFeatures;
864+
863865
protected enum BridgeType {
864866
NATIVE, OPENVSWITCH, TUNGSTEN
865867
}
@@ -1302,15 +1304,8 @@ public boolean configure(final String name, final Map<String, Object> params) th
13021304
params.put("guest.cpu.model", guestCpuModel);
13031305
}
13041306

1305-
final String cpuFeatures = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES);
1306-
if (cpuFeatures != null) {
1307-
this.cpuFeatures = new ArrayList<String>();
1308-
for (final String feature: cpuFeatures.split(" ")) {
1309-
if (!feature.isEmpty()) {
1310-
this.cpuFeatures.add(feature);
1311-
}
1312-
}
1313-
}
1307+
this.cpuFeatures = parseCpuFeatures(AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES));
1308+
this.systemVmCpuFeatures = parseCpuFeatures(AgentPropertiesFileHandler.getPropertyValue(AgentProperties.SYSTEMVM_GUEST_CPU_FEATURES));
13141309

13151310
final String[] info = NetUtils.getNetworkParams(privateNic);
13161311

@@ -1416,6 +1411,22 @@ public boolean configure(final String name, final Map<String, Object> params) th
14161411
return true;
14171412
}
14181413

1414+
/**
1415+
* Parses a string containing whitespace-separated CPU feature names and converts it into a list.
1416+
*
1417+
* @param features A string containing whitespace-separated CPU feature names to be parsed.
1418+
* @return A list of CPU feature strings. Returns an empty list if {@code features} is null.
1419+
*/
1420+
protected List<String> parseCpuFeatures(String features) {
1421+
if (features == null) {
1422+
return new ArrayList<>();
1423+
}
1424+
1425+
return Arrays.stream(features.split(" "))
1426+
.filter(feature -> !feature.isEmpty())
1427+
.collect(Collectors.toList());
1428+
}
1429+
14191430
/**
14201431
* Gets the ID list of the VMs to set memory balloon stats period.
14211432
* @param conn the Libvirt connection.
@@ -3005,6 +3016,8 @@ private CpuModeDef createCpuModeDef(VirtualMachineTO vmTO, int vcpus) {
30053016
cmd.setModel(cpuModel);
30063017
if (VirtualMachine.Type.User.equals(vmTO.getType())) {
30073018
cmd.setFeatures(cpuFeatures);
3019+
} else if (vmTO.getType().isUsedBySystem()) {
3020+
cmd.setFeatures(systemVmCpuFeatures);
30083021
}
30093022
int vCpusInDef = vmTO.getVcpuMaxLimit() == null ? vcpus : vmTO.getVcpuMaxLimit();
30103023
setCpuTopology(cmd, vCpusInDef, vmTO.getDetails());

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6779,4 +6779,20 @@ public void manuallyDeleteUnusedSnapshotFileTestLibvirtSupportingFlagDeleteOnCom
67796779
libvirtComputingResourceSpy.manuallyDeleteUnusedSnapshotFile(true, "");
67806780
Mockito.verify(libvirtComputingResourceSpy, Mockito.never()).deleteIfExists("");
67816781
}
6782+
6783+
@Test
6784+
public void parseCpuFeaturesTestReturnEmptyListWhenFeaturesIsNull() {
6785+
List<String> cpuFeatures = libvirtComputingResourceSpy.parseCpuFeatures(null);
6786+
Assert.assertEquals(0, cpuFeatures.size());
6787+
}
6788+
6789+
@Test
6790+
public void parseCpuFeaturesTestReturnListOfCpuFeaturesAndIgnoreMultipleWhitespacesAlongsideEachOther() {
6791+
List<String> cpuFeatures = libvirtComputingResourceSpy.parseCpuFeatures(" -mca mce -mmx hle ");
6792+
Assert.assertEquals(4, cpuFeatures.size());
6793+
Assert.assertEquals("-mca", cpuFeatures.get(0));
6794+
Assert.assertEquals("mce", cpuFeatures.get(1));
6795+
Assert.assertEquals("-mmx", cpuFeatures.get(2));
6796+
Assert.assertEquals("hle", cpuFeatures.get(3));
6797+
}
67826798
}

0 commit comments

Comments
 (0)