NAS backup: resume paused VM on backup failure and fix missing exit (#12822)

* NAS backup: resume paused VM on backup failure and fix missing exit

When a NAS backup job fails (e.g. due to backup storage being full or
I/O errors), the VM may remain indefinitely paused because:

1. The cleanup() function never checks or resumes the VM's paused state
   that was set by virsh backup-begin during the push backup operation.

2. The 'Failed' case in the backup job monitoring loop calls cleanup()
   but lacks an 'exit' statement, causing an infinite loop where the
   script repeatedly detects the failed job and calls cleanup().

3. Similarly, backup_stopped_vm() calls cleanup() on qemu-img convert
   failure but does not exit, allowing the loop to continue with
   subsequent disks despite the failure.

This fix:
- Adds VM state detection and resume to cleanup(), ensuring the VM is
  always resumed if found in a paused state during error handling
- Adds missing 'exit 1' after cleanup() in the Failed backup job case
  to prevent the infinite monitoring loop
- Adds missing 'exit 1' after cleanup() in backup_stopped_vm() on
  qemu-img convert failure

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* ci: retrigger workflow (flaky/stale shards)

* nasbackup.sh: keep cleanup() best-effort if domstate fails

With set -eo pipefail, a non-zero virsh domstate (libvirt unavailable or the
domain gone) in the vm_state assignment would abort cleanup() before the
rm/umount/rmdir. Append '|| true' so cleanup always runs to completion.
Addresses the review suggestion (Copilot, endorsed by @abh1sar and @weizhouapache).

Signed-off-by: James Peru <jmsperu@gmail.com>

---------

Signed-off-by: James Peru <jmsperu@gmail.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: jmsperu <jmsperu@users.noreply.github.com>
This commit is contained in:
James Peru Mmbono 2026-07-07 14:04:50 +03:00 committed by GitHub
parent 4d006b5f85
commit 9f281c4851
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 1 deletions

View File

@ -167,7 +167,8 @@ backup_running_vm() {
break ;;
Failed)
echo "Virsh backup job failed"
cleanup ;;
cleanup
exit 1 ;;
esac
sleep 5
done
@ -221,6 +222,7 @@ backup_stopped_vm() {
if ! qemu-img convert -O qcow2 "$disk" "$output" > "$logFile" 2> >(cat >&2); then
echo "qemu-img convert failed for $disk $output"
cleanup
exit 1
fi
name="datadisk"
done
@ -265,6 +267,19 @@ mount_operation() {
cleanup() {
local status=0
# Resume the VM if it was paused during backup to prevent it from
# remaining indefinitely paused when the backup job fails (e.g. due
# to storage full or I/O errors on the backup target)
local vm_state
vm_state=$(virsh -c qemu:///system domstate "$VM" 2>/dev/null || true)
if [[ "$vm_state" == "paused" ]]; then
log -ne "Resuming paused VM $VM during backup cleanup"
if ! virsh -c qemu:///system resume "$VM" > /dev/null 2>&1; then
echo "Failed to resume VM $VM"
status=1
fi
fi
rm -rf "$dest" || { echo "Failed to delete $dest"; status=1; }
umount "$mount_point" || { echo "Failed to unmount $mount_point"; status=1; }
rmdir "$mount_point" || { echo "Failed to remove mount point $mount_point"; status=1; }