-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Suspending the VM prior to deleting snapshots to avoid corruption, th… #4032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
eaab9ae
30bd160
43f1c87
214baed
633438f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ | |
| import org.apache.log4j.Logger; | ||
| import org.libvirt.Connect; | ||
| import org.libvirt.Domain; | ||
| import org.libvirt.DomainInfo.DomainState; | ||
| import org.libvirt.DomainInfo; | ||
| import org.libvirt.DomainSnapshot; | ||
| import org.libvirt.LibvirtException; | ||
|
|
||
|
|
@@ -52,18 +52,33 @@ public Answer execute(final DeleteVMSnapshotCommand cmd, final LibvirtComputingR | |
| final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr(); | ||
| Domain dm = null; | ||
| DomainSnapshot snapshot = null; | ||
| DomainInfo.DomainState oldState = null; | ||
| boolean tryingResume = false; | ||
| Connect conn = null; | ||
| try { | ||
| final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper(); | ||
| Connect conn = libvirtUtilitiesHelper.getConnection(); | ||
| conn = libvirtUtilitiesHelper.getConnection(); | ||
| dm = libvirtComputingResource.getDomain(conn, vmName); | ||
|
|
||
| snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName()); | ||
|
|
||
| s_logger.debug("Suspending domain " + vmName); | ||
| dm.suspend(); // suspend the vm to avoid image corruption | ||
| oldState = dm.getInfo().state; | ||
| if (oldState == DomainInfo.DomainState.VIR_DOMAIN_RUNNING) { | ||
| s_logger.debug("Suspending domain " + vmName); | ||
| dm.suspend(); // suspend the vm to avoid image corruption | ||
| } | ||
|
|
||
| snapshot.delete(0); // only remove this snapshot, not children | ||
|
|
||
| if (oldState == DomainInfo.DomainState.VIR_DOMAIN_RUNNING) { | ||
| // Resume the VM | ||
| tryingResume = true; | ||
| dm = libvirtComputingResource.getDomain(conn, vmName); | ||
| if (dm.getInfo().state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) { | ||
| dm.resume(); | ||
| } | ||
| } | ||
|
|
||
| return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs()); | ||
| } catch (LibvirtException e) { | ||
| String msg = " Delete VM snapshot failed due to " + e.toString(); | ||
|
|
@@ -97,21 +112,26 @@ public Answer execute(final DeleteVMSnapshotCommand cmd, final LibvirtComputingR | |
| } else if (snapshot == null) { | ||
| s_logger.debug("Can not find vm snapshot " + cmd.getTarget().getSnapshotName() + " on vm: " + vmName + ", return true"); | ||
| return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs()); | ||
| } else if (tryingResume) { | ||
| s_logger.error("Failed to resume vm after delete snapshot " + cmd.getTarget().getSnapshotName() + " on vm: " + vmName + " return true : " + e); | ||
| return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs()); | ||
| } | ||
|
|
||
| s_logger.warn(msg, e); | ||
| return new DeleteVMSnapshotAnswer(cmd, false, msg); | ||
| } finally { | ||
| if (dm != null) { | ||
| // Make sure if the VM is paused, then resume it, in case we got an exception during our delete() and didn't have the chance before | ||
| try { | ||
| if (dm.getInfo().state == DomainState.VIR_DOMAIN_PAUSED) { | ||
| dm = libvirtComputingResource.getDomain(conn, vmName); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dm is already not null, why overwrite it again?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was under the impression that dm needed to be refreshed after a state change, as I saw this same thing being done in KVMStorageProcessor -> backupSnapshot(). Rather than validate that it was necessary or not, I decided that safer is always better, as refreshing the domain variable won't cause harm. |
||
| if (oldState == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && dm.getInfo().state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) { | ||
| s_logger.debug("Resuming domain " + vmName); | ||
| dm.resume(); | ||
| } | ||
| dm.free(); | ||
| } catch (LibvirtException l) { | ||
| s_logger.trace("Ignoring libvirt error.", l); | ||
| }; | ||
| } catch (LibvirtException e) { | ||
| s_logger.error("Failed to resume vm after delete snapshot " + cmd.getTarget().getSnapshotName() + " on vm: " + vmName + " return true : " + e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1031,7 +1031,7 @@ public Answer backupSnapshot(final CopyCommand cmd) { | |
| } | ||
| } | ||
| } catch (final Exception ex) { | ||
| s_logger.debug("Failed to delete snapshots on primary", ex); | ||
| s_logger.error("Failed to delete snapshots on primary", ex); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.