Skip to content

Commit bdc6f60

Browse files
CPU features for system VMs
1 parent 86827f8 commit bdc6f60

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

427427
/**
428-
* This param will require CPU features on the CPU section.<br>
429-
* The features listed in this property must be separated by a blank space (see example below).<br>
428+
* Specifies required CPU features for end-user VMs (non-system VMs).<br>
429+
* These features must be present on the host CPU for VM deployment.<br>
430+
* Multiple features should be separated by whitespace (see example below).<br>
430431
* Possible values: vmx vme <br>
431432
* Data type: String.<br>
432433
* Default value: <code>null</code>
433434
*/
434435
public static final Property<String> GUEST_CPU_FEATURES = new Property<>("guest.cpu.features", null, String.class);
435436

437+
/**
438+
* Specifies required CPU features for system VMs.<br>
439+
* These features must be present on the host CPU for VM deployment.<br>
440+
* Multiple features should be separated by whitespace (see example below).<br>
441+
* Possible values: vmx vme <br>
442+
* Data type: String.<br>
443+
* Default value: <code>null</code>
444+
*/
445+
public static final Property<String> SYSTEMVM_GUEST_CPU_FEATURES = new Property<>("systemvm.guest.cpu.features", null, String.class);
446+
436447
/**
437448
* Disables memory ballooning on VM guests for overcommit.<br>
438449
* 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
@@ -869,6 +869,8 @@ protected String getDefaultScriptsDir() {
869869

870870
protected List<String> cpuFeatures;
871871

872+
protected List<String> systemVmCpuFeatures;
873+
872874
protected enum BridgeType {
873875
NATIVE, OPENVSWITCH, TUNGSTEN
874876
}
@@ -1316,15 +1318,8 @@ public boolean configure(final String name, final Map<String, Object> params) th
13161318
params.put("guest.cpu.model", guestCpuModel);
13171319
}
13181320

1319-
final String cpuFeatures = AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES);
1320-
if (cpuFeatures != null) {
1321-
this.cpuFeatures = new ArrayList<String>();
1322-
for (final String feature: cpuFeatures.split(" ")) {
1323-
if (!feature.isEmpty()) {
1324-
this.cpuFeatures.add(feature);
1325-
}
1326-
}
1327-
}
1321+
this.cpuFeatures = parseCpuFeatures(AgentPropertiesFileHandler.getPropertyValue(AgentProperties.GUEST_CPU_FEATURES));
1322+
this.systemVmCpuFeatures = parseCpuFeatures(AgentPropertiesFileHandler.getPropertyValue(AgentProperties.SYSTEMVM_GUEST_CPU_FEATURES));
13281323

13291324
final String[] info = NetUtils.getNetworkParams(privateNic);
13301325

@@ -1430,6 +1425,22 @@ public boolean configure(final String name, final Map<String, Object> params) th
14301425
return true;
14311426
}
14321427

1428+
/**
1429+
* Parses a string containing whitespace-separated CPU feature names and converts it into a list.
1430+
*
1431+
* @param features A string containing whitespace-separated CPU feature names to be parsed.
1432+
* @return A list of CPU feature strings. Returns an empty list if {@code features} is null.
1433+
*/
1434+
protected List<String> parseCpuFeatures(String features) {
1435+
if (features == null) {
1436+
return new ArrayList<>();
1437+
}
1438+
1439+
return Arrays.stream(features.split(" "))
1440+
.filter(feature -> !feature.isEmpty())
1441+
.collect(Collectors.toList());
1442+
}
1443+
14331444
/**
14341445
* Gets the ID list of the VMs to set memory balloon stats period.
14351446
* @param conn the Libvirt connection.
@@ -3212,6 +3223,8 @@ private CpuModeDef createCpuModeDef(VirtualMachineTO vmTO, int vcpus) {
32123223
cmd.setModel(cpuModel);
32133224
if (VirtualMachine.Type.User.equals(vmTO.getType())) {
32143225
cmd.setFeatures(cpuFeatures);
3226+
} else if (vmTO.getType().isUsedBySystem()) {
3227+
cmd.setFeatures(systemVmCpuFeatures);
32153228
}
32163229
int vCpusInDef = vmTO.getVcpuMaxLimit() == null ? vcpus : vmTO.getVcpuMaxLimit();
32173230
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
@@ -7126,4 +7126,20 @@ public void testParseGpuDevicesFromResult_withSuccess() {
71267126
assertEquals("2g.10gb", vfInstance2.getModelName());
71277127
assertNull(vfInstance2.getVmName());
71287128
}
7129+
7130+
@Test
7131+
public void parseCpuFeaturesTestReturnEmptyListWhenFeaturesIsNull() {
7132+
List<String> cpuFeatures = libvirtComputingResourceSpy.parseCpuFeatures(null);
7133+
Assert.assertEquals(0, cpuFeatures.size());
7134+
}
7135+
7136+
@Test
7137+
public void parseCpuFeaturesTestReturnListOfCpuFeaturesAndIgnoreMultipleWhitespacesAlongsideEachOther() {
7138+
List<String> cpuFeatures = libvirtComputingResourceSpy.parseCpuFeatures(" -mca mce -mmx hle ");
7139+
Assert.assertEquals(4, cpuFeatures.size());
7140+
Assert.assertEquals("-mca", cpuFeatures.get(0));
7141+
Assert.assertEquals("mce", cpuFeatures.get(1));
7142+
Assert.assertEquals("-mmx", cpuFeatures.get(2));
7143+
Assert.assertEquals("hle", cpuFeatures.get(3));
7144+
}
71297145
}

0 commit comments

Comments
 (0)