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"