Search in sources :

Example 56 with LibvirtException

use of org.libvirt.LibvirtException in project cloudstack by apache.

the class LibvirtCheckVirtualMachineCommandWrapper method execute.

@Override
public Answer execute(final CheckVirtualMachineCommand command, final LibvirtComputingResource libvirtComputingResource) {
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
        final PowerState state = libvirtComputingResource.getVmState(conn, command.getVmName());
        Integer vncPort = null;
        if (state == PowerState.PowerOn) {
            vncPort = libvirtComputingResource.getVncPort(conn, command.getVmName());
        }
        return new CheckVirtualMachineAnswer(command, state, vncPort);
    } catch (final LibvirtException e) {
        return new CheckVirtualMachineAnswer(command, e.getMessage());
    }
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 57 with LibvirtException

use of org.libvirt.LibvirtException in project cloudstack by apache.

the class LibvirtGetVmDiskStatsCommandWrapper method execute.

@Override
public Answer execute(final GetVmDiskStatsCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final List<String> vmNames = command.getVmNames();
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
    try {
        final HashMap<String, List<VmDiskStatsEntry>> vmDiskStatsNameMap = new HashMap<String, List<VmDiskStatsEntry>>();
        final Connect conn = libvirtUtilitiesHelper.getConnection();
        for (final String vmName : vmNames) {
            try {
                final List<VmDiskStatsEntry> statEntry = libvirtComputingResource.getVmDiskStat(conn, vmName);
                if (statEntry == null) {
                    continue;
                }
                vmDiskStatsNameMap.put(vmName, statEntry);
            } catch (LibvirtException e) {
                s_logger.warn("Can't get vm disk stats: " + e.toString() + ", continue");
            }
        }
        return new GetVmDiskStatsAnswer(command, "", command.getHostName(), vmDiskStatsNameMap);
    } catch (final LibvirtException e) {
        s_logger.debug("Can't get vm disk stats: " + e.toString());
        return new GetVmDiskStatsAnswer(command, null, null, null);
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) HashMap(java.util.HashMap) Connect(org.libvirt.Connect) List(java.util.List) VmDiskStatsEntry(com.cloud.agent.api.VmDiskStatsEntry) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer)

Example 58 with LibvirtException

use of org.libvirt.LibvirtException in project cloudstack by apache.

the class LibvirtComputingResource method stopVM.

protected String stopVM(final Connect conn, final String vmName, final boolean force) {
    Domain dm = null;
    try {
        dm = conn.domainLookupByName(vmName);
        final int persist = dm.isPersistent();
        if (force) {
            if (dm.isActive() == 1) {
                dm.destroy();
                if (persist == 1) {
                    dm.undefine();
                }
            }
        } else {
            if (dm.isActive() == 0) {
                return null;
            }
            dm.shutdown();
            int retry = _stopTimeout / 2000;
            /* Wait for the domain gets into shutoff state. When it does
                   the dm object will no longer work, so we need to catch it. */
            try {
                while (dm.isActive() == 1 && retry >= 0) {
                    Thread.sleep(2000);
                    retry--;
                }
            } catch (final LibvirtException e) {
                final String error = e.toString();
                if (error.contains("Domain not found")) {
                    s_logger.debug("successfully shut down vm " + vmName);
                } else {
                    s_logger.debug("Error in waiting for vm shutdown:" + error);
                }
            }
            if (retry < 0) {
                s_logger.warn("Timed out waiting for domain " + vmName + " to shutdown gracefully");
                return Script.ERR_TIMEOUT;
            } else {
                if (persist == 1) {
                    dm.undefine();
                }
            }
        }
    } catch (final LibvirtException e) {
        if (e.getMessage().contains("Domain not found")) {
            s_logger.debug("VM " + vmName + " doesn't exist, no need to stop it");
            return null;
        }
        s_logger.debug("Failed to stop VM :" + vmName + " :", e);
        return e.getMessage();
    } catch (final InterruptedException ie) {
        s_logger.debug("Interrupted sleep");
        return ie.getMessage();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (final LibvirtException e) {
            s_logger.trace("Ignoring libvirt error.", e);
        }
    }
    return null;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain)

Example 59 with LibvirtException

use of org.libvirt.LibvirtException in project cloudstack by apache.

the class LibvirtComputingResource method cleanupNetworkElementCommand.

protected ExecutionResult cleanupNetworkElementCommand(final IpAssocCommand cmd) {
    final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    final String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    final String lastIp = cmd.getAccessDetail(NetworkElementCommand.NETWORK_PUB_LAST_IP);
    Connect conn;
    try {
        conn = LibvirtConnection.getConnectionByVmName(routerName);
        final List<InterfaceDef> nics = getInterfaces(conn, routerName);
        final Map<String, Integer> broadcastUriAllocatedToVM = new HashMap<String, Integer>();
        Integer nicPos = 0;
        for (final InterfaceDef nic : nics) {
            if (nic.getBrName().equalsIgnoreCase(_linkLocalBridgeName)) {
                broadcastUriAllocatedToVM.put("LinkLocal", nicPos);
            } else {
                if (nic.getBrName().equalsIgnoreCase(_publicBridgeName) || nic.getBrName().equalsIgnoreCase(_privBridgeName) || nic.getBrName().equalsIgnoreCase(_guestBridgeName)) {
                    broadcastUriAllocatedToVM.put(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED).toString(), nicPos);
                } else {
                    final String broadcastUri = getBroadcastUriFromBridge(nic.getBrName());
                    broadcastUriAllocatedToVM.put(broadcastUri, nicPos);
                }
            }
            nicPos++;
        }
        final IpAddressTO[] ips = cmd.getIpAddresses();
        final int numOfIps = ips.length;
        int nicNum = 0;
        for (final IpAddressTO ip : ips) {
            if (!broadcastUriAllocatedToVM.containsKey(ip.getBroadcastUri())) {
                /* plug a vif into router */
                VifHotPlug(conn, routerName, ip.getBroadcastUri(), ip.getVifMacAddress());
                broadcastUriAllocatedToVM.put(ip.getBroadcastUri(), nicPos++);
            }
            nicNum = broadcastUriAllocatedToVM.get(ip.getBroadcastUri());
            if (org.apache.commons.lang.StringUtils.equalsIgnoreCase(lastIp, "true") && !ip.isAdd()) {
                // in isolated network eth2 is the default public interface. We don't want to delete it.
                if (nicNum != 2) {
                    vifHotUnPlug(conn, routerName, ip.getVifMacAddress());
                    networkUsage(routerIp, "deleteVif", "eth" + nicNum);
                }
            }
        }
    } catch (final LibvirtException e) {
        s_logger.error("ipassoccmd failed", e);
        return new ExecutionResult(false, e.getMessage());
    } catch (final InternalErrorException e) {
        s_logger.error("ipassoccmd failed", e);
        return new ExecutionResult(false, e.getMessage());
    }
    return new ExecutionResult(true, null);
}
Also used : IpAddressTO(com.cloud.agent.api.to.IpAddressTO) LibvirtException(org.libvirt.LibvirtException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Connect(org.libvirt.Connect) ExecutionResult(com.cloud.utils.ExecutionResult) InternalErrorException(com.cloud.exception.InternalErrorException) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)

Example 60 with LibvirtException

use of org.libvirt.LibvirtException in project cloudstack by apache.

the class LibvirtGetVncPortCommandWrapper method execute.

@Override
public Answer execute(final GetVncPortCommand command, final LibvirtComputingResource libvirtComputingResource) {
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getName());
        final Integer vncPort = libvirtComputingResource.getVncPort(conn, command.getName());
        return new GetVncPortAnswer(command, libvirtComputingResource.getPrivateIp(), 5900 + vncPort);
    } catch (final LibvirtException e) {
        return new GetVncPortAnswer(command, e.toString());
    }
}
Also used : GetVncPortAnswer(com.cloud.agent.api.GetVncPortAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect)

Aggregations

LibvirtException (org.libvirt.LibvirtException)176 Connect (org.libvirt.Connect)109 Answer (com.cloud.agent.api.Answer)63 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)58 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)55 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)55 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)55 Test (org.junit.Test)55 Domain (org.libvirt.Domain)53 InternalErrorException (com.cloud.exception.InternalErrorException)41 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)41 URISyntaxException (java.net.URISyntaxException)32 StoragePool (org.libvirt.StoragePool)27 NicTO (com.cloud.agent.api.to.NicTO)25 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)23 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)22 ConfigurationException (javax.naming.ConfigurationException)22 IOException (java.io.IOException)21 ArrayList (java.util.ArrayList)21 FileNotFoundException (java.io.FileNotFoundException)17