1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#!/bin/sh
usage() {
cat 1>&2 <<EOL
usage: $0 [ -w ] [ -a ] <vm>
stops the given VM, restores the current snapshot and restarts it again
OPTIONS
-w attach host's webcam to VM
-a if no <vm> is given, try to automatically guess it
(using the currently running one, if this is unambiguous)
EOL
if [ "x${1}" != "x" ]; then
cat 1>&2 <<EOL
here's a list of running VMs:
EOL
vboxmanage list runningvms
echo "" 1>&2
fi
}
webcam=no
auto_vm=no
while getopts "h?wa" opt; do
case $opt in
w)
webcam=yes
;;
a)
auto_vm=yes
;;
h|\?)
usage
exit 0
;;
:)
usage
exit 0
;;
esac
done
shift $((OPTIND-1))
vm=$1
if test "x${auto_vm}" = "xyes" && test "x${vm}" = "x"; then
vm=$(vboxmanage list runningvms | grep -c .)
if [ "${vm}" -eq 1 ]; then
vboxmanage list runningvms
vm=$(vboxmanage list runningvms | awk '{print $2}' | tail -1)
else
cat 1>&2 <<EOL
unable to unambiguously determine VM
EOL
usage vms
exit 1
fi
fi
if [ "x${vm}" = "x" ]; then
usage 1
exit 1
fi
restore() {
vboxmanage controlvm "${vm}" poweroff \
&& vboxmanage snapshot "${vm}" restorecurrent
}
restart() {
# don't know why, but if we start the VM immediately after restoring the
# current snapshot, it fails...
sleep 2
vboxmanage startvm "${vm}"
}
configure() {
if [ "x${webcam}" = "xyes" ]; then
vboxmanage controlvm "${vm}" webcam attach /dev/video0
fi
}
restore && restart && configure
|