Search in sources :

Example 6 with DiskDef

use of com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef 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 7 with DiskDef

use of com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef 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 8 with DiskDef

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

the class CloudZonesComputingResource method execute.

@Override
protected synchronized StartAnswer execute(StartCommand cmd) {
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    String vmName = vmSpec.getName();
    LibvirtVMDef vm = null;
    State state = State.Stopped;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
        synchronized (_vms) {
            _vms.put(vmName, State.Starting);
        }
        vm = createVMFromSpec(vmSpec);
        createVbd(conn, vmSpec, vmName, vm);
        createVifs(conn, vmSpec, vm);
        s_logger.debug("starting " + vmName + ": " + vm.toString());
        startDomain(conn, vmName, vm.toString());
        NicTO[] nics = vmSpec.getNics();
        for (NicTO nic : nics) {
            if (nic.isSecurityGroupEnabled()) {
                if (vmSpec.getType() != VirtualMachine.Type.User) {
                    default_network_rules_for_systemvm(conn, vmName);
                } else {
                    nic.setIp(null);
                    default_network_rules(conn, vmName, nic, vmSpec.getId());
                }
            }
        }
        // attached disk
        for (DiskDef disk : vm.getDevices().getDisks()) {
            if (disk.isAttachDeferred()) {
                attachOrDetachDevice(conn, true, vmName, disk.toString());
            }
        }
        if (vmSpec.getType() == VirtualMachine.Type.User) {
            for (NicTO nic : nics) {
                if (nic.getType() == TrafficType.Guest) {
                    InetAddress ipAddr = _dhcpSnooper.getIPAddr(nic.getMac(), vmName);
                    if (ipAddr == null) {
                        s_logger.debug("Failed to get guest DHCP ip, stop it");
                        StopCommand stpCmd = new StopCommand(vmName);
                        execute(stpCmd);
                        return new StartAnswer(cmd, "Failed to get guest DHCP ip, stop it");
                    }
                    s_logger.debug(ipAddr);
                    nic.setIp(ipAddr.getHostAddress());
                    post_default_network_rules(conn, vmName, nic, vmSpec.getId(), _dhcpSnooper.getDhcpServerIP(), _hostIp, _hostMacAddress);
                    _vmDataServer.handleVmStarted(cmd.getVirtualMachine());
                }
            }
        }
        state = State.Running;
        return new StartAnswer(cmd);
    } catch (Exception e) {
        s_logger.warn("Exception ", e);
        if (conn != null) {
            handleVmStartFailure(conn, vmName, vm);
        }
        return new StartAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != State.Stopped) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : LibvirtVMDef(com.cloud.agent.resource.computing.LibvirtVMDef) DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) StopCommand(com.cloud.agent.api.StopCommand) StartAnswer(com.cloud.agent.api.StartAnswer) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) InetAddress(java.net.InetAddress) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) ConfigurationException(javax.naming.ConfigurationException) LibvirtException(org.libvirt.LibvirtException) NicTO(com.cloud.agent.api.to.NicTO)

Example 9 with DiskDef

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

the class LibvirtComputingResource method createVbd.

protected void createVbd(Connect conn, VirtualMachineTO vmSpec, String vmName, LibvirtVMDef vm) throws InternalErrorException, LibvirtException, URISyntaxException {
    List<VolumeTO> disks = Arrays.asList(vmSpec.getDisks());
    Collections.sort(disks, new Comparator<VolumeTO>() {

        @Override
        public int compare(VolumeTO arg0, VolumeTO arg1) {
            return arg0.getDeviceId() > arg1.getDeviceId() ? 1 : -1;
        }
    });
    for (VolumeTO volume : disks) {
        KVMPhysicalDisk physicalDisk = null;
        KVMStoragePool pool = null;
        if (volume.getType() == Volume.Type.ISO && volume.getPath() != null) {
            String volPath = volume.getPath();
            int index = volPath.lastIndexOf("/");
            String volDir = volPath.substring(0, index);
            String volName = volPath.substring(index + 1);
            KVMStoragePool secondaryStorage = _storagePoolMgr.getStoragePoolByURI(volDir);
            physicalDisk = secondaryStorage.getPhysicalDisk(volName);
        } else if (volume.getType() != Volume.Type.ISO) {
            pool = _storagePoolMgr.getStoragePool(volume.getPoolUuid());
            physicalDisk = pool.getPhysicalDisk(volume.getPath());
        }
        String volPath = null;
        if (physicalDisk != null) {
            volPath = physicalDisk.getPath();
        }
        DiskDef.diskBus diskBusType = getGuestDiskModel(vmSpec.getOs());
        DiskDef disk = new DiskDef();
        if (volume.getType() == Volume.Type.ISO) {
            if (volPath == null) {
                /* Add iso as placeholder */
                disk.defISODisk(null);
            } else {
                disk.defISODisk(volPath);
            }
        } else {
            int devId = (int) volume.getDeviceId();
            if (volume.getType() == Volume.Type.DATADISK) {
                disk.defFileBasedDisk(physicalDisk.getPath(), devId, DiskDef.diskBus.VIRTIO, DiskDef.diskFmtType.QCOW2);
            } else {
                disk.defFileBasedDisk(physicalDisk.getPath(), devId, diskBusType, DiskDef.diskFmtType.QCOW2);
            }
        }
        vm.getDevices().addDevice(disk);
    }
    if (vmSpec.getType() != VirtualMachine.Type.User) {
        if (_sysvmISOPath != null) {
            DiskDef iso = new DiskDef();
            iso.defISODisk(_sysvmISOPath);
            vm.getDevices().addDevice(iso);
        }
        createPatchVbd(conn, vmName, vm, vmSpec);
    }
}
Also used : VolumeTO(com.cloud.agent.api.to.VolumeTO) DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) KVMStoragePool(com.cloud.agent.storage.KVMStoragePool) KVMPhysicalDisk(com.cloud.agent.storage.KVMPhysicalDisk)

Aggregations

DiskDef (com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef)9 KVMPhysicalDisk (com.cloud.agent.storage.KVMPhysicalDisk)3 KVMStoragePool (com.cloud.agent.storage.KVMStoragePool)3 InternalErrorException (com.cloud.exception.InternalErrorException)3 LibvirtException (org.libvirt.LibvirtException)3 VolumeTO (com.cloud.agent.api.to.VolumeTO)2 InterfaceDef (com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef)2 State (com.cloud.vm.VirtualMachine.State)2 IOException (java.io.IOException)2 ConfigurationException (javax.naming.ConfigurationException)2 Connect (org.libvirt.Connect)2 Domain (org.libvirt.Domain)2 StartAnswer (com.cloud.agent.api.StartAnswer)1 StopAnswer (com.cloud.agent.api.StopAnswer)1 StopCommand (com.cloud.agent.api.StopCommand)1 NicTO (com.cloud.agent.api.to.NicTO)1 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)1 LibvirtVMDef (com.cloud.agent.resource.computing.LibvirtVMDef)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 Script (com.cloud.utils.script.Script)1