From da71eed94b1d26e132cd9e5c3b552b4a0360b9c0 Mon Sep 17 00:00:00 2001 From: "Andreas B. Mundt" Date: Mon, 4 Sep 2023 13:20:17 +0200 Subject: [PATCH] Clean up VM scripts a bit. --- roles/lmn_vm/files/create-clone.sh | 43 ------------- roles/lmn_vm/files/run-vm.sh | 92 +++++++++++++++++---------- roles/lmn_vm/files/start-virtiofsd.sh | 2 +- roles/lmn_vm/files/virtiofs@.service | 9 +-- roles/lmn_vm/tasks/main.yml | 1 - 5 files changed, 66 insertions(+), 81 deletions(-) delete mode 100755 roles/lmn_vm/files/create-clone.sh diff --git a/roles/lmn_vm/files/create-clone.sh b/roles/lmn_vm/files/create-clone.sh deleted file mode 100755 index 04d7a78..0000000 --- a/roles/lmn_vm/files/create-clone.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/bash -# create VM clone - -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" >&2 - echo "Usage: $0 vm_name" >&2 - exit 1 -fi - -# change to image-directory -cd /lmn/vm - -VM_NAME=$1 -VM_DIR="/tmp/${UID}/vm" -VM_XML="${VM_DIR}/${VM_NAME}-clone.xml" - -if ! [[ -f "${VM_NAME}.xml" && -f "${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/virtiofsd/${VM_NAME}-clone.sock:" "${VM_XML}" diff --git a/roles/lmn_vm/files/run-vm.sh b/roles/lmn_vm/files/run-vm.sh index 30deedf..45f5685 100755 --- a/roles/lmn_vm/files/run-vm.sh +++ b/roles/lmn_vm/files/run-vm.sh @@ -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" diff --git a/roles/lmn_vm/files/start-virtiofsd.sh b/roles/lmn_vm/files/start-virtiofsd.sh index f346397..da06c74 100755 --- a/roles/lmn_vm/files/start-virtiofsd.sh +++ b/roles/lmn_vm/files/start-virtiofsd.sh @@ -13,4 +13,4 @@ VM_NAME="$1" ## Make sure VMs can read the base directory: chgrp 1010 "/lmn/media/${SUDO_USER}" -systemctl restart virtiofs@${VM_NAME}-clone:${SUDO_USER}.service +systemctl start virtiofs@${VM_NAME}-clone:${SUDO_USER}.service diff --git a/roles/lmn_vm/files/virtiofs@.service b/roles/lmn_vm/files/virtiofs@.service index 2a436b6..a54865c 100644 --- a/roles/lmn_vm/files/virtiofs@.service +++ b/roles/lmn_vm/files/virtiofs@.service @@ -4,10 +4,11 @@ Description=share home folders via virtiofsd [Service] Type=exec -ExecStart=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; usr="$${v##*:}"; exec /usr/lib/qemu/virtiofsd --socket-path="/run/virtiofsd/$${soc}.sock" -o source="/lmn/media/$${usr}"' -ExecStartPost=chmod 755 /run/virtiofsd/ -ExecStartPost=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; usr="$${v##*:}"; chown "$$usr" "/run/virtiofsd/$${soc}.sock"' -ExecStopPost=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; rm "/run/virtiofsd/$${soc}.sock"' +ExecStartPre=/usr/bin/mkdir -p /run/virtiofs/ +ExecStart=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; usr="$${v##*:}"; exec /usr/lib/qemu/virtiofsd --socket-path="/run/virtiofs/$${soc}.sock" -o source="/lmn/media/$${usr}"' +ExecStartPost=/usr/bin/chmod 755 /run/virtiofs/ +ExecStartPost=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; usr="$${v##*:}"; chown "$$usr" "/run/virtiofs/$${soc}.sock"' +ExecStopPost=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; rm "/run/virtiofs/$${soc}.sock"' SuccessExitStatus=1 [Install] diff --git a/roles/lmn_vm/tasks/main.yml b/roles/lmn_vm/tasks/main.yml index 4e8a0a8..4c09f69 100644 --- a/roles/lmn_vm/tasks/main.yml +++ b/roles/lmn_vm/tasks/main.yml @@ -114,7 +114,6 @@ - mounthome.sh - create-vm.sh - rebase-vm.sh - - create-clone.sh - run-vm.sh - upload-vm.sh - sync-vm.sh