Search in sources :

Example 51 with VolumeInfo

use of com.cloud.engine.subsystem.api.storage.VolumeInfo in project cosmic by MissionCriticalCloud.

the class DefaultEndPointSelector method select.

@Override
public EndPoint select(final DataObject object, final StorageAction action) {
    if (StorageAction.TAKESNAPSHOT.equals(action)) {
        final SnapshotInfo snapshotInfo = (SnapshotInfo) object;
        if (Hypervisor.HypervisorType.KVM.equals(snapshotInfo.getHypervisorType())) {
            final VolumeInfo volumeInfo = snapshotInfo.getBaseVolume();
            final VirtualMachine vm = volumeInfo.getAttachedVM();
            if (vm != null && VirtualMachine.State.Running.equals(vm.getState())) {
                final Long hostId = vm.getHostId();
                return getEndPointFromHostId(hostId);
            }
        }
    } else if (StorageAction.MIGRATEVOLUME.equals(action)) {
        final VolumeInfo volumeInfo = (VolumeInfo) object;
        if (Hypervisor.HypervisorType.KVM.equals(volumeInfo.getHypervisorType())) {
            final VirtualMachine vm = volumeInfo.getAttachedVM();
            if (vm != null && VirtualMachine.State.Running.equals(vm.getState()) && volumeInfo.isToBeLiveMigrated()) {
                final Long hostId = vm.getHostId();
                return getEndPointFromHostId(hostId);
            }
        }
    }
    return select(object);
}
Also used : SnapshotInfo(com.cloud.engine.subsystem.api.storage.SnapshotInfo) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 52 with VolumeInfo

use of com.cloud.engine.subsystem.api.storage.VolumeInfo in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method orchestrateExtractVolume.

private String orchestrateExtractVolume(final long volumeId, final long zoneId) {
    // get latest volume state to make sure that it is not updated by other parallel operations
    final VolumeVO volume = _volsDao.findById(volumeId);
    if (volume == null || volume.getState() != Volume.State.Ready) {
        throw new InvalidParameterValueException("Volume to be extracted has been removed or not in right state!");
    }
    // perform extraction
    final ImageStoreEntity secStore = (ImageStoreEntity) dataStoreMgr.getImageStore(zoneId);
    final String value = _configDao.getValue(Config.CopyVolumeWait.toString());
    NumbersUtil.parseInt(value, Integer.parseInt(Config.CopyVolumeWait.getDefaultValue()));
    // Copy volume from primary to secondary storage
    final VolumeInfo srcVol = volFactory.getVolume(volumeId);
    final AsyncCallFuture<VolumeApiResult> cvAnswer = volService.copyVolume(srcVol, secStore);
    // Check if you got a valid answer.
    final VolumeApiResult cvResult;
    try {
        cvResult = cvAnswer.get();
    } catch (final InterruptedException e1) {
        s_logger.debug("failed copy volume", e1);
        throw new CloudRuntimeException("Failed to copy volume", e1);
    } catch (final ExecutionException e1) {
        s_logger.debug("failed copy volume", e1);
        throw new CloudRuntimeException("Failed to copy volume", e1);
    }
    if (cvResult == null || cvResult.isFailed()) {
        final String errorString = "Failed to copy the volume from the source primary storage pool to secondary storage.";
        throw new CloudRuntimeException(errorString);
    }
    final VolumeInfo vol = cvResult.getVolume();
    final String extractUrl = secStore.createEntityExtractUrl(vol.getPath(), vol.getFormat(), vol);
    final VolumeDataStoreVO volumeStoreRef = _volumeStoreDao.findByVolume(volumeId);
    volumeStoreRef.setExtractUrl(extractUrl);
    volumeStoreRef.setExtractUrlCreated(DateUtil.now());
    _volumeStoreDao.update(volumeStoreRef.getId(), volumeStoreRef);
    return extractUrl;
}
Also used : InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeDataStoreVO(com.cloud.storage.datastore.db.VolumeDataStoreVO) ImageStoreEntity(com.cloud.storage.image.datastore.ImageStoreEntity) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) VolumeApiResult(com.cloud.engine.subsystem.api.storage.VolumeService.VolumeApiResult) ExecutionException(java.util.concurrent.ExecutionException)

Example 53 with VolumeInfo

use of com.cloud.engine.subsystem.api.storage.VolumeInfo in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method uploadVolume.

/*
     * Upload the volume to secondary storage.
     */
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_UPLOAD, eventDescription = "uploading volume", async = true)
public VolumeVO uploadVolume(final UploadVolumeCmd cmd) throws ResourceAllocationException {
    final Account caller = CallContext.current().getCallingAccount();
    final long ownerId = cmd.getEntityOwnerId();
    final Account owner = _entityMgr.findById(Account.class, ownerId);
    final Long zoneId = cmd.getZoneId();
    final String volumeName = cmd.getVolumeName();
    final String url = cmd.getUrl();
    final String format = cmd.getFormat();
    final Long diskOfferingId = cmd.getDiskOfferingId();
    final String imageStoreUuid = cmd.getImageStoreUuid();
    final DataStore store = _tmpltMgr.getImageStore(imageStoreUuid, zoneId);
    validateVolume(caller, ownerId, zoneId, volumeName, url, format, diskOfferingId);
    final VolumeVO volume = persistVolume(owner, zoneId, volumeName, url, cmd.getFormat(), diskOfferingId, Volume.State.Allocated);
    final VolumeInfo vol = volFactory.getVolume(volume.getId());
    final RegisterVolumePayload payload = new RegisterVolumePayload(cmd.getUrl(), cmd.getChecksum(), cmd.getFormat());
    vol.addPayload(payload);
    volService.registerVolume(vol, store);
    return volume;
}
Also used : Account(com.cloud.user.Account) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 54 with VolumeInfo

use of com.cloud.engine.subsystem.api.storage.VolumeInfo in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method orchestrateResizeVolume.

private VolumeVO orchestrateResizeVolume(final long volumeId, final long currentSize, final long newSize, final Long newMinIops, final Long newMaxIops, final Long newDiskOfferingId, final boolean shrinkOk) {
    VolumeVO volume = _volsDao.findById(volumeId);
    final UserVmVO userVm = _userVmDao.findById(volume.getInstanceId());
    /*
         * get a list of hosts to send the commands to, try the system the
         * associated vm is running on first, then the last known place it ran.
         * If not attached to a userVm, we pass 'none' and resizevolume.sh is ok
         * with that since it only needs the vm name to live resize
         */
    long[] hosts = null;
    String instanceName = "none";
    if (userVm != null) {
        instanceName = userVm.getInstanceName();
        if (userVm.getHostId() != null) {
            hosts = new long[] { userVm.getHostId() };
        } else if (userVm.getLastHostId() != null) {
            hosts = new long[] { userVm.getLastHostId() };
        }
        final String errorMsg = "The VM must be stopped or the disk detached in order to resize with the XenServer Hypervisor.";
        final StoragePoolVO storagePool = _storagePoolDao.findById(volume.getPoolId());
        if (storagePool.isManaged() && storagePool.getHypervisor() == HypervisorType.Any && hosts != null && hosts.length > 0) {
            final HostVO host = _hostDao.findById(hosts[0]);
            if (currentSize != newSize && host.getHypervisorType() == HypervisorType.XenServer && !userVm.getState().equals(State.Stopped)) {
                throw new InvalidParameterValueException(errorMsg);
            }
        }
        /* Xen only works offline, SR does not support VDI.resizeOnline */
        if (currentSize != newSize && _volsDao.getHypervisorType(volume.getId()) == HypervisorType.XenServer && !userVm.getState().equals(State.Stopped)) {
            throw new InvalidParameterValueException(errorMsg);
        }
    }
    final ResizeVolumePayload payload = new ResizeVolumePayload(newSize, newMinIops, newMaxIops, shrinkOk, instanceName, hosts);
    try {
        final VolumeInfo vol = volFactory.getVolume(volume.getId());
        vol.addPayload(payload);
        final StoragePoolVO storagePool = _storagePoolDao.findById(vol.getPoolId());
        // needs to tell the hypervisor to resize the disk
        if (storagePool.isManaged() && currentSize != newSize) {
            if (hosts != null && hosts.length > 0) {
                volService.resizeVolumeOnHypervisor(volumeId, newSize, hosts[0], instanceName);
            }
            volume.setSize(newSize);
            _volsDao.update(volume.getId(), volume);
        }
        // this call to resize has a different impact depending on whether the
        // underlying primary storage is managed or not
        // if managed, this is the chance for the plug-in to change IOPS value, if applicable
        // if not managed, this is the chance for the plug-in to talk to the hypervisor layer
        // to change the size of the disk
        final AsyncCallFuture<VolumeApiResult> future = volService.resize(vol);
        final VolumeApiResult result = future.get();
        if (result.isFailed()) {
            s_logger.warn("Failed to resize the volume " + volume);
            String details = "";
            if (result.getResult() != null && !result.getResult().isEmpty()) {
                details = result.getResult();
            }
            throw new CloudRuntimeException(details);
        }
        volume = _volsDao.findById(volume.getId());
        if (newDiskOfferingId != null) {
            volume.setDiskOfferingId(newDiskOfferingId);
        }
        if (currentSize != newSize) {
            volume.setSize(newSize);
        }
        _volsDao.update(volume.getId(), volume);
        /* Update resource count for the account on primary storage resource */
        if (!shrinkOk) {
            _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, volume.isDisplayVolume(), new Long(newSize - currentSize));
        } else {
            _resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, volume.isDisplayVolume(), new Long(currentSize - newSize));
        }
        return volume;
    } catch (final InterruptedException e) {
        s_logger.warn("failed get resize volume result", e);
        throw new CloudRuntimeException(e.getMessage());
    } catch (final ExecutionException e) {
        s_logger.warn("failed get resize volume result", e);
        throw new CloudRuntimeException(e.getMessage());
    } catch (final Exception e) {
        s_logger.warn("failed get resize volume result", e);
        throw new CloudRuntimeException(e.getMessage());
    }
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) VolumeApiResult(com.cloud.engine.subsystem.api.storage.VolumeService.VolumeApiResult) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) CloudException(com.cloud.exception.CloudException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) MalformedURLException(java.net.MalformedURLException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) ExecutionException(java.util.concurrent.ExecutionException)

Example 55 with VolumeInfo

use of com.cloud.engine.subsystem.api.storage.VolumeInfo in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method allocSnapshotForVm.

@Override
public Snapshot allocSnapshotForVm(final Long vmId, Long volumeId, final String snapshotName) throws ResourceAllocationException {
    final Account caller = CallContext.current().getCallingAccount();
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("Creating snapshot failed due to vm:" + vmId + " doesn't exist");
    }
    _accountMgr.checkAccess(caller, null, true, vm);
    final VolumeInfo volume = volFactory.getVolume(volumeId);
    if (volume == null) {
        throw new InvalidParameterValueException("Creating snapshot failed due to volume:" + volumeId + " doesn't exist");
    }
    _accountMgr.checkAccess(caller, null, true, volume);
    final VirtualMachine attachVM = volume.getAttachedVM();
    if (attachVM == null || attachVM.getId() != vm.getId()) {
        throw new InvalidParameterValueException("Creating snapshot failed due to volume:" + volumeId + " doesn't attach to vm :" + vm);
    }
    final DataCenter zone = _dcDao.findById(volume.getDataCenterId());
    if (zone == null) {
        throw new InvalidParameterValueException("Can't find zone by id " + volume.getDataCenterId());
    }
    if (AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(caller.getId())) {
        throw new PermissionDeniedException("Cannot perform this operation, Zone is currently disabled: " + zone.getName());
    }
    if (volume.getState() != Volume.State.Ready) {
        throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in " + Volume.State.Ready + " state but " + volume.getState() + ". Cannot take snapshot.");
    }
    if (volume.getTemplateId() != null) {
        final VMTemplateVO template = _templateDao.findById(volume.getTemplateId());
        if (template != null && template.getTemplateType() == Storage.TemplateType.SYSTEM) {
            throw new InvalidParameterValueException("VolumeId: " + volumeId + " is for System VM , Creating snapshot against System VM volumes is not supported");
        }
    }
    final StoragePool storagePool = (StoragePool) volume.getDataStore();
    if (storagePool == null) {
        throw new InvalidParameterValueException("VolumeId: " + volumeId + " please attach this volume to a VM before create snapshot for it");
    }
    return snapshotMgr.allocSnapshot(volumeId, Snapshot.MANUAL_POLICY_ID, snapshotName, true);
}
Also used : Account(com.cloud.user.Account) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) VMInstanceVO(com.cloud.vm.VMInstanceVO) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) VirtualMachine(com.cloud.vm.VirtualMachine)

Aggregations

VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)63 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)39 ExecutionException (java.util.concurrent.ExecutionException)26 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)21 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)19 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)19 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)17 VolumeVO (com.cloud.storage.VolumeVO)14 CopyCommandResult (com.cloud.engine.subsystem.api.storage.CopyCommandResult)12 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)12 DB (com.cloud.utils.db.DB)12 StoragePoolVO (com.cloud.storage.datastore.db.StoragePoolVO)9 Account (com.cloud.user.Account)9 ArrayList (java.util.ArrayList)9 PrimaryDataStore (com.cloud.engine.subsystem.api.storage.PrimaryDataStore)8 SnapshotInfo (com.cloud.engine.subsystem.api.storage.SnapshotInfo)8 ActionEvent (com.cloud.event.ActionEvent)8 VolumeDataStoreVO (com.cloud.storage.datastore.db.VolumeDataStoreVO)8 AsyncCallFuture (com.cloud.framework.async.AsyncCallFuture)7 StoragePool (com.cloud.storage.StoragePool)7