From b7a0c9fa308bdd22f84702821b5c46c4ea4454b4 Mon Sep 17 00:00:00 2001 From: Raphael Dannecker Date: Tue, 18 Jul 2023 13:19:45 +0200 Subject: [PATCH] download necessary qcow2-files automatically --- roles/lmn_vm/files/run-vm.sh | 30 ++++++++++++++++++++++++++++-- roles/lmn_vm/files/sync-vm.sh | 27 ++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/roles/lmn_vm/files/run-vm.sh b/roles/lmn_vm/files/run-vm.sh index c726a4f..3e48545 100755 --- a/roles/lmn_vm/files/run-vm.sh +++ b/roles/lmn_vm/files/run-vm.sh @@ -48,14 +48,40 @@ if [[ $# -ne 1 ]] ; then exit 1 fi +# sync vm-torrents and machine definition file +sudo /usr/local/bin/sync-vm.sh -t + VM_NAME=$1 VM_DIR="/tmp/${UID}/vmimages" + if [[ ! -f "/var/lib/libvirt/images/${VM_NAME}.qcow2" && ! -f "${VM_DIR}/${VM_NAME}.qcow2" ]]; then - echo "no base VM disk '${VM_NAME}.qcow2' found" >&2 - exit 1 + if [[ ! -f "/var/lib/libvirt/images/${VM_NAME}.qcow2.torrent" ]]; then + echo "no base VM disk '${VM_NAME}.qcow2' found and/or ${VM_NAME} not found on server" >&2 + exit 1 + fi + # sync vm-disk image by torrent + sudo /usr/local/bin/sync-vm.sh "${VM_NAME}" + echo "sudo /usr/local/bin/sync-vm.sh ${VM_NAME}" fi +echo "qcow2 seems to be available" + +imgfile="/var/lib/libvirt/images/${VM_NAME}.qcow2" && [[ -f "${VM_DIR}/${VM_NAME}.qcow2" ]] && imgfile="${VM_DIR}/${VM_NAME}.qcow2" +#backingfile=$(qemu-img info -U "${imgfile}" | grep ^image: | cut -d' ' -f2) + +backingfile=$(qemu-img info -U "${imgfile}" | grep "^backing file:" | cut -d ' ' -f 3) +while [[ ! -z "${backingfile}" ]]; do + echo $backingfile + if [[ ! -f "/var/lib/libvirt/images/${backingfile}" && ! -f "${VM_DIR}/${backingfile}" ]]; then + # sync vm-disk image by torrent + sudo /usr/local/bin/sync-vm.sh "${backingfile//.qcow2/}" + echo "sudo /usr/local/bin/sync-vm.sh ${backingfile//.qcow2/}" + fi + imgfile="/var/lib/libvirt/images/${backingfile}" && [[ -f "${VM_DIR}/${backingfile}" ]] && imgfile="${VM_DIR}/${backingfile}" + backingfile=$(qemu-img info -U "${imgfile}" | grep "^backing file:" | cut -d ' ' -f 3) +done + # check, if we have to start squid if ! killall -s 0 squid; then echo "starting squid." diff --git a/roles/lmn_vm/files/sync-vm.sh b/roles/lmn_vm/files/sync-vm.sh index 61d1d26..0a551b9 100755 --- a/roles/lmn_vm/files/sync-vm.sh +++ b/roles/lmn_vm/files/sync-vm.sh @@ -48,6 +48,30 @@ sync_all_torrents() { /var/lib/libvirt/images/ rsync -av --password-file=/etc/rsync.secret rsync://vmuser@server:/vmimages-download/xml \ /var/lib/libvirt/images/ + rsync -av --password-file=/etc/rsync.secret rsync://vmuser@server:/vmimages-download/desktop/*.desktop \ + /usr/share/applications/ + update-desktop-database /usr/share/applications +} + +create_starter() { + if [[ ! -f "/usr/share/applications/VM_${VM_NAME}_starter.desktop" ]]; then + cat << EOF >"/usr/share/applications/VM_${VM_NAME}_starter.desktop" +[Desktop Entry] +Version=1.0 +Type=Application +Name=VMstart: ${VM_NAME} +GenericName=VM starter ${VM_NAME} +Comment=Start VM ${VM_NAME} +#TryExec=konsole +Exec=/usr/local/bin/run-vm.sh ${VM_NAME} +Icon=clementine +Categories=VM;Engineering; +MimeType=image/vnd.dxf; +Keywords=design;VM;diagrams;graphics +Terminal=true +EOF + update-desktop-database /usr/share/applications + fi } while getopts ':dat' OPTION; do @@ -57,10 +81,11 @@ while getopts ':dat' OPTION; do ;; a) sync_all_images - exit 1 + exit 0 ;; t) sync_all_torrents + exit 0 ;; ?) show_help