Search in sources :

Example 96 with VolumeInfo

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

the class VolumeApiServiceImpl method createVolume.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_CREATE, eventDescription = "creating volume", async = true)
public VolumeVO createVolume(CreateVolumeCmd cmd) {
    VolumeVO volume = _volsDao.findById(cmd.getEntityId());
    boolean created = true;
    try {
        if (cmd.getSnapshotId() != null) {
            volume = createVolumeFromSnapshot(volume, cmd.getSnapshotId(), cmd.getVirtualMachineId());
            if (volume.getState() != Volume.State.Ready) {
                created = false;
            }
            // if VM Id is provided, attach the volume to the VM
            if (cmd.getVirtualMachineId() != null) {
                try {
                    attachVolumeToVM(cmd.getVirtualMachineId(), volume.getId(), volume.getDeviceId());
                } catch (Exception ex) {
                    StringBuilder message = new StringBuilder("Volume: ");
                    message.append(volume.getUuid());
                    message.append(" created successfully, but failed to attach the newly created volume to VM: ");
                    message.append(cmd.getVirtualMachineId());
                    message.append(" due to error: ");
                    message.append(ex.getMessage());
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug(message, ex);
                    }
                    throw new CloudRuntimeException(message.toString());
                }
            }
        }
        return volume;
    } catch (Exception e) {
        created = false;
        VolumeInfo vol = volFactory.getVolume(cmd.getEntityId());
        vol.stateTransit(Volume.Event.DestroyRequested);
        throw new CloudRuntimeException("Failed to create volume: " + volume.getId(), e);
    } finally {
        if (!created) {
            s_logger.trace("Decrementing volume resource count for account id=" + volume.getAccountId() + " as volume failed to create on the backend");
            _resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.volume, cmd.getDisplayVolume());
            _resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(), ResourceType.primary_storage.getOrdinal());
        }
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) JsonParseException(com.google.gson.JsonParseException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) MalformedURLException(java.net.MalformedURLException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 97 with VolumeInfo

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

the class SnapshotManagerImpl method allocSnapshot.

@Override
public Snapshot allocSnapshot(Long volumeId, Long policyId, String snapshotName, Snapshot.LocationType locationType) throws ResourceAllocationException {
    Account caller = CallContext.current().getCallingAccount();
    VolumeInfo volume = volFactory.getVolume(volumeId);
    supportedByHypervisor(volume);
    // Verify permissions
    _accountMgr.checkAccess(caller, null, true, volume);
    Type snapshotType = getSnapshotType(policyId);
    Account owner = _accountMgr.getAccount(volume.getAccountId());
    try {
        _resourceLimitMgr.checkResourceLimit(owner, ResourceType.snapshot);
        _resourceLimitMgr.checkResourceLimit(owner, ResourceType.secondary_storage, new Long(volume.getSize()).longValue());
    } catch (ResourceAllocationException e) {
        if (snapshotType != Type.MANUAL) {
            String msg = "Snapshot resource limit exceeded for account id : " + owner.getId() + ". Failed to create recurring snapshots";
            s_logger.warn(msg);
            _alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_UPDATE_RESOURCE_COUNT, 0L, 0L, msg, "Snapshot resource limit exceeded for account id : " + owner.getId() + ". Failed to create recurring snapshots; please use updateResourceLimit to increase the limit");
        }
        throw e;
    }
    // Determine the name for this snapshot
    // Snapshot Name: VMInstancename + volumeName + timeString
    String timeString = DateUtil.getDateDisplayString(DateUtil.GMT_TIMEZONE, new Date(), DateUtil.YYYYMMDD_FORMAT);
    VMInstanceVO vmInstance = _vmDao.findById(volume.getInstanceId());
    String vmDisplayName = "detached";
    if (vmInstance != null) {
        vmDisplayName = vmInstance.getHostName();
    }
    if (snapshotName == null)
        snapshotName = vmDisplayName + "_" + volume.getName() + "_" + timeString;
    HypervisorType hypervisorType = HypervisorType.None;
    StoragePoolVO storagePool = _storagePoolDao.findById(volume.getDataStore().getId());
    if (storagePool.getScope() == ScopeType.ZONE) {
        hypervisorType = storagePool.getHypervisor();
        // at the time being, managed storage only supports XenServer, ESX(i), and KVM (i.e. not Hyper-V), so the VHD file type can be mapped to XenServer
        if (storagePool.isManaged() && HypervisorType.Any.equals(hypervisorType) && ImageFormat.VHD.equals(volume.getFormat())) {
            hypervisorType = HypervisorType.XenServer;
        }
    } else {
        hypervisorType = volume.getHypervisorType();
    }
    SnapshotVO snapshotVO = new SnapshotVO(volume.getDataCenterId(), volume.getAccountId(), volume.getDomainId(), volume.getId(), volume.getDiskOfferingId(), snapshotName, (short) snapshotType.ordinal(), snapshotType.name(), volume.getSize(), volume.getMinIops(), volume.getMaxIops(), hypervisorType, locationType);
    SnapshotVO snapshot = _snapshotDao.persist(snapshotVO);
    if (snapshot == null) {
        throw new CloudRuntimeException("Failed to create snapshot for volume: " + volume.getId());
    }
    _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.snapshot);
    _resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.secondary_storage, new Long(volume.getSize()));
    return snapshot;
}
Also used : HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) Account(com.cloud.user.Account) ScopeType(com.cloud.storage.ScopeType) Type(com.cloud.storage.Snapshot.Type) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) ResourceType(com.cloud.configuration.Resource.ResourceType) IntervalType(com.cloud.utils.DateUtil.IntervalType) ResourceObjectType(com.cloud.server.ResourceTag.ResourceObjectType) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) Date(java.util.Date)

Example 98 with VolumeInfo

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

the class HypervStorageMotionStrategy method updateVolumePathsAfterMigration.

private void updateVolumePathsAfterMigration(Map<VolumeInfo, DataStore> volumeToPool, List<VolumeObjectTO> volumeTos) {
    for (Map.Entry<VolumeInfo, DataStore> entry : volumeToPool.entrySet()) {
        boolean updated = false;
        VolumeInfo volume = entry.getKey();
        StoragePool pool = (StoragePool) entry.getValue();
        for (VolumeObjectTO volumeTo : volumeTos) {
            if (volume.getId() == volumeTo.getId()) {
                VolumeVO volumeVO = volDao.findById(volume.getId());
                Long oldPoolId = volumeVO.getPoolId();
                volumeVO.setPath(volumeTo.getPath());
                volumeVO.setPodId(pool.getPodId());
                volumeVO.setPoolId(pool.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 = pool.getPath();
                if (pool.getPoolType() == StoragePoolType.SMB && folder != null && folder.contains("?")) {
                    folder = folder.substring(0, folder.indexOf("?"));
                }
                volumeVO.setFolder(folder);
                volDao.update(volume.getId(), volumeVO);
                updated = true;
                break;
            }
        }
        if (!updated) {
            s_logger.error("Volume path wasn't updated for volume " + volume + " after it was migrated.");
        }
    }
}
Also used : StoragePool(com.cloud.storage.StoragePool) VolumeVO(com.cloud.storage.VolumeVO) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) Map(java.util.Map)

Example 99 with VolumeInfo

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

the class VMwareGuru method finalizeExpungeVolumes.

@Override
public List<Command> finalizeExpungeVolumes(VirtualMachine vm) {
    List<Command> commands = new ArrayList<Command>();
    List<VolumeVO> volumes = _volumeDao.findByInstance(vm.getId());
    if (volumes != null) {
        for (VolumeVO volume : volumes) {
            StoragePoolVO storagePool = _storagePoolDao.findById(volume.getPoolId());
            // so the volume was never assigned to a storage pool)
            if (storagePool != null && storagePool.isManaged() && volume.getVolumeType() == Volume.Type.ROOT) {
                VolumeInfo volumeInfo = _volFactory.getVolume(volume.getId());
                PrimaryDataStore primaryDataStore = (PrimaryDataStore) volumeInfo.getDataStore();
                Map<String, String> details = primaryDataStore.getDetails();
                if (details == null) {
                    details = new HashMap<String, String>();
                    primaryDataStore.setDetails(details);
                }
                details.put(DiskTO.MANAGED, Boolean.TRUE.toString());
                DeleteCommand cmd = new DeleteCommand(volumeInfo.getTO());
                commands.add(cmd);
                break;
            }
        }
    }
    return commands;
}
Also used : DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) VolumeVO(com.cloud.storage.VolumeVO) DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) BackupSnapshotCommand(com.cloud.agent.api.BackupSnapshotCommand) CreateVolumeOVACommand(com.cloud.agent.api.storage.CreateVolumeOVACommand) CreatePrivateTemplateFromSnapshotCommand(com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand) PrepareOVAPackingCommand(com.cloud.agent.api.storage.PrepareOVAPackingCommand) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) UnregisterNicCommand(com.cloud.agent.api.UnregisterNicCommand) CopyVolumeCommand(com.cloud.agent.api.storage.CopyVolumeCommand) StorageSubSystemCommand(org.apache.cloudstack.storage.command.StorageSubSystemCommand) CreateVolumeFromSnapshotCommand(com.cloud.agent.api.CreateVolumeFromSnapshotCommand) CreateEntityDownloadURLCommand(com.cloud.agent.api.storage.CreateEntityDownloadURLCommand) Command(com.cloud.agent.api.Command) CreatePrivateTemplateFromVolumeCommand(com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand) ArrayList(java.util.ArrayList) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Example 100 with VolumeInfo

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

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(UploadVolumeCmd cmd) throws ResourceAllocationException {
    Account caller = CallContext.current().getCallingAccount();
    long ownerId = cmd.getEntityOwnerId();
    Account owner = _entityMgr.findById(Account.class, ownerId);
    Long zoneId = cmd.getZoneId();
    String volumeName = cmd.getVolumeName();
    String url = cmd.getUrl();
    String format = cmd.getFormat();
    Long diskOfferingId = cmd.getDiskOfferingId();
    String imageStoreUuid = cmd.getImageStoreUuid();
    DataStore store = _tmpltMgr.getImageStore(imageStoreUuid, zoneId);
    validateVolume(caller, ownerId, zoneId, volumeName, url, format, diskOfferingId);
    VolumeVO volume = persistVolume(owner, zoneId, volumeName, url, cmd.getFormat(), diskOfferingId, Volume.State.Allocated);
    VolumeInfo vol = volFactory.getVolume(volume.getId());
    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(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)112 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)51 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)50 VolumeVO (com.cloud.storage.VolumeVO)36 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)23 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)22 ExecutionException (java.util.concurrent.ExecutionException)22 VolumeApiResult (org.apache.cloudstack.engine.subsystem.api.storage.VolumeService.VolumeApiResult)22 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)19 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)19 CopyCommandResult (org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)18 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)17 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)15 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)13 Map (java.util.Map)13 SnapshotVO (com.cloud.storage.SnapshotVO)12 DB (com.cloud.utils.db.DB)12 Test (org.testng.annotations.Test)12 Account (com.cloud.user.Account)11