Search in sources :

Example 1 with SerialDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVmDef.SerialDef 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;
}
Also used : TermPolicy(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.TermPolicy) 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) GraphicDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.GraphicDef) VideoDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.VideoDef) RngDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.RngDef) DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) SCSIDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.SCSIDef) SerialDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.SerialDef) ConsoleDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ConsoleDef) InputDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InputDef) CpuModeDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.CpuModeDef) FeaturesDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.FeaturesDef) ChannelDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ChannelDef) GuestDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.GuestDef) File(java.io.File)

Aggregations

ChannelDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ChannelDef)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 DiskDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef)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 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 File (java.io.File)1