Skip to content

Commit 6ac3a53

Browse files
author
Landry JUGE
committed
Creating a unmount_operation with safety checks for nasbackup.sh
1 parent 8936e4c commit 6ac3a53

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

scripts/vm/hypervisor/kvm/nasbackup.sh

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ backup_running_vm() {
153153
virsh -c qemu:///system domjobinfo $VM --completed
154154
du -sb $dest | cut -f1
155155

156-
umount $mount_point
157-
rmdir $mount_point
156+
umount_operation
158157
}
159158

160159
backup_stopped_vm() {
@@ -183,6 +182,8 @@ backup_stopped_vm() {
183182
sync
184183

185184
ls -l --numeric-uid-gid $dest | awk '{print $5}'
185+
186+
umount_operation
186187
}
187188

188189
delete_backup() {
@@ -218,6 +219,34 @@ mount_operation() {
218219
fi
219220
}
220221

222+
umount_operation() {
223+
elapsed=0
224+
while fuser -m "$mount_point" >/dev/null 2>&1 && (( elapsed < 10 )); do
225+
sleep 1
226+
elapsed=$((elapsed + 1))
227+
done
228+
229+
# Check if timeout was reached
230+
if (( elapsed >= 10 )); then
231+
echo "Timeout for unmounting reached: still busy"
232+
fi
233+
234+
# Attempt to unmount safely and capture output
235+
set +e
236+
umount_output=$(umount "$mount_point" 2>&1)
237+
umount_exit=$?
238+
set -e
239+
240+
if [ "$umount_exit" -eq 0 ]; then
241+
# Only remove directory if unmount succeeded
242+
rmdir "$mount_point"
243+
else
244+
echo "Warning: failed to unmount $mount_point, skipping rmdir"
245+
echo "umount error message: $umount_output"
246+
log -ne "Warning: failed to unmount $mount_point, error: $umount_output"
247+
fi
248+
}
249+
221250
cleanup() {
222251
local status=0
223252

0 commit comments

Comments
 (0)