| 1 | #!/usr/bin/env bash
|
|---|
| 2 | # vim: set noexpandtab :
|
|---|
| 3 |
|
|---|
| 4 | VM=Windows7_32bit
|
|---|
| 5 | SNAP='Snapshot for '`basename $0`
|
|---|
| 6 | TMPF=/tmp/VBTest_Tempfile
|
|---|
| 7 | CPIOFILE=/tmp/VBTest_Saved_Files.cpio
|
|---|
| 8 |
|
|---|
| 9 | set -u #die if undef shell variable is referenced
|
|---|
| 10 | set -m #enable job control, so bg commands have their own pgrps
|
|---|
| 11 |
|
|---|
| 12 | memk=`sed -n -e 's/MemTotal: *\([0-9]*\) kB.*/\1/p' /proc/meminfo`
|
|---|
| 13 | [ -n "$memk" ] || { echo "Could not determine mem size" >&2; exit 1; }
|
|---|
| 14 |
|
|---|
| 15 | numcpu=`grep '^processor[ ]*:' /proc/cpuinfo | wc -l`
|
|---|
| 16 | [ -n "$numcpu" ] || { echo "Could not determine num cpus" >&2; exit 1; }
|
|---|
| 17 |
|
|---|
| 18 | declare -a BGPids
|
|---|
| 19 | stop_bg_jobs() {
|
|---|
| 20 | set +m # avoid status messages when bg jobs are killed
|
|---|
| 21 | if [ ${#BGPids[@]} -gt 0 ] ; then
|
|---|
| 22 | for pgrp in "${BGPids[@]}" ; do (set -x; kill -15 -$pgrp) ; done
|
|---|
| 23 | while kill -0 "${BGPids[@]}" 2>/dev/null ; do
|
|---|
| 24 | echo "bg jobs did not die..." ; sleep 1
|
|---|
| 25 | done
|
|---|
| 26 | fi
|
|---|
| 27 | if [ -e $TMPF ] ; then (set -x; rm -f $TMPF) ; fi
|
|---|
| 28 | }
|
|---|
| 29 | for sig in 1 2 3 15 ; do
|
|---|
| 30 | trap "echo Caught sig $sig; stop_bg_jobs; trap EXIT; exit 99" $sig
|
|---|
| 31 | done
|
|---|
| 32 | trap 'e=$?
|
|---|
| 33 | echo "=== exit code was $e ==="
|
|---|
| 34 | stop_bg_jobs
|
|---|
| 35 | trap EXIT; set -x; exit $?
|
|---|
| 36 | ' EXIT
|
|---|
| 37 |
|
|---|
| 38 | (set -x; VBoxManage snapshot $VM take "$SNAP") || exit $?
|
|---|
| 39 |
|
|---|
| 40 | echo "Backing up configuration files..."
|
|---|
| 41 | (set -x; cd $HOME; find .VirtualBox -name '*.xml' -print | cpio -ov >$CPIOFILE)
|
|---|
| 42 |
|
|---|
| 43 | ## COMMENT THESE OUT WHILE DEBUGGING
|
|---|
| 44 | echo "Start background jobs to cause interference..."
|
|---|
| 45 | # Occupy all CPUs and purturb scheduling
|
|---|
| 46 | for (( i=0 ; i<$numcpu ; i=$i+1 )) ; do
|
|---|
| 47 | (echo "Cpu-sucker ($BASHPID) started..."
|
|---|
| 48 | while : ; do : ; done
|
|---|
| 49 | ) &
|
|---|
| 50 | { set +u; BGPids=("${BGPids[@]}" $!) ; }
|
|---|
| 51 | done
|
|---|
| 52 | # Create pressure on memory by repeatedly writing a large file
|
|---|
| 53 | (echo "Big-file-writer ($BASHPID) started..."
|
|---|
| 54 | while dd if=/dev/zero of=$TMPF bs=1500 count=$memk >/dev/null; do
|
|---|
| 55 | #ls -lh $TMPF
|
|---|
| 56 | sleep 1
|
|---|
| 57 | done
|
|---|
| 58 | ) &
|
|---|
| 59 | { set +u; BGPids=("${BGPids[@]}" $!) ; }
|
|---|
| 60 |
|
|---|
| 61 | trap 'echo "Caught keyboard interrupt..."
|
|---|
| 62 | set +e
|
|---|
| 63 | stop_bg_jobs
|
|---|
| 64 | set -x
|
|---|
| 65 | VBoxManage controlvm $VM poweroff
|
|---|
| 66 | VBoxManage snapshot $VM restore "$SNAP" && VBoxManage snapshot $VM delete "$SNAP"
|
|---|
| 67 | exit 33
|
|---|
| 68 | ' INT
|
|---|
| 69 |
|
|---|
| 70 | for ((count=1; 1; count=$count+1)) ; do
|
|---|
| 71 | (set -e # exit on error
|
|---|
| 72 | echo "--- iteration $count ---"
|
|---|
| 73 | (set -x; VBoxManage startvm $VM)
|
|---|
| 74 | #(set -x; VBoxManage startvm $VM) &
|
|---|
| 75 | #startpid=$!
|
|---|
| 76 | #set +e
|
|---|
| 77 | #echo ">>>The following will probably fail because VM is still starting..." >&2
|
|---|
| 78 | #(set -x; VBoxManage controlvm $VM poweroff)
|
|---|
| 79 | #echo ">>>The following will probably fail because VM is still starting..." >&2
|
|---|
| 80 | #(set -x; VBoxManage snapshot $VM restore "$SNAP")
|
|---|
| 81 | #set -e
|
|---|
| 82 | #wait $startpid
|
|---|
| 83 | #echo ">>>These should work:" >&2
|
|---|
| 84 | (set -x; VBoxManage controlvm $VM poweroff)
|
|---|
| 85 | (set -x; VBoxManage snapshot $VM restore "$SNAP")
|
|---|
| 86 | ) || exit $?
|
|---|
| 87 | done
|
|---|