use of com.cloud.hypervisor.kvm.resource.LibvirtVmDef.QemuGuestAgentDef in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method createVmFromSpec.
public LibvirtVmDef createVmFromSpec(final VirtualMachineTO vmTo) {
final LibvirtVmDef vm = new LibvirtVmDef();
vm.setDomainName(vmTo.getName());
String uuid = vmTo.getUuid();
uuid = getUuid(uuid);
vm.setDomUuid(uuid);
vm.setDomDescription(vmTo.getOs());
vm.setPlatformEmulator(vmTo.getPlatformEmulator());
final MetadataTO metadataTo = vmTo.getMetadata();
if (metadataTo != null) {
final MetadataDef metadata = new MetadataDef();
metadata.getNodes().put("domainUuid", metadataTo.getDomainUuid());
vm.addComponent(metadata);
}
final GuestDef guest = new GuestDef();
guest.setGuestType(GuestDef.GuestType.KVM);
vm.setHvsType(HypervisorType.KVM.toString().toLowerCase());
vm.setLibvirtVersion(hypervisorLibvirtVersion);
vm.setQemuVersion(hypervisorQemuVersion);
guest.setGuestArch(vmTo.getArch());
guest.setMachineType("pc");
guest.setUuid(uuid);
guest.setManufacturer(vmTo.getManufacturer());
guest.setBootOrder(GuestDef.BootOrder.CDROM);
guest.setBootOrder(GuestDef.BootOrder.HARDISK);
vm.addComponent(guest);
final GuestResourceDef grd = new GuestResourceDef();
if (vmTo.getMinRam() != vmTo.getMaxRam() && !libvirtComputingResourceProperties.getVmMemballoonDisable()) {
grd.setMemBalloning(true);
grd.setCurrentMem(vmTo.getMinRam() / 1024);
grd.setMemorySize(vmTo.getMaxRam() / 1024);
} else {
grd.setMemorySize(vmTo.getMaxRam() / 1024);
}
final int vcpus = vmTo.getCpus();
grd.setVcpuNum(vcpus);
vm.addComponent(grd);
final CpuModeDef cmd = new CpuModeDef();
cmd.setMode(getGuestCpuMode());
cmd.setModel(getGuestCpuModel());
cmd.setCpuflags(vmTo.getCpuflags());
if (vmTo.getType() == VirtualMachine.Type.User) {
cmd.setFeatures(getCpuFeatures());
}
// multi cores per socket, for larger core configs
if (vcpus % 6 == 0) {
final int sockets = vcpus / 6;
cmd.setTopology(6, sockets);
} else if (vcpus % 4 == 0) {
final int sockets = vcpus / 4;
cmd.setTopology(4, sockets);
}
vm.addComponent(cmd);
final CpuTuneDef ctd = new CpuTuneDef();
if (VirtualMachine.Type.DomainRouter.equals(vmTo.getType())) {
ctd.setShares(vmTo.getCpus() * libvirtComputingResourceProperties.getGuestCpuSharesRouter());
} else {
ctd.setShares(vmTo.getCpus() * libvirtComputingResourceProperties.getGuestCpuShares());
}
vm.addComponent(ctd);
final FeaturesDef features = new FeaturesDef();
features.addFeatures("pae");
features.addFeatures("apic");
features.addFeatures("acpi");
vm.addComponent(features);
final TermPolicy term = new TermPolicy();
if (VirtualMachine.Type.DomainRouter.equals(vmTo.getType())) {
term.setCrashPolicy(getRouterTermpolicyCrash());
term.setPowerOffPolicy(getRouterTermpolicyPowerOff());
term.setRebootPolicy(getRouterTermpolicyReboot());
} else {
term.setCrashPolicy(getVmTermpolicyCrash());
term.setPowerOffPolicy(getVmTermpolicyPowerOff());
term.setRebootPolicy(getVmTermpolicyReboot());
}
vm.addComponent(term);
final ClockDef clock = new ClockDef();
if (vmTo.getOs().startsWith("Windows")) {
clock.setClockOffset(ClockDef.ClockOffset.LOCALTIME);
} else if (vmTo.getType() != VirtualMachine.Type.User || isGuestVirtIoCapable(vmTo.getOs())) {
if (hypervisorLibvirtVersion >= 9 * 1000 + 10) {
clock.addTimer("kvmclock", null, null, isKvmclockDisabled());
}
}
// Recommended default clock/timer settings - https://bugzilla.redhat.com/show_bug.cgi?id=1053847
clock.addTimer("rtc", "catchup", null);
clock.addTimer("pit", "delay", null);
vm.addComponent(clock);
final DevicesDef devices = new DevicesDef();
devices.setEmulatorPath(hypervisorPath);
devices.setGuestType(guest.getGuestType());
final SerialDef serial = new SerialDef("pty", null, (short) 0);
devices.addDevice(serial);
final QemuGuestAgentDef guestagent = new QemuGuestAgentDef();
devices.addDevice(guestagent);
if (libvirtComputingResourceProperties.getVmRngEnable()) {
final RngDef rngDevice = new RngDef(rngPath, rngBackendModel);
devices.addDevice(rngDevice);
}
final WatchDogDef watchDog = new WatchDogDef(libvirtComputingResourceProperties.getVmWatchdogAction(), libvirtComputingResourceProperties.getVmWatchdogModel());
devices.addDevice(watchDog);
final VideoDef videoCard = new VideoDef(libvirtComputingResourceProperties.getVmVideoHardware(), libvirtComputingResourceProperties.getVmVideoRam());
devices.addDevice(videoCard);
final ConsoleDef console = new ConsoleDef("pty", null, null, (short) 0);
devices.addDevice(console);
// add the VNC port passwd here, get the passwd from the vmInstance.
final String passwd = vmTo.getVncPassword();
final GraphicDef grap = new GraphicDef("vnc", (short) 0, true, passwd, null);
devices.addDevice(grap);
final InputDef input = new InputDef("tablet", "usb");
devices.addDevice(input);
LibvirtDiskDef.DiskBus diskBusType = getDiskModelFromVmDetail(vmTo);
if (diskBusType == null) {
diskBusType = getGuestDiskModel(vmTo.getPlatformEmulator());
}
// If we're using virtio scsi, then we need to add a virtual scsi controller
if (diskBusType == LibvirtDiskDef.DiskBus.SCSI) {
vmTo.getName();
final ScsiDef sd = new ScsiDef((short) 0, 0, 0, 9, 0);
devices.addDevice(sd);
logger.debug("Adding SCSI definition for " + vmTo.getName() + ":\n" + sd.toString());
}
vm.addComponent(devices);
return vm;
}
Aggregations