Search in sources :

Example 1 with SnapshotAndCopyAnswer

use of org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer in project cloudstack by apache.

the class XenServerStorageProcessor method snapshotAndCopy.

// if the source SR needs to be attached to, do so
// take a snapshot of the source VDI (on the source SR)
// create an iSCSI SR based on the new back-end volume
// copy the snapshot to the new SR
// delete the snapshot
// detach the new SR
// if we needed to perform an attach to the source SR, detach from it
@Override
public SnapshotAndCopyAnswer snapshotAndCopy(final SnapshotAndCopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    try {
        SR sourceSr = null;
        final Map<String, String> sourceDetails = cmd.getSourceDetails();
        if (sourceDetails != null && sourceDetails.keySet().size() > 0) {
            final String iScsiName = sourceDetails.get(DiskTO.IQN);
            final String storageHost = sourceDetails.get(DiskTO.STORAGE_HOST);
            final String chapInitiatorUsername = sourceDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
            final String chapInitiatorSecret = sourceDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
            sourceSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, false);
        }
        final VDI vdiToSnapshot = VDI.getByUuid(conn, cmd.getUuidOfSourceVdi());
        final VDI vdiSnapshot = vdiToSnapshot.snapshot(conn, new HashMap<String, String>());
        final Map<String, String> destDetails = cmd.getDestDetails();
        final String iScsiName = destDetails.get(DiskTO.IQN);
        final String storageHost = destDetails.get(DiskTO.STORAGE_HOST);
        final String chapInitiatorUsername = destDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
        final String chapInitiatorSecret = destDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
        final SR newSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, false);
        final VDI vdiCopy = vdiSnapshot.copy(conn, newSr);
        final String vdiUuid = vdiCopy.getUuid(conn);
        vdiSnapshot.destroy(conn);
        if (sourceSr != null) {
            hypervisorResource.removeSR(conn, sourceSr);
        }
        hypervisorResource.removeSR(conn, newSr);
        final SnapshotAndCopyAnswer snapshotAndCopyAnswer = new SnapshotAndCopyAnswer();
        snapshotAndCopyAnswer.setPath(vdiUuid);
        return snapshotAndCopyAnswer;
    } catch (final Exception ex) {
        s_logger.warn("Failed to take and copy snapshot: " + ex.toString(), ex);
        return new SnapshotAndCopyAnswer(ex.getMessage());
    }
}
Also used : SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) Connection(com.xensource.xenapi.Connection) VDI(com.xensource.xenapi.VDI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SR(com.xensource.xenapi.SR)

Example 2 with SnapshotAndCopyAnswer

use of org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer in project cloudstack by apache.

the class StorageSystemSnapshotStrategy method performSnapshotAndCopyOnHostSide.

private void performSnapshotAndCopyOnHostSide(VolumeInfo volumeInfo, SnapshotInfo snapshotInfo) {
    Map<String, String> sourceDetails = null;
    VolumeVO volumeVO = volumeDao.findById(volumeInfo.getId());
    Long vmInstanceId = volumeVO.getInstanceId();
    VMInstanceVO vmInstanceVO = vmInstanceDao.findById(vmInstanceId);
    Long hostId = null;
    // if the volume to snapshot is associated with a VM
    if (vmInstanceVO != null) {
        hostId = vmInstanceVO.getHostId();
        // if the VM is not associated with a host
        if (hostId == null) {
            hostId = vmInstanceVO.getLastHostId();
            if (hostId == null) {
                sourceDetails = getSourceDetails(volumeInfo);
            }
        }
    } else // volume to snapshot is not associated with a VM (could be a data disk in the detached state)
    {
        sourceDetails = getSourceDetails(volumeInfo);
    }
    HostVO hostVO = null;
    if (hostId != null) {
        hostVO = hostDao.findById(hostId);
    }
    if (hostVO == null || !ResourceState.Enabled.equals(hostVO.getResourceState())) {
        Optional<HostVO> optHostVO = getHost(volumeInfo.getDataCenterId(), false);
        if (optHostVO.isPresent()) {
            hostVO = optHostVO.get();
        }
    }
    if (hostVO == null) {
        final String errMsg = "Unable to locate an applicable host";
        s_logger.error("performSnapshotAndCopyOnHostSide: " + errMsg);
        throw new CloudRuntimeException(errMsg);
    }
    long storagePoolId = volumeVO.getPoolId();
    StoragePoolVO storagePoolVO = storagePoolDao.findById(storagePoolId);
    DataStore dataStore = dataStoreMgr.getDataStore(storagePoolId, DataStoreRole.Primary);
    Map<String, String> destDetails = getDestDetails(storagePoolVO, snapshotInfo);
    SnapshotAndCopyCommand snapshotAndCopyCommand = new SnapshotAndCopyCommand(volumeInfo.getPath(), sourceDetails, destDetails);
    SnapshotAndCopyAnswer snapshotAndCopyAnswer;
    try {
        // if sourceDetails != null, we need to connect the host(s) to the volume
        if (sourceDetails != null) {
            volService.grantAccess(volumeInfo, hostVO, dataStore);
        }
        volService.grantAccess(snapshotInfo, hostVO, dataStore);
        snapshotAndCopyAnswer = (SnapshotAndCopyAnswer) agentMgr.send(hostVO.getId(), snapshotAndCopyCommand);
    } catch (Exception ex) {
        throw new CloudRuntimeException(ex.getMessage());
    } finally {
        try {
            volService.revokeAccess(snapshotInfo, hostVO, dataStore);
            // if sourceDetails != null, we need to disconnect the host(s) from the volume
            if (sourceDetails != null) {
                volService.revokeAccess(volumeInfo, hostVO, dataStore);
            }
        } catch (Exception ex) {
            s_logger.debug(ex.getMessage(), ex);
        }
    }
    if (snapshotAndCopyAnswer == null || !snapshotAndCopyAnswer.getResult()) {
        final String errMsg;
        if (snapshotAndCopyAnswer != null && snapshotAndCopyAnswer.getDetails() != null && !snapshotAndCopyAnswer.getDetails().isEmpty()) {
            errMsg = snapshotAndCopyAnswer.getDetails();
        } else {
            errMsg = "Unable to perform host-side operation";
        }
        throw new CloudRuntimeException(errMsg);
    }
    // for XenServer, this is the VDI's UUID
    String path = snapshotAndCopyAnswer.getPath();
    SnapshotDetailsVO snapshotDetail = new SnapshotDetailsVO(snapshotInfo.getId(), DiskTO.PATH, path, false);
    snapshotDetailsDao.persist(snapshotDetail);
}
Also used : VMInstanceVO(com.cloud.vm.VMInstanceVO) SnapshotDetailsVO(com.cloud.storage.dao.SnapshotDetailsVO) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) SnapshotAndCopyCommand(org.apache.cloudstack.storage.command.SnapshotAndCopyCommand)

Aggregations

CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 SnapshotAndCopyAnswer (org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer)2 InternalErrorException (com.cloud.exception.InternalErrorException)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 HostVO (com.cloud.host.HostVO)1 VolumeVO (com.cloud.storage.VolumeVO)1 SnapshotDetailsVO (com.cloud.storage.dao.SnapshotDetailsVO)1 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 Connection (com.xensource.xenapi.Connection)1 SR (com.xensource.xenapi.SR)1 XenAPIException (com.xensource.xenapi.Types.XenAPIException)1 VDI (com.xensource.xenapi.VDI)1 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)1 SnapshotAndCopyCommand (org.apache.cloudstack.storage.command.SnapshotAndCopyCommand)1 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)1 XmlRpcException (org.apache.xmlrpc.XmlRpcException)1