Search in sources :

Example 31 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InterfaceDef in project cloudstack by apache.

the class LibvirtComputingResource method rebootVM.

public String rebootVM(final Connect conn, final String vmName) {
    Domain dm = null;
    String msg = null;
    try {
        dm = conn.domainLookupByName(vmName);
        // Get XML Dump including the secure information such as VNC password
        // By passing 1, or VIR_DOMAIN_XML_SECURE flag
        // https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainXMLFlags
        String vmDef = dm.getXMLDesc(1);
        final LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
        parser.parseDomainXML(vmDef);
        for (final InterfaceDef nic : parser.getInterfaces()) {
            if (nic.getNetType() == GuestNetType.BRIDGE && nic.getBrName().startsWith("cloudVirBr")) {
                try {
                    final int vnetId = Integer.parseInt(nic.getBrName().replaceFirst("cloudVirBr", ""));
                    final String pifName = getPif(_guestBridgeName);
                    final String newBrName = "br" + pifName + "-" + vnetId;
                    vmDef = vmDef.replaceAll("'" + nic.getBrName() + "'", "'" + newBrName + "'");
                    s_logger.debug("VM bridge name is changed from " + nic.getBrName() + " to " + newBrName);
                } catch (final NumberFormatException e) {
                    continue;
                }
            }
        }
        s_logger.debug(vmDef);
        msg = stopVM(conn, vmName);
        msg = startVM(conn, vmName, vmDef);
        return null;
    } catch (final LibvirtException e) {
        s_logger.warn("Failed to create vm", e);
        msg = e.getMessage();
    } catch (final InternalErrorException e) {
        s_logger.warn("Failed to create vm", e);
        msg = e.getMessage();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (final LibvirtException e) {
            s_logger.trace("Ignoring libvirt error.", e);
        }
    }
    return msg;
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) InternalErrorException(com.cloud.exception.InternalErrorException) Domain(org.libvirt.Domain)

Example 32 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InterfaceDef in project cloudstack by apache.

the class LibvirtComputingResourceTest method testPlugNicCommandInternalError.

@SuppressWarnings("unchecked")
@Test
public void testPlugNicCommandInternalError() {
    final NicTO nic = Mockito.mock(NicTO.class);
    final String instanceName = "Test";
    final Type vmtype = Type.DomainRouter;
    final PlugNicCommand command = new PlugNicCommand(nic, instanceName, vmtype);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final Domain vm = Mockito.mock(Domain.class);
    final VifDriver vifDriver = Mockito.mock(VifDriver.class);
    final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
    final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
    nics.add(intDef);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
    when(intDef.getDevName()).thenReturn("eth0");
    when(intDef.getBrName()).thenReturn("br0");
    when(intDef.getMacAddress()).thenReturn("00:00:00:00");
    when(nic.getMac()).thenReturn("00:00:00:01");
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
        when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
        when(libvirtComputingResource.getVifDriver(nic.getType())).thenReturn(vifDriver);
        when(vifDriver.plug(nic, "Other PV", "")).thenThrow(InternalErrorException.class);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException 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(command.getVmName());
        verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName);
        verify(libvirtComputingResource, times(1)).getVifDriver(nic.getType());
        verify(vifDriver, times(1)).plug(nic, "Other PV", "");
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) InternalErrorException(com.cloud.exception.InternalErrorException) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) 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) TrafficType(com.cloud.network.Networks.TrafficType) Type(com.cloud.vm.VirtualMachine.Type) StoragePoolType(com.cloud.storage.Storage.StoragePoolType) BootloaderType(com.cloud.template.VirtualMachineTemplate.BootloaderType) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) Domain(org.libvirt.Domain) PlugNicCommand(com.cloud.agent.api.PlugNicCommand) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) NicTO(com.cloud.agent.api.to.NicTO) Test(org.junit.Test)

Example 33 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InterfaceDef in project cloudstack by apache.

the class LibvirtComputingResourceTest method testSecurityGroupRulesCmdException.

@SuppressWarnings("unchecked")
@Test
public void testSecurityGroupRulesCmdException() {
    final String guestIp = "127.0.0.1";
    final String guestIp6 = "2001:db8::cad:40ff:fefd:75c4";
    final String guestMac = "00:00:00:00";
    final String vmName = "Test";
    final Long vmId = 1l;
    final String signature = "signature";
    final Long seqNum = 1l;
    final IpPortAndProto[] ingressRuleSet = new IpPortAndProto[] { Mockito.mock(IpPortAndProto.class) };
    final IpPortAndProto[] egressRuleSet = new IpPortAndProto[] { Mockito.mock(IpPortAndProto.class) };
    final List<String> secIps = new Vector<String>();
    final SecurityGroupRulesCmd command = new SecurityGroupRulesCmd(guestIp, guestIp6, guestMac, vmName, vmId, signature, seqNum, ingressRuleSet, egressRuleSet, secIps);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
    final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class);
    nics.add(interfaceDef);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenThrow(LibvirtException.class);
    } 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(command.getVmName());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : SecurityGroupRulesCmd(com.cloud.agent.api.SecurityGroupRulesCmd) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) IpPortAndProto(com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) 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) Vector(java.util.Vector) Test(org.junit.Test)

Aggregations

InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)33 LibvirtException (org.libvirt.LibvirtException)23 Connect (org.libvirt.Connect)22 Domain (org.libvirt.Domain)15 Answer (com.cloud.agent.api.Answer)11 Test (org.junit.Test)11 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)10 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)10 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)10 ArrayList (java.util.ArrayList)10 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)10 NicTO (com.cloud.agent.api.to.NicTO)8 InternalErrorException (com.cloud.exception.InternalErrorException)8 DiskDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef)7 UnPlugNicCommand (com.cloud.agent.api.UnPlugNicCommand)5 ExecutionResult (com.cloud.utils.ExecutionResult)5 IpAddressTO (com.cloud.agent.api.to.IpAddressTO)4 VifDriver (com.cloud.hypervisor.kvm.resource.VifDriver)4 Script (com.cloud.utils.script.Script)4 PlugNicCommand (com.cloud.agent.api.PlugNicCommand)3