Search in sources :

Example 1 with HypervisorCapabilitiesVO

use of com.cloud.hypervisor.HypervisorCapabilitiesVO in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method migrateVirtualMachineWithVolume.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_MIGRATE, eventDescription = "migrating VM", async = true)
public VirtualMachine migrateVirtualMachineWithVolume(final Long vmId, final Host destinationHost, final Map<String, String> volumeToPool) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException {
    // Access check - only root administrator can migrate VM.
    final Account caller = CallContext.current().getCallingAccount();
    if (!_accountMgr.isRootAdmin(caller.getId())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Caller is not a root admin, permission denied to migrate the VM");
        }
        throw new PermissionDeniedException("No permission to migrate VM, Only Root Admin can migrate a VM!");
    }
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("Unable to find the vm by id " + vmId);
    }
    if (vm.getState() != State.Running) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("VM is not Running, unable to migrate the vm " + vm);
        }
        final CloudRuntimeException ex = new CloudRuntimeException("VM is not Running, unable to migrate the vm with" + " specified id");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    if (serviceOfferingDetailsDao.findDetail(vm.getServiceOfferingId(), GPU.Keys.pciDevice.toString()) != null) {
        throw new InvalidParameterValueException("Live Migration of GPU enabled VM is not supported");
    }
    if (!vm.getHypervisorType().equals(HypervisorType.XenServer) && !vm.getHypervisorType().equals(HypervisorType.KVM)) {
        throw new InvalidParameterValueException("Unsupported hypervisor type for vm migration, we support" + " XenServer/KVM only");
    }
    final long srcHostId = vm.getHostId();
    final Host srcHost = _resourceMgr.getHost(srcHostId);
    if (srcHost == null) {
        throw new InvalidParameterValueException("Cannot migrate VM, there is not Host with id: " + srcHostId);
    }
    // Check if src and destination hosts are valid and migrating to same host
    if (destinationHost.getId() == srcHostId) {
        throw new InvalidParameterValueException("Cannot migrate VM, VM is already present on this host, please" + " specify valid destination host to migrate the VM");
    }
    // Check if the source and destination hosts are of the same type and support storage motion.
    if (!srcHost.getHypervisorType().equals(destinationHost.getHypervisorType())) {
        throw new CloudRuntimeException("The source and destination hosts are not of the same type " + "Source hypervisor type and version: " + srcHost.getHypervisorType().toString() + " " + srcHost.getHypervisorVersion() + ", Destination hypervisor type and version: " + destinationHost.getHypervisorType().toString() + " " + destinationHost.getHypervisorVersion());
    }
    if (srcHost.getHypervisorVersion() != null && !srcHost.getHypervisorVersion().equals(destinationHost.getHypervisorVersion())) {
        throw new CloudRuntimeException("The source and destination hosts are not of the same version. " + "Source hypervisor type and version: " + srcHost.getHypervisorType().toString() + " " + srcHost.getHypervisorVersion() + ", Destination hypervisor type and version: " + destinationHost.getHypervisorType().toString() + " " + destinationHost.getHypervisorVersion());
    }
    final HypervisorCapabilitiesVO capabilities = _hypervisorCapabilitiesDao.findByHypervisorTypeAndVersion(srcHost.getHypervisorType(), srcHost.getHypervisorVersion());
    if (!capabilities.isStorageMotionSupported()) {
        throw new CloudRuntimeException("Migration with storage isn't supported on hypervisor " + srcHost.getHypervisorType() + " of version " + srcHost.getHypervisorVersion());
    }
    // Check if destination host is up.
    if (destinationHost.getState() != com.cloud.host.Status.Up || destinationHost.getResourceState() != ResourceState.Enabled) {
        throw new CloudRuntimeException("Cannot migrate VM, destination host is not in correct state, has " + "status: " + destinationHost.getState() + ", state: " + destinationHost.getResourceState());
    }
    // Check that Vm does not have VM Snapshots
    if (_vmSnapshotDao.findByVm(vmId).size() > 0) {
        throw new InvalidParameterValueException("VM with VM Snapshots cannot be migrated with storage, please remove all VM snapshots");
    }
    final List<VolumeVO> vmVolumes = _volsDao.findUsableVolumesForInstance(vm.getId());
    final Map<Long, Long> volToPoolObjectMap = new HashMap<>();
    if (!isVMUsingLocalStorage(vm) && destinationHost.getClusterId().equals(srcHost.getClusterId())) {
        if (volumeToPool.isEmpty()) {
            // then fail the call. migrateVirtualMachine api should have been used.
            throw new InvalidParameterValueException("Migration of the vm " + vm + "from host " + srcHost + " to destination host " + destinationHost + " doesn't involve migrating the volumes.");
        }
    }
    if (!volumeToPool.isEmpty()) {
        // Check if all the volumes and pools passed as parameters are valid.
        for (final Map.Entry<String, String> entry : volumeToPool.entrySet()) {
            final VolumeVO volume = _volsDao.findByUuid(entry.getKey());
            final StoragePoolVO pool = _storagePoolDao.findByUuid(entry.getValue());
            if (volume == null) {
                throw new InvalidParameterValueException("There is no volume present with the given id " + entry.getKey());
            } else if (pool == null) {
                throw new InvalidParameterValueException("There is no storage pool present with the given id " + entry.getValue());
            } else if (pool.isInMaintenance()) {
                throw new InvalidParameterValueException("Cannot migrate volume " + volume + "to the destination storage pool " + pool.getName() + " as the storage pool is in maintenance mode.");
            } else {
                // Verify the volume given belongs to the vm.
                if (!vmVolumes.contains(volume)) {
                    throw new InvalidParameterValueException("There volume " + volume + " doesn't belong to " + "the virtual machine " + vm + " that has to be migrated");
                }
                volToPoolObjectMap.put(volume.getId(), pool.getId());
            }
        }
    }
    // Check if all the volumes are in the correct state.
    for (final VolumeVO volume : vmVolumes) {
        if (volume.getState() != Volume.State.Ready) {
            throw new CloudRuntimeException("Volume " + volume + " of the VM is not in Ready state. Cannot " + "migrate the vm with its volumes.");
        }
    }
    // Check max guest vm limit for the destinationHost.
    final HostVO destinationHostVO = _hostDao.findById(destinationHost.getId());
    if (_capacityMgr.checkIfHostReachMaxGuestLimit(destinationHostVO)) {
        throw new VirtualMachineMigrationException("Host name: " + destinationHost.getName() + ", hostId: " + destinationHost.getId() + " already has max running vms (count includes system VMs). Cannot" + " migrate to this host");
    }
    checkHostsDedication(vm, srcHostId, destinationHost.getId());
    _itMgr.migrateWithStorage(vm.getUuid(), srcHostId, destinationHost.getId(), volToPoolObjectMap);
    return _vmDao.findById(vm.getId());
}
Also used : HypervisorCapabilitiesVO(com.cloud.hypervisor.HypervisorCapabilitiesVO) Account(com.cloud.user.Account) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Host(com.cloud.host.Host) HostVO(com.cloud.host.HostVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ActionEvent(com.cloud.event.ActionEvent)

Example 2 with HypervisorCapabilitiesVO

use of com.cloud.hypervisor.HypervisorCapabilitiesVO in project cloudstack by apache.

the class HypervisorCapabilitiesDaoImpl method getHypervisorsWithDefaultEntries.

@Override
public List<HypervisorType> getHypervisorsWithDefaultEntries() {
    SearchCriteria<HypervisorCapabilitiesVO> sc = HypervisorTypeAndVersionSearch.create();
    sc.setParameters("hypervisorVersion", DEFAULT_VERSION);
    List<HypervisorCapabilitiesVO> hypervisorCapabilitiesVOS = listBy(sc);
    List<HypervisorType> hvs = new ArrayList<>();
    for (HypervisorCapabilitiesVO capabilitiesVO : hypervisorCapabilitiesVOS) {
        hvs.add(capabilitiesVO.getHypervisorType());
    }
    return hvs;
}
Also used : HypervisorCapabilitiesVO(com.cloud.hypervisor.HypervisorCapabilitiesVO) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) ArrayList(java.util.ArrayList)

Example 3 with HypervisorCapabilitiesVO

use of com.cloud.hypervisor.HypervisorCapabilitiesVO in project cosmic by MissionCriticalCloud.

the class ManagementServerImpl method addGuestOsMapping.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_GUEST_OS_MAPPING_ADD, eventDescription = "Adding new guest OS to hypervisor name mapping", create = true)
public GuestOSHypervisor addGuestOsMapping(final AddGuestOsMappingCmd cmd) {
    final Long osTypeId = cmd.getOsTypeId();
    final String osStdName = cmd.getOsStdName();
    final String hypervisor = cmd.getHypervisor();
    final String hypervisorVersion = cmd.getHypervisorVersion();
    final String osNameForHypervisor = cmd.getOsNameForHypervisor();
    GuestOS guestOs = null;
    if (osTypeId == null && (osStdName == null || osStdName.isEmpty())) {
        throw new InvalidParameterValueException("Please specify either a guest OS name or UUID");
    }
    final HypervisorType hypervisorType = HypervisorType.getType(hypervisor);
    if (!(hypervisorType == HypervisorType.KVM || hypervisorType == HypervisorType.XenServer)) {
        throw new InvalidParameterValueException("Please specify a valid hypervisor : XenServer or KVM");
    }
    final HypervisorCapabilitiesVO hypervisorCapabilities = _hypervisorCapabilitiesDao.findByHypervisorTypeAndVersion(hypervisorType, hypervisorVersion);
    if (hypervisorCapabilities == null) {
        throw new InvalidParameterValueException("Please specify a valid hypervisor and supported version");
    }
    // by this point either osTypeId or osStdType is non-empty. Find by either of them. ID takes preference if both are specified
    if (osTypeId != null) {
        guestOs = ApiDBUtils.findGuestOSById(osTypeId);
    } else if (osStdName != null) {
        guestOs = ApiDBUtils.findGuestOSByDisplayName(osStdName);
    }
    if (guestOs == null) {
        throw new InvalidParameterValueException("Unable to find the guest OS by name or UUID");
    }
    // check for duplicates
    final GuestOSHypervisorVO duplicate = _guestOSHypervisorDao.findByOsIdAndHypervisorAndUserDefined(guestOs.getId(), hypervisorType.toString(), hypervisorVersion, true);
    if (duplicate != null) {
        throw new InvalidParameterValueException("Mapping from hypervisor : " + hypervisorType.toString() + ", version : " + hypervisorVersion + " and guest OS : " + guestOs.getDisplayName() + " already exists!");
    }
    final GuestOSHypervisorVO guestOsMapping = new GuestOSHypervisorVO();
    guestOsMapping.setGuestOsId(guestOs.getId());
    guestOsMapping.setGuestOsName(osNameForHypervisor);
    guestOsMapping.setHypervisorType(hypervisorType.toString());
    guestOsMapping.setHypervisorVersion(hypervisorVersion);
    guestOsMapping.setIsUserDefined(true);
    return _guestOSHypervisorDao.persist(guestOsMapping);
}
Also used : HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) HypervisorCapabilitiesVO(com.cloud.hypervisor.HypervisorCapabilitiesVO) GuestOSHypervisorVO(com.cloud.storage.GuestOSHypervisorVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) GuestOS(com.cloud.storage.GuestOS) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 4 with HypervisorCapabilitiesVO

use of com.cloud.hypervisor.HypervisorCapabilitiesVO in project cosmic by MissionCriticalCloud.

the class ManagementServerImpl method listHostsForMigrationOfVM.

@Override
public Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Host, Boolean>> listHostsForMigrationOfVM(final Long vmId, final Long startIndex, final Long pageSize, final String keyword) {
    final Account caller = getCaller();
    if (!_accountMgr.isRootAdmin(caller.getId())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Caller is not a root admin, permission denied to migrate the VM");
        }
        throw new PermissionDeniedException("No permission to migrate VM, Only Root Admin can migrate a VM!");
    }
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    if (vm == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find the VM with given id");
        throw ex;
    }
    if (vm.getState() != State.Running) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("VM is not running, cannot migrate the vm" + vm);
        }
        final InvalidParameterValueException ex = new InvalidParameterValueException("VM is not Running, cannot " + "migrate the vm with specified id");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    if (_serviceOfferingDetailsDao.findDetail(vm.getServiceOfferingId(), GPU.Keys.pciDevice.toString()) != null) {
        s_logger.info(" Live Migration of GPU enabled VM : " + vm.getInstanceName() + " is not supported");
        // Return empty list.
        return new Ternary<>(new Pair<>(new ArrayList<HostVO>(), new Integer(0)), new ArrayList<>(), new HashMap<>());
    }
    if (!vm.getHypervisorType().equals(HypervisorType.XenServer) && !vm.getHypervisorType().equals(HypervisorType.KVM)) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug(vm + " is not XenServer/KVM, cannot migrate this VM.");
        }
        throw new InvalidParameterValueException("Unsupported Hypervisor Type for VM migration, we support " + "XenServer/KVM only");
    }
    final long srcHostId = vm.getHostId();
    final Host srcHost = _hostDao.findById(srcHostId);
    if (srcHost == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Unable to find the host with id: " + srcHostId + " of this VM:" + vm);
        }
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find the host (with specified id) of VM with specified id");
        ex.addProxyObject(String.valueOf(srcHostId), "hostId");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    // Check if the vm can be migrated with storage.
    boolean canMigrateWithStorage = false;
    if (vm.getType() == VirtualMachine.Type.User) {
        final HypervisorCapabilitiesVO capabilities = _hypervisorCapabilitiesDao.findByHypervisorTypeAndVersion(srcHost.getHypervisorType(), srcHost.getHypervisorVersion());
        if (capabilities != null) {
            canMigrateWithStorage = capabilities.isStorageMotionSupported();
        }
    }
    // Check if the vm is using any disks on local storage.
    final VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vm, null, _offeringDao.findById(vm.getId(), vm.getServiceOfferingId()), null, null);
    final List<VolumeVO> volumes = _volumeDao.findCreatedByInstance(vmProfile.getId());
    boolean usesLocal = false;
    for (final VolumeVO volume : volumes) {
        final DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId());
        final DiskProfile diskProfile = new DiskProfile(volume, diskOffering, vmProfile.getHypervisorType());
        if (diskProfile.useLocalStorage()) {
            usesLocal = true;
            break;
        }
    }
    if (!canMigrateWithStorage && usesLocal) {
        throw new InvalidParameterValueException("Unsupported operation, VM uses Local storage, cannot migrate");
    }
    final Type hostType = srcHost.getType();
    final Pair<List<HostVO>, Integer> allHostsPair;
    final List<HostVO> allHosts;
    final Map<Host, Boolean> requiresStorageMotion = new HashMap<>();
    final DataCenterDeployment plan;
    if (canMigrateWithStorage) {
        allHostsPair = searchForServers(startIndex, pageSize, null, hostType, null, srcHost.getDataCenterId(), null, null, null, keyword, null, null, srcHost.getHypervisorType(), srcHost.getHypervisorVersion());
        allHosts = allHostsPair.first();
        allHosts.remove(srcHost);
        for (final VolumeVO volume : volumes) {
            final Long volClusterId = _poolDao.findById(volume.getPoolId()).getClusterId();
            // only check for volume which are not in zone wide primary store, as only those may require storage motion
            if (volClusterId != null) {
                for (final Iterator<HostVO> iterator = allHosts.iterator(); iterator.hasNext(); ) {
                    final Host host = iterator.next();
                    if (!host.getClusterId().equals(volClusterId) || usesLocal) {
                        if (hasSuitablePoolsForVolume(volume, host, vmProfile)) {
                            requiresStorageMotion.put(host, true);
                        } else {
                            iterator.remove();
                        }
                    }
                }
            }
        }
        plan = new DataCenterDeployment(srcHost.getDataCenterId(), null, null, null, null, null);
    } else {
        final Long cluster = srcHost.getClusterId();
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Searching for all hosts in cluster " + cluster + " for migrating VM " + vm);
        }
        allHostsPair = searchForServers(startIndex, pageSize, null, hostType, null, null, null, cluster, null, keyword, null, null, null, null);
        // Filter out the current host.
        allHosts = allHostsPair.first();
        allHosts.remove(srcHost);
        plan = new DataCenterDeployment(srcHost.getDataCenterId(), srcHost.getPodId(), srcHost.getClusterId(), null, null, null);
    }
    final Pair<List<? extends Host>, Integer> otherHosts = new Pair<>(allHosts, new Integer(allHosts.size()));
    List<Host> suitableHosts = new ArrayList<>();
    final ExcludeList excludes = new ExcludeList();
    excludes.addHost(srcHostId);
    // call affinitygroup chain
    final long vmGroupCount = _affinityGroupVMMapDao.countAffinityGroupsForVm(vm.getId());
    if (vmGroupCount > 0) {
        for (final AffinityGroupProcessor processor : _affinityProcessors) {
            processor.process(vmProfile, plan, excludes);
        }
    }
    final Account account = vmProfile.getOwner();
    for (final HostVO host : allHosts) {
        final DedicatedResourceVO dedicatedResourceVO = dedicatedResourceDao.findByHostId(host.getId());
        if (dedicatedResourceVO != null && dedicatedResourceVO.getDomainId() != account.getDomainId()) {
            final Domain domain = _domainDao.findById(dedicatedResourceVO.getDomainId());
            if (domain != null) {
                s_logger.debug("Host " + host.getName() + " is dedicated to domain " + domain.getName() + " so not suitable for migration for VM " + vmProfile.getInstanceName());
            }
            excludes.addHost(host.getId());
        }
    }
    for (final HostAllocator allocator : hostAllocators) {
        if (canMigrateWithStorage) {
            suitableHosts = allocator.allocateTo(vmProfile, plan, Host.Type.Routing, excludes, allHosts, HostAllocator.RETURN_UPTO_ALL, false);
        } else {
            suitableHosts = allocator.allocateTo(vmProfile, plan, Host.Type.Routing, excludes, HostAllocator.RETURN_UPTO_ALL, false);
        }
        if (suitableHosts != null && !suitableHosts.isEmpty()) {
            break;
        }
    }
    if (s_logger.isDebugEnabled()) {
        if (suitableHosts.isEmpty()) {
            s_logger.debug("No suitable hosts found");
        } else {
            s_logger.debug("Hosts having capacity and suitable for migration: " + suitableHosts);
        }
    }
    return new Ternary<>(otherHosts, suitableHosts, requiresStorageMotion);
}
Also used : HypervisorCapabilitiesVO(com.cloud.hypervisor.HypervisorCapabilitiesVO) Account(com.cloud.user.Account) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HostAllocator(com.cloud.agent.manager.allocator.HostAllocator) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) AffinityGroupProcessor(com.cloud.affinity.AffinityGroupProcessor) Pair(com.cloud.utils.Pair) SSHKeyPair(com.cloud.user.SSHKeyPair) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) Ternary(com.cloud.utils.Ternary) VirtualMachineProfileImpl(com.cloud.vm.VirtualMachineProfileImpl) VMInstanceVO(com.cloud.vm.VMInstanceVO) Host(com.cloud.host.Host) DiskProfile(com.cloud.vm.DiskProfile) HostVO(com.cloud.host.HostVO) ResourceObjectType(com.cloud.server.ResourceTag.ResourceObjectType) VlanType(com.cloud.dc.Vlan.VlanType) JoinType(com.cloud.utils.db.JoinBuilder.JoinType) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) Type(com.cloud.host.Host.Type) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) Domain(com.cloud.domain.Domain)

Example 5 with HypervisorCapabilitiesVO

use of com.cloud.hypervisor.HypervisorCapabilitiesVO in project cosmic by MissionCriticalCloud.

the class ManagementServerImpl method listStoragePoolsForMigrationOfVolume.

@Override
public Pair<List<? extends StoragePool>, List<? extends StoragePool>> listStoragePoolsForMigrationOfVolume(final Long volumeId) {
    final Account caller = getCaller();
    if (!_accountMgr.isRootAdmin(caller.getId())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Caller is not a root admin, permission denied to migrate the volume");
        }
        throw new PermissionDeniedException("No permission to migrate volume, only root admin can migrate a volume");
    }
    final VolumeVO volume = _volumeDao.findById(volumeId);
    if (volume == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find volume with" + " specified id.");
        ex.addProxyObject(volumeId.toString(), "volumeId");
        throw ex;
    }
    // Volume must be attached to an instance for live migration.
    final List<StoragePool> allPools = new ArrayList<>();
    final List<StoragePool> suitablePools = new ArrayList<>();
    // Volume must be in Ready state to be migrated.
    if (!Volume.State.Ready.equals(volume.getState())) {
        s_logger.info("Volume " + volume + " must be in ready state for migration.");
        return new Pair<>(allPools, suitablePools);
    }
    if (!_volumeMgr.volumeOnSharedStoragePool(volume)) {
        s_logger.info("Volume " + volume + " is on local storage. It cannot be migrated to another pool.");
        return new Pair<>(allPools, suitablePools);
    }
    final Long instanceId = volume.getInstanceId();
    VMInstanceVO vm = null;
    if (instanceId != null) {
        vm = _vmInstanceDao.findById(instanceId);
    }
    if (vm == null) {
        s_logger.info("Volume " + volume + " isn't attached to any vm. Looking for storage pools in the " + "zone to which this volumes can be migrated.");
    } else if (vm.getState() != State.Running) {
        s_logger.info("Volume " + volume + " isn't attached to any running vm. Looking for storage pools in the " + "cluster to which this volumes can be migrated.");
    } else {
        s_logger.info("Volume " + volume + " is attached to any running vm. Looking for storage pools in the " + "cluster to which this volumes can be migrated.");
        boolean storageMotionSupported = false;
        // Check if the underlying hypervisor supports storage motion.
        final Long hostId = vm.getHostId();
        if (hostId != null) {
            final HostVO host = _hostDao.findById(hostId);
            HypervisorCapabilitiesVO capabilities = null;
            if (host != null) {
                capabilities = _hypervisorCapabilitiesDao.findByHypervisorTypeAndVersion(host.getHypervisorType(), host.getHypervisorVersion());
            } else {
                s_logger.error("Details of the host on which the vm " + vm + ", to which volume " + volume + " is " + "attached, couldn't be retrieved.");
            }
            if (capabilities != null) {
                storageMotionSupported = capabilities.isStorageMotionSupported();
            } else {
                s_logger.error("Capabilities for host " + host + " couldn't be retrieved.");
            }
        }
        if (!storageMotionSupported) {
            s_logger.info("Volume " + volume + " is attached to a running vm and the hypervisor doesn't support" + " storage motion.");
            return new Pair<>(allPools, suitablePools);
        }
    }
    // Source pool of the volume.
    final StoragePoolVO srcVolumePool = _poolDao.findById(volume.getPoolId());
    // Get all the pools available. Only shared pools are considered because only a volume on a shared pools
    // can be live migrated while the virtual machine stays on the same host.
    final List<StoragePoolVO> storagePools;
    if (srcVolumePool.getClusterId() == null) {
        storagePools = _poolDao.findZoneWideStoragePoolsByTags(volume.getDataCenterId(), null);
    } else {
        storagePools = _poolDao.findPoolsByTags(volume.getDataCenterId(), srcVolumePool.getPodId(), srcVolumePool.getClusterId(), null);
    }
    storagePools.remove(srcVolumePool);
    for (final StoragePoolVO pool : storagePools) {
        if (pool.isShared()) {
            allPools.add((StoragePool) dataStoreMgr.getPrimaryDataStore(pool.getId()));
        }
    }
    // Get all the suitable pools.
    // Exclude the current pool from the list of pools to which the volume can be migrated.
    final ExcludeList avoid = new ExcludeList();
    avoid.addPool(srcVolumePool.getId());
    // Volume stays in the same cluster after migration.
    final DataCenterDeployment plan = new DataCenterDeployment(volume.getDataCenterId(), srcVolumePool.getPodId(), srcVolumePool.getClusterId(), null, null, null);
    final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
    final DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId());
    final DiskProfile diskProfile = new DiskProfile(volume, diskOffering, profile.getHypervisorType());
    // Call the storage pool allocator to find the list of storage pools.
    for (final StoragePoolAllocator allocator : _storagePoolAllocators) {
        final List<StoragePool> pools = allocator.allocateToPool(diskProfile, profile, plan, avoid, StoragePoolAllocator.RETURN_UPTO_ALL);
        if (pools != null && !pools.isEmpty()) {
            suitablePools.addAll(pools);
            break;
        }
    }
    return new Pair<>(allPools, suitablePools);
}
Also used : HypervisorCapabilitiesVO(com.cloud.hypervisor.HypervisorCapabilitiesVO) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) Account(com.cloud.user.Account) StoragePool(com.cloud.storage.StoragePool) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) VirtualMachineProfileImpl(com.cloud.vm.VirtualMachineProfileImpl) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) DiskProfile(com.cloud.vm.DiskProfile) HostVO(com.cloud.host.HostVO) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) StoragePoolAllocator(com.cloud.engine.subsystem.api.storage.StoragePoolAllocator) Pair(com.cloud.utils.Pair) SSHKeyPair(com.cloud.user.SSHKeyPair)

Aggregations

HypervisorCapabilitiesVO (com.cloud.hypervisor.HypervisorCapabilitiesVO)16 HostVO (com.cloud.host.HostVO)6 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)6 Pair (com.cloud.utils.Pair)6 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)6 ArrayList (java.util.ArrayList)6 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)5 ActionEvent (com.cloud.event.ActionEvent)5 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 Account (com.cloud.user.Account)5 SSHKeyPair (com.cloud.user.SSHKeyPair)5 VMInstanceVO (com.cloud.vm.VMInstanceVO)5 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)4 VolumeVO (com.cloud.storage.VolumeVO)4 DB (com.cloud.utils.db.DB)4 List (java.util.List)4 Host (com.cloud.host.Host)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 DiskProfile (com.cloud.vm.DiskProfile)3 DataCenterDeployment (com.cloud.deploy.DataCenterDeployment)2