Search in sources :

Example 6 with SnapshotStrategy

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy in project cloudstack by apache.

the class SnapshotManagerImpl method backupSnapshotFromVmSnapshot.

@Override
public Snapshot backupSnapshotFromVmSnapshot(Long snapshotId, Long vmId, Long volumeId, Long vmSnapshotId) {
    VMInstanceVO vm = _vmDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("Creating snapshot failed due to vm:" + vmId + " doesn't exist");
    }
    if (!HypervisorType.KVM.equals(vm.getHypervisorType())) {
        throw new InvalidParameterValueException("Unsupported hypervisor type " + vm.getHypervisorType() + ". This supports KVM only");
    }
    VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(vmSnapshotId);
    if (vmSnapshot == null) {
        throw new InvalidParameterValueException("Creating snapshot failed due to vmSnapshot:" + vmSnapshotId + " doesn't exist");
    }
    // check vmsnapshot permissions
    Account caller = CallContext.current().getCallingAccount();
    _accountMgr.checkAccess(caller, null, true, vmSnapshot);
    SnapshotVO snapshot = _snapshotDao.findById(snapshotId);
    if (snapshot == null) {
        throw new InvalidParameterValueException("Creating snapshot failed due to snapshot:" + snapshotId + " doesn't exist");
    }
    VolumeInfo volume = volFactory.getVolume(volumeId);
    if (volume == null) {
        throw new InvalidParameterValueException("Creating snapshot failed due to volume:" + volumeId + " doesn't exist");
    }
    if (volume.getState() != Volume.State.Ready) {
        throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in " + Volume.State.Ready + " state but " + volume.getState() + ". Cannot take snapshot.");
    }
    DataStore store = volume.getDataStore();
    SnapshotDataStoreVO parentSnapshotDataStoreVO = _snapshotStoreDao.findParent(store.getRole(), store.getId(), volumeId);
    if (parentSnapshotDataStoreVO != null) {
        // Double check the snapshot is removed or not
        SnapshotVO parentSnap = _snapshotDao.findById(parentSnapshotDataStoreVO.getSnapshotId());
        if (parentSnap != null && parentSnapshotDataStoreVO.getInstallPath() != null && parentSnapshotDataStoreVO.getInstallPath().equals(vmSnapshot.getName())) {
            throw new InvalidParameterValueException("Creating snapshot failed due to snapshot : " + parentSnap.getUuid() + " is created from the same vm snapshot");
        }
    }
    SnapshotInfo snapshotInfo = this.snapshotFactory.getSnapshot(snapshotId, store);
    snapshotInfo = (SnapshotInfo) store.create(snapshotInfo);
    SnapshotDataStoreVO snapshotOnPrimaryStore = this._snapshotStoreDao.findByStoreSnapshot(store.getRole(), store.getId(), snapshot.getId());
    snapshotOnPrimaryStore.setState(ObjectInDataStoreStateMachine.State.Ready);
    snapshotOnPrimaryStore.setInstallPath(vmSnapshot.getName());
    _snapshotStoreDao.update(snapshotOnPrimaryStore.getId(), snapshotOnPrimaryStore);
    snapshot.setState(Snapshot.State.CreatedOnPrimary);
    _snapshotDao.update(snapshot.getId(), snapshot);
    snapshotInfo = this.snapshotFactory.getSnapshot(snapshotId, store);
    Long snapshotOwnerId = vm.getAccountId();
    try {
        SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
        if (snapshotStrategy == null) {
            throw new CloudRuntimeException("Unable to find snaphot strategy to handle snapshot with id '" + snapshotId + "'");
        }
        snapshotInfo = snapshotStrategy.backupSnapshot(snapshotInfo);
    } catch (Exception e) {
        s_logger.debug("Failed to backup snapshot from vm snapshot", e);
        _resourceLimitMgr.decrementResourceCount(snapshotOwnerId, ResourceType.snapshot);
        _resourceLimitMgr.decrementResourceCount(snapshotOwnerId, ResourceType.secondary_storage, new Long(volume.getSize()));
        throw new CloudRuntimeException("Failed to backup snapshot from vm snapshot", e);
    }
    return snapshotInfo;
}
Also used : Account(com.cloud.user.Account) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy)

Example 7 with SnapshotStrategy

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy in project cloudstack by apache.

the class SnapshotManagerImpl method revertSnapshot.

@Override
public Snapshot revertSnapshot(Long snapshotId) {
    SnapshotVO snapshot = _snapshotDao.findById(snapshotId);
    if (snapshot == null) {
        throw new InvalidParameterValueException("No such snapshot");
    }
    VolumeVO volume = _volsDao.findById(snapshot.getVolumeId());
    if (volume.getState() != Volume.State.Ready) {
        throw new InvalidParameterValueException("The volume is not in Ready state.");
    }
    Long instanceId = volume.getInstanceId();
    // in order to revert the volume
    if (instanceId != null) {
        UserVmVO vm = _vmDao.findById(instanceId);
        if (vm.getState() != State.Stopped && vm.getState() != State.Shutdown) {
            throw new InvalidParameterValueException("The VM the specified disk is attached to is not in the shutdown state.");
        }
        // If target VM has associated VM snapshots then don't allow to revert from snapshot
        List<VMSnapshotVO> vmSnapshots = _vmSnapshotDao.findByVm(instanceId);
        if (vmSnapshots.size() > 0) {
            throw new InvalidParameterValueException("Unable to revert snapshot for VM, please remove VM snapshots before reverting VM from snapshot");
        }
    }
    DataStoreRole dataStoreRole = getDataStoreRole(snapshot);
    SnapshotInfo snapshotInfo = snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
    if (snapshotInfo == null) {
        throw new CloudRuntimeException(String.format("snapshot %s [%s] does not exists in data store", snapshot.getName(), snapshot.getUuid()));
    }
    SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.REVERT);
    if (snapshotStrategy == null) {
        s_logger.error("Unable to find snaphot strategy to handle snapshot with id '" + snapshotId + "'");
        String errorMsg = String.format("Revert snapshot command failed for snapshot with id %d, because this command is supported only for KVM hypervisor", snapshotId);
        throw new CloudRuntimeException(errorMsg);
    }
    boolean result = snapshotStrategy.revertSnapshot(snapshotInfo);
    if (result) {
        // update volume size and primary storage count
        _resourceLimitMgr.decrementResourceCount(snapshot.getAccountId(), ResourceType.primary_storage, new Long(volume.getSize() - snapshot.getSize()));
        volume.setSize(snapshot.getSize());
        _volsDao.update(volume.getId(), volume);
        return snapshotInfo;
    }
    return null;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) DataStoreRole(com.cloud.storage.DataStoreRole) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy)

Example 8 with SnapshotStrategy

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy in project cloudstack by apache.

the class SnapshotManagerImpl method deleteSnapshot.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_DELETE, eventDescription = "deleting snapshot", async = true)
public boolean deleteSnapshot(long snapshotId) {
    Account caller = CallContext.current().getCallingAccount();
    // Verify parameters
    SnapshotVO snapshotCheck = _snapshotDao.findById(snapshotId);
    if (snapshotCheck == null) {
        throw new InvalidParameterValueException("unable to find a snapshot with id " + snapshotId);
    }
    if (snapshotCheck.getState() == Snapshot.State.Destroyed) {
        throw new InvalidParameterValueException("Snapshot with id: " + snapshotId + " is already destroyed");
    }
    _accountMgr.checkAccess(caller, null, true, snapshotCheck);
    SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshotCheck, SnapshotOperation.DELETE);
    if (snapshotStrategy == null) {
        s_logger.error("Unable to find snaphot strategy to handle snapshot with id '" + snapshotId + "'");
        return false;
    }
    DataStoreRole dataStoreRole = getDataStoreRole(snapshotCheck);
    SnapshotDataStoreVO snapshotStoreRef = _snapshotStoreDao.findBySnapshot(snapshotId, dataStoreRole);
    try {
        boolean result = snapshotStrategy.deleteSnapshot(snapshotId);
        if (result) {
            annotationDao.removeByEntityType(AnnotationService.EntityType.SNAPSHOT.name(), snapshotCheck.getUuid());
            if (snapshotCheck.getState() == Snapshot.State.BackedUp) {
                UsageEventUtils.publishUsageEvent(EventTypes.EVENT_SNAPSHOT_DELETE, snapshotCheck.getAccountId(), snapshotCheck.getDataCenterId(), snapshotId, snapshotCheck.getName(), null, null, 0L, snapshotCheck.getClass().getName(), snapshotCheck.getUuid());
            }
            if (snapshotCheck.getState() != Snapshot.State.Error && snapshotCheck.getState() != Snapshot.State.Destroyed) {
                _resourceLimitMgr.decrementResourceCount(snapshotCheck.getAccountId(), ResourceType.snapshot);
            }
            if (snapshotCheck.getState() == Snapshot.State.BackedUp) {
                if (snapshotStoreRef != null) {
                    _resourceLimitMgr.decrementResourceCount(snapshotCheck.getAccountId(), ResourceType.secondary_storage, new Long(snapshotStoreRef.getPhysicalSize()));
                }
            }
        }
        return result;
    } catch (Exception e) {
        s_logger.debug("Failed to delete snapshot: " + snapshotCheck.getId() + ":" + e.toString());
        throw new CloudRuntimeException("Failed to delete snapshot:" + e.toString());
    }
}
Also used : Account(com.cloud.user.Account) DataStoreRole(com.cloud.storage.DataStoreRole) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 9 with SnapshotStrategy

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy in project cloudstack by apache.

the class SnapshotTest method createVolumeFromSnapshot.

@Test
public void createVolumeFromSnapshot() throws InterruptedException, ExecutionException {
    VolumeInfo vol = createCopyBaseImage();
    SnapshotVO snapshotVO = createSnapshotInDb(vol);
    SnapshotInfo snapshot = this.snapshotFactory.getSnapshot(snapshotVO.getId(), vol.getDataStore());
    boolean result = false;
    SnapshotStrategy snapshotStrategy = storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.TAKE);
    if (snapshotStrategy != null) {
        snapshot = snapshotStrategy.takeSnapshot(snapshot);
        result = true;
    }
    AssertJUnit.assertTrue(result);
    VolumeVO volVO = createVolume(vol.getTemplateId(), vol.getPoolId());
    VolumeInfo newVol = this.volFactory.getVolume(volVO.getId());
    AsyncCallFuture<VolumeApiResult> volFuture = this.volumeService.createVolumeFromSnapshot(newVol, newVol.getDataStore(), snapshot);
    VolumeApiResult apiResult = volFuture.get();
    Assert.assertTrue(apiResult.isSuccess());
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) VolumeApiResult(org.apache.cloudstack.engine.subsystem.api.storage.VolumeService.VolumeApiResult) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy) Test(org.testng.annotations.Test)

Example 10 with SnapshotStrategy

use of org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy in project cloudstack by apache.

the class SnapshotTest method deleteSnapshot.

@Test
public void deleteSnapshot() throws InterruptedException, ExecutionException {
    VolumeInfo vol = createCopyBaseImage();
    SnapshotVO snapshotVO = createSnapshotInDb(vol);
    SnapshotInfo snapshot = this.snapshotFactory.getSnapshot(snapshotVO.getId(), vol.getDataStore());
    SnapshotInfo newSnapshot = null;
    SnapshotStrategy snapshotStrategy = storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.TAKE);
    if (snapshotStrategy != null) {
        newSnapshot = snapshotStrategy.takeSnapshot(snapshot);
    }
    AssertJUnit.assertNotNull(newSnapshot);
    // create another snapshot
    for (SnapshotStrategy strategy : this.snapshotStrategies) {
        if (strategy.canHandle(snapshot, SnapshotOperation.DELETE) != StrategyPriority.CANT_HANDLE) {
            strategy.deleteSnapshot(newSnapshot.getId());
        }
    }
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy) Test(org.testng.annotations.Test)

Aggregations

SnapshotStrategy (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy)11 SnapshotVO (com.cloud.storage.SnapshotVO)9 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)6 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)5 DataStoreRole (com.cloud.storage.DataStoreRole)4 VolumeVO (com.cloud.storage.VolumeVO)4 Account (com.cloud.user.Account)4 VMSnapshotVO (com.cloud.vm.snapshot.VMSnapshotVO)4 SnapshotDataStoreVO (org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO)4 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)3 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)3 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)3 DB (com.cloud.utils.db.DB)3 ConfigurationException (javax.naming.ConfigurationException)3 Test (org.testng.annotations.Test)3 ActionEvent (com.cloud.event.ActionEvent)2 VMTemplateVO (com.cloud.storage.VMTemplateVO)2