This is an automated email from the ASF dual-hosted git repository.
sureshanaparti pushed a commit to branch 4.22
in repository https://gitbox.apache.org/repos/asf/cloudstack.git
The following commit(s) were added to refs/heads/4.22 by this push:
new 648976608ec StorPool: reject Instance Snapshots with memory (#14788)
648976608ec is described below
commit 648976608ec1a890d5ceb7723008faac8f3e0850
Author: slavkap <[email protected]>
AuthorDate: Wed Nov 25 09:15:24 2026 +0200
StorPool: reject Instance Snapshots with memory (#14788)
DefaultVMSnapshotStrategy accepts the allocation because StorPool
volumes are recorded as QCOW2, but StorPoolVMSnapshotStrategy then
takes over the creation and makes a disk-only group snapshot that is
saved as DiskAndMemory. Such a snapshot can only be reverted while the
VM is running, and StorPool then reverts the volumes under the running
VM, which corrupts its data.
Reject memory snapshots for VMs with volumes on StorPool during
allocation, do not handle allocated DiskAndMemory snapshots in the
StorPool strategy, and block reverting existing DiskAndMemory snapshots
on StorPool.
---
.../snapshot/StorPoolVMSnapshotStrategy.java | 26 ++++
.../snapshot/StorPoolVMSnapshotStrategyTest.java | 158 +++++++++++++++++++++
.../plugins/storpool/TestVmSnapshots.py | 36 ++++-
3 files changed, 219 insertions(+), 1 deletion(-)
diff --git
a/plugins/storage/volume/storpool/src/main/java/org/apache/cloudstack/storage/snapshot/StorPoolVMSnapshotStrategy.java
b/plugins/storage/volume/storpool/src/main/java/org/apache/cloudstack/storage/snapshot/StorPoolVMSnapshotStrategy.java
index f93260204a7..a8cd087757b 100644
---
a/plugins/storage/volume/storpool/src/main/java/org/apache/cloudstack/storage/snapshot/StorPoolVMSnapshotStrategy.java
+++
b/plugins/storage/volume/storpool/src/main/java/org/apache/cloudstack/storage/snapshot/StorPoolVMSnapshotStrategy.java
@@ -41,6 +41,7 @@ import org.springframework.stereotype.Component;
import com.cloud.agent.api.VMSnapshotTO;
import com.cloud.event.EventTypes;
import com.cloud.event.UsageEventUtils;
+import com.cloud.exception.InvalidParameterValueException;
import com.cloud.hypervisor.kvm.storage.StorPoolStorageAdaptor;
import com.cloud.storage.DiskOfferingVO;
import com.cloud.storage.VolumeDetailVO;
@@ -185,16 +186,36 @@ public class StorPoolVMSnapshotStrategy extends
DefaultVMSnapshotStrategy {
@Override
public StrategyPriority canHandle(VMSnapshot vmSnapshot) {
+ if (VMSnapshot.State.Allocated.equals(vmSnapshot.getState()) &&
VMSnapshot.Type.DiskAndMemory.equals(vmSnapshot.getType())) {
+ return StrategyPriority.CANT_HANDLE;
+ }
return areAllVolumesOnStorPool(vmSnapshot.getVmId());
}
public StrategyPriority canHandle(Long vmId, Long rootPoolId, boolean
snapshotMemory) {
if (snapshotMemory) {
+ if (isAnyVolumeOnStorPool(vmId)) {
+ throw new InvalidParameterValueException("Instance Snapshots
with memory are not supported for volumes on StorPool primary storage");
+ }
return StrategyPriority.CANT_HANDLE;
}
return areAllVolumesOnStorPool(vmId);
}
+ private boolean isAnyVolumeOnStorPool(Long vmId) {
+ List<VolumeObjectTO> volumeTOs =
vmSnapshotHelper.getVolumeTOList(vmId);
+ if (volumeTOs == null) {
+ return false;
+ }
+ for (VolumeObjectTO volumeTO : volumeTOs) {
+ StoragePoolVO pool = storagePool.findById(volumeTO.getPoolId());
+ if (pool != null &&
StorPoolUtil.SP_PROVIDER_NAME.equals(pool.getStorageProviderName())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
private StrategyPriority areAllVolumesOnStorPool(Long vmId) {
List<VolumeObjectTO> volumeTOs =
vmSnapshotHelper.getVolumeTOList(vmId);
if (volumeTOs == null || volumeTOs.isEmpty()) {
@@ -279,6 +300,11 @@ public class StorPoolVMSnapshotStrategy extends
DefaultVMSnapshotStrategy {
VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
UserVmVO userVm = userVmDao.findById(vmSnapshot.getVmId());
+ if (vmSnapshotVO.getType() == VMSnapshot.Type.DiskAndMemory) {
+ throw new CloudRuntimeException(String.format("Reverting Instance
Snapshot [%s] is not allowed because it is marked as a Snapshot with memory, " +
+ "but StorPool keeps only the disks and reverting them
under the running Instance would corrupt its data", vmSnapshot.getUuid()));
+ }
+
if (userVm.getState() == VirtualMachine.State.Running &&
vmSnapshotVO.getType() == VMSnapshot.Type.Disk) {
throw new CloudRuntimeException("The Instance should be in stopped
state for revert operation");
}
diff --git
a/plugins/storage/volume/storpool/src/test/java/org/apache/cloudstack/storage/snapshot/StorPoolVMSnapshotStrategyTest.java
b/plugins/storage/volume/storpool/src/test/java/org/apache/cloudstack/storage/snapshot/StorPoolVMSnapshotStrategyTest.java
new file mode 100644
index 00000000000..4896b0aa8ff
--- /dev/null
+++
b/plugins/storage/volume/storpool/src/test/java/org/apache/cloudstack/storage/snapshot/StorPoolVMSnapshotStrategyTest.java
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cloudstack.storage.snapshot;
+
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.vm.UserVmVO;
+import com.cloud.vm.VirtualMachine;
+import com.cloud.vm.dao.UserVmDao;
+import com.cloud.vm.snapshot.VMSnapshot;
+import com.cloud.vm.snapshot.VMSnapshotVO;
+import org.apache.cloudstack.engine.subsystem.api.storage.StrategyPriority;
+import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
+import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
+import org.apache.cloudstack.storage.datastore.util.StorPoolUtil;
+import org.apache.cloudstack.storage.to.VolumeObjectTO;
+import org.apache.cloudstack.storage.vmsnapshot.VMSnapshotHelper;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.List;
+
+@RunWith(MockitoJUnitRunner.class)
+public class StorPoolVMSnapshotStrategyTest {
+
+ private static final long VM_ID = 1L;
+ private static final long STORPOOL_POOL_ID = 10L;
+ private static final long OTHER_POOL_ID = 20L;
+
+ @Mock
+ private VMSnapshotHelper vmSnapshotHelper;
+ @Mock
+ private PrimaryDataStoreDao storagePool;
+ @Mock
+ private UserVmDao userVmDao;
+
+ @InjectMocks
+ private StorPoolVMSnapshotStrategy strategy;
+
+ private VolumeObjectTO mockVolumeOnPool(long poolId, String providerName) {
+ VolumeObjectTO volumeTO = Mockito.mock(VolumeObjectTO.class);
+ Mockito.when(volumeTO.getPoolId()).thenReturn(poolId);
+ StoragePoolVO pool = Mockito.mock(StoragePoolVO.class);
+ Mockito.when(pool.getStorageProviderName()).thenReturn(providerName);
+ Mockito.when(storagePool.findById(poolId)).thenReturn(pool);
+ return volumeTO;
+ }
+
+ private VMSnapshot mockVmSnapshot(VMSnapshot.State state, VMSnapshot.Type
type) {
+ VMSnapshot vmSnapshot = Mockito.mock(VMSnapshot.class);
+ Mockito.when(vmSnapshot.getVmId()).thenReturn(VM_ID);
+ Mockito.lenient().when(vmSnapshot.getState()).thenReturn(state);
+ Mockito.lenient().when(vmSnapshot.getType()).thenReturn(type);
+ return vmSnapshot;
+ }
+
+ @Test(expected = InvalidParameterValueException.class)
+ public void canHandleAllocationWithMemoryThrowsWhenAllVolumesOnStorPool() {
+ VolumeObjectTO volumeTO = mockVolumeOnPool(STORPOOL_POOL_ID,
StorPoolUtil.SP_PROVIDER_NAME);
+
Mockito.when(vmSnapshotHelper.getVolumeTOList(VM_ID)).thenReturn(List.of(volumeTO));
+
+ strategy.canHandle(VM_ID, STORPOOL_POOL_ID, true);
+ }
+
+ @Test(expected = InvalidParameterValueException.class)
+ public void canHandleAllocationWithMemoryThrowsWhenOneVolumeOnStorPool() {
+ VolumeObjectTO otherVolumeTO = mockVolumeOnPool(OTHER_POOL_ID,
"DefaultPrimary");
+ VolumeObjectTO storPoolVolumeTO = mockVolumeOnPool(STORPOOL_POOL_ID,
StorPoolUtil.SP_PROVIDER_NAME);
+
Mockito.when(vmSnapshotHelper.getVolumeTOList(VM_ID)).thenReturn(List.of(otherVolumeTO,
storPoolVolumeTO));
+
+ strategy.canHandle(VM_ID, OTHER_POOL_ID, true);
+ }
+
+ @Test
+ public void
canHandleAllocationWithMemoryReturnsCantHandleWhenNoVolumeOnStorPool() {
+ VolumeObjectTO volumeTO = mockVolumeOnPool(OTHER_POOL_ID,
"DefaultPrimary");
+
Mockito.when(vmSnapshotHelper.getVolumeTOList(VM_ID)).thenReturn(List.of(volumeTO));
+
+ Assert.assertEquals(StrategyPriority.CANT_HANDLE,
strategy.canHandle(VM_ID, OTHER_POOL_ID, true));
+ }
+
+ @Test
+ public void
canHandleAllocationWithoutMemoryReturnsHighestWhenAllVolumesOnStorPool() {
+ VolumeObjectTO volumeTO = mockVolumeOnPool(STORPOOL_POOL_ID,
StorPoolUtil.SP_PROVIDER_NAME);
+
Mockito.when(vmSnapshotHelper.getVolumeTOList(VM_ID)).thenReturn(List.of(volumeTO));
+
+ Assert.assertEquals(StrategyPriority.HIGHEST,
strategy.canHandle(VM_ID, STORPOOL_POOL_ID, false));
+ }
+
+ @Test
+ public void canHandleAllocatedDiskAndMemorySnapshotReturnsCantHandle() {
+ VMSnapshot vmSnapshot = Mockito.mock(VMSnapshot.class);
+
Mockito.when(vmSnapshot.getState()).thenReturn(VMSnapshot.State.Allocated);
+
Mockito.when(vmSnapshot.getType()).thenReturn(VMSnapshot.Type.DiskAndMemory);
+
+ Assert.assertEquals(StrategyPriority.CANT_HANDLE,
strategy.canHandle(vmSnapshot));
+ Mockito.verifyNoInteractions(vmSnapshotHelper);
+ }
+
+ @Test
+ public void
canHandleAllocatedDiskSnapshotReturnsHighestWhenAllVolumesOnStorPool() {
+ VolumeObjectTO volumeTO = mockVolumeOnPool(STORPOOL_POOL_ID,
StorPoolUtil.SP_PROVIDER_NAME);
+
Mockito.when(vmSnapshotHelper.getVolumeTOList(VM_ID)).thenReturn(List.of(volumeTO));
+
+ Assert.assertEquals(StrategyPriority.HIGHEST,
strategy.canHandle(mockVmSnapshot(VMSnapshot.State.Allocated,
VMSnapshot.Type.Disk)));
+ }
+
+ @Test
+ public void
canHandleExistingDiskAndMemorySnapshotReturnsHighestWhenAllVolumesOnStorPool() {
+ VolumeObjectTO volumeTO = mockVolumeOnPool(STORPOOL_POOL_ID,
StorPoolUtil.SP_PROVIDER_NAME);
+
Mockito.when(vmSnapshotHelper.getVolumeTOList(VM_ID)).thenReturn(List.of(volumeTO));
+
+ Assert.assertEquals(StrategyPriority.HIGHEST,
strategy.canHandle(mockVmSnapshot(VMSnapshot.State.Ready,
VMSnapshot.Type.DiskAndMemory)));
+ }
+
+ private void
assertRevertOfDiskAndMemorySnapshotIsBlocked(VirtualMachine.State vmState) {
+ UserVmVO userVm = Mockito.mock(UserVmVO.class);
+ Mockito.lenient().when(userVm.getState()).thenReturn(vmState);
+ Mockito.when(userVmDao.findById(VM_ID)).thenReturn(userVm);
+ VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
+ Mockito.when(vmSnapshot.getVmId()).thenReturn(VM_ID);
+
Mockito.when(vmSnapshot.getType()).thenReturn(VMSnapshot.Type.DiskAndMemory);
+
+ Assert.assertThrows(CloudRuntimeException.class, () ->
strategy.revertVMSnapshot(vmSnapshot));
+ Mockito.verifyNoInteractions(vmSnapshotHelper);
+ }
+
+ @Test
+ public void revertDiskAndMemorySnapshotIsBlockedWhenVmIsRunning() {
+
assertRevertOfDiskAndMemorySnapshotIsBlocked(VirtualMachine.State.Running);
+ }
+
+ @Test
+ public void revertDiskAndMemorySnapshotIsBlockedWhenVmIsStopped() {
+
assertRevertOfDiskAndMemorySnapshotIsBlocked(VirtualMachine.State.Stopped);
+ }
+}
diff --git a/test/integration/plugins/storpool/TestVmSnapshots.py
b/test/integration/plugins/storpool/TestVmSnapshots.py
index 749e534f0da..b57e4ada96f 100644
--- a/test/integration/plugins/storpool/TestVmSnapshots.py
+++ b/test/integration/plugins/storpool/TestVmSnapshots.py
@@ -16,6 +16,7 @@
# under the License.
# Import Local Modules
+import math
import random
import time
@@ -104,6 +105,7 @@ class TestVmSnapshot(cloudstackTestCase):
with description %s" % cls.services["ostype"]
cls.template = template
+ cls.root_disk_size = int(math.ceil(template.size / (1024 ** 3)))
cls.account = cls.helper.create_account(
cls.apiclient,
@@ -169,7 +171,7 @@ class TestVmSnapshot(cloudstackTestCase):
domainid=cls.account.domainid,
serviceofferingid=cls.service_offering_only.id,
hypervisor=cls.hypervisor,
- rootdisksize=10
+ rootdisksize=cls.root_disk_size
)
cls.random_data_0 = random_gen(size=100)
@@ -373,3 +375,35 @@ class TestVmSnapshot(cloudstackTestCase):
self.debug('list_snapshot_response -------------------- %s' %
list_snapshot_response)
self.assertIsNone(list_snapshot_response, "snapshot is already
deleted")
+
+ @attr(tags=["advanced", "advancedns", "smoke"], required_hardware="true")
+ def test_04_create_vm_snapshot_with_memory_fails(self):
+ """Test that creating a VM snapshot with memory is rejected for VMs
with volumes on StorPool
+ """
+ vm = list_virtual_machines(
+ self.apiclient,
+ id=self.virtual_machine.id,
+ listall=True)[0]
+ self.assertEqual(vm.state, RUNNING, "Check the VM is running")
+
+ MemorySnapshot = True
+ with self.assertRaises(Exception) as context:
+ VmSnapshot.create(
+ self.apiclient,
+ self.virtual_machine.id,
+ MemorySnapshot,
+ "TestMemorySnapshot",
+ "Display Text"
+ )
+ self.debug("Creating a VM snapshot with memory failed with: %s" %
context.exception)
+ self.assertIn(
+ "not supported for volumes on StorPool",
+ str(context.exception),
+ "Check the VM snapshot with memory is rejected by StorPool"
+ )
+
+ list_snapshot_response = VmSnapshot.list(
+ self.apiclient,
+ virtualmachineid=self.virtual_machine.id,
+ listall=True)
+ self.assertIsNone(list_snapshot_response, "Check no VM snapshot was
created")