Search in sources :

Example 11 with InterfaceDef

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

the class LibvirtComputingResource method destroyNetworkRulesForVM.

public boolean destroyNetworkRulesForVM(final Connect conn, final String vmName) {
    if (!_canBridgeFirewall) {
        return false;
    }
    String vif = null;
    final List<InterfaceDef> intfs = getInterfaces(conn, vmName);
    if (intfs.size() > 0) {
        final InterfaceDef intf = intfs.get(0);
        vif = intf.getDevName();
    }
    final Script cmd = new Script(_securityGroupPath, _timeout, s_logger);
    cmd.add("destroy_network_rules_for_vm");
    cmd.add("--vmname", vmName);
    if (vif != null) {
        cmd.add("--vif", vif);
    }
    final String result = cmd.execute();
    if (result != null) {
        return false;
    }
    return true;
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) Script(com.cloud.utils.script.Script)

Example 12 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InterfaceDef 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 13 with InterfaceDef

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

the class LibvirtDomainXMLParserTest method testDomainXMLParser.

public void testDomainXMLParser() {
    int vncPort = 5900;
    DiskDef.DiskBus diskBus = DiskDef.DiskBus.VIRTIO;
    DiskDef.DiskType diskType = DiskDef.DiskType.FILE;
    DiskDef.DeviceType deviceType = DiskDef.DeviceType.DISK;
    DiskDef.DiskFmtType diskFormat = DiskDef.DiskFmtType.QCOW2;
    DiskDef.DiskCacheMode diskCache = DiskDef.DiskCacheMode.NONE;
    InterfaceDef.NicModel ifModel = InterfaceDef.NicModel.VIRTIO;
    InterfaceDef.GuestNetType ifType = InterfaceDef.GuestNetType.BRIDGE;
    ChannelDef.ChannelType channelType = ChannelDef.ChannelType.UNIX;
    ChannelDef.ChannelState channelState = ChannelDef.ChannelState.DISCONNECTED;
    String ssvmAgentPath = "/var/lib/libvirt/qemu/s-2970-VM.agent";
    String ssvmAgentName = "s-2970-VM.vport";
    String guestAgentPath = "/var/lib/libvirt/qemu/guest-agent.org.qemu.guest_agent.0";
    String guestAgentName = "org.qemu.guest_agent.0";
    String diskLabel = "vda";
    String diskPath = "/var/lib/libvirt/images/my-test-image.qcow2";
    String xml = "<domain type='kvm' id='10'>" + "<name>s-2970-VM</name>" + "<uuid>4d2c1526-865d-4fc9-a1ac-dbd1801a22d0</uuid>" + "<description>Debian GNU/Linux 6(64-bit)</description>" + "<memory unit='KiB'>262144</memory>" + "<currentMemory unit='KiB'>262144</currentMemory>" + "<vcpu placement='static'>1</vcpu>" + "<cputune>" + "<shares>250</shares>" + "</cputune>" + "<resource>" + "<partition>/machine</partition>" + "</resource>" + "<os>" + "<type arch='x86_64' machine='pc-i440fx-1.5'>hvm</type>" + "<boot dev='cdrom'/>" + "<boot dev='hd'/>" + "</os>" + "<features>" + "<acpi/>" + "<apic/>" + "<pae/>" + "</features>" + "<clock offset='utc'/>" + "<on_poweroff>destroy</on_poweroff>" + "<on_reboot>restart</on_reboot>" + "<on_crash>destroy</on_crash>" + "<devices>" + "<emulator>/usr/bin/kvm-spice</emulator>" + "<disk type='" + diskType.toString() + "' device='" + deviceType.toString() + "'>" + "<driver name='qemu' type='" + diskFormat.toString() + "' cache='" + diskCache.toString() + "'/>" + "<source file='" + diskPath + "'/>" + "<target dev='" + diskLabel + "' bus='" + diskBus.toString() + "'/>" + "<alias name='virtio-disk0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>" + "</disk>" + "<disk type='file' device='cdrom'>" + "<driver name='qemu' type='raw' cache='none'/>" + "<source file='/usr/share/cloudstack-common/vms/systemvm.iso'/>" + "<target dev='hdc' bus='ide'/>" + "<readonly/>" + "<alias name='ide0-1-0'/>" + "<address type='drive' controller='0' bus='1' target='0' unit='0'/>" + "</disk>" + "<controller type='usb' index='0'>" + "<alias name='usb0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>" + "</controller>" + "<controller type='pci' index='0' model='pci-root'>" + "<alias name='pci0'/>" + "</controller>" + "<controller type='ide' index='0'>" + "<alias name='ide0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>" + "</controller>" + "<controller type='virtio-serial' index='0'>" + "<alias name='virtio-serial0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>" + "</controller>" + "<interface type='" + ifType.toString() + "'>" + "<mac address='0e:00:a9:fe:02:00'/>" + "<source bridge='cloud0'/>" + "<target dev='vnet0'/>" + "<model type='" + ifModel.toString() + "'/>" + "<alias name='net0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>" + "</interface>" + "<interface type='" + ifType.toString() + "'>" + "<mac address='06:c5:94:00:05:65'/>" + "<source bridge='cloudbr1'/>" + "<target dev='vnet1'/>" + "<model type='" + ifModel.toString() + "'/>" + "<alias name='net1'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>" + "</interface>" + "<interface type='" + ifType.toString() + "'>" + "<mac address='06:c9:f4:00:04:40'/>" + "<source bridge='cloudbr0'/>" + "<target dev='vnet2'/>" + "<model type='" + ifModel.toString() + "'/>" + "<alias name='net2'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>" + "</interface>" + "<interface type='" + ifType.toString() + "'>" + "<mac address='06:7e:c6:00:05:68'/>" + "<source bridge='cloudbr1'/>" + "<target dev='vnet3'/>" + "<model type='" + ifModel.toString() + "'/>" + "<alias name='net3'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>" + "</interface>" + "<serial type='pty'>" + "<source path='/dev/pts/3'/>" + "<target port='0'/>" + "<alias name='serial0'/>" + "</serial>" + "<console type='pty' tty='/dev/pts/3'>" + "<source path='/dev/pts/3'/>" + "<target type='serial' port='0'/>" + "<alias name='serial0'/>" + "</console>" + "<channel type='unix'>" + "<source mode='bind' path='/var/lib/libvirt/qemu/s-2970-VM.agent'/>" + "<target type='virtio' name='s-2970-VM.vport' state='disconnected'/>" + "<alias name='channel0'/>" + "<address type='virtio-serial' controller='0' bus='0' port='1'/>" + "</channel>" + "<input type='tablet' bus='usb'>" + "<alias name='input0'/>" + "</input>" + "<input type='mouse' bus='ps2'/>" + "<graphics type='vnc' port='" + vncPort + "' autoport='yes' listen='0.0.0.0'>" + "<listen type='address' address='0.0.0.0'/>" + "</graphics>" + "<video>" + "<model type='cirrus' vram='9216' heads='1'/>" + "<alias name='video0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>" + "</video>" + "<memballoon model='virtio'>" + "<alias name='balloon0'/>" + "<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>" + "</memballoon>" + "<rng model='virtio'>" + "<rate period='5000' bytes='4096' />" + "<backend model='random'>/dev/random</backend>" + "</rng>" + "<channel type='unix'>" + "<source mode='bind' path='" + guestAgentPath + "'/>" + "<target type='virtio' name='" + guestAgentName + "'/>" + "<alias name='channel0'/>" + "<address type='virtio-serial' controller='0' bus='0' port='1'/>" + "</channel>" + "</devices>" + "<seclabel type='none'/>" + "</domain>";
    LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
    parser.parseDomainXML(xml);
    assertEquals(vncPort - 5900, (int) parser.getVncPort());
    List<DiskDef> disks = parser.getDisks();
    /* Disk 0 is the first disk, the QCOW2 file backed virto disk */
    int diskId = 0;
    assertEquals(diskLabel, disks.get(diskId).getDiskLabel());
    assertEquals(diskPath, disks.get(diskId).getDiskPath());
    assertEquals(diskCache, disks.get(diskId).getCacheMode());
    assertEquals(diskBus, disks.get(diskId).getBusType());
    assertEquals(diskType, disks.get(diskId).getDiskType());
    assertEquals(deviceType, disks.get(diskId).getDeviceType());
    assertEquals(diskFormat, disks.get(diskId).getDiskFormatType());
    List<ChannelDef> channels = parser.getChannels();
    for (int i = 0; i < channels.size(); i++) {
        assertEquals(channelType, channels.get(i).getChannelType());
        assertEquals(channelType, channels.get(i).getChannelType());
    }
    /* SSVM provisioning port/channel */
    assertEquals(channelState, channels.get(0).getChannelState());
    assertEquals(new File(ssvmAgentPath), channels.get(0).getPath());
    assertEquals(ssvmAgentName, channels.get(0).getName());
    /* Qemu Guest Agent port/channel */
    assertEquals(new File(guestAgentPath), channels.get(1).getPath());
    assertEquals(guestAgentName, channels.get(1).getName());
    List<InterfaceDef> ifs = parser.getInterfaces();
    for (int i = 0; i < ifs.size(); i++) {
        assertEquals(ifModel, ifs.get(i).getModel());
        assertEquals(ifType, ifs.get(i).getNetType());
    }
    List<RngDef> rngs = parser.getRngs();
    assertEquals("/dev/random", rngs.get(0).getPath());
    assertEquals(RngDef.RngBackendModel.RANDOM, rngs.get(0).getRngBackendModel());
    assertEquals(4096, rngs.get(0).getRngRateBytes());
    assertEquals(5000, rngs.get(0).getRngRatePeriod());
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) RngDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.RngDef) DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) ChannelDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.ChannelDef) File(java.io.File)

Example 14 with InterfaceDef

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

the class LibvirtStopCommandWrapper method execute.

@Override
public Answer execute(final StopCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = command.getVmName();
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
    if (command.checkBeforeCleanup()) {
        try {
            final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
            final Domain vm = conn.domainLookupByName(command.getVmName());
            if (vm != null && vm.getInfo().state == DomainState.VIR_DOMAIN_RUNNING) {
                return new StopAnswer(command, "vm is still running on host", false);
            }
        } catch (final Exception e) {
            s_logger.debug("Failed to get vm status in case of checkboforecleanup is true", e);
        }
    }
    File pemFile = new File(LibvirtComputingResource.SSHPRVKEYPATH);
    try {
        if (vmName.startsWith("s-") || vmName.startsWith("v-")) {
            //move the command line file to backup.
            s_logger.debug("backing up the cmdline");
            try {
                Pair<Boolean, String> ret = SshHelper.sshExecute(command.getControlIp(), 3922, "root", pemFile, null, "mv -f " + CMDLINE_PATH + " " + CMDLINE_BACKUP_PATH);
                if (!ret.first()) {
                    s_logger.debug("Failed to backup cmdline file due to " + ret.second());
                }
            } catch (Exception e) {
                s_logger.debug("Failed to backup cmdline file due to " + e.getMessage());
            }
        }
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
        final List<DiskDef> disks = libvirtComputingResource.getDisks(conn, vmName);
        final List<InterfaceDef> ifaces = libvirtComputingResource.getInterfaces(conn, vmName);
        libvirtComputingResource.destroyNetworkRulesForVM(conn, vmName);
        final String result = libvirtComputingResource.stopVM(conn, vmName);
        if (result == null) {
            for (final DiskDef disk : disks) {
                libvirtComputingResource.cleanupDisk(disk);
            }
            for (final InterfaceDef iface : ifaces) {
                // each interface at this point, so inform all vif drivers
                for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) {
                    vifDriver.unplug(iface);
                }
            }
        }
        return new StopAnswer(command, result, true);
    } catch (final LibvirtException e) {
        s_logger.debug("unable to stop VM:" + vmName + " due to" + e.getMessage());
        try {
            if (vmName.startsWith("s-") || vmName.startsWith("v-"))
                s_logger.debug("restoring cmdline file from backup");
            Pair<Boolean, String> ret = SshHelper.sshExecute(command.getControlIp(), 3922, "root", pemFile, null, "mv " + CMDLINE_BACKUP_PATH + " " + CMDLINE_PATH);
            if (!ret.first()) {
                s_logger.debug("unable to restore cmdline due to " + ret.second());
            }
        } catch (final Exception ex) {
            s_logger.debug("unable to restore cmdline due to:" + ex.getMessage());
        }
        return new StopAnswer(command, e.getMessage(), false);
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) LibvirtException(org.libvirt.LibvirtException) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) Domain(org.libvirt.Domain) StopAnswer(com.cloud.agent.api.StopAnswer) File(java.io.File) Pair(com.cloud.utils.Pair)

Example 15 with InterfaceDef

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

the class LibvirtUnPlugNicCommandWrapper method execute.

@Override
public Answer execute(final UnPlugNicCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final NicTO nic = command.getNic();
    final String vmName = command.getVmName();
    Domain vm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
        vm = libvirtComputingResource.getDomain(conn, vmName);
        final List<InterfaceDef> pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName);
        for (final InterfaceDef pluggedNic : pluggedNics) {
            if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
                vm.detachDevice(pluggedNic.toString());
                // each interface at this point, so inform all vif drivers
                for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) {
                    vifDriver.unplug(pluggedNic);
                }
                return new UnPlugNicAnswer(command, true, "success");
            }
        }
        return new UnPlugNicAnswer(command, true, "success");
    } catch (final LibvirtException e) {
        final String msg = " Unplug Nic failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new UnPlugNicAnswer(command, false, msg);
    } finally {
        if (vm != null) {
            try {
                vm.free();
            } catch (final LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) NicTO(com.cloud.agent.api.to.NicTO)

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