Skip to content

Commit d47e273

Browse files
authored
server: Prevent NPE if hypervisor's capabilities are null (#5029)
If the hypervisor's capabilities are null, CloudRuntimeException will be thrown; Format the error message.
1 parent c6ba3d1 commit d47e273

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6201,22 +6201,16 @@ public VirtualMachine migrateVirtualMachineWithVolume(Long vmId, Host destinatio
62016201

62026202
HypervisorCapabilitiesVO capabilities = _hypervisorCapabilitiesDao.findByHypervisorTypeAndVersion(srcHost.getHypervisorType(), srcHost.getHypervisorVersion());
62036203

6204-
if (capabilities == null && HypervisorType.KVM.equals(srcHost.getHypervisorType())) {
6205-
List<HypervisorCapabilitiesVO> lstHypervisorCapabilities = _hypervisorCapabilitiesDao.listAllByHypervisorType(HypervisorType.KVM);
6206-
6207-
if (lstHypervisorCapabilities != null) {
6208-
for (HypervisorCapabilitiesVO hypervisorCapabilities : lstHypervisorCapabilities) {
6209-
if (hypervisorCapabilities.isStorageMotionSupported()) {
6210-
capabilities = hypervisorCapabilities;
6211-
6212-
break;
6213-
}
6214-
}
6204+
if (capabilities == null) {
6205+
if (!HypervisorType.KVM.equals(srcHost.getHypervisorType())) {
6206+
throw new CloudRuntimeException(String.format("Cannot migrate VM with storage, as the capabilities are not found for the hypervisor %s with version %s", srcHost.getHypervisorType(), srcHost.getHypervisorVersion()));
62156207
}
6216-
}
6208+
List<HypervisorCapabilitiesVO> lstHypervisorCapabilities = _hypervisorCapabilitiesDao.listAllByHypervisorType(HypervisorType.KVM);
62176209

6218-
if (!capabilities.isStorageMotionSupported()) {
6219-
throw new CloudRuntimeException("Migration with storage isn't supported on hypervisor " + srcHost.getHypervisorType() + " of version " + srcHost.getHypervisorVersion());
6210+
capabilities = lstHypervisorCapabilities.stream().filter(hvCapabilities -> hvCapabilities.isStorageMotionSupported()).findAny()
6211+
.orElseThrow(() -> new CloudRuntimeException(String.format("Cannot migrate VM with storage, as the capabilities are not found for the hypervisor %s with version %s", srcHost.getHypervisorType(), srcHost.getHypervisorVersion())));
6212+
} else if (!capabilities.isStorageMotionSupported()) {
6213+
throw new CloudRuntimeException(String.format("Migration with storage isn't supported on hypervisor %s of version %s", srcHost.getHypervisorType(), srcHost.getHypervisorVersion()));
62206214
}
62216215

62226216
// Check if destination host is up.

0 commit comments

Comments
 (0)