Skip to content

KVM NAS backup: resume VM and exit on backup failure#12872

Open
jmsperu wants to merge 1 commit intoapache:4.22from
jmsperu:fix/nasbackup-resume-vm-on-failure
Open

KVM NAS backup: resume VM and exit on backup failure#12872
jmsperu wants to merge 1 commit intoapache:4.22from
jmsperu:fix/nasbackup-resume-vm-on-failure

Conversation

@jmsperu
Copy link

@jmsperu jmsperu commented Mar 20, 2026

Summary

Fix three bugs in nasbackup.sh that caused VMs to remain paused indefinitely when backup jobs fail (e.g. storage full, I/O error):

  1. Infinite polling loop: backup_running_vm() falls through the Failed case without exiting, causing the script to poll the already-failed job forever. Fixed by adding exit 1 after cleanup.

  2. Continued processing after failure: backup_stopped_vm() continues processing subsequent disks after qemu-img convert fails. Fixed by adding exit 1 after cleanup.

  3. VM never resumed: cleanup() removes temp files and unmounts but never resumes the VM that was paused by virsh backup-begin. Fixed by adding a VM state check and virsh resume at the top of cleanup().

Root Cause

When virsh backup-begin starts a backup, the VM may be paused. If the backup fails for any reason (storage full, network issue, I/O error), the script's cleanup path never calls virsh resume, leaving the VM paused until manual intervention.

Test Plan

  • Trigger a backup with insufficient NAS storage — verify VM is resumed after failure
  • Trigger a backup with NAS unmounted mid-backup — verify VM is resumed
  • Normal backup completes successfully (no regression)
  • Verify qemu-img convert failure on stopped VM exits cleanly

Fixes #12821

Fix three bugs in nasbackup.sh that caused VMs to remain paused
indefinitely when backup jobs fail (e.g. storage full):

1. Add exit after cleanup on Failed backup job status to prevent
   infinite polling loop in backup_running_vm()
2. Add exit after cleanup on qemu-img convert failure in
   backup_stopped_vm() to stop processing subsequent disks
3. Add VM state check and virsh resume to cleanup() so paused VMs
   are automatically resumed after backup failure

Fixes apache#12821
@codecov
Copy link

codecov bot commented Mar 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 17.61%. Comparing base (e93ae1a) to head (8550e5a).

Additional details and impacted files
@@            Coverage Diff            @@
##               4.22   #12872   +/-   ##
=========================================
  Coverage     17.61%   17.61%           
  Complexity    15661    15661           
=========================================
  Files          5917     5917           
  Lines        531430   531430           
  Branches      64973    64973           
=========================================
+ Hits          93586    93588    +2     
+ Misses       427288   427286    -2     
  Partials      10556    10556           
Flag Coverage Δ
uitests 3.70% <ø> (ø)
unittests 18.68% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes failure-handling paths in the KVM NAS backup helper script (nasbackup.sh) to prevent VMs from remaining paused indefinitely when backup operations fail.

Changes:

  • Exit immediately after cleaning up on a failed virsh backup job to avoid infinite polling.
  • Exit immediately after cleaning up on qemu-img convert failure to avoid continuing disk processing.
  • Enhance cleanup() to attempt to resume a paused VM before removing temporary files and unmounting NAS storage.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

# Resume the VM if it was paused (e.g. by virsh backup-begin)
if [[ -n "$VM" ]]; then
local vm_state
vm_state=$(virsh -c qemu:///system domstate "$VM" 2>/dev/null)
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With set -eo pipefail enabled, vm_state=$(virsh ... domstate ...) will cause cleanup() to exit immediately if virsh domstate returns non-zero (e.g., VM not found / libvirt transient error), which can prevent unmount/removal and also skip the resume attempt. Please make the domstate probe non-fatal (e.g., allow failure and treat state as empty) so cleanup always completes best-effort.

Suggested change
vm_state=$(virsh -c qemu:///system domstate "$VM" 2>/dev/null)
vm_state=$(virsh -c qemu:///system domstate "$VM" 2>/dev/null || true)

Copilot uses AI. Check for mistakes.
if [[ -n "$VM" ]]; then
local vm_state
vm_state=$(virsh -c qemu:///system domstate "$VM" 2>/dev/null)
if [[ "$vm_state" == "paused" ]]; then
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

virsh domstate output can include a reason suffix (e.g., paused (ioerror)), so an exact comparison to "paused" may miss paused VMs and fail to resume them. Consider matching a paused prefix (or use domstate --reason and parse the first token) so paused VMs are reliably detected.

Suggested change
if [[ "$vm_state" == "paused" ]]; then
if [[ "$vm_state" == paused* ]]; then

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants