diff options
6 files changed, 84 insertions, 44 deletions
diff --git a/core/modules/run-virt/data/opt/openslx/scripts/systemd-run_virt_env b/core/modules/run-virt/data/opt/openslx/scripts/systemd-run_virt_env index 68afd189..6a30acf4 100755 --- a/core/modules/run-virt/data/opt/openslx/scripts/systemd-run_virt_env +++ b/core/modules/run-virt/data/opt/openslx/scripts/systemd-run_virt_env @@ -96,19 +96,31 @@ if grep -q '^flags.*\<vmx\>' "/proc/cpuinfo"; then echo "VTFLAG=0" >> "${VMCHOOSER_VIRT_CONF}" fi -# write mac +# generated mac address parts from the host's mac address if [ -n "$SLX_PXE_MAC" ]; then HOSTMACADDR="${SLX_PXE_MAC}" else ## Fallback: HOSTMACADDR="$(ip a | grep ether | grep -o -E -i '([0-9a-f]{2}:){5}[0-9a-f]{2}' | head -n 1)" fi echo "HOSTMACADDR='$HOSTMACADDR'" >> "${VMCHOOSER_VIRT_CONF}" -# TODO: We should only generate the suffix here (last 3 octets) as the first 3 are -# dependant on the virtualizer/emulator. Check if any run-virt.include still relies on -# $macguestpart/$macaddr (now VM_MAC_ADDR). If so, fix it to use its specific first 3 bytes -# and append $MACADDRSUFFIX. For now, keep MACADDRPREFIX as vmware's defaults and overwrite -# it if needed. -MACADDRPREFIX='00:50:56' +# NG: we define 10 fixed prefixes (00:FF:00 -> 00:FF:09) to use for VMs +# First, check if the first 3 bytes of the host's mac address is +# contained in our list. If it is, use the next one in the list. +# If not, or the host's prefix is the last one in the list, use the first one. +# This lays the groundwork to avoid mac addresses collisions in nested operations. +MACADDRPREFIX= +for i in {0..9}; do + if [ "x${HOSTMACADDR#00:FF:0$i}" != "x${HOSTMACADDR}" ]; then + # current prefix is indeed part of the host's mac address + # use the next prefix if != 9 + [ $i -lt 9 ] && MACADDRPREFIX="00:FF:0$(( i++ ))" + fi +done +# if it is still unset, then either the host's prefix did not match any in the list +# or it matched the 9th. Either way, use the first prefix for this level of VMs. +[ -n "${MACADDRPREFIX}" ] || MACADDRPREFIX='00:FF:00' +# grab the last 2 bytes of the host's mac and prepend our VMID +# placeholder (later replaced used by run-virt). MACADDRSUFFIX="$(echo "$HOSTMACADDR" | awk -F ":" '{print "%VMID%:" $(NF-1) ":" $NF}' | tr '[a-z]' '[A-Z]')" echo "MACADDRPREFIX='$MACADDRPREFIX'" >> "${VMCHOOSER_VIRT_CONF}" echo "MACADDRSUFFIX='$MACADDRSUFFIX'" >> "${VMCHOOSER_VIRT_CONF}" 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 index 447f98bc..8f6da0bf 120000..100755 --- a/core/modules/run-virt/data/opt/openslx/scripts/vmchooser-run_virt +++ b/core/modules/run-virt/data/opt/openslx/scripts/vmchooser-run_virt @@ -1 +1,41 @@ -/opt/openslx/vmchooser/vmchooser-run_virt
\ No newline at end of file +#!/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... + slxlog "run-virt-wrapper" "Runvirt expects only 1 argument! $# given: $@" + exit 1 + fi + + local RUNVIRT_SCRIPT="/opt/openslx/vmchooser/vmchooser-run_virt" + if [ ! -s "${RUNVIRT_SCRIPT}" ]; then + slxlog "run-virt-wrapper" "Missing/empty main run-virt script expected at $RUNVIRT_SCRIPT!" + exit 1 + fi + if ! bash -n "${RUNVIRT_SCRIPT}"; then + slxlog "run-virt-wrapper" "Syntax error in '${RUNVIRT_SCRIPT}'!" + exit 1 + fi + # we now manage the logfile externally + # create it at the expected place, namely: + local LOGFILE="/var/log/openslx/run-virt.$(whoami).$$.log" + if ! echo "$(date +%Y-%m-%d-%H-%M-%S): ## Wrapper init ##" >> "${LOGFILE}"; then + slxlog "run-virt-wrapper" "Failed to create '${LOGFILE}'." + # fallback: runvirt will create it itself + fi + + "${RUNVIRT_SCRIPT}" "$1" "${LOGFILE}" & + + wait "$!" + # script exited here, check for exit code and send logfile to sat if appropriate + local RUNVIRT_RET="$?" + if [ ${RUNVIRT_RET} -ne 0 ]; then + slxlog "run-virt-failed" "Runvirt failed with '${RUNVIRT_RET}'. See attached log." "${LOGFILE}" + exit ${RUNVIRT_RET} + fi + echo "$(date +%Y-%m-%d-%H-%M-%S): ## Wrapper exit ##" >> "${LOGFILE}" + +} + +launch_runvirt $@ + 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 8e83811c..e551e5e8 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 @@ -5,9 +5,17 @@ declare -rg VMCHOOSER_DIR="/opt/openslx/vmchooser" declare -rg VMCHOOSER_CONF_DIR="$VMCHOOSER_DIR/config" declare -rg USER="$(whoami)" -declare -rg LOGFILE="/var/log/openslx/run-virt.${USER}.$$.log" declare -rg TMPDIR="/tmp/virt/${USER}/$$" +# Check if the path to the logfile was given externally (by the wrapper) +unset WRAPPED +if isset LOGFILE; then + # given by wrapper + declare -rg WRAPPED=aye +else + declare -rg LOGFILE="/var/log/openslx/run-virt.${USER}.$$.log" +fi + # Create temporary directory for current invocation if check_dep mkdir && ! mkdir -p "$TMPDIR"; then writelog "Could not create temporary directory '$TMPDIR' for session" diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/set_runvirt_hardware_variables.inc b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/set_runvirt_hardware_variables.inc index 5fb14806..4bf46023 100644 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/set_runvirt_hardware_variables.inc +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/set_runvirt_hardware_variables.inc @@ -10,12 +10,6 @@ # is the last in the list, use the first one again. # This way we support up to 10 nested/concurrent VMs. # Use: 00:FF:00 for firtual machines ;) -declare -gA MAC_PREFIXES -for i in {0..9}; do - # make an associate array, it will make our life easier later - MAC_PREFIXES["00:FF:0$i"]="$i" -done - ## Functions ## # Sets the VM's hostname to the original hostname prefixed with a fixed string and its ID @@ -71,20 +65,7 @@ set_virt_memory() { set_virt_mac() { # First, determine prefix of the host's MAC address isset HOSTMACADDR || writelog "Host's MAC address was not defined in ${VMCHOOSER_CONF_DIR}/virtualizer.conf" - local HOST_MAC_PREFIX=$(echo "$HOSTMACADDR" | tr '[a-z]' '[A-Z]' | grep -o -E '^[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}') - unset MACADDRPREFIX - if notempty HOST_MAC_PREFIX && notempty MAC_PREFIXES[${HOST_MAC_PREFIX}]; then - # the host's prefix was in our list, so use the next one in the list - local -i HOST_MAC_PREFIX_INDEX="${MAC_PREFIXES[${HOST_MAC_PREFIX}]}" # this will be at least 0 - # increment if its between 0 and 8 - if [[ $HOST_MAC_PREFIX_INDEX =~ ^[0-8]$ ]]; then - declare -g MACADDRPREFIX="${HOST_MAC_PREFIX%?}$(( ++HOST_MAC_PREFIX_INDEX ))" - fi - fi - # host's mac prefix could not be determined (practically this should never happen) - # or the prefix is not in our mac address list or we reached the end of the list. - # Either way, use first mac prefix in the list - notempty MACADDRPREFIX || declare -g MACADDRPREFIX="$(echo ${!MAC_PREFIXES[@]} | awk '{print $1}')" + isset MACADDRPREFIX || writelog "No MAC address prefix was defined in ${VMCHOOSER_CONF_DIR}/virtualizer.conf" # Fill in VM_ID local MACADDRSUFFIX=${MACADDRSUFFIX//%VMID%/"${VM_ID}"} 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 d09f6a75..2222b0af 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 @@ -134,10 +134,10 @@ cleanexit() { fi # Now see if we need to do the catch all error stuff - # no exit code given, return 129 - [ $# -eq 0 ] && exit 129 # if 0 given, exit 0 [ "x$1" = "x0" ] && exit 0 + # if no code was given, exit 129 + [ $# -eq 0 ] && writelog "Cleanexit called without arguments! Dev error?" # given exit code is set and not 0, handle the error now # now evaluate the EXIT_{TYPE,REASON} variables and generate error title/text @@ -168,8 +168,8 @@ cleanexit() { fi # 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'. - [ "x${EXIT_TYPE}" != "xuser" ] && \ + # 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}" # finally display the error @@ -179,7 +179,8 @@ ${ERR_TEXT} ${ERR_FOOTER} " writelog "All done. Exiting." - exit "$1" + # if no exit code was given as $1, exit 129 + exit "${1:-129}" } ################# SOURCING FUNCTIONS ################# @@ -360,13 +361,13 @@ is_array() { # array_contains ARRAY_TO_TEST <values...> array_contains() { if [ $# -lt 2 ]; then - writelog "${FUNCNAME[0]}: Expects at least 2 arguments, $# given." + #writelog "${FUNCNAME[0]}: Expects at least 2 arguments, $# given." return 1 fi # is $1 even defined? local ARRAY_DEF="$(declare -p $1 2>/dev/null)" if isempty ARRAY_DEF; then - writelog "${FUNCNAME[0]}: '$1' not defined!" + #writelog "${FUNCNAME[0]}: '$1' not defined!" return 1 fi local ARRAY_NAME="$1" @@ -374,7 +375,7 @@ array_contains() { # sanity check on $ARRAY_DEF being either indexed or associative array if ! is_array "${ARRAY_NAME}"; then - writelog "${FUNCNAME[0]}: '${ARRAY_NAME}' not an array! Declared as:\t${ARRAY_DEF}" + #writelog "${FUNCNAME[0]}: '${ARRAY_NAME}' not an array! Declared as:\t${ARRAY_DEF}" return 1 fi @@ -382,7 +383,7 @@ array_contains() { while [ $# -gt 0 ]; do # check if ARRAY_DEF contains '"<value>"' if [[ ! "${ARRAY_DEF}" =~ '="'${1}'"'[^\]]+ ]]; then - writelog "${FUNCNAME[0]}: '${1}' not in '${ARRAY_NAME}'" + #writelog "${FUNCNAME[0]}: '${1}' not in '${ARRAY_NAME}'" return 1 fi shift 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 1bf9de3e..c4c053af 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 @@ -18,18 +18,16 @@ # tools and then include the specific plugin which configures the speci- # fied virtualization tool. ################################################################################ - -# HACK: workaround if $0 is a symlink, exec its symlink target instead SELF=$(readlink -f "$0") -if [ -n "$SELF" ] && [ "$0" != "$SELF" ]; then - exec "$SELF" "$@" -fi -declare -rg RUN_VIRT_INCLUDE_DIR="$(dirname $SELF)/run-virt-includes" # 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 +declare -rg LOGFILE="$2" + # Functions needed by vmchooser-run_virt (writelog(), cleanexit(), safesource()) +declare -rg RUN_VIRT_INCLUDE_DIR="$(dirname $SELF)/run-virt-includes" 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 |