Clean up VM scripts a bit.

This commit is contained in:
Andreas B. Mundt 2023-09-04 13:20:17 +02:00
parent 3ea922c263
commit da71eed94b
5 changed files with 66 additions and 81 deletions

View file

@ -21,36 +21,6 @@ exit_script() {
kill -- -$$ # Sends SIGTERM to child/sub processes
}
QEMU='qemu:///session'
NEWCLONE=0
while getopts ':ns' OPTION; do
case "$OPTION" in
n)
NEWCLONE=1
;;
s)
QEMU='qemu:///system'
;;
?)
show_help
exit 1
;;
esac
done
shift "$((OPTIND -1))"
# if less than one arguments supplied, display usage
if [[ $# -ne 1 ]] ; then
show_help
exit 1
fi
VM_NAME=$1
VM_DIR="/tmp/${UID}/vm"
check_images() {
# sync vm-torrents and machine definition file
sudo -u lmnsynci /usr/local/bin/sync-vm.sh -t
@ -99,7 +69,65 @@ check_images() {
echo "VM-Image and required backingfiles available and checked"
}
#################
create-clone() {
local VM_NAME="$1"
local VM_DIR="/tmp/${UID}/vm"
local VM_XML="${VM_DIR}/${VM_NAME}-clone.xml"
local VM_SYSDIR="/lmn/vm"
if ! [[ -f "$VM_SYSDIR/${VM_NAME}.xml" && -f "$VM_SYSDIR/${VM_NAME}.qcow2" ]] && ! [[ -f "${VM_DIR}/${VM_NAME}.xml" && -f "${VM_DIR}/${VM_NAME}.qcow2" ]]; then
echo "xml or qcow2 File does not exists." >&2
exit 1
fi
# Create User-VM-Dir and link system VM-Images
[[ -d "${VM_DIR}" ]] || mkdir -p "${VM_DIR}"
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"
# Create machine-definition-file
cp "${VM_DIR}/${VM_NAME}.xml" "${VM_XML}"
# set VM_DIR:
sed -i "s:VMIMAGEDIR:${VM_DIR}:" "${VM_XML}"
# and actually rename the vm (this also updates part of the storage path):
sed -i "s/${VM_NAME}/${VM_NAME}-clone/" "${VM_XML}"
# set virtiofs-Socket
sed -i "s:VIRTIOFSSOCKET:/run/virtiofs/${VM_NAME}-clone.sock:" "${VM_XML}"
}
QEMU='qemu:///session'
NEWCLONE=0
while getopts ':ns' OPTION; do
case "$OPTION" in
n)
NEWCLONE=1
;;
s)
QEMU='qemu:///system'
;;
?)
show_help
exit 1
;;
esac
done
shift "$((OPTIND -1))"
# if less than one arguments supplied, display usage
if [[ $# -ne 1 ]] ; then
show_help
exit 1
fi
VM_NAME=$1
VM_DIR="/tmp/${UID}/vm"
# check, if we have to start squid
if ! killall -s 0 squid; then
@ -114,7 +142,7 @@ if ! virsh --connect="${QEMU}" list | grep "${VM_NAME}-clone"; then
echo "VM not yet running."
check_images
if [[ "${NEWCLONE}" = 1 ]] || [[ ! -f "${VM_DIR}/${VM_NAME}-clone.qcow2" ]]; then
create-clone.sh "${VM_NAME}"
create-clone "${VM_NAME}"
fi
# delete the old vm
virsh --connect=qemu:///session undefine "${VM_NAME}-clone" || echo "${VM_NAME}-clone did not exist"