Search in sources :

Example 91 with VirtualMachineTO

use of com.cloud.agent.api.to.VirtualMachineTO in project cosmic by MissionCriticalCloud.

the class KVMGuru method implement.

@Override
public VirtualMachineTO implement(final VirtualMachineProfile vm) {
    final VirtualMachineTO to = toVirtualMachineTO(vm);
    // Determine the VM's OS description
    final GuestOSVO guestOS = _guestOsDao.findByIdIncludingRemoved(vm.getVirtualMachine().getGuestOSId());
    to.setOs(guestOS.getDisplayName());
    to.setCpuflags(guestOS.getCpuflags());
    to.setManufacturer(guestOS.getManufacturer());
    final HostVO host = _hostDao.findById(vm.getVirtualMachine().getHostId());
    GuestOSHypervisorVO guestOsMapping = null;
    if (host != null) {
        guestOsMapping = _guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), getHypervisorType().toString(), host.getHypervisorVersion());
    }
    if (guestOsMapping == null) {
        to.setPlatformEmulator("Default - VirtIO capable OS (64-bit)");
    } else {
        to.setPlatformEmulator(guestOsMapping.getGuestOsName());
    }
    return to;
}
Also used : GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) GuestOSVO(com.cloud.storage.GuestOSVO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) HostVO(com.cloud.host.HostVO)

Example 92 with VirtualMachineTO

use of com.cloud.agent.api.to.VirtualMachineTO in project CloudStack-archive by CloudStack-extras.

the class CloudZonesComputingResource method execute.

@Override
protected synchronized StartAnswer execute(StartCommand cmd) {
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    String vmName = vmSpec.getName();
    LibvirtVMDef vm = null;
    State state = State.Stopped;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
        synchronized (_vms) {
            _vms.put(vmName, State.Starting);
        }
        vm = createVMFromSpec(vmSpec);
        createVbd(conn, vmSpec, vmName, vm);
        createVifs(conn, vmSpec, vm);
        s_logger.debug("starting " + vmName + ": " + vm.toString());
        startDomain(conn, vmName, vm.toString());
        NicTO[] nics = vmSpec.getNics();
        for (NicTO nic : nics) {
            if (nic.isSecurityGroupEnabled()) {
                if (vmSpec.getType() != VirtualMachine.Type.User) {
                    default_network_rules_for_systemvm(conn, vmName);
                } else {
                    nic.setIp(null);
                    default_network_rules(conn, vmName, nic, vmSpec.getId());
                }
            }
        }
        // attached disk
        for (DiskDef disk : vm.getDevices().getDisks()) {
            if (disk.isAttachDeferred()) {
                attachOrDetachDevice(conn, true, vmName, disk.toString());
            }
        }
        if (vmSpec.getType() == VirtualMachine.Type.User) {
            for (NicTO nic : nics) {
                if (nic.getType() == TrafficType.Guest) {
                    InetAddress ipAddr = _dhcpSnooper.getIPAddr(nic.getMac(), vmName);
                    if (ipAddr == null) {
                        s_logger.debug("Failed to get guest DHCP ip, stop it");
                        StopCommand stpCmd = new StopCommand(vmName);
                        execute(stpCmd);
                        return new StartAnswer(cmd, "Failed to get guest DHCP ip, stop it");
                    }
                    s_logger.debug(ipAddr);
                    nic.setIp(ipAddr.getHostAddress());
                    post_default_network_rules(conn, vmName, nic, vmSpec.getId(), _dhcpSnooper.getDhcpServerIP(), _hostIp, _hostMacAddress);
                    _vmDataServer.handleVmStarted(cmd.getVirtualMachine());
                }
            }
        }
        state = State.Running;
        return new StartAnswer(cmd);
    } catch (Exception e) {
        s_logger.warn("Exception ", e);
        if (conn != null) {
            handleVmStartFailure(conn, vmName, vm);
        }
        return new StartAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != State.Stopped) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : LibvirtVMDef(com.cloud.agent.resource.computing.LibvirtVMDef) DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) StopCommand(com.cloud.agent.api.StopCommand) StartAnswer(com.cloud.agent.api.StartAnswer) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) InetAddress(java.net.InetAddress) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) ConfigurationException(javax.naming.ConfigurationException) LibvirtException(org.libvirt.LibvirtException) NicTO(com.cloud.agent.api.to.NicTO)

Example 93 with VirtualMachineTO

use of com.cloud.agent.api.to.VirtualMachineTO in project CloudStack-archive by CloudStack-extras.

the class MockVmManagerImpl method startVM.

@Override
public Answer startVM(StartCommand cmd, SimulatorInfo info) {
    VirtualMachineTO vm = cmd.getVirtualMachine();
    String result = startVM(vm.getName(), vm.getNics(), vm.getCpus() * vm.getSpeed(), vm.getMaxRam(), vm.getBootArgs(), info.getHostUuid());
    if (result != null) {
        return new StartAnswer(cmd, result);
    } else {
        return new StartAnswer(cmd);
    }
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO)

Example 94 with VirtualMachineTO

use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.

the class VMwareGuru method implement.

@Override
public VirtualMachineTO implement(VirtualMachineProfile vm) {
    VirtualMachineTO to = toVirtualMachineTO(vm);
    to.setBootloader(BootloaderType.HVM);
    Map<String, String> details = to.getDetails();
    if (details == null)
        details = new HashMap<String, String>();
    Type vmType = vm.getType();
    boolean userVm = !(vmType.equals(VirtualMachine.Type.DomainRouter) || vmType.equals(VirtualMachine.Type.ConsoleProxy) || vmType.equals(VirtualMachine.Type.SecondaryStorageVm));
    String nicDeviceType = details.get(VmDetailConstants.NIC_ADAPTER);
    if (!userVm) {
        if (nicDeviceType == null) {
            details.put(VmDetailConstants.NIC_ADAPTER, _vmwareMgr.getSystemVMDefaultNicAdapterType());
        } else {
            try {
                VirtualEthernetCardType.valueOf(nicDeviceType);
            } catch (Exception e) {
                s_logger.warn("Invalid NIC device type " + nicDeviceType + " is specified in VM details, switch to default E1000");
                details.put(VmDetailConstants.NIC_ADAPTER, VirtualEthernetCardType.E1000.toString());
            }
        }
    } else {
        // for user-VM, use E1000 as default
        if (nicDeviceType == null) {
            details.put(VmDetailConstants.NIC_ADAPTER, VirtualEthernetCardType.E1000.toString());
        } else {
            try {
                VirtualEthernetCardType.valueOf(nicDeviceType);
            } catch (Exception e) {
                s_logger.warn("Invalid NIC device type " + nicDeviceType + " is specified in VM details, switch to default E1000");
                details.put(VmDetailConstants.NIC_ADAPTER, VirtualEthernetCardType.E1000.toString());
            }
        }
    }
    String diskDeviceType = details.get(VmDetailConstants.ROOT_DISK_CONTROLLER);
    if (userVm) {
        if (diskDeviceType == null) {
            details.put(VmDetailConstants.ROOT_DISK_CONTROLLER, _vmwareMgr.getRootDiskController());
        }
    }
    String diskController = details.get(VmDetailConstants.DATA_DISK_CONTROLLER);
    if (userVm) {
        if (diskController == null) {
            details.put(VmDetailConstants.DATA_DISK_CONTROLLER, DiskControllerType.lsilogic.toString());
        }
    }
    List<NicProfile> nicProfiles = vm.getNics();
    for (NicProfile nicProfile : nicProfiles) {
        if (nicProfile.getTrafficType() == TrafficType.Guest) {
            if (_networkMgr.isProviderSupportServiceInNetwork(nicProfile.getNetworkId(), Service.Firewall, Provider.CiscoVnmc)) {
                details.put("ConfigureVServiceInNexus", Boolean.TRUE.toString());
            }
            break;
        }
    }
    long clusterId = getClusterId(vm.getId());
    details.put(VmwareReserveCpu.key(), VmwareReserveCpu.valueIn(clusterId).toString());
    details.put(VmwareReserveMemory.key(), VmwareReserveMemory.valueIn(clusterId).toString());
    to.setDetails(details);
    if (vmType.equals(VirtualMachine.Type.DomainRouter)) {
        NicProfile publicNicProfile = null;
        for (NicProfile nicProfile : nicProfiles) {
            if (nicProfile.getTrafficType() == TrafficType.Public) {
                publicNicProfile = nicProfile;
                break;
            }
        }
        if (publicNicProfile != null) {
            NicTO[] nics = to.getNics();
            // reserve extra NICs
            NicTO[] expandedNics = new NicTO[nics.length + _vmwareMgr.getRouterExtraPublicNics()];
            int i = 0;
            int deviceId = -1;
            for (i = 0; i < nics.length; i++) {
                expandedNics[i] = nics[i];
                if (nics[i].getDeviceId() > deviceId)
                    deviceId = nics[i].getDeviceId();
            }
            deviceId++;
            long networkId = publicNicProfile.getNetworkId();
            NetworkVO network = _networkDao.findById(networkId);
            for (; i < nics.length + _vmwareMgr.getRouterExtraPublicNics(); i++) {
                NicTO nicTo = new NicTO();
                nicTo.setDeviceId(deviceId++);
                nicTo.setBroadcastType(publicNicProfile.getBroadcastType());
                nicTo.setType(publicNicProfile.getTrafficType());
                nicTo.setIp("0.0.0.0");
                nicTo.setNetmask("255.255.255.255");
                try {
                    String mac = _networkMgr.getNextAvailableMacAddressInNetwork(networkId);
                    nicTo.setMac(mac);
                } catch (InsufficientAddressCapacityException e) {
                    throw new CloudRuntimeException("unable to allocate mac address on network: " + networkId);
                }
                nicTo.setDns1(publicNicProfile.getIPv4Dns1());
                nicTo.setDns2(publicNicProfile.getIPv4Dns2());
                if (publicNicProfile.getIPv4Gateway() != null) {
                    nicTo.setGateway(publicNicProfile.getIPv4Gateway());
                } else {
                    nicTo.setGateway(network.getGateway());
                }
                nicTo.setDefaultNic(false);
                nicTo.setBroadcastUri(publicNicProfile.getBroadCastUri());
                nicTo.setIsolationuri(publicNicProfile.getIsolationUri());
                Integer networkRate = _networkMgr.getNetworkRate(network.getId(), null);
                nicTo.setNetworkRateMbps(networkRate);
                expandedNics[i] = nicTo;
            }
            to.setNics(expandedNics);
        }
        StringBuffer sbMacSequence = new StringBuffer();
        for (NicTO nicTo : sortNicsByDeviceId(to.getNics())) {
            sbMacSequence.append(nicTo.getMac()).append("|");
        }
        if (!sbMacSequence.toString().isEmpty()) {
            sbMacSequence.deleteCharAt(sbMacSequence.length() - 1);
            String bootArgs = to.getBootArgs();
            to.setBootArgs(bootArgs + " nic_macs=" + sbMacSequence.toString());
        }
    }
    // Should only be done on user machines
    if (userVm) {
        configureNestedVirtualization(details, to);
    }
    // Determine the VM's OS description
    GuestOSVO guestOS = _guestOsDao.findByIdIncludingRemoved(vm.getVirtualMachine().getGuestOSId());
    to.setOs(guestOS.getDisplayName());
    to.setHostName(vm.getHostName());
    HostVO host = _hostDao.findById(vm.getVirtualMachine().getHostId());
    GuestOSHypervisorVO guestOsMapping = null;
    if (host != null) {
        guestOsMapping = _guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), getHypervisorType().toString(), host.getHypervisorVersion());
    }
    if (guestOsMapping == null || host == null) {
        to.setPlatformEmulator(null);
    } else {
        to.setPlatformEmulator(guestOsMapping.getGuestOsName());
    }
    return to;
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) HashMap(java.util.HashMap) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) GuestOSVO(com.cloud.storage.GuestOSVO) NicProfile(com.cloud.vm.NicProfile) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HostVO(com.cloud.host.HostVO) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) TrafficType(com.cloud.network.Networks.TrafficType) Type(com.cloud.vm.VirtualMachine.Type) DiskControllerType(com.cloud.hypervisor.vmware.mo.DiskControllerType) BootloaderType(com.cloud.template.VirtualMachineTemplate.BootloaderType) DataObjectType(com.cloud.agent.api.to.DataObjectType) BroadcastDomainType(com.cloud.network.Networks.BroadcastDomainType) VirtualEthernetCardType(com.cloud.hypervisor.vmware.mo.VirtualEthernetCardType) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NicTO(com.cloud.agent.api.to.NicTO)

Example 95 with VirtualMachineTO

use of com.cloud.agent.api.to.VirtualMachineTO in project cosmic by MissionCriticalCloud.

the class LibvirtStartCommandWrapper method execute.

@Override
public Answer execute(final StartCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final String vmName = vmSpec.getName();
    LibvirtVmDef vm = null;
    DomainState state = DomainState.VIR_DOMAIN_SHUTOFF;
    final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
    Connect conn = null;
    try {
        vm = libvirtComputingResource.createVmFromSpec(vmSpec);
        conn = libvirtUtilitiesHelper.getConnectionByType(vm.getHvsType());
        final Long remainingMem = getFreeMemory(conn, libvirtComputingResource);
        if (remainingMem == null) {
            return new StartAnswer(command, "Failed to get free memory");
        } else if (remainingMem < vmSpec.getMinRam()) {
            return new StartAnswer(command, "Not enough memory on the host, remaining: " + remainingMem + ", asking: " + vmSpec.getMinRam());
        }
        final NicTO[] nics = vmSpec.getNics();
        libvirtComputingResource.createVbd(conn, vmSpec, vmName, vm);
        if (!storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)) {
            return new StartAnswer(command, "Failed to connect physical disks to host");
        }
        libvirtComputingResource.createVifs(vmSpec, vm);
        s_logger.debug("starting " + vmName + ": " + vm.toString());
        libvirtComputingResource.startVm(conn, vmName, vm.toString());
        // system vms
        if (vmSpec.getType() != VirtualMachine.Type.User) {
            // pass cmdline with config for the systemvm to configure itself
            if (libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs())) {
                s_logger.debug("Passing cmdline succeeded");
            } else {
                final String errorMessage = "Passing cmdline failed, aborting.";
                s_logger.debug(errorMessage);
                return new StartAnswer(command, errorMessage);
            }
            String controlIp = null;
            for (final NicTO nic : nics) {
                if (nic.getType() == TrafficType.Control) {
                    controlIp = nic.getIp();
                    break;
                }
            }
            // connect to the router by using its linklocal address (that should now be configured)
            s_logger.debug("Starting ssh attempts to " + controlIp);
            final VirtualRoutingResource virtRouterResource = libvirtComputingResource.getVirtRouterResource();
            if (!virtRouterResource.connect(controlIp, 30, 5000)) {
                final String errorMessage = "Unable to login to router via linklocal address " + controlIp + " after 30 tries, aborting.";
                s_logger.debug(errorMessage);
                return new StartAnswer(command, errorMessage);
            }
            s_logger.debug("Successfully completed ssh attempts to " + controlIp);
        }
        state = DomainState.VIR_DOMAIN_RUNNING;
        return new StartAnswer(command);
    } catch (final LibvirtException | InternalErrorException | URISyntaxException e) {
        s_logger.warn("Exception while starting VM: " + ExceptionUtils.getRootCauseMessage(e));
        if (conn != null) {
            libvirtComputingResource.handleVmStartFailure(vm);
        }
        return new StartAnswer(command, e.getMessage());
    } finally {
        if (state != DomainState.VIR_DOMAIN_RUNNING) {
            storagePoolMgr.disconnectPhysicalDisksViaVmSpec(vmSpec);
        }
    }
}
Also used : LibvirtVmDef(com.cloud.hypervisor.kvm.resource.LibvirtVmDef) StartAnswer(com.cloud.agent.api.StartAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) VirtualRoutingResource(com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource) InternalErrorException(com.cloud.exception.InternalErrorException) URISyntaxException(java.net.URISyntaxException) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) DomainState(org.libvirt.DomainInfo.DomainState) KvmStoragePoolManager(com.cloud.hypervisor.kvm.storage.KvmStoragePoolManager) NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)179 Test (org.junit.Test)92 NicTO (com.cloud.agent.api.to.NicTO)69 Answer (com.cloud.agent.api.Answer)62 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)52 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)41 LibvirtException (org.libvirt.LibvirtException)38 Connect (org.libvirt.Connect)35 HashMap (java.util.HashMap)30 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)28 Connection (com.xensource.xenapi.Connection)28 URISyntaxException (java.net.URISyntaxException)28 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)27 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)27 InternalErrorException (com.cloud.exception.InternalErrorException)25 StartAnswer (com.cloud.agent.api.StartAnswer)24 StartCommand (com.cloud.agent.api.StartCommand)21 ArrayList (java.util.ArrayList)20 DiskTO (com.cloud.agent.api.to.DiskTO)18 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)18