Skip to content

Commit 1b0bba9

Browse files
CSTACKEX-18_2: plugin has to consider VM for snapshot in running and stopped state both.
1 parent ae96e9b commit 1b0bba9

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/vmsnapshot/OntapVMSnapshotStrategy.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,13 @@ boolean allVolumesOnOntapManagedStorage(long vmId) {
198198
return false;
199199
}
200200

201-
if (!VirtualMachine.State.Running.equals(userVm.getState())) {
202-
logger.debug("ONTAP VM snapshot strategy requires a running VM, VM [{}] is in state [{}]",
203-
vmId, userVm.getState());
201+
// ONTAP VM snapshots work for both Running and Stopped VMs.
202+
// Running VMs may be frozen/thawed (if quiesce is requested).
203+
// Stopped VMs don't need freeze/thaw - just take the FlexVol snapshot directly.
204+
VirtualMachine.State vmState = userVm.getState();
205+
if (!VirtualMachine.State.Running.equals(vmState) && !VirtualMachine.State.Stopped.equals(vmState)) {
206+
logger.info("allVolumesOnOntapManagedStorage: ONTAP VM snapshot strategy requires VM to be Running or Stopped, VM [{}] is in state [{}], returning false",
207+
vmId, vmState);
204208
return false;
205209
}
206210

@@ -289,7 +293,14 @@ public VMSnapshot takeVMSnapshot(VMSnapshot vmSnapshot) {
289293
quiescevm = options.needQuiesceVM();
290294
}
291295

292-
if (quiescevm) {
296+
// Check if VM is actually running - freeze/thaw only makes sense for running VMs
297+
boolean vmIsRunning = VirtualMachine.State.Running.equals(userVm.getState());
298+
boolean shouldFreezeThaw = quiescevm && vmIsRunning;
299+
300+
if (!vmIsRunning) {
301+
logger.info("VM [{}] is in state [{}] (not Running). Skipping freeze/thaw - " +
302+
"FlexVolume snapshot will be taken directly.", userVm.getInstanceName(), userVm.getState());
303+
} else if (quiescevm) {
293304
logger.info("Quiesce option is enabled for ONTAP VM Snapshot of VM [{}]. " +
294305
"VM file systems will be frozen/thawed for application-consistent snapshots.", userVm.getInstanceName());
295306
} else {
@@ -324,8 +335,8 @@ public VMSnapshot takeVMSnapshot(VMSnapshot vmSnapshot) {
324335
logger.info("VM [{}] has {} volumes across {} unique FlexVolume(s)",
325336
userVm.getInstanceName(), volumeTOs.size(), flexVolGroups.size());
326337

327-
// ── Step 1: Freeze the VM (only if quiescing is requested) ──
328-
if (quiescevm) {
338+
// ── Step 1: Freeze the VM (only if quiescing is requested AND VM is running) ──
339+
if (shouldFreezeThaw) {
329340
FreezeThawVMCommand freezeCommand = new FreezeThawVMCommand(userVm.getInstanceName());
330341
freezeCommand.setOption(FreezeThawVMCommand.FREEZE);
331342
freezeAnswer = (FreezeThawVMAnswer) agentMgr.send(hostId, freezeCommand);
@@ -342,7 +353,8 @@ public VMSnapshot takeVMSnapshot(VMSnapshot vmSnapshot) {
342353

343354
logger.info("VM [{}] frozen successfully via QEMU guest agent", userVm.getInstanceName());
344355
} else {
345-
logger.info("Skipping VM freeze for VM [{}] as quiesce is not requested", userVm.getInstanceName());
356+
logger.info("Skipping VM freeze for VM [{}] (quiesce={}, vmIsRunning={})",
357+
userVm.getInstanceName(), quiescevm, vmIsRunning);
346358
}
347359

348360
// ── Step 2: Create FlexVolume-level snapshots ──

plugins/storage/volume/ontap/src/test/java/org/apache/cloudstack/storage/vmsnapshot/OntapVMSnapshotStrategyTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ private UserVmVO setupTakeSnapshotCommon(VMSnapshotVO vmSnapshot) throws Excepti
887887
when(userVm.getGuestOSId()).thenReturn(GUEST_OS_ID);
888888
when(userVm.getInstanceName()).thenReturn(VM_INSTANCE_NAME);
889889
when(userVm.getUuid()).thenReturn(VM_UUID);
890+
when(userVm.getState()).thenReturn(VirtualMachine.State.Running);
890891
when(userVmDao.findById(VM_ID)).thenReturn(userVm);
891892

892893
GuestOSVO guestOS = mock(GuestOSVO.class);

0 commit comments

Comments
 (0)