From 12d3e925024d1856562315ccd7d3d045f8755c6f Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Thu, 26 Nov 2026 11:57:28 +0100 Subject: [PATCH 1/5] nas: restore an RBD image to its own size after replaying the diff chain rbd import-diff resizes the image to the size recorded in each diff, which is the size the volume had when that backup was taken. A volume grown since then keeps its new size in the database, so the restored image would be smaller than the volume. Resize the image back to the size it was recreated with once the chain has been replayed. --- .../LibvirtRestoreBackupCommandWrapper.java | 13 ++ ...ibvirtRestoreBackupCommandWrapperTest.java | 113 ++++++++++++++++++ 2 files changed, 126 insertions(+) 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