summaryrefslogtreecommitdiffstats
path: root/core/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt
blob: 52ded2484777e65a67c96ea84e629142bac5c3a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
# Full bash required
# -----------------------------------------------------------------------------
#
# Copyright (c) 2007..2018 bwLehrpool-Projektteam
#
# This program/file is free software distributed under the GPL version 2.
# See https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
#
# If you have any feedback please consult https://bwlehrpool.de and
# send your feedback to bwlehrpool@hs-offenburg.de.
#
# General information about bwLehrpool can be found at https://bwlehrpool.de
#
# -----------------------------------------------------------------------------
# run-virt.sh
#    - This is the generic wrapper for the several virtualization solutions.
#      The idea is to setup a set of variables used by at least two different
#      tools and then include the specific plugin which configures the speci-
#      fied virtualization tool.
################################################################################
SELF=$(readlink -f "$0")

# 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 -g 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
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.

# 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")


# 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")

# 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")

# 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 ####################"

# 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!
		fi
	else
		writelog "\tFeature '$FEAT' has no handler! This function will be unavailable."
		notify_user "Feature '$FEAT' nicht unterstützt"
	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}

writelog "Virtualizer exited with '$?'. Bye."

cleanexit 0