Search in sources :

Example 31 with LibvirtException

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

the class KVMHABase method getMountPoint.

protected String getMountPoint(NfsStoragePool storagePool) {
    StoragePool pool = null;
    String poolName = null;
    try {
        pool = LibvirtConnection.getConnection().storagePoolLookupByUUIDString(storagePool._poolUUID);
        if (pool != null) {
            StoragePoolInfo spi = pool.getInfo();
            if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
                pool.create(0);
            } else {
            /*
                     * Sometimes, the mount point is lost, even libvirt thinks
                     * the storage pool still running
                     */
            }
            poolName = pool.getName();
        }
    } catch (LibvirtException e) {
        s_logger.debug("Ignoring libvirt error.", e);
    } finally {
        try {
            if (pool != null) {
                pool.free();
            }
        } catch (LibvirtException e) {
            s_logger.debug("Ignoring libvirt error.", e);
        }
    }
    return checkingMountPoint(storagePool, poolName);
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) StoragePoolInfo(org.libvirt.StoragePoolInfo)

Example 32 with LibvirtException

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

the class LibvirtComputingResource method getAllVmNames.

protected List<String> getAllVmNames(final Connect conn) {
    final ArrayList<String> la = new ArrayList<String>();
    try {
        final String[] names = conn.listDefinedDomains();
        for (int i = 0; i < names.length; i++) {
            la.add(names[i]);
        }
    } catch (final LibvirtException e) {
        s_logger.warn("Failed to list Defined domains", e);
    }
    int[] ids = null;
    try {
        ids = conn.listDomains();
    } catch (final LibvirtException e) {
        s_logger.warn("Failed to list domains", e);
        return la;
    }
    Domain dm = null;
    for (int i = 0; i < ids.length; i++) {
        try {
            dm = conn.domainLookupByID(ids[i]);
            la.add(dm.getName());
        } catch (final LibvirtException e) {
            s_logger.warn("Unable to get vms", e);
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (final LibvirtException e) {
                s_logger.trace("Ignoring libvirt error.", e);
            }
        }
    }
    return la;
}
Also used : LibvirtException(org.libvirt.LibvirtException) ArrayList(java.util.ArrayList) Domain(org.libvirt.Domain)

Example 33 with LibvirtException

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

the class LibvirtComputingResource method getInterfaces.

public List<InterfaceDef> getInterfaces(final Connect conn, final String vmName) {
    final LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
    Domain dm = null;
    try {
        dm = conn.domainLookupByName(vmName);
        parser.parseDomainXML(dm.getXMLDesc(0));
        return parser.getInterfaces();
    } catch (final LibvirtException e) {
        s_logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<InterfaceDef>();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (final LibvirtException e) {
            s_logger.trace("Ignoring libvirt error.", e);
        }
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain)

Example 34 with LibvirtException

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

the class LibvirtComputingResource method startVM.

public String startVM(final Connect conn, final String vmName, final String domainXML) throws LibvirtException, InternalErrorException {
    try {
        /*
                We create a transient domain here. When this method gets
                called we receive a full XML specification of the guest,
                so no need to define it persistent.

                This also makes sure we never have any old "garbage" defined
                in libvirt which might haunt us.
             */
        // check for existing inactive vm definition and remove it
        // this can sometimes happen during crashes, etc
        Domain dm = null;
        try {
            dm = conn.domainLookupByName(vmName);
            if (dm != null && dm.isPersistent() == 1) {
                // this is safe because it doesn't stop running VMs
                dm.undefine();
            }
        } catch (final LibvirtException e) {
        // this is what we want, no domain found
        } finally {
            if (dm != null) {
                dm.free();
            }
        }
        conn.domainCreateXML(domainXML, 0);
    } catch (final LibvirtException e) {
        throw e;
    }
    return null;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain)

Example 35 with LibvirtException

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

the class LibvirtComputingResourceTest method testPvlanSetupCommandDhcpAdd.

@Test
public void testPvlanSetupCommandDhcpAdd() {
    final String op = "add";
    final URI uri = URI.create("http://localhost");
    final String networkTag = "/105";
    final String dhcpName = "dhcp";
    final String dhcpMac = "00:00:00:00";
    final String dhcpIp = "127.0.0.1";
    final PvlanSetupCommand command = PvlanSetupCommand.createDhcpSetup(op, uri, networkTag, dhcpName, dhcpMac, dhcpIp);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final String guestBridgeName = "br0";
    when(libvirtComputingResource.getGuestBridgeName()).thenReturn(guestBridgeName);
    when(libvirtComputingResource.getTimeout()).thenReturn(Duration.ZERO);
    final String ovsPvlanDhcpHostPath = "/pvlan";
    when(libvirtComputingResource.getOvsPvlanDhcpHostPath()).thenReturn(ovsPvlanDhcpHostPath);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    final List<InterfaceDef> ifaces = new ArrayList<InterfaceDef>();
    final InterfaceDef nic = Mockito.mock(InterfaceDef.class);
    ifaces.add(nic);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(dhcpName)).thenReturn(conn);
        when(libvirtComputingResource.getInterfaces(conn, dhcpName)).thenReturn(ifaces);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(dhcpName);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) PvlanSetupCommand(com.cloud.agent.api.PvlanSetupCommand) URI(java.net.URI) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) Test(org.junit.Test)

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