From daa8b9fd8255d9033637c304241deba8d7f3bd58 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 21 Oct 2016 17:27:16 +0200 Subject: [run-virt/...] More cleanup, refactoring, comments --- .../check_runvirt_needed_files.inc | 53 --------- .../vmchooser/run-virt-includes/detect_legacy.inc | 34 ------ .../run-virt-includes/download_vm_metadata.inc | 34 ++++++ .../run-virt-includes/get_xml_file_variables.inc | 131 +++++++-------------- .../set_runvirt_hardware_variables.inc | 21 ++-- .../run-virt-includes/set_runvirt_variables.inc | 2 +- .../run-virt-includes/setup_image_access.inc | 81 ++++++++----- .../run-virt-includes/setup_vm_hypervisor.inc | 26 ++-- .../vmchooser_runvirt_functions.inc | 19 +++ .../data/opt/openslx/vmchooser/vmchooser-run_virt | 41 ++++--- 10 files changed, 195 insertions(+), 247 deletions(-) delete mode 100644 remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/check_runvirt_needed_files.inc delete mode 100644 remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/detect_legacy.inc create mode 100644 remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/download_vm_metadata.inc (limited to 'remote/modules/run-virt') diff --git a/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/check_runvirt_needed_files.inc b/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/check_runvirt_needed_files.inc deleted file mode 100644 index fbb0bc70..00000000 --- a/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/check_runvirt_needed_files.inc +++ /dev/null @@ -1,53 +0,0 @@ -################################################################# -# Include: Check for important files used by vmchooser_run-virt # -################################################################# - -# WARNING: This is perhaps vestigial! - -filecheck() { - filecheck=$(LANG=us ls -lh ${diskfile} 2>&1) - writelog "Filecheck:\n${filecheck}\n" - noimage=$(echo ${filecheck} | grep -i "no such file or directory" | wc -l) - rightsfile=${diskfile} - - # check if link - if [ -L "${diskfile}" ]; then - # take link target - rightsfile=$(ls -lh ${diskfile} 2>&1 | awk -F '-> *' '{print $2}') - rightsfile=${vmdir}/${rightsfile} - filecheck=$(LANG=us ls -lh ${rightsfile} 2>&1) - fi - - # does file exist - if [ "${noimage}" -ge "1" ]; then - writelog "Virtual Machine Image Problem:\c " - writelog "\tThe image you've specified doesn't exist." - writelog "Filecheck says:\c " - writelog "\t\t${diskfile}:\n\t\t\tNo such file or directory" - writelog "Hint:\c " - writelog "\t\t\tCompare spelling of the image with your options.\n" - exit 1 - fi - # readable by calling user - if ! [ -r "${diskfile}" >/dev/null 2>&1 \ - -o -r "${diskfile}" >/dev/null 2>&1 ]; then - writelog "Vmware Image Problem:\c " - writelog "\tThe image you've specified has wrong rights." - writelog "Filecheck says:\t\t$(echo ${filecheck} \ - | awk '{print $1" "$3" "$4}') ${rightsfile}" - writelog "Hint:\t\t\tChange rights with: chmod a+r ${rightsfile}\n" - exit 1 - fi - - # writable (for persistent-mode)? - if ! [ -w "${diskfile}" >/dev/null 2>&1 \ - -o -w "${diskfile}" >/dev/null 2>&1 ] \ - && [ "${np}" = "independent-persistent" ]; then - writelog "Vmware Image Problem:\c " - writelog "\tThe image you have specified has wrong rights." - writelog "Filecheck says:\t\t$(echo ${filecheck} \ - | awk '{print $1" "$3" "$4}') ${rightsfile}" - writelog "Hint:\t\t\tUse nonpersistent-mode or change rights to rw\n" - exit 1 - fi -} diff --git a/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/detect_legacy.inc b/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/detect_legacy.inc deleted file mode 100644 index 8bdfd8b1..00000000 --- a/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/detect_legacy.inc +++ /dev/null @@ -1,34 +0,0 @@ -######################################################## -# Include: Detect, whether runvirt runs in legacy mode # -######################################################## - -# Legacy mode: As runvirt has been before. -# New mode: uuid in xml _and_ vmx given via http. - -writelog "Detecting current/legacy mode ..." - -# First, let's try to extract an imguuid from xmlfile: -declare -rg IMGUUID=$(grep -i -o '/dev/null >&2; then - writelog "wget ${SLX_VMCHOOSER_BASE_URL}/lecture/${IMGUUID}. Triggering legacy mode." - else - writelog "wget ${SLX_VMCHOOSER_BASE_URL}/lecture/${IMGUUID} successful." - if [ ! -s "$TMPCONFIG" ]; then - writelog "Server sent zero byte virtual machine description file. Triggering legacy mode." - else - writelog "Triggering current (non-legacy) mode." - LEGACY= # everything worked - clear legacy mode variable (so we use the "current" mode) - fi - fi -fi - diff --git a/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/download_vm_metadata.inc b/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/download_vm_metadata.inc new file mode 100644 index 00000000..b1995d09 --- /dev/null +++ b/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/download_vm_metadata.inc @@ -0,0 +1,34 @@ +######################################################## +# Include: Detect, whether runvirt runs in legacy mode # +######################################################## + +# Legacy mode: As runvirt has been before. +# New mode: uuid in xml _and_ vmx given via http. + +writelog "Detecting current/legacy mode ..." + +declare -rg TMPCONFIG="$TMPDIR/vmconfig.tmp" + +writelog "IMGUUID extracted: $IMGUUID" + +# Assume legacy mode by default, only trigger "current" mode if everything else below worked +LEGACY=yes +if [ -z "$IMGUUID" ]; then # Keine uuid: Abbruch, Legacy + writelog "Could not extract a uuid param from ${xmlfile}. Triggering legacy mode." +else + # Now getting template file: + if ! wget -T 6 -O "$TMPCONFIG" "${SLX_VMCHOOSER_BASE_URL}/lecture/${IMGUUID}" 2>/dev/null >&2; then + writelog "wget ${SLX_VMCHOOSER_BASE_URL}/lecture/${IMGUUID}. Triggering legacy mode." + else + writelog "wget ${SLX_VMCHOOSER_BASE_URL}/lecture/${IMGUUID} successful." + if [ ! -s "$TMPCONFIG" ]; then + writelog "Server sent zero byte virtual machine description file. Triggering legacy mode." + else + writelog "Triggering current (non-legacy) mode." + LEGACY= # everything worked - clear legacy mode variable (so we use the "current" mode) + fi + fi +fi + +readonly LEGACY + diff --git a/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/get_xml_file_variables.inc b/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/get_xml_file_variables.inc index b7ed648c..eb699ec0 100644 --- a/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/get_xml_file_variables.inc +++ b/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/get_xml_file_variables.inc @@ -1,103 +1,62 @@ -########################################### -# Include: Set hardware related variables # -########################################### +############################################ +# Include: Get needed values from XML file # +############################################ -writelog "Starting configuration..." -writelog "\tLogfile:\t\t${LOGFILE}" -writelog "\t/tmp info:\t\t$(df -h | grep " /tmp$" | awk '{print $2}') \c" -writelog "$(grep "/tmp " /proc/mounts | awk '{print $1" "$2" "$3" "$4}')" -writelog "\tVM XML dir:\t\t$(dirname "${xmlfile}")" -writelog "\tXML file:\t\t${xmlfile}" -writelog "Enable 3D:\t\t${enable3d}" -writelog "VM config:" +writelog "Parsing XML..." + +declare -rg VMSTORE_PATH=/mnt/vmstore + +# Need some lean and mean xml parser some day in the far future +get_xml () { + grep -Pio "<$1"'\s+param=[^"]*".*' "${xmlfile}" \ + | awk -F '"' '{ print $2 }' \ + | sed 's/&/+/g;s/"/"/g;s/<//g' +} # # Name of the virt image -imgname=$(grep -io ' "$dnbd3_tmplog" 2>&1 + dnbd3-fuse -f -o allow_other,max_readahead=262144 -h "$SLX_DNBD3_SERVERS" -i "${SRC_IMG_RELATIVE}" "${dnbd3_fuse_mount_point}" > "$dnbd3_tmplog" 2>&1 RET=$? touch "$dnbd3_exitflag" if [ "$RET" != "0" ]; then writelog "dnbd3-fuse stopped working (Exit code $RET)" - slxlog "virt-dnbd3-fuse" "dnbd3-fuse stopped/refused serving '${imgrelpath}' from '${SLX_DNBD3_SERVERS}' with error code: $RET" "${dnbd3_tmplog}" + slxlog "virt-dnbd3-fuse" "dnbd3-fuse stopped/refused serving '${SRC_IMG_RELATIVE}' from '${SLX_DNBD3_SERVERS}' with error code: $RET" "${dnbd3_tmplog}" fi ) & # give it a bit of time @@ -36,36 +44,55 @@ else for TIMEOUT in 0.5 1 1 OUT; do if [ -r "${dnbd3_fuse_mount_point}/img" ]; then vm_revision="$(grep -m 1 "^Revision:" "${dnbd3_fuse_mount_point}/status" | cut -d" " -f2)" - vm_diskfile="${dnbd3_fuse_mount_point}/img" - writelog "DNBD3: $imgrelpath on $vm_diskfile with rid $vm_revision" - vmpath="$vm_diskfile" + VM_DISKFILE_RO="${dnbd3_fuse_mount_point}/img" + writelog "DNBD3: $SRC_IMG_RELATIVE on $VM_DISKFILE_RO with rid $vm_revision" break fi [ "$TIMEOUT" = "OUT" -o -e "$dnbd3_exitflag" ] && break sleep "$TIMEOUT" done - if [ -z "$vm_diskfile" ]; then - slxlog "virt-dnbd3" "No dnbd3 server for ${imgrelpath} found, trying NFS/CIFS..." "$dnbd3_tmplog" + if [ -z "$VM_DISKFILE_RO" ]; then + slxlog "virt-dnbd3" "No dnbd3 server for ${SRC_IMG_RELATIVE} found, trying NFS/CIFS..." "$dnbd3_tmplog" writelog "No working dnbd3 server found :-(" fi +} + +# See if we should setup dnbd3 image access at all +if ! which dnbd3-fuse; then + writelog "Can't use dnbd3 as dnbd3-fuse binary is not in PATH" +elif [ -z "$SRC_IMG_RELATIVE" ]; then + writelog "Can't use dnbd3 as SRC_IMG_RELATIVE is not set" +elif [ -z "$SLX_DNBD3_SERVERS" ] || [ "x$SLX_VM_DNBD3" != "xyes" ]; then + writelog "Can't use dnbd3 as no servers are given in config, or SLX_VM_DNBD3 is not set to yes" +else + setup_dnbd3 fi -# $vm_diskfile will be empty if dnbd3 is not used or failed. Let's try to fall back to NFS/CIFS via file system -if [ -z "$vm_diskfile" ]; then - new_vmpath=$(ls "${vmpath}.r"* | grep -E -o '\.r[0-9]+$' | grep -o -E '[0-9]+' | sort -n | tail -n 1) - [ -n "$new_vmpath" ] && vmpath="${vmpath}.r${new_vmpath}" - vm_diskfile="$vmpath" +# VM_DISKFILE_RO will be empty if dnbd3 is not used or failed to connect. +# Let's try to fall back to NFS/CIFS via file system +if [ -z "$VM_DISKFILE_RO" ]; then + # Maybe we're reading a dnbd3 directory with RIDs encoded into the filename - use latest one + rid_suffix=$(ls -1 "${SRC_IMG_ABSOLUTE}.r"* | grep -E -o '\.r[0-9]+$' | grep -o -E '[0-9]+$' | sort -n | tail -n 1) + if [ -n "$rid_suffix" ]; then + # found + VM_DISKFILE_RO="${SRC_IMAGE_ABSOLUTE}.r${rid_suffix}" + elif [ -e "$SRC_IMAGE_ABSOLUTE" ]; then + # try name we got from xml in the first place + VM_DISKFILE_RO="$SRC_IMAGE_ABSOLUTE" + fi fi # Check if virtual machine container file exists -if ! [ -e "${vmpath}" ]; then - slxlog "virt-image-missing" "VM image $vmpath not found!" - writelog "Virtual machine image ${vmpath} not found!" +if [ -z "$VM_DISKFILE_RO" ] || ! [ -e "${VM_DISKFILE_RO}" ]; then + slxlog "virt-image-missing" "VM image $VM_DISKFILE_RO not found!" + writelog "Virtual machine image ${VM_DISKFILE_RO} not found!" error_user "Das Image für die gewählte Virtuelle Maschine konnte nicht gefunden werden. Versuchen Sie zunächst, den Computer komplett neu zu starten. Sollte das Problem bestehen bleiben, wenden Sie sich bitte an den Support." cleanexit 1 fi -writelog "Virtual machine disk file: $vm_diskfile" +readonly VM_DISKFILE_RO + +writelog "Disk file to use: $VM_DISKFILE_RO" diff --git a/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/setup_vm_hypervisor.inc b/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/setup_vm_hypervisor.inc index ce15524f..7709a85d 100644 --- a/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/setup_vm_hypervisor.inc +++ b/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/setup_vm_hypervisor.inc @@ -3,30 +3,30 @@ ########################################################################## # Get all virtual machine specific stuff from the respective include file -if [ ! -e "$VMCHOOSER_DIR/$xmlvirt/run-virt.include" ] ; then - slxlog "virt-plugin-missing" "Could not find run-virt.include for $xmlvirt ($VMCHOOSER_DIR/$xmlvirt/run-virt.include)" - writelog "Failed because of missing ${xmlvirt} plugin." - error_user "Konnte den Virtualisierer '$xmlvirt' nicht finden. Starten der Virtuellen Maschine fehlgeschlagen. +if [ ! -e "$VMCHOOSER_DIR/$PLUGIN_ID/run-virt.include" ] ; then + slxlog "virt-plugin-missing" "Could not find run-virt.include for $PLUGIN_ID ($VMCHOOSER_DIR/$PLUGIN_ID/run-virt.include)" + writelog "Failed because of missing ${PLUGIN_ID} plugin." + error_user "Konnte den Virtualisierer '$PLUGIN_ID' nicht finden. Starten der Virtuellen Maschine fehlgeschlagen. Starten Sie den Computer neu und wenden Sie sich an den Support, wenn das Problem weiterhin besteht." cleanexit 1 fi -self="${xmlvirt}" +self="${PLUGIN_ID}" -if ! bash -n "$VMCHOOSER_DIR/$xmlvirt/run-virt.include"; then - slxlog "virt-plugin-syntax" "run-virt.include for $xmlvirt contains syntax errors (bash -n run-virt.include failed)" - writelog "Erroneous run-virt.include for $xmlvirt (syntax check)" - error_user "Das Start-Script für den Virtualisierer '$xmlvirt' ist fehlerhaft. +if ! bash -n "$VMCHOOSER_DIR/$PLUGIN_ID/run-virt.include"; then + slxlog "virt-plugin-syntax" "run-virt.include for $PLUGIN_ID contains syntax errors (bash -n run-virt.include failed)" + writelog "Erroneous run-virt.include for $PLUGIN_ID (syntax check)" + error_user "Das Start-Script für den Virtualisierer '$PLUGIN_ID' ist fehlerhaft. Starten Sie den Computer neu und wenden Sie sich an den Support, wenn das Problem weiterhin besteht." cleanexit 1 fi setup_vm_commandline () { # Now including the hypervisor specific include file: - if ! source "$VMCHOOSER_DIR/$xmlvirt/run-virt.include"; then - slxlog "virt-plugin-error" "run-virt.include for $xmlvirt could not be sourced properly." - writelog "Erroneous run-virt.include for ${xmlvirt}? Source returned != 0" - error_user "Das Start-Script für den Virtualisierer '$xmlvirt' hat einen fehlercode zurückgegeben. + if ! source "$VMCHOOSER_DIR/$PLUGIN_ID/run-virt.include"; then + slxlog "virt-plugin-error" "run-virt.include for $PLUGIN_ID could not be sourced properly." + writelog "Erroneous run-virt.include for ${PLUGIN_ID}? Source returned != 0" + error_user "Das Start-Script für den Virtualisierer '$PLUGIN_ID' hat einen fehlercode zurückgegeben. Starten Sie den Computer neu, falls es beim Ausführen der VM zu Problemen kommt und wenden Sie sich an den Support, wenn das Problem weiterhin besteht." fi diff --git a/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc b/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc index c0d17dbe..7519a48d 100644 --- a/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc +++ b/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc @@ -77,3 +77,22 @@ rv_clean_string() { tr '[A-Z]' '[a-z]' | tr -d -c '[a-z0-9\-]' fi } + +# Check if the given variables are set (empty or not) +isset() { + while [ $# -gt 0 ]; do + [ -z "${!1+x}" ] && return 1 + shift + done + return 0 +} + +# Check if the given variables are not empty +notempty() { + while [ $# -gt 0 ]; do + [ -z "${!1}" ] && return 1 + shift + done + return 0 +} + diff --git a/remote/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt b/remote/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt index 10943c51..b945cca2 100755 --- a/remote/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt +++ b/remote/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt @@ -19,8 +19,8 @@ # fied virtualization tool. ################################################################################ -RUNVIRTINCLUDEDIR=/opt/openslx/vmchooser/run-virt-includes -xmlfile="$1" +declare -rg RUNVIRTINCLUDEDIR=/opt/openslx/vmchooser/run-virt-includes +declare -rg xmlfile="$1" # Functions needed by vmchooser-run_virt (writelog(), cleanexit(), rv_clean_string()) if ! source "${RUNVIRTINCLUDEDIR}/vmchooser_runvirt_functions.inc"; then @@ -29,10 +29,20 @@ if ! source "${RUNVIRTINCLUDEDIR}/vmchooser_runvirt_functions.inc"; then fi trap 'trap "" SIGINT SIGTERM; cleanexit' SIGINT SIGTERM + # Define default dirs / get configs source "${RUNVIRTINCLUDEDIR}/set_runvirt_variables.inc" -# Function to detect whether we can use the new way (vmx via http) or the old way (legacy): -source "${RUNVIRTINCLUDEDIR}/detect_legacy.inc" # This yields LEGACY, IMGUUID, IMGVMX + +# Read needed variables from XML file +source "${RUNVIRTINCLUDEDIR}/get_xml_file_variables.inc" + +# Download meta data from server (e.g. vmx for vmware) +source "${RUNVIRTINCLUDEDIR}/download_vm_metadata.inc" + +if ! isset IMGUUID TMPCONFIG TMPDIR USER; then + slxlog "run-virt" "Internal sanity check failed: One of IMGUUID TMPCONFIG TMPDIR USER is not set." + cleanexit 1 +fi if [ "$LEGACY" ]; then @@ -54,13 +64,6 @@ fi # Proper meta data received - proceed normally -# check for important files used (filecheck()) - vestigial? -# This include does not currently work. TODO. -# source ${RUNVIRTINCLUDEDIR}/check_runvirt_needed_files.inc && filecheck - -# Read needed variables from XML file -source ${RUNVIRTINCLUDEDIR}/get_xml_file_variables.inc - # Helper that looks for virtualizer-specific include, show error to user if not found source "${RUNVIRTINCLUDEDIR}/setup_vm_hypervisor.inc" @@ -72,22 +75,22 @@ source "${RUNVIRTINCLUDEDIR}/setup_firewall.inc" || writelog "Could not source s setup_firewall || writelog "Could not run setup_firewall" # Sound setup -source ${RUNVIRTINCLUDEDIR}/setup_sound.inc +source "${RUNVIRTINCLUDEDIR}/setup_sound.inc" # Declaration of hardware relatedt variables -source ${RUNVIRTINCLUDEDIR}/set_runvirt_hardware_variables.inc +source "${RUNVIRTINCLUDEDIR}/set_runvirt_hardware_variables.inc" # Start printer daemon -source ${RUNVIRTINCLUDEDIR}/setup_printer_lpd.inc +source "${RUNVIRTINCLUDEDIR}/setup_printer_lpd.inc" # Setup virtual floppy b: for windows guests with config.xml, openslx.exe etc. -source ${RUNVIRTINCLUDEDIR}/setup_virtual_floppy.inc +source "${RUNVIRTINCLUDEDIR}/setup_virtual_floppy.inc" # Try to use dnbd3 to access the image, nfs/cifs fallback -source ${RUNVIRTINCLUDEDIR}/setup_image_access.inc +source "${RUNVIRTINCLUDEDIR}/setup_image_access.inc" # Window manager required for handling of popups etc. -source ${RUNVIRTINCLUDEDIR}/start_windowmanager.inc +source "${RUNVIRTINCLUDEDIR}/start_windowmanager.inc" # Source run-virt.include of virtualizer setup_vm_commandline @@ -95,9 +98,9 @@ setup_vm_commandline # It should have set this variable if all went well if [ -z "${VIRTCMD}" ]; then error_user "Fehler beim Starten der VM-Sitzung" " -Das Start-Script für den Virtualisierer $xmlvirt hat kein Kommando +Das Start-Script für den Virtualisierer $PLUGIN_ID hat kein Kommando zum Starten der Sitzung definiert. Kann Sitzung nicht initialisieren." - slxlog "virt-plugin-error" "run-virt.include for $xmlvirt did not set VIRTCMD" + slxlog "virt-plugin-error" "run-virt.include for $PLUGIN_ID did not set VIRTCMD" cleanexit 1 fi -- cgit v1.2.3-55-g7522