Skip to content

Commit bab3f0f

Browse files
author
Anthony Xu
committed
CLOUDSTACK-4976
merge createVmFromTemplate in XSFP1 to CitrixResourceBase instead of fixing one bug in two different place. Anthony
1 parent e94ec6b commit bab3f0f

2 files changed

Lines changed: 86 additions & 182 deletions

File tree

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java‎

Lines changed: 86 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
import com.cloud.exception.InternalErrorException;
175175
import com.cloud.host.Host.Type;
176176
import com.cloud.hypervisor.Hypervisor.HypervisorType;
177+
import com.cloud.hypervisor.xen.resource.CitrixHelper;
177178
import com.cloud.network.HAProxyConfigurator;
178179
import com.cloud.network.LoadBalancerConfigurator;
179180
import com.cloud.network.Networks;
@@ -1247,69 +1248,108 @@ protected VBD createVbd(Connection conn, DiskTO volume, String vmName, VM vm, Bo
12471248

12481249
return vbd;
12491250
}
1251+
1252+
1253+
public long getStaticMax(String os, boolean b, long dynamicMinRam, long dynamicMaxRam){
1254+
return dynamicMaxRam;
1255+
}
1256+
1257+
public long getStaticMin(String os, boolean b, long dynamicMinRam, long dynamicMaxRam){
1258+
return dynamicMinRam;
1259+
}
12501260

12511261
protected VM createVmFromTemplate(Connection conn, VirtualMachineTO vmSpec, Host host) throws XenAPIException, XmlRpcException {
12521262
String guestOsTypeName = getGuestOsType(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD);
1253-
if ( guestOsTypeName == null ) {
1254-
String msg = " Hypervisor " + this.getClass().getName() + " doesn't support guest OS type " + vmSpec.getOs()
1255-
+ ". you can choose 'Other install media' to run it as HVM";
1256-
s_logger.warn(msg);
1257-
throw new CloudRuntimeException(msg);
1258-
}
12591263
Set<VM> templates = VM.getByNameLabel(conn, guestOsTypeName);
12601264
assert templates.size() == 1 : "Should only have 1 template but found " + templates.size();
1261-
if (!templates.iterator().hasNext()) {
1262-
throw new CloudRuntimeException("No matching OS type found for starting a [" + vmSpec.getOs()
1263-
+ "] VM on host " + host.getHostname(conn));
1264-
}
12651265
VM template = templates.iterator().next();
1266-
VM vm = template.createClone(conn, vmSpec.getName());
1267-
VM.Record vmr = vm.getRecord(conn);
1268-
if (s_logger.isDebugEnabled()) {
1269-
s_logger.debug("Created VM " + vmr.uuid + " for " + vmSpec.getName());
1266+
1267+
VM.Record vmr = template.getRecord(conn);
1268+
vmr.affinity = host;
1269+
vmr.otherConfig.remove("disks");
1270+
vmr.otherConfig.remove("default_template");
1271+
vmr.otherConfig.remove("mac_seed");
1272+
vmr.isATemplate = false;
1273+
vmr.nameLabel = vmSpec.getName();
1274+
vmr.actionsAfterCrash = Types.OnCrashBehaviour.DESTROY;
1275+
vmr.actionsAfterShutdown = Types.OnNormalExit.DESTROY;
1276+
1277+
if (isDmcEnabled(conn, host) && vmSpec.isEnableDynamicallyScaleVm()) {
1278+
//scaling is allowed
1279+
vmr.memoryStaticMin = getStaticMin(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD, vmSpec.getMinRam(), vmSpec.getMaxRam());
1280+
vmr.memoryStaticMax = getStaticMax(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD, vmSpec.getMinRam(), vmSpec.getMaxRam());
1281+
vmr.memoryDynamicMin = vmSpec.getMinRam();
1282+
vmr.memoryDynamicMax = vmSpec.getMaxRam();
1283+
} else {
1284+
//scaling disallowed, set static memory target
1285+
if (vmSpec.isEnableDynamicallyScaleVm() && !isDmcEnabled(conn, host)) {
1286+
s_logger.warn("Host "+ host.getHostname(conn) +" does not support dynamic scaling, so the vm " + vmSpec.getName() + " is not dynamically scalable");
1287+
}
1288+
vmr.memoryStaticMin = vmSpec.getMinRam();
1289+
vmr.memoryStaticMax = vmSpec.getMaxRam();
1290+
vmr.memoryDynamicMin = vmSpec.getMinRam();
1291+
vmr.memoryDynamicMax = vmSpec.getMaxRam();
12701292
}
12711293

1272-
for (Console console : vmr.consoles) {
1273-
console.destroy(conn);
1294+
if (guestOsTypeName.toLowerCase().contains("windows")) {
1295+
vmr.VCPUsMax = (long) vmSpec.getCpus();
1296+
} else {
1297+
vmr.VCPUsMax = 32L;
12741298
}
12751299

1276-
vm.setIsATemplate(conn, false);
1277-
vm.setAffinity(conn, host);
1278-
vm.removeFromOtherConfig(conn, "disks");
1279-
vm.setNameLabel(conn, vmSpec.getName());
1280-
setMemory(conn, vm, vmSpec.getMinRam(),vmSpec.getMaxRam());
1281-
vm.setVCPUsMax(conn, (long)vmSpec.getCpus());
1282-
vm.setVCPUsAtStartup(conn, (long)vmSpec.getCpus());
1300+
Map<String, String> details = vmSpec.getDetails();
1301+
if ( details != null ) {
1302+
String timeoffset = details.get("timeoffset");
1303+
if (timeoffset != null) {
1304+
Map<String, String> platform = vmr.platform;
1305+
platform.put("timeoffset", timeoffset);
1306+
vmr.platform = platform;
1307+
}
1308+
1309+
String coresPerSocket = details.get("cpu.corespersocket");
1310+
if (coresPerSocket != null) {
1311+
Map<String, String> platform = vmr.platform;
1312+
platform.put("cores-per-socket", coresPerSocket);
1313+
vmr.platform = platform;
1314+
}
1315+
}
1316+
1317+
vmr.VCPUsAtStartup = (long) vmSpec.getCpus();
1318+
vmr.consoles.clear();
1319+
1320+
VM vm = VM.create(conn, vmr);
1321+
if (s_logger.isDebugEnabled()) {
1322+
s_logger.debug("Created VM " + vm.getUuid(conn) + " for " + vmSpec.getName());
1323+
}
12831324

12841325
Map<String, String> vcpuParams = new HashMap<String, String>();
12851326

12861327
Integer speed = vmSpec.getMinSpeed();
12871328
if (speed != null) {
12881329

1289-
int cpuWeight = _maxWeight; //cpu_weight
1290-
long utilization = 0; // max CPU cap, default is unlimited
1330+
int cpuWeight = _maxWeight; // cpu_weight
1331+
int utilization = 0; // max CPU cap, default is unlimited
12911332

1292-
// weight based allocation
1293-
cpuWeight = (int)((speed*0.99) / _host.speed * _maxWeight);
1333+
// weight based allocation, CPU weight is calculated per VCPU
1334+
cpuWeight = (int) ((speed * 0.99) / _host.speed * _maxWeight);
12941335
if (cpuWeight > _maxWeight) {
12951336
cpuWeight = _maxWeight;
12961337
}
12971338

12981339
if (vmSpec.getLimitCpuUse()) {
1299-
utilization = ((long)speed * 100 * vmSpec.getCpus()) / _host.speed ;
1340+
// CPU cap is per VM, so need to assign cap based on the number of vcpus
1341+
utilization = (int) ((speed * 0.99 * vmSpec.getCpus()) / _host.speed * 100);
13001342
}
13011343

13021344
vcpuParams.put("weight", Integer.toString(cpuWeight));
1303-
vcpuParams.put("cap", Long.toString(utilization));
1345+
vcpuParams.put("cap", Integer.toString(utilization));
1346+
13041347
}
13051348

13061349
if (vcpuParams.size() > 0) {
13071350
vm.setVCPUsParams(conn, vcpuParams);
13081351
}
13091352

1310-
vm.setActionsAfterCrash(conn, Types.OnCrashBehaviour.DESTROY);
1311-
vm.setActionsAfterShutdown(conn, Types.OnNormalExit.DESTROY);
1312-
13131353
String bootArgs = vmSpec.getBootArgs();
13141354
if (bootArgs != null && bootArgs.length() > 0) {
13151355
String pvargs = vm.getPVArgs(conn);
@@ -1322,43 +1362,40 @@ protected VM createVmFromTemplate(Connection conn, VirtualMachineTO vmSpec, Host
13221362

13231363
if (!(guestOsTypeName.startsWith("Windows") || guestOsTypeName.startsWith("Citrix") || guestOsTypeName.startsWith("Other"))) {
13241364
if (vmSpec.getBootloader() == BootloaderType.CD) {
1325-
DiskTO [] disks = vmSpec.getDisks();
1365+
DiskTO[] disks = vmSpec.getDisks();
13261366
for (DiskTO disk : disks) {
1327-
Volume.Type type = disk.getType();
1328-
if (type == Volume.Type.ISO) {
1329-
TemplateObjectTO tmpl = (TemplateObjectTO)disk.getData();
1330-
String osType = tmpl.getGuestOsType();
1331-
if (tmpl.getFormat() == ImageFormat.ISO && osType != null ) {
1332-
String isoGuestOsName = getGuestOsType(osType, vmSpec.getBootloader() == BootloaderType.CD);
1333-
if (!isoGuestOsName.equals(guestOsTypeName)) {
1334-
vmSpec.setBootloader(BootloaderType.PyGrub);
1335-
}
1336-
}
1367+
if (disk.getType() == Volume.Type.ISO ) {
1368+
TemplateObjectTO iso = (TemplateObjectTO)disk.getData();
1369+
String osType = iso.getGuestOsType();
1370+
if (osType != null) {
1371+
String isoGuestOsName = getGuestOsType(osType, vmSpec.getBootloader() == BootloaderType.CD);
1372+
if (!isoGuestOsName.equals(guestOsTypeName)) {
1373+
vmSpec.setBootloader(BootloaderType.PyGrub);
1374+
}
1375+
}
13371376
}
13381377
}
13391378
}
13401379
if (vmSpec.getBootloader() == BootloaderType.CD) {
13411380
vm.setPVBootloader(conn, "eliloader");
1342-
Map<String, String> otherConfig = vm.getOtherConfig(conn);
1343-
if ( ! vm.getOtherConfig(conn).containsKey("install-repository") ) {
1344-
otherConfig.put( "install-repository", "cdrom");
1381+
if (!vm.getOtherConfig(conn).containsKey("install-repository")) {
1382+
vm.addToOtherConfig(conn, "install-repository", "cdrom");
13451383
}
1346-
vm.setOtherConfig(conn, otherConfig);
1347-
} else if (vmSpec.getBootloader() == BootloaderType.PyGrub ){
1384+
} else if (vmSpec.getBootloader() == BootloaderType.PyGrub) {
13481385
vm.setPVBootloader(conn, "pygrub");
13491386
} else {
13501387
vm.destroy(conn);
13511388
throw new CloudRuntimeException("Unable to handle boot loader type: " + vmSpec.getBootloader());
13521389
}
13531390
}
1354-
13551391
try {
13561392
finalizeVmMetaData(vm, conn, vmSpec);
13571393
} catch ( Exception e) {
13581394
throw new CloudRuntimeException("Unable to finalize VM MetaData: " + vmSpec);
13591395
}
13601396
return vm;
13611397
}
1398+
13621399

13631400
protected void finalizeVmMetaData(VM vm, Connection conn, VirtualMachineTO vmSpec) throws Exception {
13641401
}

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServer56FP1Resource.java‎

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -152,139 +152,6 @@ public long getStaticMin(String os, boolean b, long dynamicMinRam, long dynamicM
152152
return dynamicMinRam;
153153
}
154154

155-
@Override
156-
protected VM createVmFromTemplate(Connection conn, VirtualMachineTO vmSpec, Host host) throws XenAPIException, XmlRpcException {
157-
String guestOsTypeName = getGuestOsType(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD);
158-
Set<VM> templates = VM.getByNameLabel(conn, guestOsTypeName);
159-
assert templates.size() == 1 : "Should only have 1 template but found " + templates.size();
160-
VM template = templates.iterator().next();
161-
162-
VM.Record vmr = template.getRecord(conn);
163-
vmr.affinity = host;
164-
vmr.otherConfig.remove("disks");
165-
vmr.otherConfig.remove("default_template");
166-
vmr.otherConfig.remove("mac_seed");
167-
vmr.isATemplate = false;
168-
vmr.nameLabel = vmSpec.getName();
169-
vmr.actionsAfterCrash = Types.OnCrashBehaviour.DESTROY;
170-
vmr.actionsAfterShutdown = Types.OnNormalExit.DESTROY;
171-
172-
if (isDmcEnabled(conn, host) && vmSpec.isEnableDynamicallyScaleVm()) {
173-
//scaling is allowed
174-
vmr.memoryStaticMin = getStaticMin(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD, vmSpec.getMinRam(), vmSpec.getMaxRam());
175-
vmr.memoryStaticMax = getStaticMax(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD, vmSpec.getMinRam(), vmSpec.getMaxRam());
176-
vmr.memoryDynamicMin = vmSpec.getMinRam();
177-
vmr.memoryDynamicMax = vmSpec.getMaxRam();
178-
} else {
179-
//scaling disallowed, set static memory target
180-
if (vmSpec.isEnableDynamicallyScaleVm() && !isDmcEnabled(conn, host)) {
181-
s_logger.warn("Host "+ host.getHostname(conn) +" does not support dynamic scaling, so the vm " + vmSpec.getName() + " is not dynamically scalable");
182-
}
183-
vmr.memoryStaticMin = vmSpec.getMinRam();
184-
vmr.memoryStaticMax = vmSpec.getMaxRam();
185-
vmr.memoryDynamicMin = vmSpec.getMinRam();
186-
vmr.memoryDynamicMax = vmSpec.getMaxRam();
187-
}
188-
189-
if (guestOsTypeName.toLowerCase().contains("windows")) {
190-
vmr.VCPUsMax = (long) vmSpec.getCpus();
191-
} else {
192-
vmr.VCPUsMax = 32L;
193-
}
194-
195-
Map<String, String> details = vmSpec.getDetails();
196-
if ( details != null ) {
197-
String timeoffset = details.get("timeoffset");
198-
if (timeoffset != null) {
199-
Map<String, String> platform = vmr.platform;
200-
platform.put("timeoffset", timeoffset);
201-
vmr.platform = platform;
202-
}
203-
204-
String coresPerSocket = details.get("cpu.corespersocket");
205-
if (coresPerSocket != null) {
206-
Map<String, String> platform = vmr.platform;
207-
platform.put("cores-per-socket", coresPerSocket);
208-
vmr.platform = platform;
209-
}
210-
}
211-
212-
vmr.VCPUsAtStartup = (long) vmSpec.getCpus();
213-
vmr.consoles.clear();
214-
215-
VM vm = VM.create(conn, vmr);
216-
if (s_logger.isDebugEnabled()) {
217-
s_logger.debug("Created VM " + vm.getUuid(conn) + " for " + vmSpec.getName());
218-
}
219-
220-
Map<String, String> vcpuParams = new HashMap<String, String>();
221-
222-
Integer speed = vmSpec.getMinSpeed();
223-
if (speed != null) {
224-
225-
int cpuWeight = _maxWeight; // cpu_weight
226-
int utilization = 0; // max CPU cap, default is unlimited
227-
228-
// weight based allocation, CPU weight is calculated per VCPU
229-
cpuWeight = (int) ((speed * 0.99) / _host.speed * _maxWeight);
230-
if (cpuWeight > _maxWeight) {
231-
cpuWeight = _maxWeight;
232-
}
233-
234-
if (vmSpec.getLimitCpuUse()) {
235-
// CPU cap is per VM, so need to assign cap based on the number of vcpus
236-
utilization = (int) ((speed * 0.99 * vmSpec.getCpus()) / _host.speed * 100);
237-
}
238-
239-
vcpuParams.put("weight", Integer.toString(cpuWeight));
240-
vcpuParams.put("cap", Integer.toString(utilization));
241-
242-
}
243-
244-
if (vcpuParams.size() > 0) {
245-
vm.setVCPUsParams(conn, vcpuParams);
246-
}
247-
248-
String bootArgs = vmSpec.getBootArgs();
249-
if (bootArgs != null && bootArgs.length() > 0) {
250-
String pvargs = vm.getPVArgs(conn);
251-
pvargs = pvargs + vmSpec.getBootArgs().replaceAll(" ", "%");
252-
if (s_logger.isDebugEnabled()) {
253-
s_logger.debug("PV args are " + pvargs);
254-
}
255-
vm.setPVArgs(conn, pvargs);
256-
}
257-
258-
if (!(guestOsTypeName.startsWith("Windows") || guestOsTypeName.startsWith("Citrix") || guestOsTypeName.startsWith("Other"))) {
259-
if (vmSpec.getBootloader() == BootloaderType.CD) {
260-
DiskTO[] disks = vmSpec.getDisks();
261-
for (DiskTO disk : disks) {
262-
if (disk.getType() == Volume.Type.ISO ) {
263-
TemplateObjectTO iso = (TemplateObjectTO)disk.getData();
264-
String osType = iso.getGuestOsType();
265-
if (osType != null) {
266-
String isoGuestOsName = getGuestOsType(osType, vmSpec.getBootloader() == BootloaderType.CD);
267-
if (!isoGuestOsName.equals(guestOsTypeName)) {
268-
vmSpec.setBootloader(BootloaderType.PyGrub);
269-
}
270-
}
271-
}
272-
}
273-
}
274-
if (vmSpec.getBootloader() == BootloaderType.CD) {
275-
vm.setPVBootloader(conn, "eliloader");
276-
if (!vm.getOtherConfig(conn).containsKey("install-repository")) {
277-
vm.addToOtherConfig(conn, "install-repository", "cdrom");
278-
}
279-
} else if (vmSpec.getBootloader() == BootloaderType.PyGrub) {
280-
vm.setPVBootloader(conn, "pygrub");
281-
} else {
282-
vm.destroy(conn);
283-
throw new CloudRuntimeException("Unable to handle boot loader type: " + vmSpec.getBootloader());
284-
}
285-
}
286-
return vm;
287-
}
288155

289156
/**
290157
* When Dynamic Memory Control (DMC) is enabled -

0 commit comments

Comments
 (0)