Skip to content

Instantly share code, notes, and snippets.

@charego
Created January 5, 2018 15:37
Show Gist options
  • Save charego/cf61e1bee862adea38c352ad418fe523 to your computer and use it in GitHub Desktop.
Save charego/cf61e1bee862adea38c352ad418fe523 to your computer and use it in GitHub Desktop.
VirtualBox VM helper
#!/bin/bash
VM_NAME='ubuntu-64'
vmStart() {
# & (the first one) detaches the command from stdin.
# >/dev/null detaches the shell session from stdout and stderr.
# &disown removes the command from the shell's job list.
VBoxHeadless --startvm "${VM_NAME}" &>/dev/null &disown
echo "VirtualBox VM started: ${VM_NAME}"
}
vmStop() {
VBoxManage controlvm "${VM_NAME}" savestate
}
vmStatus() {
local RUNNING_VMS=$(VBoxManage list runningvms)
if [[ $? != 0 ]]; then
echo "Command failed."
elif [[ $RUNNING_VMS ]]; then
echo "Running VirtualBox VMs:"
echo $RUNNING_VMS
else
echo "No VirtualBox VMs are running."
fi
}
vmConnect() {
ssh -p 2222 cgould@127.0.0.1
}
case "$1" in
start)
vmStart;;
stop)
vmStop;;
status)
vmStatus;;
connect)
vmConnect;;
*)
echo $"Usage: $0 {start|stop|status|connect}"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment