assert templates.size() == 1 : "Should only have 1 template but found " + templates.size();
if (!templates.iterator().hasNext()) {
throw new CloudRuntimeException("No matching OS type found for starting a [" + vmSpec.getOs()
+ "] VM on host " + host.getHostname(conn));
}
VM template = templates.iterator().next();
VM vm = template.createClone(conn, vmSpec.getName());
VM.Record vmr = vm.getRecord(conn);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Created VM " + vmr.uuid + " for " + vmSpec.getName());
}
for (Console console : vmr.consoles) {
console.destroy(conn);
}
vm.setIsATemplate(conn, false);
vm.setAffinity(conn, host);
vm.removeFromOtherConfig(conn, "disks");
vm.setNameLabel(conn, vmSpec.getName());
setMemory(conn, vm, vmSpec.getMinRam(),vmSpec.getMaxRam());
vm.setVCPUsMax(conn, (long)vmSpec.getCpus());
vm.setVCPUsAtStartup(conn, (long)vmSpec.getCpus());
Map<String, String> vcpuParams = new HashMap<String, String>();
Integer speed = vmSpec.getMinSpeed();
if (speed != null) {
int cpuWeight = _maxWeight; //cpu_weight
long utilization = 0; // max CPU cap, default is unlimited
// weight based allocation
cpuWeight = (int)((speed*0.99) / _host.speed * _maxWeight);
if (cpuWeight > _maxWeight) {
cpuWeight = _maxWeight;
}
if (vmSpec.getLimitCpuUse()) {
utilization = ((long)speed * 100 * vmSpec.getCpus()) / _host.speed ;
}
vcpuParams.put("weight", Integer.toString(cpuWeight));
vcpuParams.put("cap", Long.toString(utilization));
}
if (vcpuParams.size() > 0) {
vm.setVCPUsParams(conn, vcpuParams);
}
vm.setActionsAfterCrash(conn, Types.OnCrashBehaviour.DESTROY);
vm.setActionsAfterShutdown(conn, Types.OnNormalExit.DESTROY);
String bootArgs = vmSpec.getBootArgs();
if (bootArgs != null && bootArgs.length() > 0) {
String pvargs = vm.getPVArgs(conn);
pvargs = pvargs + vmSpec.getBootArgs().replaceAll(" ", "%");
if (s_logger.isDebugEnabled()) {
s_logger.debug("PV args are " + pvargs);
}
vm.setPVArgs(conn, pvargs);
}
if (!(guestOsTypeName.startsWith("Windows") || guestOsTypeName.startsWith("Citrix") || guestOsTypeName.startsWith("Other"))) {
if (vmSpec.getBootloader() == BootloaderType.CD) {
DiskTO [] disks = vmSpec.getDisks();
for (DiskTO disk : disks) {
Volume.Type type = disk.getType();
if (type == Volume.Type.ISO) {
TemplateObjectTO tmpl = (TemplateObjectTO)disk.getData();
String osType = tmpl.getGuestOsType();
if (tmpl.getFormat() == ImageFormat.ISO && osType != null ) {
String isoGuestOsName = getGuestOsType(osType, vmSpec.getBootloader() == BootloaderType.CD);
if (!isoGuestOsName.equals(guestOsTypeName)) {
vmSpec.setBootloader(BootloaderType.PyGrub);
}
}
}
}
}
if (vmSpec.getBootloader() == BootloaderType.CD) {
vm.setPVBootloader(conn, "eliloader");
Map<String, String> otherConfig = vm.getOtherConfig(conn);
if ( ! vm.getOtherConfig(conn).containsKey("install-repository") ) {
otherConfig.put( "install-repository", "cdrom");
}
vm.setOtherConfig(conn, otherConfig);
} else if (vmSpec.getBootloader() == BootloaderType.PyGrub ){
vm.setPVBootloader(conn, "pygrub");
} else {
vm.destroy(conn);
throw new CloudRuntimeException("Unable to handle boot loader type: " + vmSpec.getBootloader());
}
}
return vm;
}