Search in sources :

Example 11 with VolumeVO

use of com.cloud.storage.VolumeVO in project cloudstack by apache.

the class VolumeOrchestrator method prepare.

@Override
public void prepare(VirtualMachineProfile vm, DeployDestination dest) throws StorageUnavailableException, InsufficientStorageCapacityException, ConcurrentOperationException {
    if (dest == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("DeployDestination cannot be null, cannot prepare Volumes for the vm: " + vm);
        }
        throw new CloudRuntimeException("Unable to prepare Volume for vm because DeployDestination is null, vm:" + vm);
    }
    // don't allow to start vm that doesn't have a root volume
    if (_volsDao.findByInstanceAndType(vm.getId(), Volume.Type.ROOT).isEmpty()) {
        throw new CloudRuntimeException("Unable to prepare volumes for vm as ROOT volume is missing");
    }
    List<VolumeVO> vols = _volsDao.findUsableVolumesForInstance(vm.getId());
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Checking if we need to prepare " + vols.size() + " volumes for " + vm);
    }
    List<VolumeTask> tasks = getTasks(vols, dest.getStorageForDisks(), vm);
    Volume vol = null;
    StoragePool pool = null;
    for (VolumeTask task : tasks) {
        if (task.type == VolumeTaskType.NOP) {
            pool = (StoragePool) dataStoreMgr.getDataStore(task.pool.getId(), DataStoreRole.Primary);
            vol = task.volume;
        } else if (task.type == VolumeTaskType.MIGRATE) {
            pool = (StoragePool) dataStoreMgr.getDataStore(task.pool.getId(), DataStoreRole.Primary);
            vol = migrateVolume(task.volume, pool);
        } else if (task.type == VolumeTaskType.RECREATE) {
            Pair<VolumeVO, DataStore> result = recreateVolume(task.volume, vm, dest);
            pool = (StoragePool) dataStoreMgr.getDataStore(result.second().getId(), DataStoreRole.Primary);
            vol = result.first();
        }
        VolumeInfo volumeInfo = volFactory.getVolume(vol.getId());
        DataTO volTO = volumeInfo.getTO();
        DiskTO disk = storageMgr.getDiskWithThrottling(volTO, vol.getVolumeType(), vol.getDeviceId(), vol.getPath(), vm.getServiceOfferingId(), vol.getDiskOfferingId());
        DataStore dataStore = dataStoreMgr.getDataStore(vol.getPoolId(), DataStoreRole.Primary);
        disk.setDetails(getDetails(volumeInfo, dataStore));
        vm.addDisk(disk);
        // If hypervisor is vSphere, check for clone type setting.
        if (vm.getHypervisorType().equals(HypervisorType.VMware)) {
            // retrieve clone flag.
            UserVmCloneType cloneType = UserVmCloneType.linked;
            Boolean value = CapacityManager.VmwareCreateCloneFull.valueIn(vol.getPoolId());
            if (value != null && value) {
                cloneType = UserVmCloneType.full;
            }
            try {
                UserVmCloneSettingVO cloneSettingVO = _vmCloneSettingDao.findByVmId(vm.getId());
                if (cloneSettingVO != null) {
                    if (!cloneSettingVO.getCloneType().equals(cloneType.toString())) {
                        cloneSettingVO.setCloneType(cloneType.toString());
                        _vmCloneSettingDao.update(cloneSettingVO.getVmId(), cloneSettingVO);
                    }
                } else {
                    UserVmCloneSettingVO vmCloneSettingVO = new UserVmCloneSettingVO(vm.getId(), cloneType.toString());
                    _vmCloneSettingDao.persist(vmCloneSettingVO);
                }
            } catch (Throwable e) {
                s_logger.debug("[NSX_PLUGIN_LOG] ERROR: " + e.getMessage());
            }
        }
    }
}
Also used : StoragePool(com.cloud.storage.StoragePool) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) UserVmCloneSettingVO(com.cloud.vm.UserVmCloneSettingVO) DataTO(com.cloud.agent.api.to.DataTO) VolumeVO(com.cloud.storage.VolumeVO) VmWorkMigrateVolume(com.cloud.vm.VmWorkMigrateVolume) VmWorkAttachVolume(com.cloud.vm.VmWorkAttachVolume) Volume(com.cloud.storage.Volume) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 12 with VolumeVO

use of com.cloud.storage.VolumeVO in project cloudstack by apache.

the class VolumeOrchestrator method cleanupVolumeDuringSnapshotFailure.

private void cleanupVolumeDuringSnapshotFailure(Long volumeId, Long snapshotId) {
    _snapshotSrv.cleanupVolumeDuringSnapshotFailure(volumeId, snapshotId);
    VolumeVO volume = _volsDao.findById(volumeId);
    if (volume.getState() == Volume.State.Snapshotting) {
        s_logger.debug("change volume state back to Ready: " + volume.getId());
        volume.setState(Volume.State.Ready);
        _volsDao.update(volume.getId(), volume);
    }
}
Also used : VolumeVO(com.cloud.storage.VolumeVO)

Example 13 with VolumeVO

use of com.cloud.storage.VolumeVO in project cloudstack by apache.

the class VolumeOrchestrator method cleanupVolumes.

@Override
@DB
public void cleanupVolumes(long vmId) throws ConcurrentOperationException {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Cleaning storage for vm: " + vmId);
    }
    final List<VolumeVO> volumesForVm = _volsDao.findByInstance(vmId);
    final List<VolumeVO> toBeExpunged = new ArrayList<VolumeVO>();
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            for (VolumeVO vol : volumesForVm) {
                if (vol.getVolumeType().equals(Type.ROOT)) {
                    // Destroy volume if not already destroyed
                    boolean volumeAlreadyDestroyed = (vol.getState() == Volume.State.Destroy || vol.getState() == Volume.State.Expunged || vol.getState() == Volume.State.Expunging);
                    if (!volumeAlreadyDestroyed) {
                        volService.destroyVolume(vol.getId());
                    } else {
                        s_logger.debug("Skipping destroy for the volume " + vol + " as its in state " + vol.getState().toString());
                    }
                    toBeExpunged.add(vol);
                } else {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Detaching " + vol);
                    }
                    _volsDao.detachVolume(vol.getId());
                }
            }
        }
    });
    AsyncCallFuture<VolumeApiResult> future = null;
    for (VolumeVO expunge : toBeExpunged) {
        future = volService.expungeVolumeAsync(volFactory.getVolume(expunge.getId()));
        try {
            future.get();
        } catch (InterruptedException e) {
            s_logger.debug("failed expunge volume" + expunge.getId(), e);
        } catch (ExecutionException e) {
            s_logger.debug("failed expunge volume" + expunge.getId(), e);
        }
    }
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) VolumeApiResult(org.apache.cloudstack.engine.subsystem.api.storage.VolumeService.VolumeApiResult) ExecutionException(java.util.concurrent.ExecutionException) DB(com.cloud.utils.db.DB)

Example 14 with VolumeVO

use of com.cloud.storage.VolumeVO in project cloudstack by apache.

the class VolumeOrchestrator method cleanupVolumeDuringMigrationFailure.

private void cleanupVolumeDuringMigrationFailure(Long volumeId, Long destPoolId) {
    StoragePool destPool = (StoragePool) dataStoreMgr.getDataStore(destPoolId, DataStoreRole.Primary);
    if (destPool == null) {
        return;
    }
    VolumeVO volume = _volsDao.findById(volumeId);
    if (volume.getState() == Volume.State.Migrating) {
        VolumeVO duplicateVol = _volsDao.findByPoolIdName(destPoolId, volume.getName());
        if (duplicateVol != null) {
            s_logger.debug("Remove volume " + duplicateVol.getId() + " on storage pool " + destPoolId);
            _volsDao.remove(duplicateVol.getId());
        }
        s_logger.debug("change volume state to ready from migrating in case migration failure for vol: " + volumeId);
        volume.setState(Volume.State.Ready);
        _volsDao.update(volumeId, volume);
    }
}
Also used : StoragePool(com.cloud.storage.StoragePool) VolumeVO(com.cloud.storage.VolumeVO)

Example 15 with VolumeVO

use of com.cloud.storage.VolumeVO in project cloudstack by apache.

the class XenserverSnapshotStrategy method deleteSnapshot.

@Override
public boolean deleteSnapshot(Long snapshotId) {
    SnapshotVO snapshotVO = snapshotDao.findById(snapshotId);
    if (snapshotVO.getState() == Snapshot.State.Allocated) {
        snapshotDao.remove(snapshotId);
        return true;
    }
    if (snapshotVO.getState() == Snapshot.State.Destroyed) {
        return true;
    }
    if (Snapshot.State.Error.equals(snapshotVO.getState())) {
        List<SnapshotDataStoreVO> storeRefs = snapshotStoreDao.findBySnapshotId(snapshotId);
        for (SnapshotDataStoreVO ref : storeRefs) {
            snapshotStoreDao.expunge(ref.getId());
        }
        snapshotDao.remove(snapshotId);
        return true;
    }
    if (snapshotVO.getState() == Snapshot.State.CreatedOnPrimary) {
        s_logger.debug("delete snapshot on primary storage:");
        snapshotVO.setState(Snapshot.State.Destroyed);
        snapshotDao.update(snapshotId, snapshotVO);
        return true;
    }
    if (!Snapshot.State.BackedUp.equals(snapshotVO.getState()) && !Snapshot.State.Error.equals(snapshotVO.getState())) {
        throw new InvalidParameterValueException("Can't delete snapshotshot " + snapshotId + " due to it is in " + snapshotVO.getState() + " Status");
    }
    // first mark the snapshot as destroyed, so that ui can't see it, but we
    // may not destroy the snapshot on the storage, as other snapshots may
    // depend on it.
    SnapshotInfo snapshotOnImage = snapshotDataFactory.getSnapshot(snapshotId, DataStoreRole.Image);
    if (snapshotOnImage == null) {
        s_logger.debug("Can't find snapshot on backup storage, delete it in db");
        snapshotDao.remove(snapshotId);
        return true;
    }
    SnapshotObject obj = (SnapshotObject) snapshotOnImage;
    try {
        obj.processEvent(Snapshot.Event.DestroyRequested);
    } catch (NoTransitionException e) {
        s_logger.debug("Failed to set the state to destroying: ", e);
        return false;
    }
    try {
        boolean result = deleteSnapshotChain(snapshotOnImage);
        obj.processEvent(Snapshot.Event.OperationSucceeded);
        if (result) {
            //snapshot is deleted on backup storage, need to delete it on primary storage
            SnapshotDataStoreVO snapshotOnPrimary = snapshotStoreDao.findBySnapshot(snapshotId, DataStoreRole.Primary);
            if (snapshotOnPrimary != null) {
                SnapshotInfo snapshotOnPrimaryInfo = snapshotDataFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
                long volumeId = snapshotOnPrimary.getVolumeId();
                VolumeVO volumeVO = volumeDao.findById(volumeId);
                if (((PrimaryDataStoreImpl) snapshotOnPrimaryInfo.getDataStore()).getPoolType() == StoragePoolType.RBD && volumeVO != null) {
                    snapshotSvr.deleteSnapshot(snapshotOnPrimaryInfo);
                }
                snapshotOnPrimary.setState(State.Destroyed);
                snapshotStoreDao.update(snapshotOnPrimary.getId(), snapshotOnPrimary);
            }
        }
    } catch (Exception e) {
        s_logger.debug("Failed to delete snapshot: ", e);
        try {
            obj.processEvent(Snapshot.Event.OperationFailed);
        } catch (NoTransitionException e1) {
            s_logger.debug("Failed to change snapshot state: " + e.toString());
        }
        return false;
    }
    return true;
}
Also used : SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PrimaryDataStoreImpl(org.apache.cloudstack.storage.datastore.PrimaryDataStoreImpl) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Aggregations

VolumeVO (com.cloud.storage.VolumeVO)156 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)44 ArrayList (java.util.ArrayList)39 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)36 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)30 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)26 HostVO (com.cloud.host.HostVO)24 VMInstanceVO (com.cloud.vm.VMInstanceVO)24 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)22 Account (com.cloud.user.Account)19 HashMap (java.util.HashMap)17 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)16 VolumeApiResult (org.apache.cloudstack.engine.subsystem.api.storage.VolumeService.VolumeApiResult)16 StoragePool (com.cloud.storage.StoragePool)15 HostPodVO (com.cloud.dc.HostPodVO)14 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)13 Pair (com.cloud.utils.Pair)13 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)11 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)11 DiskOfferingVO (com.cloud.storage.DiskOfferingVO)11