sync-vm with torrent support
This commit is contained in:
parent
50cf61844c
commit
399c3d0d66
7 changed files with 329 additions and 12 deletions
|
@ -4,23 +4,42 @@ set -eu
|
|||
|
||||
show_help() {
|
||||
cat << EOF >&2
|
||||
Usage: $(basename "$0") [-u vmname] [-d vmname] [-a]"
|
||||
Usage: $(basename "$0") [-u vmname] [-d vmname] [-a] [-t]"
|
||||
When using option -u (upload), the disk from VM vmname will be synced on server.
|
||||
Otherwise 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"
|
||||
|
||||
upload_image() {
|
||||
# check if VM-Diskimage exists
|
||||
if [[ ! -f "/var/lib/libvirt/images/${VM_NAME}.qcow2" ]]; then
|
||||
if [[ ! (-f "/var/lib/libvirt/images/${VM_NAME}.qcow2" || -f "${VM_DIR}/${VM_NAME}.qcow2") ]]; then
|
||||
echo "File not found ${VM_NAME}.qcow2" >&2
|
||||
exit 1
|
||||
fi
|
||||
# link private VM-Diskimage to system-Dir
|
||||
if [[ -f "${VM_DIR}/${VM_NAME}.qcow2" \
|
||||
&& ( -f "/var/lib/libvirt/images/${VM_NAME}.qcow2" && ("${VM_DIR}/${VM_NAME}.qcow2" -nt "/var/lib/libvirt/images/${VM_NAME}.qcow2") \
|
||||
|| ! -f "/var/lib/libvirt/images/${VM_NAME}.qcow2") ]]; then
|
||||
echo "copy private VM-Diskimage to system-dir"
|
||||
ln -f "${VM_DIR}/${VM_NAME}.qcow2" "/var/lib/libvirt/images/${VM_NAME}.qcow2"
|
||||
fi
|
||||
# check if VM-Machine-Definition XML exists
|
||||
if [[ ! -f "/var/lib/libvirt/images/xml/${VM_NAME}.xml" ]]; then
|
||||
if [[ ! (-f "/var/lib/libvirt/images/xml/${VM_NAME}.xml" || -f "${VM_DIR}/xml/${VM_NAME}") ]]; then
|
||||
echo "File not found ${VM_NAME}.xml" >&2
|
||||
exit 1
|
||||
fi
|
||||
# copy private VM-Maschine-Definition XML to system-Dir
|
||||
if [[ -f "${VM_DIR}/xml/${VM_NAME}.xml" \
|
||||
&& ( -f "/var/lib/libvirt/images/xml/${VM_NAME}.xml" && $(cmp -s "${VM_DIR}/xml/${VM_NAME}.xml" "/var/lib/libvirt/images/xml/${VM_NAME}.xml") \
|
||||
|| ! -f "/var/lib/libvirt/images/xml/${VM_NAME}.xml") ]]; then
|
||||
echo "copy private VM-Maschine-Definition XML to system-dir"
|
||||
cp "${VM_DIR}/${VM_NAME}.xml" "/var/lib/libvirt/images/${VM_NAME}.xml"
|
||||
fi
|
||||
[[ -f "/var/lib/libvirt/images/${VMNAME}.qcow2.torrent" ]] && rsync -av --password-file=/etc/rsync.secret \
|
||||
"/var/lib/libvirt/images/${VM_NAME}.qcow2" rsync://vmuser@server:/vmimages-upload/
|
||||
rsync -av --password-file=/etc/rsync.secret "/var/lib/libvirt/images/${VM_NAME}.qcow2" \
|
||||
rsync://vmuser@server:/vmimages-upload/
|
||||
rsync -av --password-file=/etc/rsync.secret "/var/lib/libvirt/images/xml/${VM_NAME}.xml" \
|
||||
|
@ -43,7 +62,14 @@ sync_all_images() {
|
|||
/var/lib/libvirt/images/
|
||||
}
|
||||
|
||||
while getopts ':u:d:a' OPTION; do
|
||||
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 ':u:d:a:t' OPTION; do
|
||||
case "$OPTION" in
|
||||
u)
|
||||
VM_NAME=$OPTARG
|
||||
|
@ -56,6 +82,9 @@ while getopts ':u:d:a' OPTION; do
|
|||
a)
|
||||
sync_all_images
|
||||
;;
|
||||
t)
|
||||
sync_all_torrents
|
||||
;;
|
||||
?)
|
||||
show_help
|
||||
exit 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue