diff options
author | Jonathan Bauer | 2018-02-06 15:30:22 +0100 |
---|---|---|
committer | Jonathan Bauer | 2018-02-06 15:30:22 +0100 |
commit | 8ca3139a4420a191c4a6a414bb577dabc136ae14 (patch) | |
tree | c0675f1d6017ad2f1a199e0b2c5727f869fbba90 /core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes | |
parent | [vbox-src] addon-init to start vbox.service (diff) | |
download | mltk-8ca3139a4420a191c4a6a414bb577dabc136ae14.tar.gz mltk-8ca3139a4420a191c4a6a414bb577dabc136ae14.tar.xz mltk-8ca3139a4420a191c4a6a414bb577dabc136ae14.zip |
[vbox-src] wrappers for xmlstarlet
and use them when creating the final machine configuration
Diffstat (limited to 'core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes')
3 files changed, 202 insertions, 59 deletions
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" - <VirtualBox xmlns="http://www.innotek.de/VirtualBox-settings" version="1.12-linux"> - <Global> - <ExtraData> - <ExtraDataItem name="GUI/Input/AutoCapture" value="true"/> - <ExtraDataItem name="GUI/Input/HostKey" value="65300"/> <!-- scroll lock / Rollen --> - <ExtraDataItem name="GUI/LastVMSelected" value="${MACHINE_UUID}"/> - <ExtraDataItem name="GUI/HostScreenSaverDisabled" value="false"/> - <ExtraDataItem name="GUI/LicenseAgreed" value="1,2,3,4,5,6,7,8,9"/> - <ExtraDataItem name="GUI/MaxGuestResolution" value="any"/> <!-- auto, width,height --> - <ExtraDataItem name="GUI/UpdateCheckCount" value="2"/> - <ExtraDataItem name="GUI/UpdateDate" value="never"/> - <ExtraDataItem name="GUI/SuppressMessages" value=",remindAboutAutoCapture,confirmInputCapture,remindAboutWrongColorDepth,confirmGoingFullscreen,remindAboutMouseIntegrationOn,remindAboutMouseIntegrationOff,showRuntimeError.warning.DevATA_DISKFULL,remindAboutPausedVMInput,confirmVMReset,showRuntimeError.warning.HostAudioNotResponding,showRuntimeError.warning.ExtPackNoEhci,remindAboutMouseIntegration"/> - <ExtraDataItem name="GUI/TrayIcon/Enabled" value="false"/> - </ExtraData> - <MachineRegistry> - <MachineEntry uuid="{${MACHINE_UUID}}" src="Machines/${VM_CLEANNAME}/${VM_CLEANNAME}.xml"/> - </MachineRegistry> - <MediaRegistry> - <HardDisks> - <HardDisk uuid="{${HDD_UUID}}" location="${VBOX_HDD_LINK}" format="${VBOX_HDD_FORMAT}" type="${VBOX_HDD_TYPE}"> - <HardDisk uuid="{${SNAPSHOT_UUID}}" location="${VBOX_SNAPSHOT_DIR}/{${SNAPSHOT_UUID}}.vdi" format="VDI" autoReset="true"/> - </HardDisk> - </HardDisks> - <FloppyImages> - <Image uuid="{288d5452-2dd3-44f2-bfc8-78e205a4fa87}" location="${SLX_FLOPPY_IMG}"/> - </FloppyImages> - </MediaRegistry> - <NetserviceRegistry> - <DHCPServers> - <DHCPServer networkName="HostInterfaceNetworking-vboxnet0" IPAddress="0.0.0.0" networkMask="0.0.0.0" lowerIP="0.0.0.0" upperIP="0.0.0.0" enabled="0"/> - </DHCPServers> - </NetserviceRegistry> - <SystemProperties defaultMachineFolder="${VBOX_MACHINES_DIR}" defaultHardDiskFolder="${VBOX_HDD_DIR}" defaultHardDiskFormat="VDI" remoteDisplayAuthLibrary="VBoxAuth" webServiceAuthLibrary="VBoxAuth" LogHistoryCount="3"/> - <USBDeviceFilters/> - </Global> - </VirtualBox> - 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 <<EOF > "${VBOX_ROOT}/VirtualBox.xml" +<VirtualBox xmlns="http://www.innotek.de/VirtualBox-settings" version="1.12-linux"> + <Global> + <ExtraData> + <ExtraDataItem name="GUI/Input/AutoCapture" value="true"/> + <!-- Host Key: left ctrl + left winkey + left alt --> + <ExtraDataItem name="GUI/Input/HostKeyCombination" value="65507,65513,65515"/> + <ExtraDataItem name="GUI/LastVMSelected" value="${MACHINE_UUID}"/> + <ExtraDataItem name="GUI/HostScreenSaverDisabled" value="false"/> + <ExtraDataItem name="GUI/LicenseAgreed" value="1,2,3,4,5,6,7,8,9"/> + <ExtraDataItem name="GUI/MaxGuestResolution" value="any"/> <!-- auto, width,height --> + <ExtraDataItem name="GUI/UpdateCheckCount" value="2"/> + <ExtraDataItem name="GUI/UpdateDate" value="never"/> + <ExtraDataItem name="GUI/SuppressMessages" value=",remindAboutAutoCapture,confirmInputCapture,remindAboutWrongColorDepth,confirmGoingFullscreen,remindAboutMouseIntegrationOn,remindAboutMouseIntegrationOff,showRuntimeError.warning.DevATA_DISKFULL,remindAboutPausedVMInput,confirmVMReset,showRuntimeError.warning.HostAudioNotResponding,showRuntimeError.warning.ExtPackNoEhci,remindAboutMouseIntegration"/> + <ExtraDataItem name="GUI/TrayIcon/Enabled" value="false"/> + </ExtraData> + <MachineRegistry> + <MachineEntry uuid="{${MACHINE_UUID}}" src="Machines/${VM_CLEANNAME}/${VM_CLEANNAME}.xml"/> + </MachineRegistry> + <MediaRegistry> + <HardDisks> + <HardDisk uuid="{${HDD_UUID}}" location="${VBOX_HDD_LINK}" format="${VBOX_HDD_FORMAT}" type="${VBOX_HDD_TYPE}"> + <HardDisk uuid="{${SNAPSHOT_UUID}}" location="${VBOX_SNAPSHOT_DIR}/{${SNAPSHOT_UUID}}.vdi" format="VDI" autoReset="true"/> + </HardDisk> + </HardDisks> + <FloppyImages> + <Image uuid="{${SLX_FLOPPY_UUID}}" location="${SLX_FLOPPY_IMG}"/> + </FloppyImages> + </MediaRegistry> + <NetserviceRegistry> + <DHCPServers/> + </NetserviceRegistry> + <SystemProperties defaultMachineFolder="${VBOX_MACHINES_DIR}" defaultHardDiskFolder="${VBOX_HDD_DIR}" defaultHardDiskFormat="VDI" remoteDisplayAuthLibrary="VBoxAuth" webServiceAuthLibrary="VBoxAuth" LogHistoryCount="3"/> + <USBDeviceFilters/> + </Global> +</VirtualBox> +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 <parent> <newnode> <attrs...> +# 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 <key> -v <value> + 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] <node_xpath> <attr=value> +# --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" |