From 08349fb2fd1a79861996df893cb07b152dded777 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 9 Jul 2019 17:11:35 +0200 Subject: [run-virt] Get rid of wrapper and handle directly in run-virt --- .../data/opt/openslx/scripts/vmchooser-run_virt | 65 +------ .../vmchooser/run-virt-includes/init_core.inc | 18 -- .../vmchooser_runvirt_functions.inc | 5 +- .../data/opt/openslx/vmchooser/vmchooser-run_virt | 187 ++++++++++++--------- 4 files changed, 114 insertions(+), 161 deletions(-) mode change 100755 => 120000 core/modules/run-virt/data/opt/openslx/scripts/vmchooser-run_virt (limited to 'core') diff --git a/core/modules/run-virt/data/opt/openslx/scripts/vmchooser-run_virt b/core/modules/run-virt/data/opt/openslx/scripts/vmchooser-run_virt deleted file mode 100755 index 307b6872..00000000 --- a/core/modules/run-virt/data/opt/openslx/scripts/vmchooser-run_virt +++ /dev/null @@ -1,64 +0,0 @@ -#!/bin/bash -# Wrapper script to handle the real vmchooser-run_virt -launch_runvirt() { - if [ $# -ne 1 ]; then - # we except 1 arg only! This case is weird, so lets just not do weird things... - log "Runvirt (wrapper) expects 1 argument! $# given: $@" - return 1 - fi - - local RUNVIRT_SCRIPT="/opt/openslx/vmchooser/vmchooser-run_virt" - if [ ! -s "${RUNVIRT_SCRIPT}" ]; then - log "Missing/empty main run-virt script expected at '${RUNVIRT_SCRIPT}'!" - return 1 - fi - if ! bash -n "${RUNVIRT_SCRIPT}"; then - log "Syntax error in '${RUNVIRT_SCRIPT}'!" - return 1 - fi - - # start the main runvirt script - "${RUNVIRT_SCRIPT}" "$1" "${LOGFILE}" & - local RUNVIRT_PID="$!" - usleep 50000 - # from now on, check if the LOGFILE still exists before writing to it - # if its not, runvirt deleted it and handles the logfile on its own - if ! kill -0 "${RUNVIRT_PID}"; then - [ -f "${LOGFILE}" ] && log "Failed to start main runvirt script. Seems that it exited early?." - return 1 - fi - wait "${RUNVIRT_PID}" - # script exited here, check for exit code and send logfile to sat if appropriate - local RUNVIRT_RET="$?" - if [ ${RUNVIRT_RET} -ne 0 ]; then - if [ ${RUNVIRT_RET} -eq 141 ]; then - # 141 happens on alt + print screen + k or upon automatic logout via systemd - # just sleep here to avoid these annoying (and misleading) slxlogs.... - sleep 3 - fi - [ -f "${LOGFILE}" ] && log "Runvirt failed with '${RUNVIRT_RET}'." - return 1 - fi - return 0 -} -# Log helper -log() { - echo "$(date +%Y-%m-%d-%H-%M-%S): ## WRAPPER ## $@" >> "${LOGFILE}" -} - -## Main part -# logfile is now managed by this wrapper and given as argument to runvirt -declare -rg LOGFILE="/var/log/openslx/run-virt.$(whoami).$$.log" -if ! touch "${LOGFILE}"; then - # this should practically never happen, log it just in case - slxlog "run-virt-wrapper" "Failed to create '${LOGFILE}'." - # fallback: runvirt will try to handle it itself -fi -log "Launching runvirt with arguments '$@'..." -if ! launch_runvirt $@; then - [ -f "${LOGFILE}" ] && slxlog "run-virt-wrapper" "Failed to launch runvirt!" "${LOGFILE}" - exit 1 -fi -log "Runvirt exited as expected." -exit 0 - diff --git a/core/modules/run-virt/data/opt/openslx/scripts/vmchooser-run_virt b/core/modules/run-virt/data/opt/openslx/scripts/vmchooser-run_virt new file mode 120000 index 00000000..1c989e10 --- /dev/null +++ b/core/modules/run-virt/data/opt/openslx/scripts/vmchooser-run_virt @@ -0,0 +1 @@ +../vmchooser/vmchooser-run_virt \ No newline at end of file diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/init_core.inc b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/init_core.inc index cc204d53..79b2ddd1 100644 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/init_core.inc +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/init_core.inc @@ -3,24 +3,6 @@ # Include: Sets core variables and directories # ################################################ -# Check if the path to the logfile was given externally (by the wrapper) -unset WRAPPED -if notempty LOGFILE; then - if [ ! -w "${LOGFILE}" ]; then - # given logfile is not writeable or non-existant - # so handle it ourself (so do not set WRAPPED) - rm -f "${LOGFILE}" - unset LOGFILE - else - # given logfile was ok, mark that we are wrapped - # (for cleanexit to not slxlog the log since - # the wrapper is handling that for us) - declare -rg WRAPPED=aye - fi -fi -isset LOGFILE || declare -rg LOGFILE="/var/log/openslx/run-virt.${USER}.$$.log" -ln -sf "$LOGFILE" "/var/log/openslx/run-virt.latest.log" - # Create temporary directory for current invocation if check_dep mkdir && ! mkdir -p "$TMPDIR" "$CONFDIR"; then writelog "Could not create temporary directories for session" diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc index 7b4554ea..aaace562 100644 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc @@ -176,8 +176,9 @@ cleanexit() { # first send the logfile (in case the user does not close the error before using magic keys e.g.) # for any other error types besides 'user'. Do no slxlog if we run wrapped (from /opt/openslx/scripts/vmchooser-run_virt) - [ "x${EXIT_TYPE}" != "xuser" ] && ! isset WRAPPED && \ - slxlog "runvirt-exit-${EXIT_TYPE}" "Critical error happened in '${BASH_SOURCE[1]}:${FUNCNAME[1]}', see logs." "${LOGFILE}" + if [ "x${EXIT_TYPE}" != "xuser" ]; then + writelog "Critical error happened in '${BASH_SOURCE[1]}:${FUNCNAME[1]}'" + fi # finally display the error error_user "${ERR_TITLE}" " diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt b/core/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt index 349f901b..f545145b 100755 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt @@ -69,102 +69,135 @@ fi # This script expects the path to the xml file describing the VM to be started declare -rg XML_FILE="$1" -# A path to the logfile can be given as second argument (will be set later if empty) +# A path to the logfile can be given as second argument declare -g LOGFILE="$2" - -# Functions needed by vmchooser-run_virt (writelog(), cleanexit(), safesource()) -BASEDIR="$( dirname "${SELF}" )" -RUN_VIRT_INCLUDE_DIR="${BASEDIR}/run-virt-includes" -readonly BASEDIR RUN_VIRT_INCLUDE_DIR -if ! source "${RUN_VIRT_INCLUDE_DIR}/vmchooser_runvirt_functions.inc"; then - slxlog "run-virt" "Could not source ${RUN_VIRT_INCLUDE_DIR}/vmchooser_runvirt_functions.inc" - exit 1 +if [ -z "$LOGFILE" ] || ! touch "$LOGFILE" || ! [ -f "$LOGFILE" ]; then + LOGFILE="/var/log/openslx/run-virt.$(whoami).$$.log" + touch "$LOGFILE" || LOGFILE="$(mktemp)" fi -trap 'trap "" SIGINT SIGTERM; cleanexit' SIGINT SIGTERM +main() { + # Functions needed by vmchooser-run_virt (writelog(), cleanexit(), safesource()) + BASEDIR="$( dirname "${SELF}" )" + RUN_VIRT_INCLUDE_DIR="${BASEDIR}/run-virt-includes" + readonly BASEDIR RUN_VIRT_INCLUDE_DIR + if ! source "${RUN_VIRT_INCLUDE_DIR}/vmchooser_runvirt_functions.inc"; then + slxlog "run-virt" "Could not source ${RUN_VIRT_INCLUDE_DIR}/vmchooser_runvirt_functions.inc" + exit 1 + fi + + trap 'trap "" SIGINT SIGTERM; cleanexit' SIGINT SIGTERM -# Starting sourcing the includes files. Note that the critical ones should use -# the '--exit' option of safesource to trigger cleanexit in case of a corrupted/bad -# include file. + # Starting sourcing the includes files. Note that the critical ones should use + # the '--exit' option of safesource to trigger cleanexit in case of a corrupted/bad + # include file. -# Set core runvirt variables and directories -$(safesource --exit "${RUN_VIRT_INCLUDE_DIR}/init_core.inc") -writelog "#################### Initialization ####################" + # Set core runvirt variables and directories + $(safesource --exit "${RUN_VIRT_INCLUDE_DIR}/init_core.inc") + writelog "#################### Initialization ####################" -# Window manager required early for user feedback through popups (e.g. errors) etc. -$(safesource "${RUN_VIRT_INCLUDE_DIR}/start_windowmanager.inc") + # Window manager required early for user feedback through popups (e.g. errors) etc. + $(safesource "${RUN_VIRT_INCLUDE_DIR}/start_windowmanager.inc") -# Read vmchooser.conf, (generated) virtualization.conf and slx config files -$(safesource --exit "${RUN_VIRT_INCLUDE_DIR}/load_configs.inc") + # Read vmchooser.conf, (generated) virtualization.conf and slx config files + $(safesource --exit "${RUN_VIRT_INCLUDE_DIR}/load_configs.inc") -# Read needed variables from XML file -$(safesource --exit "${RUN_VIRT_INCLUDE_DIR}/get_xml_file_variables.inc") + # Read needed variables from XML file + $(safesource --exit "${RUN_VIRT_INCLUDE_DIR}/get_xml_file_variables.inc") -# Download metadata from server (e.g. vmx for vmware) -$(safesource --exit "${RUN_VIRT_INCLUDE_DIR}/download_vm_metadata.inc") + # Download metadata from server (e.g. vmx for vmware) + $(safesource --exit "${RUN_VIRT_INCLUDE_DIR}/download_vm_metadata.inc") -# Declaration of hardware related variables -$(safesource --exit "${RUN_VIRT_INCLUDE_DIR}/set_runvirt_hardware_variables.inc") + # Declaration of hardware related variables + $(safesource --exit "${RUN_VIRT_INCLUDE_DIR}/set_runvirt_hardware_variables.inc") -# Try to use dnbd3 to access the image, nfs/cifs fallback -$(safesource --exit "${RUN_VIRT_INCLUDE_DIR}/setup_image_access.inc") + # Try to use dnbd3 to access the image, nfs/cifs fallback + $(safesource --exit "${RUN_VIRT_INCLUDE_DIR}/setup_image_access.inc") -# Mark the end of generic run-virt part -writelog "Done with generic run-virt. Now loading virtualizer specific includes." -writelog "#################### Plugin init: $PLUGIN_ID ####################" + # Mark the end of generic run-virt part + writelog "Done with generic run-virt. Now loading virtualizer specific includes." + writelog "#################### Plugin init: $PLUGIN_ID ####################" -# NG: first include the hypervisor includes -$(safesource "${RUN_VIRT_INCLUDE_DIR}/setup_vm_hypervisor.inc") + # NG: first include the hypervisor includes + $(safesource "${RUN_VIRT_INCLUDE_DIR}/setup_vm_hypervisor.inc") -# It must declare PLUGIN_FEATURES to set which features are needed. -# Features are those defined by run-virt.d include files. -# After sourcing the plugin, check that it defined both PLUGIN_FEATURES and -# the main function 'run_plugin' which will be called later by the main scripts. -if ! isset PLUGIN_FEATURES || ! is_function run_plugin; then - writelog "Bad plugin '$PLUGIN_ID': either it did not set PLUGIN_FEATURES or did not define 'run_plugin'." - EXIT_TYPE="internal" EXIT_REASON="Fehlerhaftes vmchooser plugin: '$PLUGIN_ID'." cleanexit 1 -fi -writelog "Requested features:\t${PLUGIN_FEATURES}" - -# Source the *.inc files in run-virt.d -for FILE in ${VMCHOOSER_DIR}/run-virt.d/*.inc; do - $(safesource "$FILE") -done - -# Now look which features were requested and call the handler if one is defined. -for FEAT in $PLUGIN_FEATURES; do - if notempty FEATURE_HANDLERS["${FEAT}"]; then - writelog "Initialising '${FEAT}'..." - if ! ${FEATURE_HANDLERS["$FEAT"]}; then - writelog "\tFailed to run '${FEATURE_HANDLERS["$FEAT"]}'." - error_user "Konnte Feature namens '$FEAT' nicht initialisieren! -Diese Funktion wird nicht verfügbar sein!" # not critical, do not exit! + # It must declare PLUGIN_FEATURES to set which features are needed. + # Features are those defined by run-virt.d include files. + # After sourcing the plugin, check that it defined both PLUGIN_FEATURES and + # the main function 'run_plugin' which will be called later by the main scripts. + if ! isset PLUGIN_FEATURES || ! is_function run_plugin; then + writelog "Bad plugin '$PLUGIN_ID': either it did not set PLUGIN_FEATURES or did not define 'run_plugin'." + EXIT_TYPE="internal" EXIT_REASON="Fehlerhaftes vmchooser plugin: '$PLUGIN_ID'." cleanexit 1 + fi + writelog "Requested features:\t${PLUGIN_FEATURES}" + + # Source the *.inc files in run-virt.d + for FILE in ${VMCHOOSER_DIR}/run-virt.d/*.inc; do + $(safesource "$FILE") + done + + # Now look which features were requested and call the handler if one is defined. + for FEAT in $PLUGIN_FEATURES; do + if notempty FEATURE_HANDLERS["${FEAT}"]; then + writelog "Initialising '${FEAT}'..." + if ! ${FEATURE_HANDLERS["$FEAT"]}; then + writelog "\tFailed to run '${FEATURE_HANDLERS["$FEAT"]}'." + error_user "Konnte Feature namens '$FEAT' nicht initialisieren! + Diese Funktion wird nicht verfügbar sein!" # not critical, do not exit! + fi + else + writelog "\tFeature '$FEAT' has no handler! This function will be unavailable." + notify_user "Feature '$FEAT' nicht unterstützt" fi - else - writelog "\tFeature '$FEAT' has no handler! This function will be unavailable." - notify_user "Feature '$FEAT' nicht unterstützt" + done + # The features should now be initialized, call the main 'run_plugin' function of the hypervisor + writelog "#################### Plugin run: $PLUGIN_ID ####################" + writelog "Calling 'run_plugin' of '$PLUGIN_ID'..." + run_plugin || writelog "Failed to run 'run_plugin' of '$PLUGIN_ID'." + + # It should have set this variable if all went well + if isempty VIRTCMD; then + error_user "Fehler beim Starten der VM-Sitzung" " + 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 $PLUGIN_ID did not set VIRTCMD" + cleanexit 1 + fi + + writelog "VM command: ${VIRTCMD} ${VIRTCMDOPTS}" + # This will start the VM (no eval needed!) + ${VIRTCMD} ${VIRTCMDOPTS} + + writelog "Virtualizer exited with '$?'. Bye." + + cleanexit 0 +} + +# Detach +main & +RUNVIRT_PID="$!" + +# Give some time +usleep 100000 +if ! kill -0 "${RUNVIRT_PID}"; then + if [ -n "${LOGFILE}" ] && [ -f "${LOGFILE}" ]; then + echo "Failed to start runvirt main function. Seems that it exited early (<100ms)." >> "${LOGFILE}" fi -done -# The features should now be initialized, call the main 'run_plugin' function of the hypervisor -writelog "#################### Plugin run: $PLUGIN_ID ####################" -writelog "Calling 'run_plugin' of '$PLUGIN_ID'..." -run_plugin || writelog "Failed to run 'run_plugin' of '$PLUGIN_ID'." - -# It should have set this variable if all went well -if isempty VIRTCMD; then - error_user "Fehler beim Starten der VM-Sitzung" " -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 $PLUGIN_ID did not set VIRTCMD" - cleanexit 1 fi -writelog "VM command: ${VIRTCMD} ${VIRTCMDOPTS}" -# This will start the VM (no eval needed!) -${VIRTCMD} ${VIRTCMDOPTS} +wait "$RUNVIRT_PID" +RUNVIRT_RET="$?" -writelog "Virtualizer exited with '$?'. Bye." +if [ "${RUNVIRT_RET}" -ne 0 ]; then + if [ "${RUNVIRT_RET}" -eq 141 ]; then + # 141 happens on alt + print screen + k or upon automatic logout via systemd + # just sleep here to avoid these annoying (and misleading) slxlogs.... + sleep 2 ; sleep 2 + fi + [ -s "${LOGFILE}" ] && slxlog "run-virt-wrapper" "Failed to launch runvirt!" "${LOGFILE}" + exit "$RUNVIRT_RET" +fi -cleanexit 0 +exit 0 -- cgit v1.2.3-55-g7522