Search in sources :

Example 1 with MigrateWithStorageAcrossClustersAnswer

use of com.cloud.legacymodel.communication.answer.MigrateWithStorageAcrossClustersAnswer 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 2 with MigrateWithStorageAcrossClustersAnswer

use of com.cloud.legacymodel.communication.answer.MigrateWithStorageAcrossClustersAnswer in project cosmic by MissionCriticalCloud.

the class KVMStorageMotionStrategy method migrateVmWithVolumesAcrossCluster.

@Override
protected Answer migrateVmWithVolumesAcrossCluster(final VMInstanceVO instanceTo, final VirtualMachineTO vmTo, final Host srcHost, final Host destHost, final Map<VolumeInfo, DataStore> volumeToPoolMap) throws AgentUnavailableException {
    try {
        final List<Pair<VolumeTO, StorageFilerTO>> volumeMapping = buildVolumeMapping(volumeToPoolMap);
        final MigrateWithStorageAcrossClustersCommand command = new MigrateWithStorageAcrossClustersCommand(vmTo, volumeMapping, destHost.getPrivateIpAddress());
        final MigrateWithStorageAcrossClustersAnswer answer = (MigrateWithStorageAcrossClustersAnswer) agentMgr.send(srcHost.getId(), command);
        if (answer == null) {
            final String errorMessage = String.format("Error when trying to migrate VM %s (with storage) to host %s", instanceTo, destHost);
            s_logger.error(errorMessage);
            throw new CloudRuntimeException(errorMessage);
        } else if (!answer.getResult()) {
            final String errorMessage = String.format("Error when trying to migrate VM %s (with storage) to host %s => Details: %s", instanceTo, destHost, answer.getDetails());
            s_logger.error(errorMessage);
            throw new CloudRuntimeException(errorMessage);
        } else {
            // Update the volume details after migration.
            updateVolumePathsAfterMigration(volumeToPoolMap, answer.getVolumes());
        }
        return answer;
    } catch (final OperationTimedoutException e) {
        final String errorMessage = String.format("Operation timed out when attempting to migrate VM %s to host %s", instanceTo, destHost);
        s_logger.error(errorMessage, e);
        throw new AgentUnavailableException(errorMessage, srcHost.getId());
    }
}
Also used : OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) MigrateWithStorageAcrossClustersAnswer(com.cloud.legacymodel.communication.answer.MigrateWithStorageAcrossClustersAnswer) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) MigrateWithStorageAcrossClustersCommand(com.cloud.legacymodel.communication.command.MigrateWithStorageAcrossClustersCommand) Pair(com.cloud.legacymodel.utils.Pair)

Aggregations

MigrateWithStorageAcrossClustersAnswer (com.cloud.legacymodel.communication.answer.MigrateWithStorageAcrossClustersAnswer)2 LibvirtStorageVolumeDef (com.cloud.agent.resource.kvm.xml.LibvirtStorageVolumeDef)1 MigrateWithStorageAcrossClustersCommand (com.cloud.legacymodel.communication.command.MigrateWithStorageAcrossClustersCommand)1 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)1 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)1 OperationTimedoutException (com.cloud.legacymodel.exceptions.OperationTimedoutException)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 Pair (com.cloud.legacymodel.utils.Pair)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 XPath (javax.xml.xpath.XPath)1 XPathExpression (javax.xml.xpath.XPathExpression)1 Connect (org.libvirt.Connect)1 Domain (org.libvirt.Domain)1 LibvirtException (org.libvirt.LibvirtException)1 StoragePool (org.libvirt.StoragePool)1 StorageVol (org.libvirt.StorageVol)1