Package com.cloud.hypervisor.kvm.resource.LibvirtVMDef

Examples of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef


            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 (pool.getType() == StoragePoolType.RBD) {
                    /*
                            For RBD pools we use the secret mechanism in libvirt.
                            We store the secret under the UUID of the pool, that's why
                            we pass the pool's UUID as the authSecret
                    */
                    disk.defNetworkBasedDisk(physicalDisk.getPath().replace("rbd:", ""), pool.getSourceHost(), pool.getSourcePort(),
                                             pool.getAuthUserName(), pool.getUuid(),
                                             devId, diskBusType, diskProtocol.RBD);
                } else if (pool.getType() == StoragePoolType.CLVM) {
                    disk.defBlockBasedDisk(physicalDisk.getPath(), devId,
                    diskBusType);
                } else {
                    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);
        }
View Full Code Here


    private void createPatchVbd(Connect conn, String vmName, LibvirtVMDef vm,
            VirtualMachineTO vmSpec) throws LibvirtException,
            InternalErrorException {

        List<DiskDef> disks = vm.getDevices().getDisks();
        DiskDef rootDisk = disks.get(0);
        VolumeTO rootVol = getVolume(vmSpec, Volume.Type.ROOT);
        String patchName = vmName + "-patchdisk";
        KVMStoragePool pool = _storagePoolMgr.getStoragePool(rootVol.getPoolUuid());
        String patchDiskPath = pool.getLocalPath() + "/" + patchName;

        List<KVMPhysicalDisk> phyDisks = pool.listPhysicalDisks();
        boolean foundDisk = false;

        for (KVMPhysicalDisk phyDisk : phyDisks) {
            if (phyDisk.getPath().equals(patchDiskPath)) {
                foundDisk = true;
                break;
            }
        }

        if (!foundDisk) {
            s_logger.debug("generating new patch disk for " + vmName + " since none was found");
            KVMPhysicalDisk disk = pool.createPhysicalDisk(patchName, KVMPhysicalDisk.PhysicalDiskFormat.RAW,
                10L * 1024 * 1024);
        } else {
            s_logger.debug("found existing patch disk at " + patchDiskPath + " using it for " + vmName);
        }

        /* Format/create fs on this disk */
        final Script command = new Script(_createvmPath, _timeout, s_logger);
        command.add("-f", patchDiskPath);
        String result = command.execute();
        if (result != null) {
            s_logger.debug("Failed to create data disk: " + result);
            throw new InternalErrorException("Failed to create data disk: "
                    + result);
        }

        /* add patch disk */
        DiskDef patchDisk = new DiskDef();

        if (pool.getType() == StoragePoolType.CLVM) {
            patchDisk.defBlockBasedDisk(patchDiskPath, 1, rootDisk.getBusType());
        } else {
            patchDisk.defFileBasedDisk(patchDiskPath, 1, rootDisk.getBusType(),
            DiskDef.diskFmtType.RAW);
        }
        disks.add(patchDisk);

View Full Code Here

            KVMStoragePool secondaryPool = _storagePoolMgr
                    .getStoragePoolByURI(path);
            KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
            isoPath = isoVol.getPath();

            DiskDef iso = new DiskDef();
            iso.defISODisk(isoPath);
            isoXml = iso.toString();
        } else {
            DiskDef iso = new DiskDef();
            iso.defISODisk(null);
            isoXml = iso.toString();
        }

        List<DiskDef> disks = getDisks(conn, vmName);
        String result = attachOrDetachDevice(conn, true, vmName, isoXml);
        if (result == null && !isAttach) {
View Full Code Here

    protected synchronized String attachOrDetachDisk(Connect conn,
            boolean attach, String vmName, KVMPhysicalDisk attachingDisk,
            int devId) throws LibvirtException, InternalErrorException {
        List<DiskDef> disks = null;
        Domain dm = null;
        DiskDef diskdef = null;
        try {
            if (!attach) {
                dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
                        .getBytes()));
                LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
                String xml = dm.getXMLDesc(0);
                parser.parseDomainXML(xml);
                disks = parser.getDisks();

                for (DiskDef disk : disks) {
                    String file = disk.getDiskPath();
                    if (file != null
                            && file.equalsIgnoreCase(attachingDisk.getPath())) {
                        diskdef = disk;
                        break;
                    }
                }
                if (diskdef == null) {
                    throw new InternalErrorException("disk: "
                            + attachingDisk.getPath()
                            + " is not attached before");
                }
            } else {
                diskdef = new DiskDef();
                if (attachingDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
                    diskdef.defFileBasedDisk(attachingDisk.getPath(), devId,
                            DiskDef.diskBus.VIRTIO, DiskDef.diskFmtType.QCOW2);
                } else if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) {
                    diskdef.defBlockBasedDisk(attachingDisk.getPath(), devId,
                            DiskDef.diskBus.VIRTIO);
                }
            }

            String xml = diskdef.toString();
            return attachOrDetachDevice(conn, attach, vmName, xml);
        } finally {
            if (dm != null) {
                dm.free();
            }
View Full Code Here

    protected synchronized String attachOrDetachDisk(Connect conn,
            boolean attach, String vmName, KVMPhysicalDisk attachingDisk,
            int devId, Long bytesReadRate, Long bytesWriteRate, Long iopsReadRate, Long iopsWriteRate) throws LibvirtException, InternalErrorException {
        List<DiskDef> disks = null;
        Domain dm = null;
        DiskDef diskdef = null;
        KVMStoragePool attachingPool = attachingDisk.getPool();
        try {
            if (!attach) {
                dm = conn.domainLookupByName(vmName);
                LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
                String xml = dm.getXMLDesc(0);
                parser.parseDomainXML(xml);
                disks = parser.getDisks();

                for (DiskDef disk : disks) {
                    String file = disk.getDiskPath();
                    if (file != null
                            && file.equalsIgnoreCase(attachingDisk.getPath())) {
                        diskdef = disk;
                        break;
                    }
                }
                if (diskdef == null) {
                    throw new InternalErrorException("disk: "
                            + attachingDisk.getPath()
                            + " is not attached before");
                }
            } else {
                diskdef = new DiskDef();
                if (attachingPool.getType() == StoragePoolType.RBD) {
                    diskdef.defNetworkBasedDisk(attachingDisk.getPath(),
                            attachingPool.getSourceHost(), attachingPool.getSourcePort(),
                            attachingPool.getAuthUserName(), attachingPool.getUuid(), devId,
                            DiskDef.diskBus.VIRTIO, diskProtocol.RBD);
                } else if (attachingDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
                    diskdef.defFileBasedDisk(attachingDisk.getPath(), devId,
                            DiskDef.diskBus.VIRTIO, DiskDef.diskFmtType.QCOW2);
                } else if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) {
                    diskdef.defBlockBasedDisk(attachingDisk.getPath(), devId,
                            DiskDef.diskBus.VIRTIO);
                }
                if ((bytesReadRate != null) && (bytesReadRate > 0))
                    diskdef.setBytesReadRate(bytesReadRate);
                if ((bytesWriteRate != null) && (bytesWriteRate > 0))
                    diskdef.setBytesWriteRate(bytesWriteRate);
                if ((iopsReadRate != null) && (iopsReadRate > 0))
                    diskdef.setIopsReadRate(iopsReadRate);
                if ((iopsWriteRate != null) && (iopsWriteRate > 0))
                    diskdef.setIopsWriteRate(iopsWriteRate);
            }

            String xml = diskdef.toString();
            return attachOrDetachDevice(conn, attach, vmName, xml);
        } finally {
            if (dm != null) {
                dm.free();
            }
View Full Code Here

            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 = volume.getDiskSeq().intValue();

                if (pool.getType() == StoragePoolType.RBD) {
                    /*
                            For RBD pools we use the secret mechanism in libvirt.
                            We store the secret under the UUID of the pool, that's why
                            we pass the pool's UUID as the authSecret
                     */
                    disk.defNetworkBasedDisk(physicalDisk.getPath().replace("rbd:", ""), pool.getSourceHost(), pool.getSourcePort(),
                            pool.getAuthUserName(), pool.getUuid(),
                            devId, diskBusType, diskProtocol.RBD);
                } else if (pool.getType() == StoragePoolType.CLVM) {
                    disk.defBlockBasedDisk(physicalDisk.getPath(), devId,
                            diskBusType);
                } else {
                    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);
                    }

                }

            }

            if (data instanceof VolumeObjectTO) {
                VolumeObjectTO volumeObjectTO = (VolumeObjectTO)data;
                if ((volumeObjectTO.getBytesReadRate() != null) && (volumeObjectTO.getBytesReadRate()  > 0))
                    disk.setBytesReadRate(volumeObjectTO.getBytesReadRate());
                if ((volumeObjectTO.getBytesWriteRate() != null) && (volumeObjectTO.getBytesWriteRate() > 0))
                    disk.setBytesWriteRate(volumeObjectTO.getBytesWriteRate());
                if ((volumeObjectTO.getIopsReadRate() != null) && (volumeObjectTO.getIopsReadRate() > 0))
                    disk.setIopsReadRate(volumeObjectTO.getIopsReadRate());
                if ((volumeObjectTO.getIopsWriteRate() != null) && (volumeObjectTO.getIopsWriteRate() > 0))
                    disk.setIopsWriteRate(volumeObjectTO.getIopsWriteRate());
            }
            vm.getDevices().addDevice(disk);
        }

        if (vmSpec.getType() != VirtualMachine.Type.User) {
            if (_sysvmISOPath != null) {
                DiskDef iso = new DiskDef();
                iso.defISODisk(_sysvmISOPath);
                vm.getDevices().addDevice(iso);
            }
        }

        // For LXC, find and add the root filesystem
View Full Code Here

            KVMStoragePool secondaryPool = _storagePoolMgr.getStoragePoolByURI(
                    path);
            KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
            isoPath = isoVol.getPath();

            DiskDef iso = new DiskDef();
            iso.defISODisk(isoPath);
            isoXml = iso.toString();
        } else {
            DiskDef iso = new DiskDef();
            iso.defISODisk(null);
            isoXml = iso.toString();
        }

        List<DiskDef> disks = getDisks(conn, vmName);
        String result = attachOrDetachDevice(conn, true, vmName, isoXml);
        if (result == null && !isAttach) {
View Full Code Here

                    "devices").item(0);
            NodeList disks = devices.getElementsByTagName("disk");
            for (int i = 0; i < disks.getLength(); i++) {
                Element disk = (Element) disks.item(i);
                String type = disk.getAttribute("type");
                DiskDef def = new DiskDef();
                if (type.equalsIgnoreCase("network")) {
                    String diskFmtType = getAttrValue("driver", "type", disk);
                    String diskPath = getAttrValue("source", "name", disk);
                    String protocol = getAttrValue("source", "protocol", disk);
                    String authUserName = getAttrValue("auth", "username", disk);
                    String poolUuid = getAttrValue("secret", "uuid", disk);
                    String host = getAttrValue("host", "name", disk);
                    int port = Integer.parseInt(getAttrValue("host", "port", disk));
                    String diskLabel = getAttrValue("target", "dev", disk);
                    String bus = getAttrValue("target", "bus", disk);
                    def.defNetworkBasedDisk(diskPath, host, port, authUserName, poolUuid, diskLabel,
                                            DiskDef.diskBus.valueOf(bus.toUpperCase()), DiskDef.diskProtocol.valueOf(protocol.toUpperCase()));
                } else {
                    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 device = disk.getAttribute("device");

                    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()));
                    }
                }

                NodeList iotune = disk.getElementsByTagName("iotune");
                if ((iotune != null) && (iotune.getLength() !=0)) {
                    String bytesReadRateStr = getTagValue("read_bytes_sec", (Element)iotune.item(0));
                    if (bytesReadRateStr != null) {
                        Long bytesReadRate = Long.parseLong(bytesReadRateStr);
                        def.setBytesReadRate(bytesReadRate);
                    }
                    String bytesWriteRateStr = getTagValue("write_bytes_sec", (Element)iotune.item(0));
                    if (bytesWriteRateStr != null) {
                        Long bytesWriteRate = Long.parseLong(bytesWriteRateStr);
                        def.setBytesWriteRate(bytesWriteRate);
                    }
                    String iopsReadRateStr = getTagValue("read_iops_sec", (Element)iotune.item(0));
                    if (iopsReadRateStr != null) {
                        Long iopsReadRate = Long.parseLong(iopsReadRateStr);
                        def.setIopsReadRate(iopsReadRate);
                    }
                    String iopsWriteRateStr = getTagValue("write_iops_sec", (Element)iotune.item(0));
                    if (iopsWriteRateStr != null) {
                        Long iopsWriteRate = Long.parseLong(iopsWriteRateStr);
                        def.setIopsWriteRate(iopsWriteRate);
                    }
                }

                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();
                NodeList bandwidth = nic.getElementsByTagName("bandwidth");
                Integer networkRateKBps = 0;
                if ((bandwidth != null) && (bandwidth.getLength() !=0)) {
                    Integer inbound = Integer.valueOf(getAttrValue("inbound", "average", (Element)bandwidth.item(0)));
                    Integer outbound = Integer.valueOf(getAttrValue("outbound", "average", (Element)bandwidth.item(0)));
                    if (inbound == outbound)
                        networkRateKBps = inbound;
                }
                if (type.equalsIgnoreCase("network")) {
                    String network = getAttrValue("source", "network", nic);
                    def.defPrivateNet(network, dev, mac,
                            nicModel.valueOf(model.toUpperCase()), networkRateKBps);
                } else if (type.equalsIgnoreCase("bridge")) {
                    String bridge = getAttrValue("source", "bridge", nic);
                    def.defBridgeNet(bridge, dev, mac,
                            nicModel.valueOf(model.toUpperCase()), networkRateKBps);
                } else if (type.equalsIgnoreCase("ethernet"))  {
                    String scriptPath = getAttrValue("script", "path", nic);
                    def.defEthernet(dev, mac, nicModel.valueOf(model.toUpperCase()), scriptPath, networkRateKBps);
                }
                interfaces.add(def);
            }

            Element graphic = (Element) devices
View Full Code Here

            String name = isoPath.substring(index + 1);
            KVMStoragePool secondaryPool = storagePoolMgr.getStoragePoolByURI(path);
            KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
            isoPath = isoVol.getPath();

            DiskDef iso = new DiskDef();
            iso.defISODisk(isoPath);
            isoXml = iso.toString();
        } else {
            DiskDef iso = new DiskDef();
            iso.defISODisk(null);
            isoXml = iso.toString();
        }

        List<DiskDef> disks = this.resource.getDisks(conn, vmName);
        String result = attachOrDetachDevice(conn, true, vmName, isoXml);
        if (result == null && !isAttach) {
View Full Code Here

    protected synchronized String attachOrDetachDisk(Connect conn, boolean attach, String vmName,
            KVMPhysicalDisk attachingDisk, int devId) throws LibvirtException, InternalErrorException {
        List<DiskDef> disks = null;
        Domain dm = null;
        DiskDef diskdef = null;
        KVMStoragePool attachingPool = attachingDisk.getPool();
        try {
            if (!attach) {
                dm = conn.domainLookupByName(vmName);
                LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
                String xml = dm.getXMLDesc(0);
                parser.parseDomainXML(xml);
                disks = parser.getDisks();

                for (DiskDef disk : disks) {
                    String file = disk.getDiskPath();
                    if (file != null && file.equalsIgnoreCase(attachingDisk.getPath())) {
                        diskdef = disk;
                        break;
                    }
                }
                if (diskdef == null) {
                    throw new InternalErrorException("disk: " + attachingDisk.getPath() + " is not attached before");
                }
            } else {
                diskdef = new DiskDef();
                if (attachingPool.getType() == StoragePoolType.RBD) {
                    diskdef.defNetworkBasedDisk(attachingDisk.getPath(),
                            attachingPool.getSourceHost(), attachingPool.getSourcePort(),
                            attachingPool.getAuthUserName(), attachingPool.getUuid(), devId,
                            DiskDef.diskBus.VIRTIO, diskProtocol.RBD);
                } else if (attachingDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
                    diskdef.defFileBasedDisk(attachingDisk.getPath(), devId,
                            DiskDef.diskBus.VIRTIO, DiskDef.diskFmtType.QCOW2);
                } else if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) {
                    diskdef.defBlockBasedDisk(attachingDisk.getPath(), devId,
                            DiskDef.diskBus.VIRTIO);
                }
            }

            String xml = diskdef.toString();
            return attachOrDetachDevice(conn, attach, vmName, xml);
        } finally {
            if (dm != null) {
                dm.free();
            }
View Full Code Here

TOP

Related Classes of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.