Search in sources :

Example 6 with PrimaryDataStoreInfo

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

the class VolumeApiServiceImpl method orchestrateAttachVolumeToVM.

private Volume orchestrateAttachVolumeToVM(Long vmId, Long volumeId, Long deviceId) {
    VolumeInfo volumeToAttach = volFactory.getVolume(volumeId);
    if (volumeToAttach.isAttachedVM()) {
        throw new CloudRuntimeException("This volume is already attached to a VM.");
    }
    UserVmVO vm = _userVmDao.findById(vmId);
    VolumeVO exstingVolumeOfVm = null;
    List<VolumeVO> rootVolumesOfVm = _volsDao.findByInstanceAndType(vmId, Volume.Type.ROOT);
    if (rootVolumesOfVm.size() > 1) {
        throw new CloudRuntimeException("The VM " + vm.getHostName() + " has more than one ROOT volume and is in an invalid state.");
    } else {
        if (!rootVolumesOfVm.isEmpty()) {
            exstingVolumeOfVm = rootVolumesOfVm.get(0);
        } else {
            // locate data volume of the vm
            List<VolumeVO> diskVolumesOfVm = _volsDao.findByInstanceAndType(vmId, Volume.Type.DATADISK);
            for (VolumeVO diskVolume : diskVolumesOfVm) {
                if (diskVolume.getState() != Volume.State.Allocated) {
                    exstingVolumeOfVm = diskVolume;
                    break;
                }
            }
        }
    }
    HypervisorType rootDiskHyperType = vm.getHypervisorType();
    HypervisorType volumeToAttachHyperType = _volsDao.getHypervisorType(volumeToAttach.getId());
    VolumeInfo newVolumeOnPrimaryStorage = volumeToAttach;
    //don't create volume on primary storage if its being attached to the vm which Root's volume hasn't been created yet
    StoragePoolVO destPrimaryStorage = null;
    if (exstingVolumeOfVm != null && !exstingVolumeOfVm.getState().equals(Volume.State.Allocated)) {
        destPrimaryStorage = _storagePoolDao.findById(exstingVolumeOfVm.getPoolId());
    }
    boolean volumeOnSecondary = volumeToAttach.getState() == Volume.State.Uploaded;
    if (destPrimaryStorage != null && (volumeToAttach.getState() == Volume.State.Allocated || volumeOnSecondary)) {
        try {
            newVolumeOnPrimaryStorage = _volumeMgr.createVolumeOnPrimaryStorage(vm, volumeToAttach, rootDiskHyperType, destPrimaryStorage);
        } catch (NoTransitionException e) {
            s_logger.debug("Failed to create volume on primary storage", e);
            throw new CloudRuntimeException("Failed to create volume on primary storage", e);
        }
    }
    // reload the volume from db
    newVolumeOnPrimaryStorage = volFactory.getVolume(newVolumeOnPrimaryStorage.getId());
    boolean moveVolumeNeeded = needMoveVolume(exstingVolumeOfVm, newVolumeOnPrimaryStorage);
    if (moveVolumeNeeded) {
        PrimaryDataStoreInfo primaryStore = (PrimaryDataStoreInfo) newVolumeOnPrimaryStorage.getDataStore();
        if (primaryStore.isLocal()) {
            throw new CloudRuntimeException("Failed to attach local data volume " + volumeToAttach.getName() + " to VM " + vm.getDisplayName() + " as migration of local data volume is not allowed");
        }
        StoragePoolVO vmRootVolumePool = _storagePoolDao.findById(exstingVolumeOfVm.getPoolId());
        try {
            newVolumeOnPrimaryStorage = _volumeMgr.moveVolume(newVolumeOnPrimaryStorage, vmRootVolumePool.getDataCenterId(), vmRootVolumePool.getPodId(), vmRootVolumePool.getClusterId(), volumeToAttachHyperType);
        } catch (ConcurrentOperationException e) {
            s_logger.debug("move volume failed", e);
            throw new CloudRuntimeException("move volume failed", e);
        } catch (StorageUnavailableException e) {
            s_logger.debug("move volume failed", e);
            throw new CloudRuntimeException("move volume failed", e);
        }
    }
    VolumeVO newVol = _volsDao.findById(newVolumeOnPrimaryStorage.getId());
    // Getting the fresh vm object in case of volume migration to check the current state of VM
    if (moveVolumeNeeded || volumeOnSecondary) {
        vm = _userVmDao.findById(vmId);
        if (vm == null) {
            throw new InvalidParameterValueException("VM not found.");
        }
    }
    newVol = sendAttachVolumeCommand(vm, newVol, deviceId);
    return newVol;
}
Also used : PrimaryDataStoreInfo(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreInfo) UserVmVO(com.cloud.vm.UserVmVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO)

Example 7 with PrimaryDataStoreInfo

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

the class StorageManagerImpl method updateStoragePool.

@Override
public PrimaryDataStoreInfo updateStoragePool(UpdateStoragePoolCmd cmd) throws IllegalArgumentException {
    // Input validation
    Long id = cmd.getId();
    StoragePoolVO pool = _storagePoolDao.findById(id);
    if (pool == null) {
        throw new IllegalArgumentException("Unable to find storage pool with ID: " + id);
    }
    final List<String> storagePoolTags = cmd.getTags();
    if (storagePoolTags != null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Updating Storage Pool Tags to :" + storagePoolTags);
        }
        _storagePoolTagsDao.persist(pool.getId(), storagePoolTags);
    }
    Long updatedCapacityBytes = null;
    Long capacityBytes = cmd.getCapacityBytes();
    if (capacityBytes != null) {
        if (capacityBytes != pool.getCapacityBytes()) {
            updatedCapacityBytes = capacityBytes;
        }
    }
    Long updatedCapacityIops = null;
    Long capacityIops = cmd.getCapacityIops();
    if (capacityIops != null) {
        if (!capacityIops.equals(pool.getCapacityIops())) {
            updatedCapacityIops = capacityIops;
        }
    }
    if (updatedCapacityBytes != null || updatedCapacityIops != null) {
        StoragePoolVO storagePool = _storagePoolDao.findById(id);
        DataStoreProvider dataStoreProvider = _dataStoreProviderMgr.getDataStoreProvider(storagePool.getStorageProviderName());
        DataStoreLifeCycle dataStoreLifeCycle = dataStoreProvider.getDataStoreLifeCycle();
        if (dataStoreLifeCycle instanceof PrimaryDataStoreLifeCycle) {
            Map<String, String> details = new HashMap<String, String>();
            details.put(PrimaryDataStoreLifeCycle.CAPACITY_BYTES, updatedCapacityBytes != null ? String.valueOf(updatedCapacityBytes) : null);
            details.put(PrimaryDataStoreLifeCycle.CAPACITY_IOPS, updatedCapacityIops != null ? String.valueOf(updatedCapacityIops) : null);
            ((PrimaryDataStoreLifeCycle) dataStoreLifeCycle).updateStoragePool(storagePool, details);
        }
    }
    Boolean enabled = cmd.getEnabled();
    if (enabled != null) {
        if (enabled) {
            enablePrimaryStoragePool(pool);
        } else {
            disablePrimaryStoragePool(pool);
        }
    }
    if (updatedCapacityBytes != null) {
        _storagePoolDao.updateCapacityBytes(id, capacityBytes);
    }
    if (updatedCapacityIops != null) {
        _storagePoolDao.updateCapacityIops(id, capacityIops);
    }
    return (PrimaryDataStoreInfo) _dataStoreMgr.getDataStore(pool.getId(), DataStoreRole.Primary);
}
Also used : PrimaryDataStoreInfo(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreInfo) DataStoreLifeCycle(org.apache.cloudstack.engine.subsystem.api.storage.DataStoreLifeCycle) PrimaryDataStoreLifeCycle(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreLifeCycle) HashMap(java.util.HashMap) PrimaryDataStoreLifeCycle(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreLifeCycle) DataStoreProvider(org.apache.cloudstack.engine.subsystem.api.storage.DataStoreProvider) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO)

Example 8 with PrimaryDataStoreInfo

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

the class StorageManagerImpl method preparePrimaryStorageForMaintenance.

@Override
@DB
public PrimaryDataStoreInfo preparePrimaryStorageForMaintenance(Long primaryStorageId) throws ResourceUnavailableException, InsufficientCapacityException {
    StoragePoolVO primaryStorage = null;
    primaryStorage = _storagePoolDao.findById(primaryStorageId);
    if (primaryStorage == null) {
        String msg = "Unable to obtain lock on the storage pool record in preparePrimaryStorageForMaintenance()";
        s_logger.error(msg);
        throw new InvalidParameterValueException(msg);
    }
    if (!primaryStorage.getStatus().equals(StoragePoolStatus.Up) && !primaryStorage.getStatus().equals(StoragePoolStatus.ErrorInMaintenance)) {
        throw new InvalidParameterValueException("Primary storage with id " + primaryStorageId + " is not ready for migration, as the status is:" + primaryStorage.getStatus().toString());
    }
    DataStoreProvider provider = _dataStoreProviderMgr.getDataStoreProvider(primaryStorage.getStorageProviderName());
    DataStoreLifeCycle lifeCycle = provider.getDataStoreLifeCycle();
    DataStore store = _dataStoreMgr.getDataStore(primaryStorage.getId(), DataStoreRole.Primary);
    lifeCycle.maintain(store);
    return (PrimaryDataStoreInfo) _dataStoreMgr.getDataStore(primaryStorage.getId(), DataStoreRole.Primary);
}
Also used : PrimaryDataStoreInfo(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreInfo) DataStoreLifeCycle(org.apache.cloudstack.engine.subsystem.api.storage.DataStoreLifeCycle) PrimaryDataStoreLifeCycle(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreLifeCycle) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DataStoreProvider(org.apache.cloudstack.engine.subsystem.api.storage.DataStoreProvider) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) DB(com.cloud.utils.db.DB)

Example 9 with PrimaryDataStoreInfo

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

the class CloudStackPrimaryDataStoreLifeCycleImpl method attachCluster.

@Override
public boolean attachCluster(DataStore store, ClusterScope scope) {
    PrimaryDataStoreInfo primarystore = (PrimaryDataStoreInfo) store;
    // Check if there is host up in this cluster
    List<HostVO> allHosts = _resourceMgr.listAllUpAndEnabledHosts(Host.Type.Routing, primarystore.getClusterId(), primarystore.getPodId(), primarystore.getDataCenterId());
    if (allHosts.isEmpty()) {
        primaryDataStoreDao.expunge(primarystore.getId());
        throw new CloudRuntimeException("No host up to associate a storage pool with in cluster " + primarystore.getClusterId());
    }
    if (primarystore.getPoolType() == StoragePoolType.OCFS2 && !_ocfs2Mgr.prepareNodes(allHosts, primarystore)) {
        s_logger.warn("Can not create storage pool " + primarystore + " on cluster " + primarystore.getClusterId());
        primaryDataStoreDao.expunge(primarystore.getId());
        return false;
    }
    boolean success = false;
    for (HostVO h : allHosts) {
        success = createStoragePool(h.getId(), primarystore);
        if (success) {
            break;
        }
    }
    s_logger.debug("In createPool Adding the pool to each of the hosts");
    List<HostVO> poolHosts = new ArrayList<HostVO>();
    for (HostVO h : allHosts) {
        try {
            storageMgr.connectHostToSharedPool(h.getId(), primarystore.getId());
            poolHosts.add(h);
        } catch (StorageConflictException se) {
            primaryDataStoreDao.expunge(primarystore.getId());
            throw new CloudRuntimeException("Storage has already been added as local storage");
        } catch (Exception e) {
            s_logger.warn("Unable to establish a connection between " + h + " and " + primarystore, e);
        }
    }
    if (poolHosts.isEmpty()) {
        s_logger.warn("No host can access storage pool " + primarystore + " on cluster " + primarystore.getClusterId());
        primaryDataStoreDao.expunge(primarystore.getId());
        throw new CloudRuntimeException("Failed to access storage pool");
    }
    dataStoreHelper.attachCluster(store);
    return true;
}
Also used : PrimaryDataStoreInfo(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) StorageConflictException(com.cloud.exception.StorageConflictException) HostVO(com.cloud.host.HostVO) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) URISyntaxException(java.net.URISyntaxException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) StorageConflictException(com.cloud.exception.StorageConflictException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Aggregations

PrimaryDataStoreInfo (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreInfo)9 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)5 DataStoreLifeCycle (org.apache.cloudstack.engine.subsystem.api.storage.DataStoreLifeCycle)4 DataStoreProvider (org.apache.cloudstack.engine.subsystem.api.storage.DataStoreProvider)4 PrimaryDataStoreLifeCycle (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreLifeCycle)4 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)3 HostVO (com.cloud.host.HostVO)3 StoragePoolHostVO (com.cloud.storage.StoragePoolHostVO)3 ArrayList (java.util.ArrayList)3 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)3 StorageConflictException (com.cloud.exception.StorageConflictException)2 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)2 DB (com.cloud.utils.db.DB)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 DataCenterVO (com.cloud.dc.DataCenterVO)1 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1