Sync VM back to the server.

This commit is contained in:
Andreas B. Mundt 2023-02-07 19:09:47 +01:00
parent 89ddaa0138
commit c1ff7319f4
3 changed files with 73 additions and 11 deletions

View file

@ -0,0 +1 @@
%role-teacher ALL=(root) NOPASSWD: /usr/local/bin/sync-vm.sh

View file

@ -0,0 +1,64 @@
#!/usr/bin/bash
# Push VM-Disk-Image on server
set -eu
show_help() {
cat << EOF
Usage: $(basename "$0") [-u vmname] [-d vmname] [-a]"
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.
EOF
}
upload_image() {
# check if VM-Diskimage exists
if [[ ! -f "/var/lib/libvirt/images/${VM_NAME}.qcow2" ]]; then
echo "File not found ${VM_NAME}.qcow2"
exit 1
fi
# check if VM-Machine-Definition XML exists
if [[ ! -f "/var/lib/libvirt/images/xml/${VM_NAME}.xml" ]]; then
echo "File not found ${VM_NAME}.xml"
exit 1
fi
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" \
rsync://vmuser@server:/vmimages-upload/xml/
}
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/
}
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/
}
while getopts ':u:d:a' OPTION; do
case "$OPTION" in
u)
VM_NAME=$OPTARG
upload_image
;;
d)
VM_NAME=$OPTARG
download_image
;;
a)
sync_all_images
;;
?)
show_help
exit 1
;;
esac
done

View file

@ -50,21 +50,17 @@
dest: /etc/squid/squid-usermode.conf
mode: '0644'
- name: deploy sudo configuration
- name: deploy sudo configurations
copy:
src: lmn-mounthome-sudo
dest: /etc/sudoers.d/90-lmn-mounthome
owner: root
group: root
mode: '0700'
- name: deploy sudo configuration
copy:
src: lmn-create-clone-sudo
dest: /etc/sudoers.d/90-lmn-create-clone
src: "{{ item }}"
dest: "/etc/sudoers.d/90-{{ item }}"
owner: root
group: root
mode: '0700'
loop:
- lmn-mounthome-sudo
- lmn-create-clone-sudo
- lmn-sync-vm-sudo
- name: deploy mount home script
copy:
@ -79,6 +75,7 @@
- rebase-vm.sh
- create-clone.sh
- run-vm.sh
- sync-vm.sh
- name: deploy http proxy config
copy: