trixie/roles/lmn_vm/files/sync-vm.sh

62 lines
1.7 KiB
Bash
Raw Normal View History

2023-02-07 19:09:47 +01:00
#!/usr/bin/bash
# Push VM-Disk-Image on server
set -eu
show_help() {
cat << EOF >&2
2023-07-13 15:44:17 +02:00
Usage: $(basename "$0") [-d vmname] [-a] [-t]"
The images from images.list and xml-directory will be synced from server.
2023-05-03 17:24:27 +02:00
Using flag -t all torrents and xml-VM-Definitions will be synced
2023-02-07 19:09:47 +01:00
EOF
}
2023-05-03 17:24:27 +02:00
VM_DIR="/tmp/${SUDO_UID}/vmimages"
2023-02-07 19:09:47 +01:00
download_image() {
2023-07-04 10:43:57 +02:00
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 \
2023-02-07 19:09:47 +01:00
"rsync://vmuser@server:/vmimages-download/${VM_NAME}.qcow2" \
/var/lib/libvirt/images/
2023-07-04 10:43:57 +02:00
rsync -av --password-file=/etc/rsync.secret \
2023-02-07 19:09:47 +01:00
"rsync://vmuser@server:/vmimages-download/xml/${VM_NAME}.xml" \
/var/lib/libvirt/images/xml/
2023-07-04 10:43:57 +02:00
fi
2023-02-07 19:09:47 +01:00
}
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/
}
2023-05-03 17:24:27 +02:00
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/
}
2023-07-13 15:44:17 +02:00
while getopts ':d:at' OPTION; do
2023-02-07 19:09:47 +01:00
case "$OPTION" in
d)
VM_NAME=$OPTARG
download_image
;;
a)
sync_all_images
;;
2023-05-03 17:24:27 +02:00
t)
sync_all_torrents
;;
2023-02-07 19:09:47 +01:00
?)
show_help
exit 1
;;
esac
done