#!/usr/bin/bash # Push VM-Disk-Image on server set -eu show_help() { cat << EOF >&2 Usage: $(basename "$0") [-d] [-a] [-t] [vmnames]" Images from vmnames-List will be synced from server. Default by torrent. Using flag -d VMs will be synced by rsync Using flag -a images from images.list and xml-directory will be synced from server. Using flag -t all torrents and xml-VM-Definitions will be synced EOF } download_image() { rsync -av --password-file=/etc/rsync.secret \ "rsync://vmuser@server:/vmimages-download/${VM_NAME}.qcow2" \ /var/lib/libvirt/images/ rsync -av --password-file=/etc/rsync.secret \ "rsync://vmuser@server:/vmimages-download/xml/${VM_NAME}.xml" \ /var/lib/libvirt/images/xml/ rsync -av --password-file=/etc/rsync.secret \ "rsync://vmuser@server:/vmimages-download/${VM_NAME}.qcow2.torrent" \ /var/lib/libvirt/images/ /usr/local/bin/vmimage-torrent restart "${VM_NAME}.qcow2" } torrent_image() { if [[ -f "/var/lib/libvirt/images/${VM_NAME}.qcow2.torrent" ]]; then cd /var/lib/libvirt/images ctorrent -e 0 "${VM_NAME}.qcow2.torrent" /usr/local/bin/vmimage-torrent restart "${VM_NAME}.qcow2" else echo "No torrent-File found" exit 1 fi } sync_all_images() { rsync -av --password-file=/etc/rsync.secret --files-from=/var/lib/libvirt/images/images.list \ rsync://vmuser@server:/vmimages-download/ /var/lib/libvirt/images/ rsync -av --password-file=/etc/rsync.secret rsync://vmuser@server:/vmimages-download/xml \ /var/lib/libvirt/images/ } sync_all_torrents() { rsync -av --password-file=/etc/rsync.secret rsync://vmuser@server:/vmimages-download/*.torrent \ /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 case "$OPTION" in d) DOWNLOAD=1 ;; a) sync_all_images exit 0 ;; t) sync_all_torrents exit 0 ;; ?) show_help exit 1 ;; esac done shift "$((OPTIND -1))" # if less than one arguments supplied, display usage if [[ $# -lt 1 ]]; then show_help exit 1 fi for VM_NAME in "$@"; do if [[ -v "DOWNLOAD" ]]; then echo "Downloading $VM_NAME" download_image else echo "Torrenting $VM_NAME" torrent_image fi done