From ebc888071151aeb99e97db373614dfddd0e2df97 Mon Sep 17 00:00:00 2001 From: DT Date: Thu, 3 Dec 2026 15:56:30 +0000 Subject: [PATCH] KVM: migrate volumes without a backing file as full clones migrateVirtualMachineWithVolume treated a volume as a linked clone whenever it had a template ID and that template was cached on the volume's current pool. It never looked at the disk. A volume with no backing file, because an offline migrateVolume flattened it or it was created with create.full.clone=true, was then created on the destination on top of the template and copied incrementally. Every region that wasn't allocated in the source file, including ranges the guest had zeroed, read template data afterwards, and the job reported success. Before choosing the linked-clone path for a volume on a file-based pool (NFS, SharedMountPoint or local), the management server now asks the source host for the volume's backing file with GetVolumesOnStorageCommand. A volume without one is migrated as a full clone. If the host can't answer, the migration fails with an error before anything is created for that volume. Fixes #14963 Generated-by: Claude Code (claude-mythos-5-2) --- .../StorageSystemDataMotionStrategy.java | 39 ++++++++++- .../StorageSystemDataMotionStrategyTest.java | 68 +++++++++++++++++++ 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java b/engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java index bcade3a371c4..08be4867b86e 100644 --- a/engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java +++ b/engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategy.java @@ -76,12 +76,15 @@ import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; import org.apache.cloudstack.storage.to.PrimaryDataStoreTO; import org.apache.cloudstack.storage.to.VolumeObjectTO; +import org.apache.cloudstack.storage.volume.VolumeOnStorageTO; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; import com.cloud.agent.AgentManager; import com.cloud.agent.api.Answer; +import com.cloud.agent.api.GetVolumesOnStorageAnswer; +import com.cloud.agent.api.GetVolumesOnStorageCommand; import com.cloud.agent.api.MigrateAnswer; import com.cloud.agent.api.MigrateCommand; import com.cloud.agent.api.MigrateCommand.MigrateDiskInfo; @@ -96,6 +99,7 @@ import com.cloud.agent.api.to.DataTO; import com.cloud.agent.api.to.DiskTO; import com.cloud.agent.api.to.NfsTO; +import com.cloud.agent.api.to.StorageFilerTO; import com.cloud.agent.api.to.VirtualMachineTO; import com.cloud.configuration.Config; import com.cloud.dc.dao.ClusterDao; @@ -2056,7 +2060,7 @@ public void copyAsync(Map volumeDataStoreMap, VirtualMach continue; } - MigrationOptions.Type migrationType = decideMigrationTypeAndCopyTemplateIfNeeded(destHost, vmInstance, srcVolumeInfo, sourceStoragePool, destStoragePool, destDataStore); + MigrationOptions.Type migrationType = decideMigrationTypeAndCopyTemplateIfNeeded(srcHost, destHost, vmInstance, srcVolumeInfo, sourceStoragePool, destStoragePool, destDataStore); migrateNonSharedInc = migrateNonSharedInc || MigrationOptions.Type.LinkedClone.equals(migrationType); VolumeVO destVolume = duplicateVolumeOnAnotherStorage(srcVolume, destStoragePool); @@ -2211,13 +2215,14 @@ public void copyAsync(Map volumeDataStoreMap, VirtualMach } } - private MigrationOptions.Type decideMigrationTypeAndCopyTemplateIfNeeded(Host destHost, VMInstanceVO vmInstance, VolumeInfo srcVolumeInfo, StoragePoolVO sourceStoragePool, StoragePoolVO destStoragePool, DataStore destDataStore) { + private MigrationOptions.Type decideMigrationTypeAndCopyTemplateIfNeeded(Host srcHost, Host destHost, VMInstanceVO vmInstance, VolumeInfo srcVolumeInfo, StoragePoolVO sourceStoragePool, StoragePoolVO destStoragePool, DataStore destDataStore) { VMTemplateVO vmTemplate = _vmTemplateDao.findById(vmInstance.getTemplateId()); String srcVolumeBackingFile = getVolumeBackingFile(srcVolumeInfo); if (StringUtils.isNotBlank(srcVolumeBackingFile) && supportStoragePoolType(destStoragePool.getPoolType(), StoragePoolType.Filesystem) && srcVolumeInfo.getTemplateId() != null && Objects.nonNull(vmTemplate) && - !Arrays.asList(KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME, VM_IMPORT_DEFAULT_TEMPLATE_NAME).contains(vmTemplate.getName())) { + !Arrays.asList(KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME, VM_IMPORT_DEFAULT_TEMPLATE_NAME).contains(vmTemplate.getName()) && + hasBackingFileOnSourceHost(srcHost, srcVolumeInfo, sourceStoragePool)) { logger.debug(String.format("Copying template [%s] of volume [%s] from source storage pool [%s] to target storage pool [%s].", srcVolumeInfo.getTemplateId(), srcVolumeInfo.getId(), sourceStoragePool.getId(), destStoragePool.getId())); copyTemplateToTargetFilesystemStorageIfNeeded(srcVolumeInfo, sourceStoragePool, destDataStore, destStoragePool, destHost); return MigrationOptions.Type.LinkedClone; @@ -2227,6 +2232,34 @@ private MigrationOptions.Type decideMigrationTypeAndCopyTemplateIfNeeded(Host de return MigrationOptions.Type.FullClone; } + /** + * Checks on the source host if the file of the volume has a backing file. The template reference of the storage pool only tells that the template is cached there; it + * does not tell that the volume still depends on it. A volume that was flattened (for instance, by an offline migration to another storage pool) or that was created as + * a full clone has no backing file, and migrating it as a linked clone would make the template data show through every region that is not allocated in the volume file. + * Returns true without asking the host if the source storage pool does not keep volumes as files. + */ + protected boolean hasBackingFileOnSourceHost(Host srcHost, VolumeInfo srcVolumeInfo, StoragePoolVO sourceStoragePool) { + if (!supportStoragePoolType(sourceStoragePool.getPoolType(), StoragePoolType.Filesystem)) { + return true; + } + + GetVolumesOnStorageCommand command = new GetVolumesOnStorageCommand(new StorageFilerTO(sourceStoragePool), srcVolumeInfo.getPath(), null); + Answer answer = agentManager.easySend(srcHost.getId(), command); + if (!(answer instanceof GetVolumesOnStorageAnswer) || !answer.getResult() || CollectionUtils.isEmpty(((GetVolumesOnStorageAnswer) answer).getVolumes())) { + String reason = answer != null ? answer.getDetails() : "no answer from the host"; + throw new CloudRuntimeException(String.format("Unable to check on host [%s] if volume [%s] has a backing file, which is required to migrate it safely: %s", + srcHost.getUuid(), srcVolumeInfo.getUuid(), reason)); + } + + Map details = ((GetVolumesOnStorageAnswer) answer).getVolumes().get(0).getDetails(); + boolean hasBackingFile = details != null && StringUtils.isNotBlank(details.get(VolumeOnStorageTO.Detail.BACKING_FILE)); + if (!hasBackingFile) { + logger.info("Volume [{}] has template [{}] cached on storage pool [{}], but its file has no backing file on host [{}]; it will be migrated as a full clone.", + srcVolumeInfo.getUuid(), srcVolumeInfo.getTemplateId(), sourceStoragePool.getUuid(), srcHost.getUuid()); + } + return hasBackingFile; + } + protected String formatMigrationElementsAsJsonToDisplayOnLog(String objectName, Object object, Object from, Object to){ return String.format("{%s: \"%s\", from: \"%s\", to:\"%s\"}", objectName, object, from, to); } diff --git a/engine/storage/datamotion/src/test/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategyTest.java b/engine/storage/datamotion/src/test/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategyTest.java index 45357fa64b2a..086e37aa8fd1 100644 --- a/engine/storage/datamotion/src/test/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategyTest.java +++ b/engine/storage/datamotion/src/test/java/org/apache/cloudstack/storage/motion/StorageSystemDataMotionStrategyTest.java @@ -25,6 +25,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.MockitoAnnotations.initMocks; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -38,6 +39,7 @@ import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; import org.apache.cloudstack.storage.image.store.ImageStoreImpl; import org.apache.cloudstack.storage.volume.VolumeObject; +import org.apache.cloudstack.storage.volume.VolumeOnStorageTO; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -48,6 +50,10 @@ import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; +import com.cloud.agent.AgentManager; +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.GetVolumesOnStorageAnswer; +import com.cloud.agent.api.GetVolumesOnStorageCommand; import com.cloud.agent.api.MigrateCommand; import com.cloud.host.HostVO; import com.cloud.storage.DataStoreRole; @@ -57,6 +63,7 @@ import com.cloud.storage.Storage.StoragePoolType; import com.cloud.storage.Volume; import com.cloud.storage.VolumeVO; +import com.cloud.utils.exception.CloudRuntimeException; import java.util.AbstractMap; import java.util.Arrays; import java.util.HashSet; @@ -80,6 +87,8 @@ public class StorageSystemDataMotionStrategyTest { private ImageStore destinationStore; @Mock private PrimaryDataStoreDao primaryDataStoreDao; + @Mock + private AgentManager agentManager; @Mock StoragePoolVO sourceStoragePoolVoMock, destinationStoragePoolVoMock; @@ -369,4 +378,63 @@ public void validateIsStoragePoolTypeInListReturnsFalse() { assertFalse(strategy.isStoragePoolTypeInList(StoragePoolType.SharedMountPoint, listTypes)); } + + private GetVolumesOnStorageAnswer mockAnswerWithVolume(String backingFile) { + VolumeOnStorageTO volumeOnStorage = new VolumeOnStorageTO(); + if (backingFile != null) { + volumeOnStorage.addDetail(VolumeOnStorageTO.Detail.BACKING_FILE, backingFile); + } + GetVolumesOnStorageAnswer answer = mock(GetVolumesOnStorageAnswer.class); + doReturn(true).when(answer).getResult(); + doReturn(Collections.singletonList(volumeOnStorage)).when(answer).getVolumes(); + return answer; + } + + private boolean hasBackingFileOnSourceHost(StoragePoolType sourcePoolType, Answer answer) { + doReturn(sourcePoolType).when(sourceStoragePoolVoMock).getPoolType(); + lenient().doReturn(answer).when(agentManager).easySend(Mockito.anyLong(), Mockito.any(GetVolumesOnStorageCommand.class)); + return strategy.hasBackingFileOnSourceHost(mock(HostVO.class), mock(VolumeObject.class), sourceStoragePoolVoMock); + } + + @Test + public void hasBackingFileOnSourceHostTestVolumeWithBackingFile() { + assertTrue(hasBackingFileOnSourceHost(StoragePoolType.NetworkFilesystem, mockAnswerWithVolume("/mnt/pool/template"))); + } + + @Test + public void hasBackingFileOnSourceHostTestVolumeWithoutBackingFile() { + assertFalse(hasBackingFileOnSourceHost(StoragePoolType.NetworkFilesystem, mockAnswerWithVolume(null))); + } + + @Test + public void hasBackingFileOnSourceHostTestLocalAndSharedMountPointPoolsAreChecked() { + assertFalse(hasBackingFileOnSourceHost(StoragePoolType.Filesystem, mockAnswerWithVolume(null))); + assertFalse(hasBackingFileOnSourceHost(StoragePoolType.SharedMountPoint, mockAnswerWithVolume(null))); + } + + @Test + public void hasBackingFileOnSourceHostTestPoolWithoutVolumeFilesIsNotChecked() { + assertTrue(hasBackingFileOnSourceHost(StoragePoolType.RBD, null)); + Mockito.verify(agentManager, Mockito.never()).easySend(Mockito.anyLong(), Mockito.any()); + } + + @Test(expected = CloudRuntimeException.class) + public void hasBackingFileOnSourceHostTestNoAnswerFromHost() { + hasBackingFileOnSourceHost(StoragePoolType.NetworkFilesystem, null); + } + + @Test(expected = CloudRuntimeException.class) + public void hasBackingFileOnSourceHostTestFailedAnswer() { + GetVolumesOnStorageAnswer answer = mock(GetVolumesOnStorageAnswer.class); + doReturn(false).when(answer).getResult(); + hasBackingFileOnSourceHost(StoragePoolType.NetworkFilesystem, answer); + } + + @Test(expected = CloudRuntimeException.class) + public void hasBackingFileOnSourceHostTestVolumeNotFoundOnHost() { + GetVolumesOnStorageAnswer answer = mock(GetVolumesOnStorageAnswer.class); + doReturn(true).when(answer).getResult(); + doReturn(Collections.emptyList()).when(answer).getVolumes(); + hasBackingFileOnSourceHost(StoragePoolType.NetworkFilesystem, answer); + } }