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

@ -5,8 +5,8 @@ set -eu
# if less or more than one arguments supplied, display usage
if [[ $# -ne 1 ]]; then
echo "This script takes as input the name of the VM to clone"
echo "Usage: $0 vm_name"
echo "This script takes as input the name of the VM to clone" >&2
echo "Usage: $0 vm_name" >&2
exit 1
fi
@ -15,22 +15,30 @@ cd /var/lib/libvirt/images
VM_NAME=$1
if [[ ! -f "xml/${VM_NAME}.xml" ]] || [[ ! -f "${VM_NAME}.gcow2" ]]; then
echo "xml or qcow2 File does not exists."
if [[ ! -f "xml/${VM_NAME}.xml" ]] || [[ ! -f "${VM_NAME}.qcow2" ]]; then
echo "xml or qcow2 File does not exists." >&2
exit 1
fi
VM_DIR="/tmp/${UID}/vmimages"
VM_XML="${VM_DIR}/xml/${VM_NAME}-clone.xml"
# Create User-VM-Dir and link system VM-Images
[[ -d "${VM_DIR}/xml" ]] || mkdir -p "${VM_DIR}/xml"
sudo /usr/local/bin/link-images.sh
# Create backing file
cd "${VM_DIR}"
qemu-img create -f qcow2 -F qcow2 -b "${VM_NAME}.qcow2" "${VM_NAME}-clone.qcow2"
cp "xml/${VM_NAME}.xml" "xml/${VM_NAME}-clone.xml"
# Create machine-definition-file
cp "/var/lib/libvirt/images/xml/${VM_NAME}.xml" "${VM_XML}"
# and actually rename the vm: (this also updates the storage path)
sed -i "s/${VM_NAME}/${VM_NAME}-clone/" "xml/${VM_NAME}-clone.xml"
# set VM_DIR:
sed -i "s:VMIMAGEDIR:${VM_DIR}:" "${VM_XML}"
# set Username for mounting data from correct user
sed -i "s/USER/${SUDO_USER}/" "xml/${VM_NAME}-clone.xml"
# and actually rename the vm (this also updates part of the storage path):
sed -i "s/${VM_NAME}/${VM_NAME}-clone/" "${VM_XML}"
# delete the old vm
virsh --connect=qemu:///system undefine "${VM_NAME}-clone" || echo "${VM_NAME}-clone did not exist"
# finally, create the new vm
virsh --connect=qemu:///system define "xml/${VM_NAME}-clone.xml"
# set virtiofs-Socket
sed -i "s:VIRTIOFSSOCKET:${XDG_RUNTIME_DIR}/virtiofs/${VM_NAME}-clone.sock:" "${VM_XML}"