From 8ca3139a4420a191c4a6a414bb577dabc136ae14 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Tue, 6 Feb 2018 15:30:22 +0100 Subject: [vbox-src] wrappers for xmlstarlet and use them when creating the final machine configuration --- .../virtualbox/includes/create_vbox_config.inc | 94 ++++++++++-------- .../includes/finalize_machine_config.inc | 62 ++++++++---- .../plugins/virtualbox/includes/init_core.inc | 105 ++++++++++++++++++++- 3 files changed, 202 insertions(+), 59 deletions(-) (limited to 'core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes') diff --git a/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/create_vbox_config.inc b/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/create_vbox_config.inc index 2825da5d..bc0d299d 100755 --- a/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/create_vbox_config.inc +++ b/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/create_vbox_config.inc @@ -2,44 +2,62 @@ # Include: create main vbox config file and add machine, hdd, ... entries # ################################################################################ create_vbox_config() { - cat <<- EOF > "${VBOX_ROOT}/VirtualBox.xml" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - EOF + # TODO gen_uuid() + # generated machine and floppy uuids + # generate random machine/floppies UUIDs using kernel's generator + # or some defaults if it failed/does not exists + if [ -r "/proc/sys/kernel/random/uuid" ]; then + declare -rg MACHINE_UUID="$(cat /proc/sys/kernel/random/uuid)" + declare -rg SLX_FLOPPY_UUID="$(cat /proc/sys/kernel/random/uuid)" + else + # use VM's MAC address, if set (length: 6 * 2 + 5 * ':') + if isset VM_MAC_ADDR && [ ${#VM_MAC_ADDR} -eq 17 ]; then + local SUFFIX="${VM_MAC_ADDR//:/}" + declare -rg MACHINE_UUID="00000000-0000-${SUFFIX:0:4}-${SUFFIX:4:12}" + declare -rg SLX_FLOPPY_UUID="00000000-ffff-${SUFFIX:0:4}-${SUFFIX:4:12}" + else + declare -rg MACHINE_UUID="00000000-0000-0000-12345678" + declare -rg SLX_FLOPPY_UUID="00000000-0000-ffff-12345678" + fi + fi + + cat < "${VBOX_ROOT}/VirtualBox.xml" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +EOF } call_post_source create_vbox_config diff --git a/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/finalize_machine_config.inc b/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/finalize_machine_config.inc index c5b062f9..91286a41 100755 --- a/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/finalize_machine_config.inc +++ b/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/finalize_machine_config.inc @@ -31,25 +31,53 @@ finalize_machine_config() { sed -i 's,#OpenSLX_CPU_place_holder,'"${CPU_CORES}"',g' $TMPCONFIG sed -i 's,#OpenSLX_MEMORY_place_holder,'"${VM_MEM}"',g' $TMPCONFIG - # Add a HardDisk node - xmlstarlet ed -L -N x="http://www.virtualbox.org/" -s "//x:VirtualBox/x:Machine/x:MediaRegistry/x:HardDisks/x:HardDisk" -t elem -n HardDisk $TMPCONFIG - # Add attributes: - # 1) uuid attribute and point to the snapshotuuid from above - # 2) location pointing to the snapshot file - # 3) specify that format is VDI - # 4) hdd type is "normal" - xmlstarlet ed -L -N x="http://www.virtualbox.org/" \ - -i "//x:VirtualBox/x:Machine/x:MediaRegistry/x:HardDisks/x:HardDisk/x:HardDisk" -t attr -n uuid -v "{${SNAPSHOT_UUID}}" \ - -i "//x:VirtualBox/x:Machine/x:MediaRegistry/x:HardDisks/x:HardDisk/x:HardDisk" -t attr -n location -v "$VBOX_SNAPSHOT_DIR/{${SNAPSHOT_UUID}}.vdi" \ - -i "//x:VirtualBox/x:Machine/x:MediaRegistry/x:HardDisks/x:HardDisk/x:HardDisk" -t attr -n format -v "VDI" \ - -i "//x:VirtualBox/x:Machine/x:MediaRegistry/x:HardDisks/x:HardDisk/x:HardDisk" -t attr -n type -v "Normal" \ - $TMPCONFIG + # Add a HardDisk node for the snapshot + add_node \ + "/VirtualBox/Machine/MediaRegistry/HardDisks/HardDisk" "HardDisk" \ + "uuid={${SNAPSHOT_UUID}}" \ + "location=$VBOX_SNAPSHOT_DIR/{${SNAPSHOT_UUID}}.vdi" \ + "format=VDI" \ + "type=Normal" + # add storage controller and 2 floppies to it + add_node \ + "/VirtualBox/Machine/StorageControllers" "StorageController" \ + "name=Floppy" \ + "type=I82078" \ + "PortCount=1" \ + "useHostIOCache=true" + add_node \ + '/VirtualBox/Machine/StorageControllers/StorageController[@name="Floppy"]' "AttachedDevice" \ + "type=Floppy" \ + "hotpluggable=false" \ + "port=0" \ + "device=0" + add_node \ + '/VirtualBox/Machine/StorageControllers/StorageController[@name="Floppy"]' "AttachedDevice" \ + "type=Floppy" \ + "hotpluggable=false" \ + "port=0" \ + "device=1" + + # add the slx floppy to the second drive + add_node \ + '/VirtualBox/Machine/StorageControllers/StorageController/AttachedDevice[@device="1"]' "Image" \ + "uuid={${SLX_FLOPPY_UUID}}" + + # set the MAC address - xmlstarlet ed -L -N x="http://www.virtualbox.org/" -u "//x:VirtualBox/x:Machine/x:Hardware/x:Network/x:Adapter/@MACAddress" -v "$VM_VDE_MAC_ADDR" $TMPCONFIG - # delete USB controller - TODO fix - xmlstarlet ed -L -N x="http://www.virtualbox.org/" -d "//x:VirtualBox/x:Machine/x:Hardware/x:USB" $TMPCONFIG + edit_attr "/VirtualBox/Machine/Hardware/Network/Adapter" "MACAddress" "${VM_VDE_MAC_ADDR}" + + # check if KVM is available and activate it if so + if source /run/hwinfo && [ "${HW_KVM}" = "ENABLED" ]; then + edit_attr --create "/VirtualBox/Machine/Hardware/Paravirt" "provider" "KVM" + fi + # activate IOAPIC needed for multi core (?) + if [ $CPU_CORES -gt 1 ]; then + edit_attr --create "/VirtualBox/Machine/Hardware/BIOS/IOAPIC" "enabled" "true" + fi - # TODO VT-x + # TODO fix usb generally, just delete it if present for now .... + del_node "/VirtualBox/Machine/Hardware/USB" cp $TMPCONFIG /tmp/vbox-last-config cp $TMPCONFIG $VBOX_MACHINE_CONFIG diff --git a/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/init_core.inc b/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/init_core.inc index ad3cbe96..8a12dcda 100755 --- a/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/init_core.inc +++ b/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/init_core.inc @@ -1,6 +1,105 @@ ####################################################################### # Include: Declaration of core variables and some sanity checks # ####################################################################### + +# wrapper for xmlstarlet to add node with attributes and value +# to the machine configuration +# Usage: +# add_node +# E.g: +# add_node "/VirtualBox" "Machine" "uuid=1234" +add_node() { + if [ $# -lt 2 ]; then + writelog "${FUNCNAME[0]} requires 2 or more args, $# given." + cleanexit 1 + fi + local PARENT="$1" + shift + local NODE="$1" + shift + # if parent does not exists, create it aswell + if ! node_exists "${PARENT}"; then + add_node "${PARENT%/*}" "${PARENT##*/}" + fi + # now go over the list of attributes and directly create + # the xmlstarlet syntax string: -t attr -n -v + local ATTRS_OPTS= + while [ $# -ne 0 ]; do + # expects key=value syntax + local key="${1%=*}" + local value="${1#*=}" + shift + if isempty key || isempty value; then + writelog "${FUNCNAME[0]} expecting key=value, got: $1" + cleanexit 1 + fi + # the xmlstarlet internal newnode var references the newly created node + ATTRS_OPTS+=" -i \$newnode -t attr -n ${key} -v ${value}" + done + # args parsed, now create the node using xmlstarlet + # insert namespace to xpath expression + PARENT="$(print_namespaced "x" "${PARENT}")" + # create node and set the internal variable newnode to it + # to then add the attributes to it + xmlstarlet ed -L -N x="${VBOX_NAMESPACE}" \ + -s "${PARENT}" -t elem -n "${NODE}" \ + --var newnode '$prev' \ + ${ATTRS_OPTS} \ + "${TMPCONFIG}" +} +# edit_attr [--create] +# --create would create the node if it does not exist +edit_attr() { + local CREATE= + if [ "$1" = "--create" ]; then + CREATE=yo + shift + fi + if [ $# -ne 3 ]; then + writelog "${FUNCNAME[0]} requires 3 args, $# given." + cleanexit 1 + fi + if notempty CREATE && ! node_exists "$1"; then + add_node "${1%/*}" "${1##*/}" \ + "$2=$3" + else + xmlstarlet ed -L -N x="${VBOX_NAMESPACE}" \ + -u "$(print_namespaced "x" "$1")/@$2" \ + -v "$3" \ + "${TMPCONFIG}" + fi +} +del_node() { + if [ $# -ne 1]; then + writelog "${FUNCNAME[0]} requires one arg, $# given." + cleanexit 1 + fi + xmlstarlet ed -L -N x="${VBOX_NAMESPACE}" \ + -d "$(print_namespaced "x" "$1")" \ + "${TMPCONFIG}" +} +node_exists() { + if [ $# -ne 1 ]; then + writelog "${FUNCNAME[0]} requires one arg, $# given." + cleanexit 1 + fi + xmlstarlet -q sel -N x="${VBOX_NAMESPACE}" \ + -t -c "$(print_namespaced "x" "$1")" \ + "${TMPCONFIG}" +} +# adds the vbox namespace to an xpath expression using +# the given placeholder for it. +# e.g. print_namespaced "x" "/VirtualBox/Machine" +# would echo "/x:VirtualBox/x:Machine" +print_namespaced() { + if [ $# -ne 2 ]; then + writelog "${FUNCNAME[0]} expects 2 arguments, $# given." + cleanexit 1 + fi + # add namespace on single '/' not doubles! + sed -E 's,(/)*/,\1/'"$1"':,g' <<< "$2" +} + init_core() { # check for variables that should have been set by the generic run-virt if ! isset VM_CLEANNAME IMG_BASENAME SRC_IMG_ABSOLUTE VM_OS_TYPE; then @@ -38,10 +137,8 @@ init_core() { declare -rg VBOX_HDD_LINK="${VM_DISKFILE_RO}" export VBOX_USER_HOME="${VBOX_ROOT}" # instead of $HOME/.VirtualBox - # finally generate a random machine UUID, using some default if it fails. - declare -g MACHINE_UUID="$(cat /proc/sys/kernel/random/uuid)" - notempty MACHINE_UUID || MACHINE_UUID="00000000-0000-0000-0000-12345678" - readonly MACHINE_UUID + # xml namespace for vbox configs + declare -rg VBOX_NAMESPACE="http://www.virtualbox.org/" writelog "Directories:" writelog "\tConfig dir:\t\t$VBOX_ROOT" -- cgit v1.2.3-55-g7522