Skip to content

Commit d44a947

Browse files
CPU features for system VMs
1 parent 41b4f0a commit d44a947

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
@@ -390,14 +390,25 @@ public class AgentProperties{
390390
public static final Property<String> GUEST_CPU_ARCH = new Property<>("guest.cpu.arch", null, String.class);
391391

392392
/**
393-
* This param will require CPU features on the CPU section.<br>
394-
* The features listed in this property must be separated by a blank space (see example below).<br>
393+
* Specifies required CPU features for end-user VMs (non-system VMs).<br>
394+
* These features must be present on the host CPU for VM deployment.<br>
395+
* Multiple features should be separated by whitespace (see example below).<br>
395396
* Possible values: vmx vme <br>
396397
* Data type: String.<br>
397398
* Default value: <code>null</code>
398399
*/
399400
public static final Property<String> GUEST_CPU_FEATURES = new Property<>("guest.cpu.features", null, String.class);
400401

402+
/**
403+
* Specifies required CPU features for system VMs.<br>
404+
* These features must be present on the host CPU for VM deployment.<br>
405+
* Multiple features should be separated by whitespace (see example below).<br>
406+
* Possible values: vmx vme <br>
407+
* Data type: String.<br>
408+
* Default value: <code>null</code>
409+
*/
410+
public static final Property<String> SYSTEMVM_GUEST_CPU_FEATURES = new Property<>("systemvm.guest.cpu.features", null, String.class);
411+
401412
/**
402413
* Disables memory ballooning on VM guests for overcommit.<br>
403414
* 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
@@ -843,6 +843,8 @@ protected String getDefaultScriptsDir() {
843843

844844
protected List<String> cpuFeatures;
845845

846+
protected List<String> systemVmCpuFeatures;
847+
846848
protected enum BridgeType {
847849
NATIVE, OPENVSWITCH, TUNGSTEN
848850
}
@@ -1283,15 +1285,8 @@ public boolean configure(final String name, final Map<String, Object> params) th
12831285
params.put("guest.cpu.model", guestCpuModel);
12841286
}
12851287

1286-
final String cpuFeatures = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES);
1287-
if (cpuFeatures != null) {
1288-
this.cpuFeatures = new ArrayList<String>();
1289-
for (final String feature: cpuFeatures.split(" ")) {
1290-
if (!feature.isEmpty()) {
1291-
this.cpuFeatures.add(feature);
1292-
}
1293-
}
1294-
}
1288+
this.cpuFeatures = parseCpuFeatures(AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES));
1289+
this.systemVmCpuFeatures = parseCpuFeatures(AgentPropertiesFileHandler.getPropertyValue(AgentProperties.SYSTEMVM_GUEST_CPU_FEATURES));
12951290

12961291
final String[] info = NetUtils.getNetworkParams(privateNic);
12971292

@@ -1397,6 +1392,22 @@ public boolean configure(final String name, final Map<String, Object> params) th
13971392
return true;
13981393
}
13991394

1395+
/**
1396+
* Parses a string containing whitespace-separated CPU feature names and converts it into a list.
1397+
*
1398+
* @param features A string containing whitespace-separated CPU feature names to be parsed.
1399+
* @return A list of CPU feature strings. Returns an empty list if {@code features} is null.
1400+
*/
1401+
protected List<String> parseCpuFeatures(String features) {
1402+
if (features == null) {
1403+
return new ArrayList<>();
1404+
}
1405+
1406+
return Arrays.stream(features.split(" "))
1407+
.filter(feature -> !feature.isEmpty())
1408+
.collect(Collectors.toList());
1409+
}
1410+
14001411
/**
14011412
* Gets the ID list of the VMs to set memory balloon stats period.
14021413
* @param conn the Libvirt connection.
@@ -2986,6 +2997,8 @@ private CpuModeDef createCpuModeDef(VirtualMachineTO vmTO, int vcpus) {
29862997
cmd.setModel(cpuModel);
29872998
if (VirtualMachine.Type.User.equals(vmTO.getType())) {
29882999
cmd.setFeatures(cpuFeatures);
3000+
} else if (vmTO.getType().isUsedBySystem()) {
3001+
cmd.setFeatures(systemVmCpuFeatures);
29893002
}
29903003
int vCpusInDef = vmTO.getVcpuMaxLimit() == null ? vcpus : vmTO.getVcpuMaxLimit();
29913004
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
@@ -6620,4 +6620,20 @@ public void recreateCheckpointsOnVmTestVolumesHaveCheckpoints() {
66206620
Mockito.verify(libvirtComputingResourceSpy, Mockito.times(1)).recreateCheckpointsOfDisk(Mockito.any(), Mockito.any(), Mockito.any());
66216621
Assert.assertTrue(result);
66226622
}
6623+
6624+
@Test
6625+
public void parseCpuFeaturesTestReturnEmptyListWhenFeaturesIsNull() {
6626+
List<String> cpuFeatures = libvirtComputingResourceSpy.parseCpuFeatures(null);
6627+
Assert.assertEquals(0, cpuFeatures.size());
6628+
}
6629+
6630+
@Test
6631+
public void parseCpuFeaturesTestReturnListOfCpuFeaturesAndIgnoreMultipleWhitespacesAlongsideEachOther() {
6632+
List<String> cpuFeatures = libvirtComputingResourceSpy.parseCpuFeatures(" -mca mce -mmx hle ");
6633+
Assert.assertEquals(4, cpuFeatures.size());
6634+
Assert.assertEquals("-mca", cpuFeatures.get(0));
6635+
Assert.assertEquals("mce", cpuFeatures.get(1));
6636+
Assert.assertEquals("-mmx", cpuFeatures.get(2));
6637+
Assert.assertEquals("hle", cpuFeatures.get(3));
6638+
}
66236639
}

0 commit comments

Comments
 (0)