diff --git a/roles/lmn_vm/files/sync-vm.sh b/roles/lmn_vm/files/sync-vm.sh index e0ee196..195ab92 100755 --- a/roles/lmn_vm/files/sync-vm.sh +++ b/roles/lmn_vm/files/sync-vm.sh @@ -23,14 +23,39 @@ download_image() { } torrent_image() { - if [[ -f "/lmn/vm/${VM_NAME}.qcow2.torrent" ]]; then - cd /lmn/vm - ctorrent -e 0 "${VM_NAME}.qcow2.torrent" - /usr/local/bin/vmimage-torrent restart "${VM_NAME}.qcow2" - else + if [[ ! -f "/lmn/vm/${VM_NAME}.qcow2.torrent" ]]; then echo "No torrent-File found" exit 1 fi + lockfile="/tmp/sync-vm-${VM_NAME}.lock" + if ! flock -n "$lockfile" echo "try to acquire lock"; then + echo torrent seems to be in process. + echo waiting for completion ... + flock -w 3600 "$lockfile" echo "...completed" + sleep 5 + else + ( + if ! flock -n 200; then + echo "failed to acquire lock" + echo "Bitte noch einmal starten." + echo "Beliebige Taste zum Beenden." + read -n 1 + exit 1 + fi + torrent="${VM_NAME}.qcow2.torrent" + session="${torrent//./_}" + if vmimage-torrent status | grep -qw ^"$session"; then + vmimage-torrent stop "${VM_NAME}.qcow2" + fi + cd /lmn/vm + ctorrent -e 0 "${VM_NAME}.qcow2.torrent" + /usr/local/bin/vmimage-torrent restart "${VM_NAME}.qcow2" + if ! flock -u 200; then + echo failed to drop lock + exit 1 + fi + ) 200>"$lockfile" + fi } sync_all_images() {