Search in sources :

Example 6 with InterfaceDef

use of com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

public Answer execute(IpAssocCommand cmd) {
    String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
    String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
    String[] results = new String[cmd.getIpAddresses().length];
    Connect conn;
    try {
        conn = LibvirtConnection.getConnection();
        List<InterfaceDef> nics = getInterfaces(conn, routerName);
        Map<String, Integer> vlanAllocatedToVM = new HashMap<String, Integer>();
        Integer nicPos = 0;
        for (InterfaceDef nic : nics) {
            if (nic.getBrName().equalsIgnoreCase(_linkLocalBridgeName)) {
                vlanAllocatedToVM.put("LinkLocal", nicPos);
            } else {
                String vlanId = getVlanIdFromBridge(nic.getBrName());
                if (vlanId != null) {
                    vlanAllocatedToVM.put(vlanId, nicPos);
                } else {
                    vlanAllocatedToVM.put(Vlan.UNTAGGED, nicPos);
                }
            }
            nicPos++;
        }
        IpAddressTO[] ips = cmd.getIpAddresses();
        int i = 0;
        String result = null;
        int nicNum = 0;
        for (IpAddressTO ip : ips) {
            if (!vlanAllocatedToVM.containsKey(ip.getVlanId())) {
                /* plug a vif into router */
                VifHotPlug(conn, routerName, ip.getVlanId(), ip.getVifMacAddress());
                vlanAllocatedToVM.put(ip.getVlanId(), nicPos++);
            }
            nicNum = vlanAllocatedToVM.get(ip.getVlanId());
            networkUsage(routerIp, "addVif", "eth" + nicNum);
            result = _virtRouterResource.assignPublicIpAddress(routerName, routerIp, ip.getPublicIp(), ip.isAdd(), ip.isFirstIP(), ip.isSourceNat(), ip.getVlanId(), ip.getVlanGateway(), ip.getVlanNetmask(), ip.getVifMacAddress(), ip.getGuestIp(), nicNum);
            if (result != null) {
                results[i++] = IpAssocAnswer.errorResult;
            } else {
                results[i++] = ip.getPublicIp() + " - success";
                ;
            }
        }
        return new IpAssocAnswer(cmd, results);
    } catch (LibvirtException e) {
        return new IpAssocAnswer(cmd, results);
    } catch (InternalErrorException e) {
        return new IpAssocAnswer(cmd, results);
    }
}
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) InternalErrorException(com.cloud.exception.InternalErrorException) IpAssocAnswer(com.cloud.agent.api.routing.IpAssocAnswer) InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef)

Example 7 with InterfaceDef

use of com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method destroy_network_rules_for_vm.

protected boolean destroy_network_rules_for_vm(Connect conn, String vmName) {
    if (!_can_bridge_firewall) {
        return false;
    }
    String vif = null;
    List<InterfaceDef> intfs = getInterfaces(conn, vmName);
    if (intfs.size() > 0) {
        InterfaceDef intf = intfs.get(0);
        vif = intf.getDevName();
    }
    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);
    }
    String result = cmd.execute();
    if (result != null) {
        return false;
    }
    return true;
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) Script(com.cloud.utils.script.Script)

Example 8 with InterfaceDef

use of com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef in project CloudStack-archive by CloudStack-extras.

the class LibvirtDomainXMLParser method main.

public static void main(String[] args) {
    LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
    parser.parseDomainXML("<domain type='kvm' id='12'>" + "<name>r-6-CV-5002-1</name>" + "<uuid>581b5a4b-b496-8d4d-e44e-a7dcbe9df0b5</uuid>" + "<description>testVM</description>" + "<memory>131072</memory>" + "<currentMemory>131072</currentMemory>" + "<vcpu>1</vcpu>" + "<os>" + "<type arch='i686' machine='pc-0.11'>hvm</type>" + "<kernel>/var/lib/libvirt/qemu/vmlinuz-2.6.31.6-166.fc12.i686</kernel>" + "<cmdline>ro root=/dev/sda1 acpi=force selinux=0 eth0ip=10.1.1.1 eth0mask=255.255.255.0 eth2ip=192.168.10.152 eth2mask=255.255.255.0 gateway=192.168.10.1 dns1=72.52.126.11 dns2=72.52.126.12 domain=v4.myvm.com</cmdline>" + "<boot dev='hd'/>" + "</os>" + "<features>" + "<acpi/>" + "<pae/>" + "</features>" + "<clock offset='utc'/>" + "<on_poweroff>destroy</on_poweroff>" + "<on_reboot>restart</on_reboot>" + "<on_crash>destroy</on_crash>" + "<devices>" + "<emulator>/usr/bin/qemu-kvm</emulator>" + "<disk type='file' device='disk'>" + "<driver name='qemu' type='raw'/>" + "<source file='/mnt/tank//vmops/CV/vm/u000004/r000006/rootdisk'/>" + "<target dev='hda' bus='ide'/>" + "</disk>" + "<interface type='bridge'>" + "<mac address='02:00:50:02:00:01'/>" + "<source bridge='vnbr5002'/>" + "<target dev='vtap5002'/>" + "<model type='e1000'/>" + "</interface>" + "<interface type='network'>" + "<mac address='00:16:3e:77:e2:a1'/>" + "<source network='vmops-private'/>" + "<target dev='vnet3'/>" + "<model type='e1000'/>" + "</interface>" + "<interface type='bridge'>" + "<mac address='06:85:00:00:00:04'/>" + "<source bridge='br0'/>" + "<target dev='tap5002'/>" + "<model type='e1000'/>" + "</interface>" + "<input type='mouse' bus='ps2'/>" + "<graphics type='vnc' port='6031' autoport='no' listen=''/>" + "<video>" + "<model type='cirrus' vram='9216' heads='1'/>" + "</video>" + "</devices>" + "</domain>");
    for (InterfaceDef intf : parser.getInterfaces()) {
        System.out.println(intf);
    }
    for (DiskDef disk : parser.getDisks()) {
        System.out.println(disk);
    }
    System.out.println(parser.getVncPort());
    System.out.println(parser.getDescription());
    List<String> test = new ArrayList<String>(1);
    test.add("1");
    test.add("2");
    if (test.contains("1")) {
        System.out.print("fdf");
    }
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) ArrayList(java.util.ArrayList)

Example 9 with InterfaceDef

use of com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef in project CloudStack-archive by CloudStack-extras.

the class LibvirtDomainXMLParser method parseDomainXML.

public boolean parseDomainXML(String domXML) {
    DocumentBuilder builder;
    try {
        builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(domXML));
        Document doc = builder.parse(is);
        Element rootElement = doc.getDocumentElement();
        desc = getTagValue("description", rootElement);
        Element devices = (Element) rootElement.getElementsByTagName("devices").item(0);
        NodeList disks = devices.getElementsByTagName("disk");
        for (int i = 0; i < disks.getLength(); i++) {
            Element disk = (Element) disks.item(i);
            String diskFmtType = getAttrValue("driver", "type", disk);
            String diskFile = getAttrValue("source", "file", disk);
            String diskDev = getAttrValue("source", "dev", disk);
            String diskLabel = getAttrValue("target", "dev", disk);
            String bus = getAttrValue("target", "bus", disk);
            String type = disk.getAttribute("type");
            String device = disk.getAttribute("device");
            DiskDef def = new DiskDef();
            if (type.equalsIgnoreCase("file")) {
                if (device.equalsIgnoreCase("disk")) {
                    DiskDef.diskFmtType fmt = null;
                    if (diskFmtType != null) {
                        fmt = DiskDef.diskFmtType.valueOf(diskFmtType.toUpperCase());
                    }
                    def.defFileBasedDisk(diskFile, diskLabel, DiskDef.diskBus.valueOf(bus.toUpperCase()), fmt);
                } else if (device.equalsIgnoreCase("cdrom")) {
                    def.defISODisk(diskFile);
                }
            } else if (type.equalsIgnoreCase("block")) {
                def.defBlockBasedDisk(diskDev, diskLabel, DiskDef.diskBus.valueOf(bus.toUpperCase()));
            }
            diskDefs.add(def);
        }
        NodeList nics = devices.getElementsByTagName("interface");
        for (int i = 0; i < nics.getLength(); i++) {
            Element nic = (Element) nics.item(i);
            String type = nic.getAttribute("type");
            String mac = getAttrValue("mac", "address", nic);
            String dev = getAttrValue("target", "dev", nic);
            String model = getAttrValue("model", "type", nic);
            InterfaceDef def = new InterfaceDef();
            if (type.equalsIgnoreCase("network")) {
                String network = getAttrValue("source", "network", nic);
                def.defPrivateNet(network, dev, mac, nicModel.valueOf(model.toUpperCase()));
            } else if (type.equalsIgnoreCase("bridge")) {
                String bridge = getAttrValue("source", "bridge", nic);
                def.defBridgeNet(bridge, dev, mac, nicModel.valueOf(model.toUpperCase()));
            }
            interfaces.add(def);
        }
        Element graphic = (Element) devices.getElementsByTagName("graphics").item(0);
        String port = graphic.getAttribute("port");
        if (port != null) {
            try {
                vncPort = Integer.parseInt(port);
                if (vncPort != -1) {
                    vncPort = vncPort - 5900;
                } else {
                    vncPort = null;
                }
            } catch (NumberFormatException nfe) {
                vncPort = null;
            }
        }
        return true;
    } catch (ParserConfigurationException e) {
        s_logger.debug(e.toString());
    } catch (SAXException e) {
        s_logger.debug(e.toString());
    } catch (IOException e) {
        s_logger.debug(e.toString());
    }
    return false;
}
Also used : InputSource(org.xml.sax.InputSource) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 10 with InterfaceDef

use of com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method getInterfaces.

protected List<InterfaceDef> getInterfaces(Connect conn, String vmName) {
    LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
    Domain dm = null;
    try {
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        parser.parseDomainXML(dm.getXMLDesc(0));
        return parser.getInterfaces();
    } catch (LibvirtException e) {
        s_logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<InterfaceDef>();
    } catch (Exception e) {
        s_logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<InterfaceDef>();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (LibvirtException e) {
        }
    }
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Aggregations

InterfaceDef (com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef)14 Domain (org.libvirt.Domain)5 LibvirtException (org.libvirt.LibvirtException)5 Script (com.cloud.utils.script.Script)4 Connect (org.libvirt.Connect)3 DiskDef (com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef)2 InternalErrorException (com.cloud.exception.InternalErrorException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ConfigurationException (javax.naming.ConfigurationException)2 SecurityGroupRuleAnswer (com.cloud.agent.api.SecurityGroupRuleAnswer)1 StopAnswer (com.cloud.agent.api.StopAnswer)1 VmStatsEntry (com.cloud.agent.api.VmStatsEntry)1 IpAssocAnswer (com.cloud.agent.api.routing.IpAssocAnswer)1 IpAddressTO (com.cloud.agent.api.to.IpAddressTO)1 NicTO (com.cloud.agent.api.to.NicTO)1 DhcpSnooperImpl (com.cloud.agent.dhcp.DhcpSnooperImpl)1 Pair (com.cloud.utils.Pair)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 State (com.cloud.vm.VirtualMachine.State)1