Search in sources :

Example 1 with LibvirtStorageVolumeDef

use of com.cloud.agent.resource.kvm.xml.LibvirtStorageVolumeDef in project cosmic by MissionCriticalCloud.

the class LibvirtStorageAdaptor method createPhysicalDiskByLibVirt.

private KvmPhysicalDisk createPhysicalDiskByLibVirt(final String name, final KvmStoragePool pool, final PhysicalDiskFormat format, final long size) {
    final LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
    final StoragePool virtPool = libvirtPool.getPool();
    final LibvirtStorageVolumeDef.VolumeFormat libvirtformat = LibvirtStorageVolumeDef.VolumeFormat.getFormat(format);
    final String volPath;
    final String volName;
    final long volAllocation;
    final long volCapacity;
    final LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(name, size, libvirtformat, null, null, null);
    this.logger.debug(volDef.toString());
    try {
        final StorageVol vol = virtPool.storageVolCreateXML(volDef.toString(), 0);
        volPath = vol.getPath();
        volName = vol.getName();
        volAllocation = vol.getInfo().allocation;
        volCapacity = vol.getInfo().capacity;
    } catch (final LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
    final KvmPhysicalDisk disk = new KvmPhysicalDisk(volPath, volName, pool);
    disk.setFormat(format);
    disk.setSize(volAllocation);
    disk.setVirtualSize(volCapacity);
    return disk;
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtStorageVolumeDef(com.cloud.agent.resource.kvm.xml.LibvirtStorageVolumeDef) StorageVol(org.libvirt.StorageVol) LibvirtException(org.libvirt.LibvirtException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException)

Example 2 with LibvirtStorageVolumeDef

use of com.cloud.agent.resource.kvm.xml.LibvirtStorageVolumeDef in project cosmic by MissionCriticalCloud.

the class LibvirtMigrateWithStorageAcrossClustersCommandWrapper method execute.

@Override
public Answer execute(final MigrateWithStorageAcrossClustersCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final VirtualMachineTO vm = command.getVirtualMachine();
    try {
        final LibvirtUtilitiesHelper utilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect sourceConnection = utilitiesHelper.getConnectionByVmName(vm.getName());
        final Connect destinationConnection = utilitiesHelper.retrieveQemuConnection("qemu+tcp://" + command.getDestinationIpAddress() + "/system");
        final Domain domain = sourceConnection.domainLookupByName(vm.getName());
        // VIR_DOMAIN_XML_MIGRATABLE = 8
        String domainXml = domain.getXMLDesc(8);
        final XPath xPath = XPathFactory.newInstance().newXPath();
        final XPathExpression expression = xPath.compile("/pool/target/path");
        final List<VolumeObjectTO> volumes = new ArrayList<>();
        for (final Pair<VolumeTO, StorageFilerTO> entry : command.getVolumeMapping()) {
            final VolumeTO volumeTO = entry.first();
            final StorageFilerTO storageFilerTO = entry.second();
            final StoragePool sourcePool = sourceConnection.storagePoolLookupByName(volumeTO.getPoolUuid());
            final String sourcePoolXml = sourcePool.getXMLDesc(0);
            final StoragePool destinationPool = destinationConnection.storagePoolLookupByName(storageFilerTO.getUuid());
            final String destinationPoolXml = destinationPool.getXMLDesc(0);
            final String sourcePath = expression.evaluate(new InputSource(new StringReader(sourcePoolXml)));
            final String sourceLocation = sourcePath + "/" + volumeTO.getPath();
            final String destinationPath = expression.evaluate(new InputSource(new StringReader(destinationPoolXml)));
            final String destinationLocation = destinationPath + "/" + volumeTO.getPath();
            domainXml = domainXml.replace(sourceLocation, destinationLocation);
            final VolumeObjectTO volumeObjectTO = new VolumeObjectTO();
            volumeObjectTO.setId(volumeTO.getId());
            volumeObjectTO.setPath(volumeTO.getPath());
            volumes.add(volumeObjectTO);
            StorageVol storageVol = null;
            try {
                storageVol = destinationPool.storageVolLookupByName(volumeTO.getPath());
            } catch (final LibvirtException e) {
                s_logger.debug("Could not find volume " + volumeTO.getPath() + ": " + e.getMessage());
            }
            if (storageVol == null) {
                LibvirtStorageVolumeDef volumeDef = new LibvirtStorageVolumeDef(volumeTO.getPath(), volumeTO.getSize(), LibvirtStorageVolumeDef.VolumeFormat.getFormat(volumeTO.getImageFormat().name()), null, null, volumeTO.getStorageProvisioningType());
                destinationPool.storageVolCreateXML(volumeDef.toString(), 0);
            }
        }
        // VIR_MIGRATE_LIVE = 1
        // VIR_MIGRATE_UNDEFINE_SOURCE = 16
        // VIR_MIGRATE_NON_SHARED_DISK = 64
        domain.migrate(destinationConnection, 1 | 16 | 64, domainXml, vm.getName(), null, libvirtComputingResource.getMigrateSpeedAcrossCluster());
        return new MigrateWithStorageAcrossClustersAnswer(command, volumes);
    } catch (final Exception e) {
        s_logger.error("Migration of vm " + vm.getName() + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageAcrossClustersAnswer(command, e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) StoragePool(org.libvirt.StoragePool) StorageVol(org.libvirt.StorageVol) LibvirtException(org.libvirt.LibvirtException) LibvirtStorageVolumeDef(com.cloud.agent.resource.kvm.xml.LibvirtStorageVolumeDef) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) StorageFilerTO(com.cloud.legacymodel.to.StorageFilerTO) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) LibvirtException(org.libvirt.LibvirtException) VolumeTO(com.cloud.legacymodel.to.VolumeTO) MigrateWithStorageAcrossClustersAnswer(com.cloud.legacymodel.communication.answer.MigrateWithStorageAcrossClustersAnswer) StringReader(java.io.StringReader) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO) Domain(org.libvirt.Domain)

Example 3 with LibvirtStorageVolumeDef

use of com.cloud.agent.resource.kvm.xml.LibvirtStorageVolumeDef in project cosmic by MissionCriticalCloud.

the class LibvirtStorageAdaptor method getPhysicalDisk.

@Override
public KvmPhysicalDisk getPhysicalDisk(final String volumeUuid, final KvmStoragePool pool) {
    final LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool;
    try {
        final StorageVol vol = getVolume(libvirtPool.getPool(), volumeUuid);
        final KvmPhysicalDisk disk;
        final LibvirtStorageVolumeDef voldef = getStorageVolumeDef(vol);
        disk = new KvmPhysicalDisk(vol.getPath(), vol.getName(), pool);
        disk.setSize(vol.getInfo().allocation);
        disk.setVirtualSize(vol.getInfo().capacity);
        if (pool.getType() == StoragePoolType.RBD) {
            disk.setFormat(PhysicalDiskFormat.RAW);
        } else if (pool.getType() == StoragePoolType.NetworkFilesystem) {
            final QemuImg qemuImg = new QemuImg(60000);
            final Map<String, String> info = qemuImg.info(new QemuImgFile(disk.getPath()));
            disk.setFormat(PhysicalDiskFormat.valueOf(info.get("file_format").toUpperCase()));
        } else if (voldef.getFormat() == null) {
            disk.setFormat(pool.getDefaultFormat());
        } else if (voldef.getFormat() == LibvirtStorageVolumeDef.VolumeFormat.QCOW2) {
            disk.setFormat(PhysicalDiskFormat.QCOW2);
        } else if (voldef.getFormat() == LibvirtStorageVolumeDef.VolumeFormat.RAW) {
            disk.setFormat(PhysicalDiskFormat.RAW);
        }
        return disk;
    } catch (final LibvirtException | QemuImgException e) {
        this.logger.debug("Failed to get physical disk:", e);
        throw new CloudRuntimeException(e.toString());
    }
}
Also used : StorageVol(org.libvirt.StorageVol) LibvirtStorageVolumeDef(com.cloud.agent.resource.kvm.xml.LibvirtStorageVolumeDef) LibvirtException(org.libvirt.LibvirtException) QemuImgFile(com.cloud.agent.resource.kvm.storage.utils.QemuImgFile) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) QemuImgException(com.cloud.agent.resource.kvm.storage.utils.QemuImgException) HashMap(java.util.HashMap) Map(java.util.Map) QemuImg(com.cloud.agent.resource.kvm.storage.utils.QemuImg)

Example 4 with LibvirtStorageVolumeDef

use of com.cloud.agent.resource.kvm.xml.LibvirtStorageVolumeDef in project cosmic by MissionCriticalCloud.

the class ManagedNfsStorageAdaptor method getPhysicalDisk.

/*
     * creates a disk based on the created nfs storage pool using libvirt
     */
@Override
public KvmPhysicalDisk getPhysicalDisk(final String volumeUuid, final KvmStoragePool pool) {
    // now create the volume upon the given storage pool in kvm
    final Connect conn;
    StoragePool virtPool = null;
    try {
        conn = LibvirtConnection.getConnection();
        virtPool = conn.storagePoolLookupByName("/" + volumeUuid);
    } catch (final LibvirtException e1) {
        throw new CloudRuntimeException(e1.toString());
    }
    LibvirtStorageVolumeDef.VolumeFormat libvirtformat = null;
    long volCapacity = 0;
    // check whether the volume is present on the given pool
    StorageVol vol = getVolume(virtPool, volumeUuid);
    try {
        if (vol == null) {
            libvirtformat = LibvirtStorageVolumeDef.VolumeFormat.QCOW2;
            final StoragePoolInfo poolinfo = virtPool.getInfo();
            volCapacity = poolinfo.available;
            final LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(volumeUuid, volCapacity, libvirtformat, null, null, null);
            this.logger.debug(volDef.toString());
            vol = virtPool.storageVolCreateXML(volDef.toString(), 0);
        }
        final KvmPhysicalDisk disk = new KvmPhysicalDisk(vol.getPath(), volumeUuid, pool);
        disk.setFormat(PhysicalDiskFormat.QCOW2);
        disk.setSize(vol.getInfo().allocation);
        disk.setVirtualSize(vol.getInfo().capacity);
        return disk;
    } catch (final LibvirtException e) {
        throw new CloudRuntimeException(e.toString());
    }
}
Also used : StoragePool(org.libvirt.StoragePool) LibvirtException(org.libvirt.LibvirtException) LibvirtStorageVolumeDef(com.cloud.agent.resource.kvm.xml.LibvirtStorageVolumeDef) StorageVol(org.libvirt.StorageVol) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Connect(org.libvirt.Connect) StoragePoolInfo(org.libvirt.StoragePoolInfo)

Aggregations

LibvirtStorageVolumeDef (com.cloud.agent.resource.kvm.xml.LibvirtStorageVolumeDef)4 LibvirtException (org.libvirt.LibvirtException)4 StorageVol (org.libvirt.StorageVol)4 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)3 StoragePool (org.libvirt.StoragePool)3 Connect (org.libvirt.Connect)2 QemuImg (com.cloud.agent.resource.kvm.storage.utils.QemuImg)1 QemuImgException (com.cloud.agent.resource.kvm.storage.utils.QemuImgException)1 QemuImgFile (com.cloud.agent.resource.kvm.storage.utils.QemuImgFile)1 MigrateWithStorageAcrossClustersAnswer (com.cloud.legacymodel.communication.answer.MigrateWithStorageAcrossClustersAnswer)1 StorageFilerTO (com.cloud.legacymodel.to.StorageFilerTO)1 VirtualMachineTO (com.cloud.legacymodel.to.VirtualMachineTO)1 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)1 VolumeTO (com.cloud.legacymodel.to.VolumeTO)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 XPath (javax.xml.xpath.XPath)1 XPathExpression (javax.xml.xpath.XPathExpression)1