Skip to content

Commit 57d72e5

Browse files
author
Dingane Hlaluku
committed
Allow additional VM config on KVM
1 parent 363d74b commit 57d72e5

File tree

6 files changed

+70
-2
lines changed

6 files changed

+70
-2
lines changed

api/src/main/java/com/cloud/agent/api/to/VirtualMachineTO.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ public class VirtualMachineTO {
7272

7373
Double cpuQuotaPercentage = null;
7474

75+
boolean enableExtraConfig = false;
76+
77+
public boolean isExtraConfigEnabled() {
78+
return enableExtraConfig;
79+
}
80+
81+
public String getExtraConfig() {
82+
return extraConfig;
83+
}
84+
85+
String extraConfig = null;
86+
7587
Map<String, String> guestOsDetails = new HashMap<String, String>();
7688

7789
public VirtualMachineTO(long id, String instanceName, VirtualMachine.Type type, int cpus, Integer speed, long minRam, long maxRam, BootloaderType bootloader,
@@ -350,4 +362,12 @@ public Double getCpuQuotaPercentage() {
350362
public void setCpuQuotaPercentage(Double cpuQuotaPercentage) {
351363
this.cpuQuotaPercentage = cpuQuotaPercentage;
352364
}
365+
366+
public void setEnableExtraConfig(boolean enableExtraConfig) {
367+
this.enableExtraConfig = enableExtraConfig;
368+
}
369+
370+
public void setExtraConfig(String extraConfig) {
371+
this.extraConfig = extraConfig;
372+
}
353373
}

api/src/main/java/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public class DeployVMCmd extends BaseAsyncCreateCustomIdCmd implements SecurityG
200200
" an optional parameter used to create additional data disks from datadisk templates; can't be specified with diskOfferingId parameter")
201201
private Map dataDiskTemplateToDiskOfferingList;
202202

203-
@Parameter(name = ApiConstants.EXTRA_CONFIG, type = CommandType.STRING, since = "4.12", description = "an optional URL encoded string that can be passed to the virtual machine upon successful deployment", authorized = { RoleType.Admin })
203+
@Parameter(name = ApiConstants.EXTRA_CONFIG, type = CommandType.STRING, since = "4.12", description = "an optional URL encoded string that can be passed to the virtual machine upon successful deployment", authorized = { RoleType.Admin }, length = 5120)
204204
private String extraConfig;
205205

206206
/////////////////////////////////////////////////////
@@ -485,6 +485,10 @@ public Map<Long, DiskOffering> getDataDiskTemplateToDiskOfferingMap() {
485485
return dataDiskTemplateToDiskOfferingMap;
486486
}
487487

488+
public String getExtraConfig() {
489+
return extraConfig;
490+
}
491+
488492
/////////////////////////////////////////////////////
489493
/////////////// API Implementation///////////////////
490494
/////////////////////////////////////////////////////

engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
package com.cloud.vm;
1919

20+
import java.io.UnsupportedEncodingException;
2021
import java.net.URI;
22+
import java.net.URLDecoder;
2123
import java.sql.PreparedStatement;
2224
import java.sql.ResultSet;
2325
import java.sql.SQLException;
@@ -1104,6 +1106,19 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
11041106

11051107
final VirtualMachineTO vmTO = hvGuru.implement(vmProfile);
11061108

1109+
UserVmDetailVO detailVO = _uservmDetailsDao.findDetail(vm.getId(), "extraConfig");
1110+
1111+
if (detailVO != null) {
1112+
vmTO.setEnableExtraConfig(true);
1113+
String decodeURL;
1114+
try {
1115+
decodeURL = URLDecoder.decode(detailVO.getValue(), "UTF-8");
1116+
} catch (UnsupportedEncodingException e) {
1117+
throw new CloudRuntimeException("Failed to decode provided URL string");
1118+
}
1119+
vmTO.setExtraConfig(decodeURL);
1120+
}
1121+
11071122
handlePath(vmTO.getDisks(), vm.getHypervisorType());
11081123

11091124
cmds = new Commands(Command.OnError.Stop);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import javax.xml.parsers.DocumentBuilderFactory;
4848
import javax.xml.parsers.ParserConfigurationException;
4949

50+
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.GenericXML;
5051
import com.cloud.resource.RequestWrapper;
5152
import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
5253
import org.apache.cloudstack.storage.to.VolumeObjectTO;
@@ -2186,6 +2187,10 @@ So if getMinSpeed() returns null we fall back to getSpeed().
21862187

21872188
vm.addComp(devices);
21882189

2190+
GenericXML genericXML = new GenericXML();
2191+
genericXML.setExtraConfig(vmTO.getExtraConfig());
2192+
vm.addComp(genericXML);
2193+
21892194
return vm;
21902195
}
21912196

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,4 +1834,17 @@ public String toString() {
18341834
vmBuilder.append("</domain>\n");
18351835
return vmBuilder.toString();
18361836
}
1837+
1838+
public static class GenericXML {
1839+
private String extraConfig;
1840+
1841+
public void setExtraConfig(String extraConfig) {
1842+
this.extraConfig = extraConfig;
1843+
}
1844+
1845+
@Override
1846+
public String toString() {
1847+
return extraConfig;
1848+
}
1849+
}
18371850
}

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import javax.inject.Inject;
4040
import javax.naming.ConfigurationException;
4141

42+
import com.google.common.base.Strings;
4243
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
4344
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
4445
import org.apache.cloudstack.affinity.AffinityGroupService;
@@ -515,6 +516,8 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
515516
private static final ConfigKey<Boolean> AllowDeployVmIfGivenHostFails = new ConfigKey<Boolean>("Advanced", Boolean.class, "allow.deploy.vm.if.deploy.on.given.host.fails", "false",
516517
"allow vm to deploy on different host if vm fails to deploy on the given host ", true);
517518

519+
private static final ConfigKey<Boolean> EnableAdditionalVmConfig = new ConfigKey<>("Advanced", Boolean.class, "enable.additional.vm.configuration", "false", "allow additional arbitrary configuration to vm", true);
520+
518521

519522
@Override
520523
public UserVmVO getVirtualMachine(long vmId) {
@@ -4852,6 +4855,14 @@ public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityE
48524855
_tmplService.attachIso(tmpl.getId(), vm.getId());
48534856
}
48544857
}
4858+
4859+
// Add extraConfig to user_vm_details table
4860+
if (EnableAdditionalVmConfig.value()) {
4861+
String extraConfig = cmd.getExtraConfig();
4862+
if (!Strings.isNullOrEmpty(extraConfig)) {
4863+
_uservmDetailsDao.addDetail(vm.getId(), "extraConfig", extraConfig, true);
4864+
}
4865+
}
48554866
return vm;
48564867
}
48574868

@@ -6399,7 +6410,7 @@ public String getConfigComponentName() {
63996410
@Override
64006411
public ConfigKey<?>[] getConfigKeys() {
64016412
return new ConfigKey<?>[] {EnableDynamicallyScaleVm, AllowUserExpungeRecoverVm, VmIpFetchWaitInterval, VmIpFetchTrialMax, VmIpFetchThreadPoolMax,
6402-
VmIpFetchTaskWorkers, AllowDeployVmIfGivenHostFails};
6413+
VmIpFetchTaskWorkers, AllowDeployVmIfGivenHostFails, EnableAdditionalVmConfig};
64036414
}
64046415

64056416
@Override

0 commit comments

Comments
 (0)