Skip to content

Instantly share code, notes, and snippets.

@ffigiel
Last active December 28, 2017 16:54
Show Gist options
  • Save ffigiel/30ba9204bc516c454abf to your computer and use it in GitHub Desktop.
Save ffigiel/30ba9204bc516c454abf to your computer and use it in GitHub Desktop.
Simple script for managing a virtualbox machine
#!/usr/bin/env bash
VM_NAME='ubuntu'
VBoxManage () {
'/cygdrive/c/Program Files/Oracle/VirtualBox/VBoxManage.exe' $@
}
IsVmRunning () {
VBoxManage list runningvms | grep $1 > /dev/null
return $?
}
VmOn () {
if ! IsVmRunning $1; then
VBoxManage startvm $1 --type headless
fi
}
case "$1" in
'on')
VmOn ubuntu
;;
'off')
VBoxManage controlvm $VM_NAME acpipowerbutton
;;
'restart')
VBoxManage controlvm $VM_NAME reset
;;
'status')
if IsVmRunning $VM_NAME; then
echo 'VM is running'
else
echo 'VM is off'
fi
;;
'ssh')
if ! IsVmRunning $VM_NAME; then
VmOn $VM_NAME
echo "Waiting for VM to boot..."
sleep 5
fi
ssh $VM_NAME -o ConnectTimeout=60
;;
*)
echo "Usage: ubuntu (on | off | restart | status | ssh)"
exit 2
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment