use of com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ConsoleDef in project cloudstack by apache.
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 GuestDef guest = new GuestDef();
if (HypervisorType.LXC == _hypervisorType && VirtualMachine.Type.User == vmTO.getType()) {
// LXC domain is only valid for user VMs. Use KVM for system VMs.
guest.setGuestType(GuestDef.GuestType.LXC);
vm.setHvsType(HypervisorType.LXC.toString().toLowerCase());
} else {
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.setBootOrder(GuestDef.BootOrder.CDROM);
guest.setBootOrder(GuestDef.BootOrder.HARDISK);
vm.addComp(guest);
final GuestResourceDef grd = new GuestResourceDef();
if (vmTO.getMinRam() != vmTO.getMaxRam() && !_noMemBalloon) {
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.addComp(grd);
final CpuModeDef cmd = new CpuModeDef();
cmd.setMode(_guestCpuMode);
cmd.setModel(_guestCpuModel);
if (vmTO.getType() == VirtualMachine.Type.User) {
cmd.setFeatures(_cpuFeatures);
}
// 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.addComp(cmd);
if (_hypervisorLibvirtVersion >= 9000) {
final CpuTuneDef ctd = new CpuTuneDef();
/**
A 4.0.X/4.1.X management server doesn't send the correct JSON
command for getMinSpeed, it only sends a 'speed' field.
So if getMinSpeed() returns null we fall back to getSpeed().
This way a >4.1 agent can work communicate a <=4.1 management server
This change is due to the overcommit feature in 4.2
*/
if (vmTO.getMinSpeed() != null) {
ctd.setShares(vmTO.getCpus() * vmTO.getMinSpeed());
} else {
ctd.setShares(vmTO.getCpus() * vmTO.getSpeed());
}
vm.addComp(ctd);
}
final FeaturesDef features = new FeaturesDef();
features.addFeatures("pae");
features.addFeatures("apic");
features.addFeatures("acpi");
//for rhel 6.5 and above, hyperv enlightment feature is added
/*
* if (vmTO.getOs().contains("Windows Server 2008") && hostOsVersion != null && ((hostOsVersion.first() == 6 && hostOsVersion.second() >= 5) || (hostOsVersion.first() >= 7))) {
* LibvirtVMDef.HyperVEnlightenmentFeatureDef hyv = new LibvirtVMDef.HyperVEnlightenmentFeatureDef();
* hyv.setRelaxed(true);
* features.addHyperVFeature(hyv);
* }
*/
vm.addComp(features);
final TermPolicy term = new TermPolicy();
term.setCrashPolicy("destroy");
term.setPowerOffPolicy("destroy");
term.setRebootPolicy("restart");
vm.addComp(term);
final ClockDef clock = new ClockDef();
if (vmTO.getOs().startsWith("Windows")) {
clock.setClockOffset(ClockDef.ClockOffset.LOCALTIME);
clock.setTimer("rtc", "catchup", null);
} else if (vmTO.getType() != VirtualMachine.Type.User || isGuestPVEnabled(vmTO.getOs())) {
if (_hypervisorLibvirtVersion >= 9 * 1000 + 10) {
clock.setTimer("kvmclock", null, null, _noKvmClock);
}
}
vm.addComp(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);
/* Add a VirtIO channel for SystemVMs for communication and provisioning */
if (vmTO.getType() != VirtualMachine.Type.User) {
devices.addDevice(new ChannelDef(vmTO.getName() + ".vport", ChannelDef.ChannelType.UNIX, new File(_qemuSocketsPath + "/" + vmTO.getName() + ".agent")));
}
if (_rngEnable) {
final RngDef rngDevice = new RngDef(_rngPath, _rngBackendModel, _rngRateBytes, _rngRatePeriod);
devices.addDevice(rngDevice);
}
/* Add a VirtIO channel for the Qemu Guest Agent tools */
devices.addDevice(new ChannelDef(_qemuGuestAgentSocketName, ChannelDef.ChannelType.UNIX, new File(_qemuSocketsPath + "/" + vmTO.getName() + "." + _qemuGuestAgentSocketName)));
final VideoDef videoCard = new VideoDef(_videoHw, _videoRam);
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, vmTO.getVncAddr(), passwd, null);
devices.addDevice(grap);
final InputDef input = new InputDef("tablet", "usb");
devices.addDevice(input);
DiskDef.DiskBus busT = getDiskModelFromVMDetail(vmTO);
if (busT == null) {
busT = getGuestDiskModel(vmTO.getPlatformEmulator());
}
// If we're using virtio scsi, then we need to add a virtual scsi controller
if (busT == DiskDef.DiskBus.SCSI) {
final SCSIDef sd = new SCSIDef((short) 0, 0, 0, 9, 0);
devices.addDevice(sd);
}
vm.addComp(devices);
return vm;
}
Aggregations