#!/usr/bin/bash # Push VM-Disk-Image on server set -eu show_help() { cat << EOF >&2 Usage: $(basename "$0") [-d vmname] [-a] [-t]" The 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 } VM_DIR="/tmp/${SUDO_UID}/vmimages" download_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 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/ 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/ } while getopts ':d:at' OPTION; do case "$OPTION" in d) VM_NAME=$OPTARG download_image ;; a) sync_all_images ;; t) sync_all_torrents ;; ?) show_help exit 1 ;; esac done