store vmimages in /lmn/vm and mount media in /lmn/media

This commit is contained in:
Raphael Dannecker 2023-08-14 16:18:34 +02:00
parent 68239b56cb
commit e64008c714
14 changed files with 150 additions and 94 deletions

View file

@ -8,7 +8,7 @@ show_help() {
Usage: $(basename "$0") [-n] vmname"
Create a new clone, start the vm (if not yet running) and run virt-viewer.
Squid-Proxy will be started too.
User Home will be mounted on /media/USERNAME/home
### remove, old ### User Home will be mounted on /media/USERNAME/home
-n new clone will be created, even if exists
-s qemu:///system instead of default qemu:///session
EOF
@ -48,39 +48,56 @@ if [[ $# -ne 1 ]] ; then
exit 1
fi
# sync vm-torrents and machine definition file
sudo /usr/local/bin/sync-vm.sh -t
VM_NAME=$1
VM_DIR="/tmp/${UID}/vmimages"
VM_DIR="/tmp/${UID}/vm"
check_images() {
# sync vm-torrents and machine definition file
sudo -u lmnsynci /usr/local/bin/sync-vm.sh -t
if [[ ! -f "/var/lib/libvirt/images/${VM_NAME}.qcow2" && ! -f "${VM_DIR}/${VM_NAME}.qcow2" ]]; then
if [[ ! -f "/var/lib/libvirt/images/${VM_NAME}.qcow2.torrent" ]]; then
echo "no base VM disk '${VM_NAME}.qcow2' found and/or ${VM_NAME} not found on server" >&2
exit 1
fi
# sync vm-disk image by torrent
sudo /usr/local/bin/sync-vm.sh "${VM_NAME}"
echo "sudo /usr/local/bin/sync-vm.sh ${VM_NAME}"
fi
BACKINGARRAY=()
imgfile="/lmn/vm/${VM_NAME}.qcow2" && [[ -f "${VM_DIR}/${VM_NAME}.qcow2" ]] && imgfile="${VM_DIR}/${VM_NAME}.qcow2"
BACKINGARRAY+=("${imgfile}")
echo "Imgfile=$imgfile"
if [[ ! -f "${imgfile}" ]] || ! qemu-img info -U "${imgfile}" | grep "file format: qcow2"; then
if [[ ! -f "/lmn/vm/${VM_NAME}.qcow2.torrent" ]]; then
echo "no base VM disk '${VM_NAME}.qcow2' found and/or ${VM_NAME} not found on server" >&2
exit 1
fi
# sync vm-disk image by torrent
echo "Try to sync VM ${VM_NAME} by torrent"
sudo -u lmnsynci /usr/local/bin/sync-vm.sh "${VM_NAME}"
fi
echo "qcow2 seems to be available"
backingfile=$(qemu-img info -U "${imgfile}" | grep "^backing file:" | cut -d ' ' -f 3)
while [[ ! -z "${backingfile}" ]]; do
echo "Backingfile required: ${backingfile}"
imgfile="/lmn/vm/${backingfile}" && [[ -f "${VM_DIR}/${backingfile}" ]] && imgfile="${VM_DIR}/${backingfile}"
BACKINGARRAY+=("${imgfile}")
if [[ ! -f "${imgfile}" ]] || ! qemu-img info -U "${imgfile}" | grep "file format: qcow2"; then
# sync vm-disk image by torrent
echo "Try to sync backingfile ${backingfile} by torrent"
sudo -u lmnsynci /usr/local/bin/sync-vm.sh "${backingfile%.qcow2}"
fi
backingfile=$(qemu-img info -U "${imgfile}" | grep "^backing file:" | cut -d ' ' -f 3)
done
imgfile="/var/lib/libvirt/images/${VM_NAME}.qcow2" && [[ -f "${VM_DIR}/${VM_NAME}.qcow2" ]] && imgfile="${VM_DIR}/${VM_NAME}.qcow2"
#backingfile=$(qemu-img info -U "${imgfile}" | grep ^image: | cut -d' ' -f2)
echo "VM-Image and required backingfiles available"
echo "Now, let's check the images."
backingfile=$(qemu-img info -U "${imgfile}" | grep "^backing file:" | cut -d ' ' -f 3)
while [[ ! -z "${backingfile}" ]]; do
echo $backingfile
if [[ ! -f "/var/lib/libvirt/images/${backingfile}" && ! -f "${VM_DIR}/${backingfile}" ]]; then
# sync vm-disk image by torrent
sudo /usr/local/bin/sync-vm.sh "${backingfile//.qcow2/}"
echo "sudo /usr/local/bin/sync-vm.sh ${backingfile//.qcow2/}"
fi
imgfile="/var/lib/libvirt/images/${backingfile}" && [[ -f "${VM_DIR}/${backingfile}" ]] && imgfile="${VM_DIR}/${backingfile}"
backingfile=$(qemu-img info -U "${imgfile}" | grep "^backing file:" | cut -d ' ' -f 3)
done
# Check VM-Images in reverse order
for ((i=${#BACKINGARRAY[@]}-1; i>=0; i--))
do
echo "Checking ${BACKINGARRAY[$i]}"
if ! qemu-img check -U "${BACKINGARRAY[$i]}" 2>/dev/null; then
echo "check failed!"
echo "sync ${BACKINGARRAY[$i]} again"
sudo -u lmnsynci /usr/local/bin/sync-vm.sh $(basename "${BACKINGARRAY[$i]}" .qcow2)
fi
done
echo "VM-Image and required backingfiles available and checked"
}
# check, if we have to start squid
if ! killall -s 0 squid; then
@ -88,23 +105,25 @@ if ! killall -s 0 squid; then
/usr/sbin/squid -f /etc/squid/squid-usermode.conf
fi
# check, if we have to mount home
if ! findmnt "/media/${USER}/home"; then
echo "mounting home."
## check, if we have to mount home
#if ! findmnt "/media/${USER}/home"; then
# echo "mounting home."
sudo mounthome.sh
fi
#fi
# because virsh has problems with long pathnames, using diffent configdir
export XDG_CONFIG_HOME="/tmp/${UID}/.config"
if ! virsh --connect="${QEMU}" list | grep "${VM_NAME}-clone"; then
echo "VM not yet running. Try to clone and start."
echo "VM not yet running."
check_images
if [[ "${NEWCLONE}" = 1 ]] || [[ ! -f "${VM_DIR}/${VM_NAME}-clone.qcow2" ]]; then
create-clone.sh "${VM_NAME}"
fi
# delete the old vm
virsh --connect=qemu:///session undefine "${VM_NAME}-clone" || echo "${VM_NAME}-clone did not exist"
# finally, create the new vm
virsh --connect=qemu:///session define "${VM_DIR}/xml/${VM_NAME}-clone.xml"
virsh --connect=qemu:///session define "${VM_DIR}/${VM_NAME}-clone.xml"
trap exit_script SIGHUP SIGINT SIGTERM
[[ "${QEMU}" = 'qemu:///session' ]] && sudo /usr/local/bin/start-virtiofsd.sh "${VM_NAME}"
virsh --connect="${QEMU}" start "${VM_NAME}-clone"