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
18 changes: 17 additions & 1 deletion scripts/vm/hypervisor/kvm/nasbackup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ backup_running_vm() {
break ;;
Failed)
echo "Virsh backup job failed"
cleanup ;;
cleanup
exit 1 ;;
esac
sleep 5
done
Expand Down Expand Up @@ -177,6 +178,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 @@ -221,6 +223,20 @@ mount_operation() {
cleanup() {
local status=0

# 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 [[ "$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.
if virsh -c qemu:///system resume "$VM" > /dev/null 2>&1; then
log -ne "Resumed VM $VM after backup failure"
else
echo "Failed to resume VM $VM - manual intervention required (virsh resume $VM)"
status=1
fi
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