Search in sources :

Example 6 with MigrateDiskInfo

use of com.cloud.agent.api.MigrateCommand.MigrateDiskInfo in project cloudstack by apache.

the class StorageSystemDataMotionStrategy method copyAsync.

/**
 * For each disk to migrate:
 * <ul>
 *  <li>Create a volume on the target storage system.</li>
 *  <li>Make the newly created volume accessible to the target KVM host.</li>
 *  <li>Send a command to the target KVM host to connect to the newly created volume.</li>
 *  <li>Send a command to the source KVM host to migrate the VM and its storage.</li>
 * </ul>
 */
@Override
public void copyAsync(Map<VolumeInfo, DataStore> volumeDataStoreMap, VirtualMachineTO vmTO, Host srcHost, Host destHost, AsyncCompletionCallback<CopyCommandResult> callback) {
    String errMsg = null;
    try {
        if (srcHost.getHypervisorType() != HypervisorType.KVM) {
            throw new CloudRuntimeException("Invalid hypervisor type (only KVM supported for this operation at the time being)");
        }
        verifyLiveMigrationForKVM(volumeDataStoreMap, destHost);
        VMInstanceVO vmInstance = _vmDao.findById(vmTO.getId());
        vmTO.setState(vmInstance.getState());
        List<MigrateDiskInfo> migrateDiskInfoList = new ArrayList<MigrateDiskInfo>();
        Map<String, MigrateCommand.MigrateDiskInfo> migrateStorage = new HashMap<>();
        Map<VolumeInfo, VolumeInfo> srcVolumeInfoToDestVolumeInfo = new HashMap<>();
        boolean managedStorageDestination = false;
        for (Map.Entry<VolumeInfo, DataStore> entry : volumeDataStoreMap.entrySet()) {
            VolumeInfo srcVolumeInfo = entry.getKey();
            DataStore destDataStore = entry.getValue();
            VolumeVO srcVolume = _volumeDao.findById(srcVolumeInfo.getId());
            StoragePoolVO destStoragePool = _storagePoolDao.findById(destDataStore.getId());
            StoragePoolVO sourceStoragePool = _storagePoolDao.findById(srcVolumeInfo.getPoolId());
            // do not initiate migration for the same PowerFlex/ScaleIO pool
            if (sourceStoragePool.getId() == destStoragePool.getId() && sourceStoragePool.getPoolType() == Storage.StoragePoolType.PowerFlex) {
                continue;
            }
            if (!shouldMigrateVolume(sourceStoragePool, destHost, destStoragePool)) {
                continue;
            }
            if (srcVolumeInfo.getTemplateId() != null) {
                LOGGER.debug(String.format("Copying template [%s] of volume [%s] from source storage pool [%s] to target storage pool [%s].", srcVolumeInfo.getTemplateId(), srcVolumeInfo.getId(), sourceStoragePool.getId(), destStoragePool.getId()));
                copyTemplateToTargetFilesystemStorageIfNeeded(srcVolumeInfo, sourceStoragePool, destDataStore, destStoragePool, destHost);
            } else {
                LOGGER.debug(String.format("Skipping copy template from source storage pool [%s] to target storage pool [%s] before migration due to volume [%s] does not have a template.", sourceStoragePool.getId(), destStoragePool.getId(), srcVolumeInfo.getId()));
            }
            VolumeVO destVolume = duplicateVolumeOnAnotherStorage(srcVolume, destStoragePool);
            VolumeInfo destVolumeInfo = _volumeDataFactory.getVolume(destVolume.getId(), destDataStore);
            // move the volume from Allocated to Creating
            destVolumeInfo.processEvent(Event.MigrationCopyRequested);
            // move the volume from Creating to Ready
            destVolumeInfo.processEvent(Event.MigrationCopySucceeded);
            // move the volume from Ready to Migrating
            destVolumeInfo.processEvent(Event.MigrationRequested);
            setVolumeMigrationOptions(srcVolumeInfo, destVolumeInfo, vmTO, srcHost, destStoragePool);
            // create a volume on the destination storage
            destDataStore.getDriver().createAsync(destDataStore, destVolumeInfo, null);
            managedStorageDestination = destStoragePool.isManaged();
            String volumeIdentifier = managedStorageDestination ? destVolumeInfo.get_iScsiName() : destVolumeInfo.getUuid();
            destVolume = _volumeDao.findById(destVolume.getId());
            destVolume.setPath(volumeIdentifier);
            setVolumePath(destVolume);
            _volumeDao.update(destVolume.getId(), destVolume);
            postVolumeCreationActions(srcVolumeInfo, destVolumeInfo, vmTO, srcHost);
            destVolumeInfo = _volumeDataFactory.getVolume(destVolume.getId(), destDataStore);
            handleQualityOfServiceForVolumeMigration(destVolumeInfo, PrimaryDataStoreDriver.QualityOfServiceState.MIGRATION);
            _volumeService.grantAccess(destVolumeInfo, destHost, destDataStore);
            String destPath = generateDestPath(destHost, destStoragePool, destVolumeInfo);
            MigrateCommand.MigrateDiskInfo migrateDiskInfo;
            boolean isNonManagedNfsToNfsOrSharedMountPointToNfs = supportStoragePoolType(sourceStoragePool.getPoolType()) && destStoragePool.getPoolType() == StoragePoolType.NetworkFilesystem && !managedStorageDestination;
            if (isNonManagedNfsToNfsOrSharedMountPointToNfs) {
                migrateDiskInfo = new MigrateCommand.MigrateDiskInfo(srcVolumeInfo.getPath(), MigrateCommand.MigrateDiskInfo.DiskType.FILE, MigrateCommand.MigrateDiskInfo.DriverType.QCOW2, MigrateCommand.MigrateDiskInfo.Source.FILE, connectHostToVolume(destHost, destVolumeInfo.getPoolId(), volumeIdentifier));
            } else {
                migrateDiskInfo = configureMigrateDiskInfo(srcVolumeInfo, destPath);
                migrateDiskInfo.setSourceDiskOnStorageFileSystem(isStoragePoolTypeOfFile(sourceStoragePool));
                migrateDiskInfoList.add(migrateDiskInfo);
            }
            migrateStorage.put(srcVolumeInfo.getPath(), migrateDiskInfo);
            srcVolumeInfoToDestVolumeInfo.put(srcVolumeInfo, destVolumeInfo);
        }
        PrepareForMigrationCommand pfmc = new PrepareForMigrationCommand(vmTO);
        try {
            Answer pfma = agentManager.send(destHost.getId(), pfmc);
            if (pfma == null || !pfma.getResult()) {
                String details = pfma != null ? pfma.getDetails() : "null answer returned";
                String msg = "Unable to prepare for migration due to the following: " + details;
                throw new AgentUnavailableException(msg, destHost.getId());
            }
        } catch (final OperationTimedoutException e) {
            throw new AgentUnavailableException("Operation timed out", destHost.getId());
        }
        VMInstanceVO vm = _vmDao.findById(vmTO.getId());
        boolean isWindows = _guestOsCategoryDao.findById(_guestOsDao.findById(vm.getGuestOSId()).getCategoryId()).getName().equalsIgnoreCase("Windows");
        boolean migrateNonSharedInc = isSourceAndDestinationPoolTypeOfNfs(volumeDataStoreMap);
        MigrateCommand migrateCommand = new MigrateCommand(vmTO.getName(), destHost.getPrivateIpAddress(), isWindows, vmTO, true);
        migrateCommand.setWait(StorageManager.KvmStorageOnlineMigrationWait.value());
        migrateCommand.setMigrateStorage(migrateStorage);
        migrateCommand.setMigrateDiskInfoList(migrateDiskInfoList);
        migrateCommand.setMigrateStorageManaged(managedStorageDestination);
        migrateCommand.setMigrateNonSharedInc(migrateNonSharedInc);
        boolean kvmAutoConvergence = StorageManager.KvmAutoConvergence.value();
        migrateCommand.setAutoConvergence(kvmAutoConvergence);
        MigrateAnswer migrateAnswer = (MigrateAnswer) agentManager.send(srcHost.getId(), migrateCommand);
        boolean success = migrateAnswer != null && migrateAnswer.getResult();
        handlePostMigration(success, srcVolumeInfoToDestVolumeInfo, vmTO, destHost);
        if (migrateAnswer == null) {
            throw new CloudRuntimeException("Unable to get an answer to the migrate command");
        }
        if (!migrateAnswer.getResult()) {
            errMsg = migrateAnswer.getDetails();
            throw new CloudRuntimeException(errMsg);
        }
    } catch (AgentUnavailableException | OperationTimedoutException | CloudRuntimeException ex) {
        String volumesAndStorages = volumeDataStoreMap.entrySet().stream().map(entry -> formatEntryOfVolumesAndStoragesAsJsonToDisplayOnLog(entry)).collect(Collectors.joining(","));
        errMsg = String.format("Copy volume(s) to storage(s) [%s] and VM to host [%s] failed in StorageSystemDataMotionStrategy.copyAsync. Error message: [%s].", volumesAndStorages, formatMigrationElementsAsJsonToDisplayOnLog("vm", vmTO.getId(), srcHost.getId(), destHost.getId()), ex.getMessage());
        LOGGER.error(errMsg, ex);
        throw new CloudRuntimeException(errMsg);
    } finally {
        CopyCmdAnswer copyCmdAnswer = new CopyCmdAnswer(errMsg);
        CopyCommandResult result = new CopyCommandResult(null, copyCmdAnswer);
        result.setResult(errMsg);
        callback.complete(result);
    }
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) MigrateCommand(com.cloud.agent.api.MigrateCommand) MigrateAnswer(com.cloud.agent.api.MigrateAnswer) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) MigrateDiskInfo(com.cloud.agent.api.MigrateCommand.MigrateDiskInfo) VMInstanceVO(com.cloud.vm.VMInstanceVO) MigrateDiskInfo(com.cloud.agent.api.MigrateCommand.MigrateDiskInfo) ModifyTargetsAnswer(com.cloud.agent.api.ModifyTargetsAnswer) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) MigrateVolumeAnswer(com.cloud.agent.api.storage.MigrateVolumeAnswer) MigrateAnswer(com.cloud.agent.api.MigrateAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) Answer(com.cloud.agent.api.Answer) CopyVolumeAnswer(com.cloud.agent.api.storage.CopyVolumeAnswer) PrepareForMigrationCommand(com.cloud.agent.api.PrepareForMigrationCommand) Map(java.util.Map) HashMap(java.util.HashMap) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 7 with MigrateDiskInfo

use of com.cloud.agent.api.MigrateCommand.MigrateDiskInfo in project cloudstack by apache.

the class LibvirtMigrateCommandWrapper method execute.

@Override
public Answer execute(final MigrateCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final String vmName = command.getVmName();
    final Map<String, Boolean> vlanToPersistenceMap = command.getVlanToPersistenceMap();
    final String destinationUri = createMigrationURI(command.getDestinationIp(), libvirtComputingResource);
    final List<MigrateDiskInfo> migrateDiskInfoList = command.getMigrateDiskInfoList();
    String result = null;
    List<InterfaceDef> ifaces = null;
    List<DiskDef> disks;
    Domain dm = null;
    Connect dconn = null;
    Domain destDomain = null;
    Connect conn = null;
    String xmlDesc = null;
    List<Ternary<String, Boolean, String>> vmsnapshots = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
        ifaces = libvirtComputingResource.getInterfaces(conn, vmName);
        disks = libvirtComputingResource.getDisks(conn, vmName);
        VirtualMachineTO to = command.getVirtualMachine();
        dm = conn.domainLookupByName(vmName);
        /*
                We replace the private IP address with the address of the destination host.
                This is because the VNC listens on the private IP address of the hypervisor,
                but that address is of course different on the target host.

                MigrateCommand.getDestinationIp() returns the private IP address of the target
                hypervisor. So it's safe to use.

                The Domain.migrate method from libvirt supports passing a different XML
                description for the instance to be used on the target host.

                This is supported by libvirt-java from version 0.50.0

                CVE-2015-3252: Get XML with sensitive information suitable for migration by using
                               VIR_DOMAIN_XML_MIGRATABLE flag (value = 8)
                               https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainXMLFlags

                               Use VIR_DOMAIN_XML_SECURE (value = 1) prior to v1.0.0.
             */
        // 1000000 equals v1.0.0
        final int xmlFlag = conn.getLibVirVersion() >= 1000000 ? 8 : 1;
        final String target = command.getDestinationIp();
        xmlDesc = dm.getXMLDesc(xmlFlag);
        xmlDesc = replaceIpForVNCInDescFile(xmlDesc, target);
        String oldIsoVolumePath = getOldVolumePath(disks, vmName);
        String newIsoVolumePath = getNewVolumePathIfDatastoreHasChanged(libvirtComputingResource, conn, to);
        if (newIsoVolumePath != null && !newIsoVolumePath.equals(oldIsoVolumePath)) {
            s_logger.debug("Editing mount path");
            xmlDesc = replaceDiskSourceFile(xmlDesc, newIsoVolumePath, vmName);
        }
        // delete the metadata of vm snapshots before migration
        vmsnapshots = libvirtComputingResource.cleanVMSnapshotMetadata(dm);
        // Verify Format of backing file
        for (DiskDef disk : disks) {
            if (disk.getDeviceType() == DiskDef.DeviceType.DISK && disk.getDiskFormatType() == DiskDef.DiskFmtType.QCOW2) {
                libvirtComputingResource.setBackingFileFormat(disk.getDiskPath());
            }
        }
        Map<String, MigrateCommand.MigrateDiskInfo> mapMigrateStorage = command.getMigrateStorage();
        // migrateStorage is declared as final because the replaceStorage method may mutate mapMigrateStorage, but
        // migrateStorage's value should always only be associated with the initial state of mapMigrateStorage.
        final boolean migrateStorage = MapUtils.isNotEmpty(mapMigrateStorage);
        final boolean migrateStorageManaged = command.isMigrateStorageManaged();
        if (migrateStorage) {
            xmlDesc = replaceStorage(xmlDesc, mapMigrateStorage, migrateStorageManaged);
        }
        Map<String, DpdkTO> dpdkPortsMapping = command.getDpdkInterfaceMapping();
        if (MapUtils.isNotEmpty(dpdkPortsMapping)) {
            xmlDesc = replaceDpdkInterfaces(xmlDesc, dpdkPortsMapping);
        }
        dconn = libvirtUtilitiesHelper.retrieveQemuConnection(destinationUri);
        if (to.getType() == VirtualMachine.Type.User) {
            libvirtComputingResource.detachAndAttachConfigDriveISO(conn, vmName);
        }
        // run migration in thread so we can monitor it
        s_logger.info("Live migration of instance " + vmName + " initiated to destination host: " + dconn.getURI());
        final ExecutorService executor = Executors.newFixedThreadPool(1);
        boolean migrateNonSharedInc = command.isMigrateNonSharedInc() && !migrateStorageManaged;
        final Callable<Domain> worker = new MigrateKVMAsync(libvirtComputingResource, dm, dconn, xmlDesc, migrateStorage, migrateNonSharedInc, command.isAutoConvergence(), vmName, command.getDestinationIp());
        final Future<Domain> migrateThread = executor.submit(worker);
        executor.shutdown();
        long sleeptime = 0;
        while (!executor.isTerminated()) {
            Thread.sleep(100);
            sleeptime += 100;
            if (sleeptime == 1000) {
                // wait 1s before attempting to set downtime on migration, since I don't know of a VIR_DOMAIN_MIGRATING state
                final int migrateDowntime = libvirtComputingResource.getMigrateDowntime();
                if (migrateDowntime > 0) {
                    try {
                        final int setDowntime = dm.migrateSetMaxDowntime(migrateDowntime);
                        if (setDowntime == 0) {
                            s_logger.debug("Set max downtime for migration of " + vmName + " to " + String.valueOf(migrateDowntime) + "ms");
                        }
                    } catch (final LibvirtException e) {
                        s_logger.debug("Failed to set max downtime for migration, perhaps migration completed? Error: " + e.getMessage());
                    }
                }
            }
            if (sleeptime % 1000 == 0) {
                s_logger.info("Waiting for migration of " + vmName + " to complete, waited " + sleeptime + "ms");
            }
            // abort the vm migration if the job is executed more than vm.migrate.wait
            final int migrateWait = libvirtComputingResource.getMigrateWait();
            if (migrateWait > 0 && sleeptime > migrateWait * 1000) {
                DomainState state = null;
                try {
                    state = dm.getInfo().state;
                } catch (final LibvirtException e) {
                    s_logger.info("Couldn't get VM domain state after " + sleeptime + "ms: " + e.getMessage());
                }
                if (state != null && state == DomainState.VIR_DOMAIN_RUNNING) {
                    try {
                        DomainJobInfo job = dm.getJobInfo();
                        s_logger.info("Aborting " + vmName + " domain job: " + job);
                        dm.abortJob();
                        result = String.format("Migration of VM %s was cancelled by cloudstack due to time out after %d seconds", vmName, migrateWait);
                        s_logger.debug(result);
                        break;
                    } catch (final LibvirtException e) {
                        s_logger.info("Failed to abort the vm migration job of vm " + vmName + " : " + e.getMessage());
                    }
                }
            }
            // pause vm if we meet the vm.migrate.pauseafter threshold and not already paused
            final int migratePauseAfter = libvirtComputingResource.getMigratePauseAfter();
            if (migratePauseAfter > 0 && sleeptime > migratePauseAfter) {
                DomainState state = null;
                try {
                    state = dm.getInfo().state;
                } catch (final LibvirtException e) {
                    s_logger.info("Couldn't get VM domain state after " + sleeptime + "ms: " + e.getMessage());
                }
                if (state != null && state == DomainState.VIR_DOMAIN_RUNNING) {
                    try {
                        s_logger.info("Pausing VM " + vmName + " due to property vm.migrate.pauseafter setting to " + migratePauseAfter + "ms to complete migration");
                        dm.suspend();
                    } catch (final LibvirtException e) {
                        // pause could be racy if it attempts to pause right when vm is finished, simply warn
                        s_logger.info("Failed to pause vm " + vmName + " : " + e.getMessage());
                    }
                }
            }
        }
        s_logger.info("Migration thread for " + vmName + " is done");
        destDomain = migrateThread.get(AgentPropertiesFileHandler.getPropertyValue(AgentProperties.VM_MIGRATE_DOMAIN_RETRIEVE_TIMEOUT), TimeUnit.SECONDS);
        if (destDomain != null) {
            deleteOrDisconnectDisksOnSourcePool(libvirtComputingResource, migrateDiskInfoList, disks);
        }
    } catch (final LibvirtException e) {
        s_logger.debug("Can't migrate domain: " + e.getMessage());
        result = e.getMessage();
        if (result.startsWith("unable to connect to server") && result.endsWith("refused")) {
            result = String.format("Migration was refused connection to destination: %s. Please check libvirt configuration compatibility and firewall rules on the source and destination hosts.", destinationUri);
        }
    } catch (final InterruptedException | ExecutionException | TimeoutException | IOException | ParserConfigurationException | SAXException | TransformerException | URISyntaxException e) {
        s_logger.debug(String.format("%s : %s", e.getClass().getSimpleName(), e.getMessage()));
        if (result == null) {
            result = "Exception during migrate: " + e.getMessage();
        }
    } finally {
        try {
            if (dm != null && result != null) {
                // restore vm snapshots in case of failed migration
                if (vmsnapshots != null) {
                    libvirtComputingResource.restoreVMSnapshotMetadata(dm, vmName, vmsnapshots);
                }
            }
            if (dm != null) {
                if (dm.isPersistent() == 1) {
                    dm.undefine();
                }
                dm.free();
            }
            if (dconn != null) {
                dconn.close();
            }
            if (destDomain != null) {
                destDomain.free();
            }
        } catch (final LibvirtException e) {
            s_logger.trace("Ignoring libvirt error.", e);
        }
    }
    if (result != null) {
    } else {
        libvirtComputingResource.destroyNetworkRulesForVM(conn, vmName);
        for (final InterfaceDef iface : ifaces) {
            String vlanId = libvirtComputingResource.getVlanIdFromBridgeName(iface.getBrName());
            // We don't know which "traffic type" is associated with
            // each interface at this point, so inform all vif drivers
            final List<VifDriver> allVifDrivers = libvirtComputingResource.getAllVifDrivers();
            for (final VifDriver vifDriver : allVifDrivers) {
                vifDriver.unplug(iface, libvirtComputingResource.shouldDeleteBridge(vlanToPersistenceMap, vlanId));
            }
        }
    }
    return new MigrateAnswer(command, result == null, result, null);
}
Also used : LibvirtException(org.libvirt.LibvirtException) URISyntaxException(java.net.URISyntaxException) DpdkTO(com.cloud.agent.api.to.DpdkTO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) SAXException(org.xml.sax.SAXException) MigrateAnswer(com.cloud.agent.api.MigrateAnswer) DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) MigrateKVMAsync(com.cloud.hypervisor.kvm.resource.MigrateKVMAsync) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ExecutionException(java.util.concurrent.ExecutionException) DomainJobInfo(org.libvirt.DomainJobInfo) TransformerException(javax.xml.transform.TransformerException) TimeoutException(java.util.concurrent.TimeoutException) Ternary(com.cloud.utils.Ternary) Connect(org.libvirt.Connect) IOException(java.io.IOException) MigrateDiskInfo(com.cloud.agent.api.MigrateCommand.MigrateDiskInfo) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) DomainState(org.libvirt.DomainInfo.DomainState) ExecutorService(java.util.concurrent.ExecutorService) Domain(org.libvirt.Domain)

Aggregations

MigrateDiskInfo (com.cloud.agent.api.MigrateCommand.MigrateDiskInfo)7 DiskDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef)3 ArrayList (java.util.ArrayList)3 MigrateAnswer (com.cloud.agent.api.MigrateAnswer)2 MigrateCommand (com.cloud.agent.api.MigrateCommand)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Answer (com.cloud.agent.api.Answer)1 ModifyTargetsAnswer (com.cloud.agent.api.ModifyTargetsAnswer)1 PrepareForMigrationCommand (com.cloud.agent.api.PrepareForMigrationCommand)1 CopyVolumeAnswer (com.cloud.agent.api.storage.CopyVolumeAnswer)1 MigrateVolumeAnswer (com.cloud.agent.api.storage.MigrateVolumeAnswer)1 DpdkTO (com.cloud.agent.api.to.DpdkTO)1 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)1