From f210af6952dfdc2f57637f71c68dd7938e58e54a Mon Sep 17 00:00:00 2001 From: Roger Luethi Date: Tue, 26 Aug 2014 12:28:36 +0200 Subject: [PATCH] labs: add export via clone VM directory This changeset introduces a new export method: exporting the named node VMs as clones to the img directory. The -e option takes now an argument (ova or dir) to distinguish between exporting to an OVA package file and exporting to the img directory. Unlike the OVA package files, this method preserves snapshots. The resulting vbox files can be imported as VMs via VirtualBox GUI (Machine->Add). Change-Id: I7a8801de4b69796f3291a9e3a5896a055859121f --- labs/lib/osbash/virtualbox.functions | 27 +++++++++++++++++++++++++++ labs/osbash.sh | 21 +++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/labs/lib/osbash/virtualbox.functions b/labs/lib/osbash/virtualbox.functions index 2757c4e0..d27967c9 100644 --- a/labs/lib/osbash/virtualbox.functions +++ b/labs/lib/osbash/virtualbox.functions @@ -412,6 +412,33 @@ function vm_export_ova { done } +# Export node VMs by cloning VMs to directory +function vm_export_dir { + local export_dir=$1 + local nodes=$2 + + rm -rvf "$export_dir" + + for node in $nodes; do + if vm_is_running "$node"; then + echo "Powering off node VM $node." + echo "$VBM controlvm $node poweroff" + $VBM controlvm "$node" poweroff + fi + sleep 1 + vm_rm_share "$node" "$SHARE_NAME" + sleep 1 + echo "Exporting VM $node to $export_dir" + # Use all: machineandchildren works only if --snapshot is given as UUID + $VBM clonevm "$node" \ + --mode all \ + --options keepallmacs,keepdisknames \ + --name "$node" \ + --groups "/$VM_GROUP" \ + --basefolder "$export_dir" + done +} + #------------------------------------------------------------------------------- # VM unregister, remove, delete #------------------------------------------------------------------------------- diff --git a/labs/osbash.sh b/labs/osbash.sh index 36dd845e..9420a539 100755 --- a/labs/osbash.sh +++ b/labs/osbash.sh @@ -44,6 +44,8 @@ function print_config { if [ -n "${EXPORT_OVA:-}" ]; then echo "Exporting to OVA: ${EXPORT_OVA}" + elif [ -n "${EXPORT_VM_DIR:-}" ]; then + echo "Exporting to directory: ${EXPORT_VM_DIR}" else echo -n "Creating Windows batch scripts: " ${WBATCH:-:} echo "yes" @@ -58,10 +60,17 @@ function print_config { } -while getopts :efhnw opt; do +while getopts :e:fhnw opt; do case $opt in e) - EXPORT_OVA=$IMG_DIR/oslabs-$DISTRO.ova + if [ "$OPTARG" = ova ]; then + EXPORT_OVA=$IMG_DIR/oslabs-$DISTRO.ova + elif [ "$OPTARG" = dir ]; then + EXPORT_VM_DIR=$IMG_DIR/oslabs-$DISTRO + else + echo "Error: -e argument must be ova or dir" + exit + fi ;; f) source "$LIB_DIR/wbatch/batch_for_windows" @@ -77,6 +86,9 @@ while getopts :efhnw opt; do w) source "$LIB_DIR/wbatch/batch_for_windows" ;; + :) + echo "Error: -$OPTARG needs argument" + ;; ?) echo "Error: invalid option -$OPTARG" echo @@ -120,6 +132,11 @@ if [ -n "${EXPORT_OVA:-}" ]; then exit fi +if [ -n "${EXPORT_VM_DIR:-}" ]; then + vm_export_dir "$EXPORT_VM_DIR" "$nodes" + exit +fi + echo >&2 "$(date) osbash starting" clean_dir "$LOG_DIR"