From b21ee6b876c851010bfe24c944c6a5034a6c0ba6 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Tue, 14 Feb 2017 17:58:28 +0100 Subject: [runvirt] introduced wrapper script to start ... ... runvirt with to hopefully always have either a user notification or a log entry on the satellites if anything goes wrong. Also fixed the MAC address generation: the prefix is now fully defined in the systemd-run_virt_env script and correctly written in the vmware's dhcp configuration file (so that VMs the default local IP by default) --- .../data/opt/openslx/scripts/systemd-run_virt_env | 26 ++++++++++---- .../data/opt/openslx/scripts/vmchooser-run_virt | 42 +++++++++++++++++++++- 2 files changed, 60 insertions(+), 8 deletions(-) mode change 120000 => 100755 core/modules/run-virt/data/opt/openslx/scripts/vmchooser-run_virt (limited to 'core/modules/run-virt/data/opt/openslx/scripts') 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.*\' "/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 deleted file mode 120000 index 447f98bc..00000000 --- a/core/modules/run-virt/data/opt/openslx/scripts/vmchooser-run_virt +++ /dev/null @@ -1 +0,0 @@ -/opt/openslx/vmchooser/vmchooser-run_virt \ No newline at end of file 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 100755 index 00000000..8f6da0bf --- /dev/null +++ b/core/modules/run-virt/data/opt/openslx/scripts/vmchooser-run_virt @@ -0,0 +1,41 @@ +#!/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 $@ + -- cgit v1.2.3-55-g7522