use of com.cloud.storage.StoragePool in project cloudstack by apache.
the class ManagementServerImpl method findAllSuitableStoragePoolsForDetachedVolume.
private List<StoragePool> findAllSuitableStoragePoolsForDetachedVolume(Volume volume, Long diskOfferingId, List<? extends StoragePool> allPools) {
List<StoragePool> suitablePools = new ArrayList<>();
if (CollectionUtils.isEmpty(allPools)) {
return suitablePools;
}
DiskOfferingVO diskOffering = _diskOfferingDao.findById(diskOfferingId);
List<String> tags = new ArrayList<>();
String[] tagsArray = diskOffering.getTagsArray();
if (tagsArray != null && tagsArray.length > 0) {
tags = Arrays.asList(tagsArray);
}
Long[] poolIds = allPools.stream().map(StoragePool::getId).toArray(Long[]::new);
List<StoragePoolJoinVO> pools = _poolJoinDao.searchByIds(poolIds);
for (StoragePoolJoinVO storagePool : pools) {
if (StoragePoolStatus.Up.equals(storagePool.getStatus()) && (CollectionUtils.isEmpty(tags) || tags.contains(storagePool.getTag()))) {
Optional<? extends StoragePool> match = allPools.stream().filter(x -> x.getId() == storagePool.getId()).findFirst();
match.ifPresent(suitablePools::add);
}
}
return suitablePools;
}
use of com.cloud.storage.StoragePool in project cloudstack by apache.
the class ManagementServerImpl method hasSuitablePoolsForVolume.
private boolean hasSuitablePoolsForVolume(final VolumeVO volume, final Host host, final VirtualMachineProfile vmProfile) {
final DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId());
final DiskProfile diskProfile = new DiskProfile(volume, diskOffering, vmProfile.getHypervisorType());
final DataCenterDeployment plan = new DataCenterDeployment(host.getDataCenterId(), host.getPodId(), host.getClusterId(), host.getId(), null, null);
final ExcludeList avoid = new ExcludeList();
for (final StoragePoolAllocator allocator : _storagePoolAllocators) {
final List<StoragePool> poolList = allocator.allocateToPool(diskProfile, vmProfile, plan, avoid, 1);
if (poolList != null && !poolList.isEmpty()) {
return true;
}
}
return false;
}
use of com.cloud.storage.StoragePool in project cloudstack by apache.
the class ManagementServerImpl method searchForSystemVm.
@Override
public Pair<List<? extends VirtualMachine>, Integer> searchForSystemVm(final ListSystemVMsCmd cmd) {
final String type = cmd.getSystemVmType();
final Long zoneId = _accountMgr.checkAccessAndSpecifyAuthority(CallContext.current().getCallingAccount(), cmd.getZoneId());
final Long id = cmd.getId();
final String name = cmd.getSystemVmName();
final String state = cmd.getState();
final String keyword = cmd.getKeyword();
final Long podId = cmd.getPodId();
final Long hostId = cmd.getHostId();
final Long storageId = cmd.getStorageId();
final Filter searchFilter = new Filter(VMInstanceVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
final SearchBuilder<VMInstanceVO> sb = _vmInstanceDao.createSearchBuilder();
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
sb.and("hostName", sb.entity().getHostName(), SearchCriteria.Op.LIKE);
sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
sb.and("podId", sb.entity().getPodIdToDeployIn(), SearchCriteria.Op.EQ);
sb.and("hostId", sb.entity().getHostId(), SearchCriteria.Op.EQ);
sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
sb.and("nulltype", sb.entity().getType(), SearchCriteria.Op.IN);
if (storageId != null) {
StoragePoolVO storagePool = _primaryDataStoreDao.findById(storageId);
if (storagePool.getPoolType() == Storage.StoragePoolType.DatastoreCluster) {
final SearchBuilder<VolumeVO> volumeSearch = _volumeDao.createSearchBuilder();
volumeSearch.and("poolId", volumeSearch.entity().getPoolId(), SearchCriteria.Op.IN);
sb.join("volumeSearch", volumeSearch, sb.entity().getId(), volumeSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
} else {
final SearchBuilder<VolumeVO> volumeSearch = _volumeDao.createSearchBuilder();
volumeSearch.and("poolId", volumeSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
sb.join("volumeSearch", volumeSearch, sb.entity().getId(), volumeSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
}
}
final SearchCriteria<VMInstanceVO> sc = sb.create();
if (keyword != null) {
final SearchCriteria<VMInstanceVO> ssc = _vmInstanceDao.createSearchCriteria();
ssc.addOr("hostName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
ssc.addOr("state", SearchCriteria.Op.LIKE, "%" + keyword + "%");
sc.addAnd("hostName", SearchCriteria.Op.SC, ssc);
}
if (id != null) {
sc.setParameters("id", id);
}
if (name != null) {
sc.setParameters("hostName", name);
}
if (state != null) {
sc.setParameters("state", state);
}
if (zoneId != null) {
sc.setParameters("dataCenterId", zoneId);
}
if (podId != null) {
sc.setParameters("podId", podId);
}
if (hostId != null) {
sc.setParameters("hostId", hostId);
}
if (type != null) {
sc.setParameters("type", type);
} else {
sc.setParameters("nulltype", VirtualMachine.Type.SecondaryStorageVm, VirtualMachine.Type.ConsoleProxy);
}
if (storageId != null) {
StoragePoolVO storagePool = _primaryDataStoreDao.findById(storageId);
if (storagePool.getPoolType() == Storage.StoragePoolType.DatastoreCluster) {
List<StoragePoolVO> childDataStores = _primaryDataStoreDao.listChildStoragePoolsInDatastoreCluster(storageId);
List<Long> childDatastoreIds = childDataStores.stream().map(mo -> mo.getId()).collect(Collectors.toList());
sc.setJoinParameters("volumeSearch", "poolId", childDatastoreIds.toArray());
} else {
sc.setJoinParameters("volumeSearch", "poolId", storageId);
}
}
final Pair<List<VMInstanceVO>, Integer> result = _vmInstanceDao.searchAndCount(sc, searchFilter);
return new Pair<List<? extends VirtualMachine>, Integer>(result.first(), result.second());
}
use of com.cloud.storage.StoragePool in project cloudstack by apache.
the class UnmanagedVMsManagerImpl method importDisk.
private Pair<DiskProfile, StoragePool> importDisk(UnmanagedInstanceTO.Disk disk, VirtualMachine vm, Cluster cluster, DiskOffering diskOffering, Volume.Type type, String name, Long diskSize, Long minIops, Long maxIops, VirtualMachineTemplate template, Account owner, Long deviceId) {
final DataCenter zone = dataCenterDao.findById(vm.getDataCenterId());
final String path = StringUtils.isEmpty(disk.getFileBaseName()) ? disk.getImagePath() : disk.getFileBaseName();
String chainInfo = disk.getChainInfo();
if (StringUtils.isEmpty(chainInfo)) {
VirtualMachineDiskInfo diskInfo = new VirtualMachineDiskInfo();
diskInfo.setDiskDeviceBusName(String.format("%s%d:%d", disk.getController(), disk.getControllerUnit(), disk.getPosition()));
diskInfo.setDiskChain(new String[] { disk.getImagePath() });
chainInfo = gson.toJson(diskInfo);
}
StoragePool storagePool = getStoragePool(disk, zone, cluster);
DiskProfile profile = volumeManager.importVolume(type, name, diskOffering, diskSize, minIops, maxIops, vm, template, owner, deviceId, storagePool.getId(), path, chainInfo);
return new Pair<DiskProfile, StoragePool>(profile, storagePool);
}
use of com.cloud.storage.StoragePool in project cloudstack by apache.
the class UnmanagedVMsManagerImpl method migrateImportedVM.
private UserVm migrateImportedVM(HostVO sourceHost, VirtualMachineTemplate template, ServiceOfferingVO serviceOffering, UserVm userVm, final Account owner, List<Pair<DiskProfile, StoragePool>> diskProfileStoragePoolList) {
UserVm vm = userVm;
if (vm == null) {
LOGGER.error(String.format("Failed to check migrations need during VM import"));
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Failed to check migrations need during VM import"));
}
if (sourceHost == null || serviceOffering == null || diskProfileStoragePoolList == null) {
LOGGER.error(String.format("Failed to check migrations need during import, VM: %s", userVm.getInstanceName()));
cleanupFailedImportVM(vm);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Failed to check migrations need during import, VM: %s", userVm.getInstanceName()));
}
if (!hostSupportsServiceOffering(sourceHost, serviceOffering)) {
LOGGER.debug(String.format("VM %s needs to be migrated", vm.getUuid()));
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm, template, serviceOffering, owner, null);
DeploymentPlanner.ExcludeList excludeList = new DeploymentPlanner.ExcludeList();
excludeList.addHost(sourceHost.getId());
final DataCenterDeployment plan = new DataCenterDeployment(sourceHost.getDataCenterId(), sourceHost.getPodId(), sourceHost.getClusterId(), null, null, null);
DeployDestination dest = null;
try {
dest = deploymentPlanningManager.planDeployment(profile, plan, excludeList, null);
} catch (Exception e) {
LOGGER.warn(String.format("VM import failed for unmanaged vm: %s during vm migration, finding deployment destination", vm.getInstanceName()), e);
cleanupFailedImportVM(vm);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("VM import failed for unmanaged vm: %s during vm migration, finding deployment destination", vm.getInstanceName()));
}
if (dest != null) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(" Found " + dest + " for migrating the vm to");
}
}
if (dest == null) {
cleanupFailedImportVM(vm);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("VM import failed for unmanaged vm: %s during vm migration, no deployment destination found", vm.getInstanceName()));
}
try {
if (vm.getState().equals(VirtualMachine.State.Stopped)) {
VMInstanceVO vmInstanceVO = vmDao.findById(userVm.getId());
vmInstanceVO.setHostId(dest.getHost().getId());
vmInstanceVO.setLastHostId(dest.getHost().getId());
vmDao.update(vmInstanceVO.getId(), vmInstanceVO);
} else {
virtualMachineManager.migrate(vm.getUuid(), sourceHost.getId(), dest);
}
vm = userVmManager.getUserVm(vm.getId());
} catch (Exception e) {
LOGGER.error(String.format("VM import failed for unmanaged vm: %s during vm migration", vm.getInstanceName()), e);
cleanupFailedImportVM(vm);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("VM import failed for unmanaged vm: %s during vm migration. %s", userVm.getInstanceName(), e.getMessage()));
}
}
for (Pair<DiskProfile, StoragePool> diskProfileStoragePool : diskProfileStoragePoolList) {
if (diskProfileStoragePool == null || diskProfileStoragePool.first() == null || diskProfileStoragePool.second() == null) {
continue;
}
DiskProfile profile = diskProfileStoragePool.first();
DiskOffering dOffering = diskOfferingDao.findById(profile.getDiskOfferingId());
if (dOffering == null) {
continue;
}
VolumeVO volumeVO = volumeDao.findById(profile.getVolumeId());
if (volumeVO == null) {
continue;
}
boolean poolSupportsOfferings = storagePoolSupportsDiskOffering(diskProfileStoragePool.second(), dOffering);
if (poolSupportsOfferings) {
continue;
}
LOGGER.debug(String.format("Volume %s needs to be migrated", volumeVO.getUuid()));
Pair<List<? extends StoragePool>, List<? extends StoragePool>> poolsPair = managementService.listStoragePoolsForMigrationOfVolumeInternal(profile.getVolumeId(), null, null, null, null, false);
if (CollectionUtils.isEmpty(poolsPair.first()) && CollectionUtils.isEmpty(poolsPair.second())) {
cleanupFailedImportVM(vm);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("VM import failed for unmanaged vm: %s during volume ID: %s migration as no suitable pool(s) found", userVm.getInstanceName(), volumeVO.getUuid()));
}
List<? extends StoragePool> storagePools = poolsPair.second();
StoragePool storagePool = null;
if (CollectionUtils.isNotEmpty(storagePools)) {
for (StoragePool pool : storagePools) {
if (diskProfileStoragePool.second().getId() != pool.getId() && storagePoolSupportsDiskOffering(pool, dOffering)) {
storagePool = pool;
break;
}
}
}
// For zone-wide pools, at times, suitable storage pools are not returned therefore consider all pools.
if (storagePool == null && CollectionUtils.isNotEmpty(poolsPair.first())) {
storagePools = poolsPair.first();
for (StoragePool pool : storagePools) {
if (diskProfileStoragePool.second().getId() != pool.getId() && storagePoolSupportsDiskOffering(pool, dOffering)) {
storagePool = pool;
break;
}
}
}
if (storagePool == null) {
cleanupFailedImportVM(vm);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("VM import failed for unmanaged vm: %s during volume ID: %s migration as no suitable pool found", userVm.getInstanceName(), volumeVO.getUuid()));
} else {
LOGGER.debug(String.format("Found storage pool %s(%s) for migrating the volume %s to", storagePool.getName(), storagePool.getUuid(), volumeVO.getUuid()));
}
try {
Volume volume = null;
if (vm.getState().equals(VirtualMachine.State.Running)) {
volume = volumeManager.liveMigrateVolume(volumeVO, storagePool);
} else {
volume = volumeManager.migrateVolume(volumeVO, storagePool);
}
if (volume == null) {
String msg = "";
if (vm.getState().equals(VirtualMachine.State.Running)) {
msg = String.format("Live migration for volume ID: %s to destination pool ID: %s failed", volumeVO.getUuid(), storagePool.getUuid());
} else {
msg = String.format("Migration for volume ID: %s to destination pool ID: %s failed", volumeVO.getUuid(), storagePool.getUuid());
}
LOGGER.error(msg);
throw new CloudRuntimeException(msg);
}
} catch (Exception e) {
LOGGER.error(String.format("VM import failed for unmanaged vm: %s during volume migration", vm.getInstanceName()), e);
cleanupFailedImportVM(vm);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("VM import failed for unmanaged vm: %s during volume migration. %s", userVm.getInstanceName(), StringUtils.defaultString(e.getMessage())));
}
}
return userVm;
}
Aggregations