Search in sources :

Example 1 with MigrateVolumeAnswer

use of com.cloud.agent.api.storage.MigrateVolumeAnswer in project cloudstack by apache.

the class AncientDataMotionStrategy method migrateVolumeToPool.

protected Answer migrateVolumeToPool(DataObject srcData, DataObject destData) {
    String value = configDao.getValue(Config.MigrateWait.key());
    int waitInterval = NumbersUtil.parseInt(value, Integer.parseInt(Config.MigrateWait.getDefaultValue()));
    VolumeInfo volume = (VolumeInfo) srcData;
    StoragePool destPool = (StoragePool) dataStoreMgr.getDataStore(destData.getDataStore().getId(), DataStoreRole.Primary);
    MigrateVolumeCommand command = new MigrateVolumeCommand(volume.getId(), volume.getPath(), destPool, volume.getAttachedVmName(), volume.getVolumeType(), waitInterval);
    EndPoint ep = selector.select(srcData, StorageAction.MIGRATEVOLUME);
    Answer answer = null;
    if (ep == null) {
        String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
        s_logger.error(errMsg);
        answer = new Answer(command, false, errMsg);
    } else {
        answer = ep.sendMessage(command);
    }
    if (answer == null || !answer.getResult()) {
        throw new CloudRuntimeException("Failed to migrate volume " + volume + " to storage pool " + destPool);
    } else {
        // Update the volume details after migration.
        VolumeVO volumeVo = volDao.findById(volume.getId());
        Long oldPoolId = volume.getPoolId();
        volumeVo.setPath(((MigrateVolumeAnswer) answer).getVolumePath());
        String chainInfo = ((MigrateVolumeAnswer) answer).getVolumeChainInfo();
        if (chainInfo != null) {
            volumeVo.setChainInfo(chainInfo);
        }
        volumeVo.setPodId(destPool.getPodId());
        volumeVo.setPoolId(destPool.getId());
        volumeVo.setLastPoolId(oldPoolId);
        // For SMB, pool credentials are also stored in the uri query string.  We trim the query string
        // part  here to make sure the credentials do not get stored in the db unencrypted.
        String folder = destPool.getPath();
        if (destPool.getPoolType() == StoragePoolType.SMB && folder != null && folder.contains("?")) {
            folder = folder.substring(0, folder.indexOf("?"));
        }
        volumeVo.setFolder(folder);
        volDao.update(volume.getId(), volumeVo);
    }
    return answer;
}
Also used : MigrateVolumeCommand(com.cloud.agent.api.storage.MigrateVolumeCommand) Answer(com.cloud.agent.api.Answer) MigrateVolumeAnswer(com.cloud.agent.api.storage.MigrateVolumeAnswer) StoragePool(com.cloud.storage.StoragePool) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) MigrateVolumeAnswer(com.cloud.agent.api.storage.MigrateVolumeAnswer) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(org.apache.cloudstack.storage.RemoteHostEndPoint) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(org.apache.cloudstack.storage.RemoteHostEndPoint)

Example 2 with MigrateVolumeAnswer

use of com.cloud.agent.api.storage.MigrateVolumeAnswer in project cloudstack by apache.

the class XenServer610MigrateVolumeCommandWrapper method execute.

@Override
public Answer execute(final MigrateVolumeCommand command, final XenServer610Resource xenServer610Resource) {
    final Connection connection = xenServer610Resource.getConnection();
    final String volumeUUID = command.getVolumePath();
    final StorageFilerTO poolTO = command.getPool();
    try {
        final String uuid = poolTO.getUuid();
        final SR destinationPool = xenServer610Resource.getStorageRepository(connection, uuid);
        final VDI srcVolume = xenServer610Resource.getVDIbyUuid(connection, volumeUUID);
        final Map<String, String> other = new HashMap<String, String>();
        other.put("live", "true");
        // Live migrate the vdi across pool.
        final Task task = srcVolume.poolMigrateAsync(connection, destinationPool, other);
        final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
        xenServer610Resource.waitForTask(connection, task, 1000, timeout);
        xenServer610Resource.checkForSuccess(connection, task);
        final VDI dvdi = Types.toVDI(task, connection);
        return new MigrateVolumeAnswer(command, true, null, dvdi.getUuid(connection));
    } catch (final Exception e) {
        final String msg = "Catch Exception " + e.getClass().getName() + " due to " + e.toString();
        s_logger.error(msg, e);
        return new MigrateVolumeAnswer(command, false, msg, null);
    }
}
Also used : Task(com.xensource.xenapi.Task) HashMap(java.util.HashMap) MigrateVolumeAnswer(com.cloud.agent.api.storage.MigrateVolumeAnswer) Connection(com.xensource.xenapi.Connection) VDI(com.xensource.xenapi.VDI) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) SR(com.xensource.xenapi.SR)

Example 3 with MigrateVolumeAnswer

use of com.cloud.agent.api.storage.MigrateVolumeAnswer in project cloudstack by apache.

the class VmwareResource method execute.

private Answer execute(MigrateVolumeCommand cmd) {
    String volumePath = cmd.getVolumePath();
    StorageFilerTO poolTo = cmd.getPool();
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource MigrateVolumeCommand: " + _gson.toJson(cmd));
    }
    String vmName = cmd.getAttachedVmName();
    VirtualMachineMO vmMo = null;
    VmwareHypervisorHost srcHyperHost = null;
    ManagedObjectReference morDs = null;
    ManagedObjectReference morDc = null;
    VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec();
    List<VirtualMachineRelocateSpecDiskLocator> diskLocators = new ArrayList<VirtualMachineRelocateSpecDiskLocator>();
    VirtualMachineRelocateSpecDiskLocator diskLocator = null;
    String tgtDsName = "";
    try {
        srcHyperHost = getHyperHost(getServiceContext());
        morDc = srcHyperHost.getHyperHostDatacenter();
        tgtDsName = poolTo.getUuid();
        // find VM in this datacenter not just in this cluster.
        DatacenterMO dcMo = new DatacenterMO(getServiceContext(), morDc);
        vmMo = dcMo.findVm(vmName);
        if (vmMo == null) {
            String msg = "VM " + vmName + " does not exist in VMware datacenter " + morDc.getValue();
            s_logger.error(msg);
            throw new Exception(msg);
        }
        vmName = vmMo.getName();
        morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(srcHyperHost, tgtDsName);
        if (morDs == null) {
            String msg = "Unable to find the mounted datastore with name: " + tgtDsName + " on source host: " + srcHyperHost.getHyperHostName() + " to execute MigrateVolumeCommand";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        DatastoreMO targetDsMo = new DatastoreMO(srcHyperHost.getContext(), morDs);
        String fullVolumePath = VmwareStorageLayoutHelper.getVmwareDatastorePathFromVmdkFileName(targetDsMo, vmName, volumePath + ".vmdk");
        Pair<VirtualDisk, String> diskInfo = getVirtualDiskInfo(vmMo, volumePath + ".vmdk");
        String vmdkAbsFile = getAbsoluteVmdkFile(diskInfo.first());
        if (vmdkAbsFile != null && !vmdkAbsFile.isEmpty()) {
            vmMo.updateAdapterTypeIfRequired(vmdkAbsFile);
        }
        int diskId = diskInfo.first().getKey();
        diskLocator = new VirtualMachineRelocateSpecDiskLocator();
        diskLocator.setDatastore(morDs);
        diskLocator.setDiskId(diskId);
        diskLocators.add(diskLocator);
        if (cmd.getVolumeType() == Volume.Type.ROOT) {
            relocateSpec.setDatastore(morDs);
            // If a target datastore is provided for the VM, then by default all volumes associated with the VM will be migrated to that target datastore.
            // Hence set the existing datastore as target datastore for volumes that are not to be migrated.
            List<Pair<Integer, ManagedObjectReference>> diskDatastores = vmMo.getAllDiskDatastores();
            for (Pair<Integer, ManagedObjectReference> diskDatastore : diskDatastores) {
                if (diskDatastore.first().intValue() != diskId) {
                    diskLocator = new VirtualMachineRelocateSpecDiskLocator();
                    diskLocator.setDiskId(diskDatastore.first().intValue());
                    diskLocator.setDatastore(diskDatastore.second());
                    diskLocators.add(diskLocator);
                }
            }
        }
        relocateSpec.getDisk().addAll(diskLocators);
        // Change datastore
        if (!vmMo.changeDatastore(relocateSpec)) {
            throw new Exception("Change datastore operation failed during volume migration");
        } else {
            s_logger.debug("Successfully migrated volume " + volumePath + " to target datastore " + tgtDsName);
        }
        // further volume operations on the ROOT volume such as volume snapshot etc. will result in DB inconsistencies.
        if (!vmMo.consolidateVmDisks()) {
            s_logger.warn("VM disk consolidation failed after storage migration.");
        } else {
            s_logger.debug("Successfully consolidated disks of VM " + vmName + ".");
        }
        // Update and return volume path and chain info because that could have changed after migration
        if (!targetDsMo.fileExists(fullVolumePath)) {
            VirtualDisk[] disks = vmMo.getAllDiskDevice();
            for (VirtualDisk disk : disks) if (disk.getKey() == diskId) {
                volumePath = vmMo.getVmdkFileBaseName(disk);
            }
        }
        VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
        String chainInfo = _gson.toJson(diskInfoBuilder.getDiskInfoByBackingFileBaseName(volumePath, poolTo.getUuid().replace("-", "")));
        MigrateVolumeAnswer answer = new MigrateVolumeAnswer(cmd, true, null, volumePath);
        answer.setVolumeChainInfo(chainInfo);
        return answer;
    } catch (Exception e) {
        String msg = "Catch Exception " + e.getClass().getName() + " due to " + e.toString();
        s_logger.error(msg, e);
        return new MigrateVolumeAnswer(cmd, false, msg, null);
    }
}
Also used : VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) ArrayList(java.util.ArrayList) VirtualMachineDiskInfoBuilder(com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VirtualDisk(com.vmware.vim25.VirtualDisk) VirtualMachineRelocateSpec(com.vmware.vim25.VirtualMachineRelocateSpec) MigrateVolumeAnswer(com.cloud.agent.api.storage.MigrateVolumeAnswer) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) VirtualMachineRelocateSpecDiskLocator(com.vmware.vim25.VirtualMachineRelocateSpecDiskLocator) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) Pair(com.cloud.utils.Pair)

Aggregations

MigrateVolumeAnswer (com.cloud.agent.api.storage.MigrateVolumeAnswer)3 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 Answer (com.cloud.agent.api.Answer)1 MigrateVolumeCommand (com.cloud.agent.api.storage.MigrateVolumeCommand)1 CloudException (com.cloud.exception.CloudException)1 InternalErrorException (com.cloud.exception.InternalErrorException)1 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)1 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)1 VirtualMachineDiskInfoBuilder (com.cloud.hypervisor.vmware.mo.VirtualMachineDiskInfoBuilder)1 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)1 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)1 StoragePool (com.cloud.storage.StoragePool)1 VolumeVO (com.cloud.storage.VolumeVO)1 Pair (com.cloud.utils.Pair)1 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1 VirtualDisk (com.vmware.vim25.VirtualDisk)1 VirtualMachineRelocateSpec (com.vmware.vim25.VirtualMachineRelocateSpec)1 VirtualMachineRelocateSpecDiskLocator (com.vmware.vim25.VirtualMachineRelocateSpecDiskLocator)1 Connection (com.xensource.xenapi.Connection)1