From 895d09b45ece49c7c5b46b9fcb18c422cb38c4c5 Mon Sep 17 00:00:00 2001 From: Landry JUGE Date: Tue, 25 Nov 2025 13:45:37 +0100 Subject: [PATCH] Creating a unmount_operation with safety checks for nasbackup.sh --- scripts/vm/hypervisor/kvm/nasbackup.sh | 33 ++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/scripts/vm/hypervisor/kvm/nasbackup.sh b/scripts/vm/hypervisor/kvm/nasbackup.sh index 7f4a4b621929..a59a96903c48 100755 --- a/scripts/vm/hypervisor/kvm/nasbackup.sh +++ b/scripts/vm/hypervisor/kvm/nasbackup.sh @@ -154,8 +154,7 @@ backup_running_vm() { virsh -c qemu:///system domjobinfo $VM --completed du -sb $dest | cut -f1 - umount $mount_point - rmdir $mount_point + umount_operation } backup_stopped_vm() { @@ -184,6 +183,8 @@ backup_stopped_vm() { sync ls -l --numeric-uid-gid $dest | awk '{print $5}' + + umount_operation } delete_backup() { @@ -219,6 +220,34 @@ mount_operation() { fi } +umount_operation() { + elapsed=0 + while fuser -m "$mount_point" >/dev/null 2>&1 && (( elapsed < 10 )); do + sleep 1 + elapsed=$((elapsed + 1)) + done + + # Check if timeout was reached + if (( elapsed >= 10 )); then + echo "Timeout for unmounting reached: still busy" + fi + + # Attempt to unmount safely and capture output + set +e + umount_output=$(umount "$mount_point" 2>&1) + umount_exit=$? + set -e + + if [ "$umount_exit" -eq 0 ]; then + # Only remove directory if unmount succeeded + rmdir "$mount_point" + else + echo "Warning: failed to unmount $mount_point, skipping rmdir" + echo "umount error message: $umount_output" + log -ne "Warning: failed to unmount $mount_point, error: $umount_output" + fi +} + cleanup() { local status=0