Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion scripts/vm/hypervisor/kvm/nasbackup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ backup_running_vm() {
break ;;
Failed)
echo "Virsh backup job failed"
cleanup ;;
cleanup
exit 1 ;;
esac
sleep 5
done
Expand Down Expand Up @@ -178,6 +179,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
Expand Down Expand Up @@ -222,6 +224,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)
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

Because the script runs with set -e, the vm_state=$(virsh ... domstate ...) assignment will cause cleanup() (and the whole script) to exit immediately if virsh domstate returns non-zero (e.g., libvirt unavailable, domain not found). That would skip the rest of cleanup (rm/umount/rmdir) and may leave the mount behind. Consider guarding the domstate call (e.g., if vm_state=$(virsh ... 2>/dev/null); then ... fi or vm_state=$(virsh ... 2>/dev/null || true)) so cleanup best-effort behavior always runs even when state detection fails.

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 [[ "$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; }
Expand Down
Loading