diff --git a/plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java b/plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java index 9e283b431f9b..3a54ff9b5bcb 100644 --- a/plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java +++ b/plugins/backup/nas/src/main/java/org/apache/cloudstack/backup/NASBackupProvider.java @@ -225,12 +225,15 @@ protected Host getVMHypervisorHost(VirtualMachine vm) { if (hostId != null) { Host host = hostDao.findById(hostId); - if (host.getStatus() == Status.Up) { - return host; + if (host != null) { + if (host.getStatus() == Status.Up) { + return host; + } + // Try to find any Up host in the same cluster + clusterId = host.getClusterId(); } - // Try to find any Up host in the same cluster - clusterId = host.getClusterId(); - } else { + } + if (clusterId == null) { // Try to find any Up host in the same cluster as the root volume clusterId = getClusterIdFromRootVolume(vm); } @@ -851,7 +854,7 @@ protected void removeBackupAnchors(VirtualMachine vm) { LOG.warn("Failed to remove the backup anchors of VM id={}: {}", vmId, answer != null ? answer.getDetails() : "no answer received"); } - } catch (AgentUnavailableException | OperationTimedoutException | CloudRuntimeException e) { + } catch (AgentUnavailableException | OperationTimedoutException | RuntimeException e) { LOG.warn("Unable to remove the backup anchors of VM id={}: {}", vmId, e.getMessage()); } } diff --git a/plugins/backup/nas/src/test/java/org/apache/cloudstack/backup/NASBackupProviderTest.java b/plugins/backup/nas/src/test/java/org/apache/cloudstack/backup/NASBackupProviderTest.java index 4a28b149c58b..be75b171d9a9 100644 --- a/plugins/backup/nas/src/test/java/org/apache/cloudstack/backup/NASBackupProviderTest.java +++ b/plugins/backup/nas/src/test/java/org/apache/cloudstack/backup/NASBackupProviderTest.java @@ -648,6 +648,36 @@ public void testGetVMHypervisorHostFallbackToZoneWideKVMHost() { Mockito.verify(resourceManager).findOneRandomRunningHostByHypervisor(Hypervisor.HypervisorType.KVM, zoneId); } + @Test + public void testGetVMHypervisorHostWhenLastHostWasRemoved() { + Long hostId = 1L; + Long vmId = 1L; + Long clusterId = 2L; + Long poolId = 3L; + + VMInstanceVO vm = mock(VMInstanceVO.class); + Mockito.when(vm.getLastHostId()).thenReturn(hostId); + Mockito.when(vm.getId()).thenReturn(vmId); + Mockito.when(hostDao.findById(hostId)).thenReturn(null); + + VolumeVO rootVolume = mock(VolumeVO.class); + Mockito.when(rootVolume.getPoolId()).thenReturn(poolId); + Mockito.when(volumeDao.getInstanceRootVolume(vmId)).thenReturn(rootVolume); + StoragePoolVO storagePool = mock(StoragePoolVO.class); + Mockito.when(storagePool.getClusterId()).thenReturn(clusterId); + Mockito.when(storagePoolDao.findById(poolId)).thenReturn(storagePool); + + HostVO upHostInCluster = mock(HostVO.class); + Mockito.when(upHostInCluster.getId()).thenReturn(4L); + Mockito.when(upHostInCluster.getStatus()).thenReturn(Status.Up); + Mockito.when(hostDao.findHypervisorHostInCluster(clusterId)).thenReturn(List.of(upHostInCluster)); + + Host result = nasBackupProvider.getVMHypervisorHost(vm); + + Assert.assertNotNull(result); + Assert.assertEquals(4L, result.getId()); + } + private VolumeVO volume(String uuid, String path, long deviceId) { VolumeVO volume = mock(VolumeVO.class); Mockito.lenient().when(volume.getUuid()).thenReturn(uuid); diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRemoveBackupAnchorsCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRemoveBackupAnchorsCommandWrapper.java index 852fe9a23d8c..f600a62e267f 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRemoveBackupAnchorsCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRemoveBackupAnchorsCommandWrapper.java @@ -20,7 +20,6 @@ package com.cloud.hypervisor.kvm.resource.wrapper; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -51,7 +50,10 @@ public Answer execute(RemoveBackupAnchorsCommand command, LibvirtComputingResour // Only an RBD volume carries an anchor, so the volumes of every other pool type are left out // rather than passed to a script which would have nothing to ask about them. + // diskPaths are RBDStringBuilder strings, which carry the cephx key: they go to the script and + // never into a log, whose volumePaths counterpart names the same volumes without the secret List diskPaths = new ArrayList<>(); + List rbdVolumePaths = new ArrayList<>(); if (Objects.nonNull(volumePaths)) { for (int idx = 0; idx < volumePaths.size(); idx++) { PrimaryDataStoreTO volumePool = volumePools.get(idx); @@ -60,6 +62,7 @@ public Answer execute(RemoveBackupAnchorsCommand command, LibvirtComputingResour } KVMStoragePool volumeStoragePool = storagePoolMgr.getStoragePool(volumePool.getPoolType(), volumePool.getUuid()); diskPaths.add(KVMPhysicalDisk.RBDStringBuilder(volumeStoragePool, volumePaths.get(idx))); + rbdVolumePaths.add(volumePaths.get(idx)); } } @@ -77,10 +80,10 @@ public Answer execute(RemoveBackupAnchorsCommand command, LibvirtComputingResour Pair result = Script.executePipedCommands(commands, libvirtComputingResource.getCmdsTimeout()); logger.debug("Removing the backup anchors of {} returned: {}, exit code: {}", - () -> Arrays.toString(diskPaths.toArray()), result::second, result::first); + () -> rbdVolumePaths, result::second, result::first); if (result.first() != 0) { - logger.warn("Failed to remove the backup anchors of {}: {}", Arrays.toString(diskPaths.toArray()), result.second()); + logger.warn("Failed to remove the backup anchors of {}: {}", rbdVolumePaths, result.second()); return new BackupAnswer(command, false, result.second()); } return new BackupAnswer(command, true, null); diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java index 95cd243ff3fa..bc41bd120dbc 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java @@ -561,6 +561,19 @@ private boolean restoreRbdImageFromDiffChain(KVMStoragePoolManager storagePoolMg } } + // import-diff resizes the image to the size recorded in each diff, which is the size the + // volume had when that backup was taken. The volume may have been grown since, and the + // database keeps the size it has now, so bring the image back to the size it was + // recreated with. + Long restoredSize = rbdImageSize(connection, image); + if (!imageSize.equals(restoredSize)) { + logger.debug("Resizing RBD image {} from {} to {} bytes after applying the backup diff(s)", image, restoredSize, imageSize); + if (Script.executeCommandForExitValue(rbdCommand(connection, "resize", image, "--size", imageSize + "B", "--allow-shrink")) != 0) { + logger.error("Failed to resize RBD image {} to {} bytes after applying the backup diff(s)", image, imageSize); + return false; + } + } + // The snapshots import-diff recreated only existed to chain the diffs together; the // restored volume should look like any other, so drop them. if (Script.executeCommandForExitValue(rbdCommand(connection, "snap", "purge", image)) != 0) { diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java index 15f8d4cdd4ae..fcdbc4ba726b 100644 --- a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java +++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapperTest.java @@ -243,6 +243,119 @@ public void testExecuteRestoresRbdVolumeByReplayingTheWholeDiffChain() throws Ex Arrays.asList(60000L, 60000L), importTimeouts); } + /** + * {@code rbd import-diff} resizes the image to the size recorded in the diff, the size the volume + * had when it was backed up. A volume grown since then keeps its new size in the database, so the + * image has to be brought back to the size it was recreated with. + */ + @Test + public void testExecuteResizesRbdImageBackToItsSizeAfterReplayingTheDiffChain() throws Exception { + when(command.getVmName()).thenReturn("test-vm"); + lenient().when(command.getBackupPath()).thenReturn("test-vm/inc1"); + when(command.getBackupFormat()).thenReturn("rbddiff"); + when(command.getBackupChainPaths()).thenReturn(Arrays.asList("test-vm/full", "test-vm/inc1")); + when(command.getBackupRepoAddress()).thenReturn("192.168.1.100:/backup"); + when(command.getBackupRepoType()).thenReturn("nfs"); + when(command.getMountOptions()).thenReturn("rw"); + when(command.isVmExists()).thenReturn(true); + when(command.getWait()).thenReturn(60); + when(command.getMountTimeout()).thenReturn(30); + PrimaryDataStoreTO primaryDataStore = Mockito.mock(PrimaryDataStoreTO.class); + when(primaryDataStore.getPoolType()).thenReturn(Storage.StoragePoolType.RBD); + when(primaryDataStore.getUuid()).thenReturn("ceph-pool-uuid"); + when(command.getRestoreVolumePools()).thenReturn(Arrays.asList(primaryDataStore)); + when(command.getRestoreVolumePaths()).thenReturn(Arrays.asList("cephpool/vol-uuid")); + when(command.getBackupVolumesUUIDs()).thenReturn(Arrays.asList("vol-uuid")); + when(command.getBackupFiles()).thenReturn(Arrays.asList("vol-uuid")); + + KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class); + KVMStoragePool pool = Mockito.mock(KVMStoragePool.class); + when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr); + when(storagePoolMgr.getStoragePool(any(), anyString())).thenReturn(pool); + lenient().when(pool.getSourceHost()).thenReturn("10.0.1.2"); + lenient().when(pool.getSourcePort()).thenReturn(6789); + + List executed = new ArrayList<>(); + boolean[] imported = {false}; + + try (MockedStatic filesMock = mockStatic(Files.class)) { + Path tempPath = Mockito.mock(Path.class); + when(tempPath.toString()).thenReturn("/tmp/csbackup.abc123"); + filesMock.when(() -> Files.createTempDirectory(anyString())).thenReturn(tempPath); + filesMock.when(() -> Files.deleteIfExists(any(Path.class))).thenReturn(true); + + try (MockedStatic