New scripts and sudo rules. Implement qemu session mode.

This commit is contained in:
Andreas B. Mundt 2023-04-04 13:29:00 +02:00
parent 91aab8e75b
commit 7871936a67
19 changed files with 176 additions and 67 deletions

View file

@ -3,7 +3,7 @@
set -eu
show_help() {
cat << EOF
cat << EOF >&2
Usage: $(basename "$0") [-n newname] vmname"
This script takes as input the name of the VM to rebase one level down
-n new name of the rebased image
@ -31,19 +31,20 @@ if [[ $# -ne 1 ]]; then
fi
# change to Images directory
cd /var/lib/libvirt/images
VM_DIR="/tmp/${UID}/vmimages"
cd "${VM_DIR}"
VM_NAME="$1"
# check if VM-Diskimage exists
if [[ ! -f "${VM_NAME}.qcow2" ]]; then
echo "File not found ${VM_NAME}.qcow2"
echo "File not found ${VM_NAME}.qcow2" >&2
exit 1
fi
# check if new VM-Diskimage exists
if [[ -v NEWNAME ]] && [[ -f "${NEWNAME}.qcow2" ]]; then
echo "New Base already exists: ${NEWNAME}.qcow2"
echo "New Base already exists: ${NEWNAME}.qcow2" >&2
exit 1
fi
@ -52,13 +53,13 @@ NEWBASE=$(qemu-img info --backing-chain "${VM_NAME}.qcow2" | grep image | head
CURRENTBASE=$(qemu-img info --backing-chain "${VM_NAME}.qcow2" | grep image | head -n 2 | tail -n 1 | cut -d' ' -f2)
if [[ ! "${NUMBASES}" -ge 3 ]]; then
echo "Image must have at least 2 backing-files"
echo "Image must have at least 2 backing-files" >&2
exit 1
fi
# check if base Diskimage exists
if [[ ! -f "${NEWBASE}" ]] || [[ ! -f "${CURRENTBASE}" ]]; then
echo "Backingfiles not found ${CURRENTBASE}, ${NEWBASE}"
echo "Backingfiles not found ${CURRENTBASE}, ${NEWBASE}" >&2
exit 1
fi
@ -67,7 +68,14 @@ qemu-img rebase -f qcow2 -b "${NEWBASE}" -F qcow2 "${VM_NAME}.qcow2"
if [[ -v NEWNAME ]]; then
# copy and adapt machine definition file
CURRENTNAME="${CURRENTBASE/.qcow2/}"
cp "xml/${CURRENTNAME}.xml" "xml/${NEWNAME}.xml"
if [[ -f "xml/${CURRENTNAME}.xml" ]]; then
cp "xml/${CURRENTNAME}.xml" "xml/${NEWNAME}.xml"
elif [[ -f "/var/lib/libvirt/images/xml/${CURRENTNAME}.xml" ]]; then
cp "/var/lib/libvirt/images/xml/${CURRENTNAME}.xml" "xml/${NEWNAME}.xml"
else
echo "no machine definition file found" >&2
exit 1
fi
sed -i "s/${CURRENTNAME}/${NEWNAME}/" "xml/${NEWNAME}.xml"
NEWNAME="${NEWNAME}.qcow2"
else