Search in sources :

Example 1 with MetadataDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVmDef.MetadataDef 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;
}
Also used : ScsiDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ScsiDef) TermPolicy(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.TermPolicy) MetadataTO(com.cloud.agent.api.to.MetadataTO) CpuTuneDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.CpuTuneDef) DevicesDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.DevicesDef) GuestResourceDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.GuestResourceDef) ClockDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ClockDef) MetadataDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.MetadataDef) GraphicDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.GraphicDef) VideoDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.VideoDef) RngDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.RngDef) SerialDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.SerialDef) ConsoleDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ConsoleDef) InputDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InputDef) LibvirtDiskDef(com.cloud.hypervisor.kvm.resource.xml.LibvirtDiskDef) CpuModeDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.CpuModeDef) FeaturesDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.FeaturesDef) WatchDogDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.WatchDogDef) GuestDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.GuestDef) QemuGuestAgentDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef.QemuGuestAgentDef)

Aggregations

MetadataTO (com.cloud.agent.api.to.MetadataTO)1 ClockDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ClockDef)1 ConsoleDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ConsoleDef)1 CpuModeDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.CpuModeDef)1 CpuTuneDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.CpuTuneDef)1 DevicesDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.DevicesDef)1 FeaturesDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.FeaturesDef)1 GraphicDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.GraphicDef)1 GuestDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.GuestDef)1 GuestResourceDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.GuestResourceDef)1 InputDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InputDef)1 MetadataDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.MetadataDef)1 QemuGuestAgentDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.QemuGuestAgentDef)1 RngDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.RngDef)1 ScsiDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.ScsiDef)1 SerialDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.SerialDef)1 TermPolicy (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.TermPolicy)1 VideoDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.VideoDef)1 WatchDogDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.WatchDogDef)1 LibvirtDiskDef (com.cloud.hypervisor.kvm.resource.xml.LibvirtDiskDef)1