diff options
author | Jonathan Bauer | 2019-10-08 10:55:15 +0200 |
---|---|---|
committer | Jonathan Bauer | 2019-10-08 10:55:15 +0200 |
commit | 4847e73eb6c6a2c4de5a59d3c2f0cf50bcc5f94c (patch) | |
tree | 0f74d3c47c89beb348a487fbf215178a35aeca7a /core | |
parent | kernel.config: Make sure early microcode loading is enabled (diff) | |
download | mltk-4847e73eb6c6a2c4de5a59d3c2f0cf50bcc5f94c.tar.gz mltk-4847e73eb6c6a2c4de5a59d3c2f0cf50bcc5f94c.tar.xz mltk-4847e73eb6c6a2c4de5a59d3c2f0cf50bcc5f94c.zip |
[vbox-src] rework RO/RW disk image setup
Diffstat (limited to 'core')
-rwxr-xr-x | core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/create_vbox_config.inc | 8 | ||||
-rwxr-xr-x | core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/init_core.inc | 1 | ||||
-rwxr-xr-x | core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/prepare_snapshot.inc | 125 | ||||
-rwxr-xr-x | core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/run-virt.include | 6 | ||||
-rw-r--r-- | core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/snapshot.vdi.gz (renamed from core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/empty-diff.vdi.gz) | bin | 285 -> 285 bytes |
5 files changed, 98 insertions, 42 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 e432472f..75ec5ec3 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 @@ -7,7 +7,7 @@ create_vbox_config() { declare -rg SLX_FLOPPY_UUID="$(gen_uuid)" cat <<EOF > "${VBOX_ROOT}/VirtualBox.xml" -<VirtualBox xmlns="http://www.innotek.de/VirtualBox-settings" version="1.12-linux"> +<VirtualBox xmlns="http://www.virtualbox.org/" version="1.12-linux"> <Global> <ExtraData> <ExtraDataItem name="GUI/Input/AutoCapture" value="true"/> @@ -26,11 +26,7 @@ create_vbox_config() { <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> + <HardDisks/> <FloppyImages> <Image uuid="{${SLX_FLOPPY_UUID}}" location="${SLX_FLOPPY_IMG}"/> </FloppyImages> 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 2ff95bbf..9f451cf9 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 @@ -228,7 +228,6 @@ Ihren Satelliten scheint VirtualBox nicht zu unterstützen." cleanexit 1 writelog "Failed to create '${VBOX_HDD_DIR}'." cleanexit 1 fi - declare -rg VBOX_HDD_LINK="${VM_DISKFILE_RO}" # Virtualbox configuration dir instead of $HOME/.VirtualBox export VBOX_USER_HOME="${VBOX_ROOT}" diff --git a/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/prepare_snapshot.inc b/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/prepare_snapshot.inc index dcbff969..39a20bcd 100755 --- a/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/prepare_snapshot.inc +++ b/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/prepare_snapshot.inc @@ -1,43 +1,104 @@ +# Extracts the uuid of the given vdi. +# For VBox VDI Version 1.1, the 16-byte uuid is at offset 392 +# See: https://forums.virtualbox.org/viewtopic.php?p=29267#p29267 +extract_vdi_uuid() { + [ -z "$1" ] && return + od -An -t x1 -j 392 -N 16 "$1" | \ + awk '{print $4$3$2$1"-"$6$5"-"$8$7"-"$9$10"-"$11$12$13$14$15$16}' +} + +# Cleanup hook when the session stops delete_snapshot() { rm -f "${VBOX_SNAPSHOT_DIR}/{${SNAPSHOT_UUID}}.vdi" || \ writelog "Could not cleanup snapshot file: ${VBOX_SNAPSHOT_DIR}/{${SNAPSHOT_UUID}}.vdi" } -prepare_snapshot() { - declare -rg SNAPSHOT_UUID="34f617be-192a-46b3-a8ae-bce1029e093f" # snapshot UUID is static - - # get UUID of disk image using od to get 16 bytes (starting at 392) and - # order them according to the VDI UUID format with awk - HDD_UUID="$(od -An -t x1 -j 392 -N 16 ${VBOX_HDD_LINK} | awk '{print $4$3$2$1"-"$6$5"-"$8$7"-"$9$10"-"$11$12$13$14$15$16}')" +# TODO support snapshot for RW mode that are uploaded/on an NFS. +prepare_disk_image_rw() { + # more of a placeholder function at this point :) + declare -rg VBOX_HDD_UUID="$HDD_UUID" +} + +prepare_disk_image_ro() { + # extract the uuid of the snapshot... + local snapshot_path="${VBOX_PLUGIN_DIR}/snapshot.vdi" + local snapshot_tmp="${VBOX_SNAPSHOT_DIR}/${snapshot_path##*/}" + if [ -f "${snapshot_path}.gz" ]; then + gunzip -c "${snapshot_path}.gz" > "$snapshot_tmp" + elif [ -f "$snapshot_path" ]; then + cp -f -- "$snapshot_path" "$snapshot_tmp" + else + writelog "Could not find pre-generated snapshot file '$snapshot_path(|.gz)'." + # TODO fallback on creating a snapshot for this vbox image + # Note that this needs to be done after VBoxManage is aware of this VM... + fi + if [ -f "$snapshot_tmp" ]; then + declare -rg SNAPSHOT_UUID="$(extract_vdi_uuid "$snapshot_tmp")" + fi + + # use temp disk as snapshot to get CoW + declare -rg VBOX_HDD_UUID="${SNAPSHOT_UUID}" + + cp -f -- "$snapshot_tmp" "${VBOX_SNAPSHOT_DIR}/{${SNAPSHOT_UUID}}.vdi" + + # TODO Use VBoxManage to determine the UUID of the disk image (making sure + # it is a VDI image) and patch it into the prepared snapshot VDI. + dd of="${VBOX_SNAPSHOT_DIR}/{${SNAPSHOT_UUID}}.vdi" \ + seek=424 bs=1 count=16 conv=notrunc <<< "$HDD_UUID" || \ + writelog "Could not patch snapshot file" + + # TODO handle other formats... + # finally "register" the snapshot in the MediaRegistry + xmlstarlet ed -L -N x="http://www.virtualbox.org/" \ + -s "//x:VirtualBox/x:Global/x:MediaRegistry/x:HardDisks/x:HardDisk[@uuid='{${HDD_UUID}}']" \ + -t elem -n "HardDisk" --var newnode '$prev' \ + -i '$newnode' -t attr -n "uuid" -v "{${SNAPSHOT_UUID}}" \ + -i '$newnode' -t attr -n "location" -v "${VBOX_SNAPSHOT_DIR}/{${SNAPSHOT_UUID}}.vdi" \ + -i '$newnode' -t attr -n "format" -v "VDI" \ + -i '$newnode' -t attr -n "autoReset" -v "true" \ + "${VBOX_ROOT}/VirtualBox.xml" + + add_cleanup delete_snapshot +} + +# Prepares the disk image specified as VM_DISKFILE_R[OW] +# by run-virt's generic setup-image-access.inc +# * RO -> use a CoW snapshot using VBox internals +# * RW -> use it as is +prepare_disk_image() { + + # NOTE: either one is garanteed to be set by setup_image_access.inc + local handler + if [ -n "$VM_DISKFILE_RO" ]; then + declare -rg VBOX_HDD_LINK="$VM_DISKFILE_RO" + declare -rg VBOX_HDD_TYPE="Immutable" + handler="prepare_disk_image_ro" + elif [ -n "$VM_DISKFILE_RW" ]; then + declare -rg VBOX_HDD_LINK="$VM_DISKFILE_RW" + declare -rg VBOX_HDD_TYPE="Normal" + handler="prepare_disk_image_rw" + fi + + # TODO Determine HDD storage format using VBoxManage + declare -rg VBOX_HDD_FORMAT="VDI" + declare -rg HDD_UUID="$(extract_vdi_uuid ${VBOX_HDD_LINK})" if isempty HDD_UUID; then writelog "Failed to extract VDI UUID of '${VBOX_HDD_LINK}'" EXIT_TYPE="internal" EXIT_REASON="Konnte UUID der virtuellen Festplatte nicht extrahieren." cleanexit 1 fi - VBOX_HDD_FORMAT="VDI" - - if isset VM_DISKFILE_RO; then - # use temp disk as snapshot to get CoW - VBOX_HDD_TYPE="Immutable" # make disk immutable - VBOX_HDD_UUID=${SNAPSHOT_UUID} - < "${VBOX_PLUGIN_DIR}/empty-diff.vdi.gz" gunzip > "${VBOX_SNAPSHOT_DIR}/{${SNAPSHOT_UUID}}.vdi" - # patch the disk file uuid into the snapshot vdi file: - # We read from offset 424 in the source HDD and write the 16 bytes long UUID - # starting at offset 392 in the snapshot. - # TODO: This requires the image to be a vdi/vdh; Afaik, vbox supports vmdk, - # so this code would break in that case. Can we use the diskuuid we got via - # VBoxManage, convert to binary, and patch it - dd if=${VBOX_HDD_LINK} of="${VBOX_SNAPSHOT_DIR}/{${SNAPSHOT_UUID}}.vdi" seek=424 \ - skip=392 bs=1 count=16 conv=notrunc || \ - writelog "Could not patch snapshot file" - add_cleanup delete_snapshot - elif isset VM_DISKFILE_RW; then - # Image is already RW - easy - VBOX_HDD_TYPE="Normal" - VBOX_HDD_UUID=${HDD_UUID} - else - writelog "Neither VM_DISKFILE_RO nor VM_DISKFILE_RW defined!" - cleanexit 1 - fi + # add the hard disk element to the "global" VirtualBox.xml + xmlstarlet ed -L -N x="http://www.virtualbox.org/" \ + -s "//x:VirtualBox/x:Global/x:MediaRegistry/x:HardDisks" \ + -t elem -n "HardDisk" --var newnode '$prev' \ + -i '$newnode' -t attr -n "uuid" -v "{${HDD_UUID}}" \ + -i '$newnode' -t attr -n "location" -v "$VBOX_HDD_LINK" \ + -i '$newnode' -t attr -n "format" -v "$VBOX_HDD_FORMAT" \ + -i '$newnode' -t attr -n "type" -v "$VBOX_HDD_TYPE" \ + "${VBOX_ROOT}/VirtualBox.xml" + + # now handle either RW or RO images + isset handler && "$handler" + return 0 } -call_post_source prepare_snapshot +call_post_source prepare_disk_image diff --git a/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/run-virt.include b/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/run-virt.include index bca5c250..3ad81df5 100755 --- a/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/run-virt.include +++ b/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/run-virt.include @@ -28,12 +28,12 @@ run_plugin() { # determine hardware limits based on guest OD $(safesource "${VBOX_INCLUDE_DIR}/guest_hardware_limits.inc") - # prepare volatile rw layer for the disk image using a snapshot - $(safesource "${VBOX_INCLUDE_DIR}/prepare_snapshot.inc") - # create main virtualbox configuration $(safesource "${VBOX_INCLUDE_DIR}/create_vbox_config.inc") + # prepare volatile rw layer for the disk image using a snapshot + $(safesource "${VBOX_INCLUDE_DIR}/prepare_snapshot.inc") + # finalize machine configuration $(safesource "${VBOX_INCLUDE_DIR}/finalize_machine_config.inc") diff --git a/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/empty-diff.vdi.gz b/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/snapshot.vdi.gz Binary files differindex c6abbd57..c6abbd57 100644 --- a/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/empty-diff.vdi.gz +++ b/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/snapshot.vdi.gz |