slavkap commented on code in PR #11039: URL: https://github.com/apache/cloudstack/pull/11039#discussion_r2192292529
########## engine/storage/snapshot/src/main/java/org/apache/cloudstack/storage/vmsnapshot/DefaultVMSnapshotStrategy.java: ########## @@ -469,16 +488,54 @@ public boolean deleteVMSnapshotFromDB(VMSnapshot vmSnapshot, boolean unmanage) { @Override public StrategyPriority canHandle(Long vmId, Long rootPoolId, boolean snapshotMemory) { UserVmVO vm = userVmDao.findById(vmId); - if (vm.getState() == State.Running && !snapshotMemory) { + String cantHandleLog = String.format("Default VM snapshot cannot handle VM snapshot for [%s]", vm); + + if (State.Running.equals(vm.getState()) && !snapshotMemory) { + logger.debug("{} as it is running and its memory will not be affected.", cantHandleLog); + return StrategyPriority.CANT_HANDLE; + } + + if (vmHasKvmDiskOnlySnapshot(vm)) { + logger.debug("{} as it is not compatible with disk-only VM snapshot on KVM. As disk-and-memory snapshots use internal snapshots and disk-only VM " + + "snapshots use external snapshots. When restoring external snapshots, any newer internal snapshots are lost.", cantHandleLog); return StrategyPriority.CANT_HANDLE; } List<VolumeVO> volumes = volumeDao.findByInstance(vmId); for (VolumeVO volume : volumes) { if (volume.getFormat() != ImageFormat.QCOW2) { + logger.debug("{} as it has a volume [{}] that is not in the QCOW2 format.", cantHandleLog, volume); + return StrategyPriority.CANT_HANDLE; + } + if (CollectionUtils.isNotEmpty(snapshotDao.listByVolumeIdAndTypeNotInAndStateNotRemoved(volume.getId(), Snapshot.Type.GROUP))) { + logger.debug("{} as it has a volume [{}] with volume snapshots. As disk-and-memory snapshots use internal snapshots and volume snapshots use external" + + " snapshots. When restoring external snapshots, any newer internal snapshots are lost.", cantHandleLog, volume); return StrategyPriority.CANT_HANDLE; } } + + BackupOfferingVO backupOffering = backupOfferingDao.findById(vm.getBackupOfferingId()); + if (backupOffering != null && backupOffering.getProvider().equals("nas")) { + logger.debug("{} as the VM has a backup offering for a provider that is not supported.", cantHandleLog); + return StrategyPriority.CANT_HANDLE; + } + return StrategyPriority.DEFAULT; } + + protected boolean vmHasKvmDiskOnlySnapshot(UserVm vm) { + if (!Hypervisor.HypervisorType.KVM.equals(vm.getHypervisorType())) { + return false; + } + + for (VMSnapshotVO vmSnapshotVO : vmSnapshotDao.findByVmAndByType(vm.getId(), VMSnapshot.Type.Disk)) { + List<VMSnapshotDetailsVO> vmSnapshotDetails = vmSnapshotDetailsDao.listDetails(vmSnapshotVO.getId()); + if (vmSnapshotDetails.stream(). + anyMatch(vmSnapshotDetailsVO -> vmSnapshotDetailsVO.getName().equals(STORAGE_SNAPSHOT))) { Review Comment: ```suggestion anyMatch(vmSnapshotDetailsVO -> STORAGE_SNAPSHOT.equals(vmSnapshotDetailsVO.getName()) { ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@cloudstack.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org