Skip to content

Instantly share code, notes, and snippets.

@jferrao
Last active August 29, 2015 14:03
Show Gist options
  • Save jferrao/915c872f6238f80adb86 to your computer and use it in GitHub Desktop.
Save jferrao/915c872f6238f80adb86 to your computer and use it in GitHub Desktop.
Linux and MacOS X alias script to VirtualBox commands
#!/bin/bash
## Linux and MacOS X alias script for VirtualBox console commands
## to start, stop, query status and list Virtual Machines
##
## Usage: vm [vm_name] <command> <argv>...
##
## VM commands:
## start Starts the VM
## stop Stops a running VM using ACPI shutdown
## off Powers off a running VM
## status Shows the VM status
## list Lists all VMs and status
## listr Lists all running VMs
##
## Arguments:
## -h Help and usage
##
# Starts an existing VM
# $1 string The VM name
start_vm() {
VBoxManage startvm $1 --type headless
}
# Stops a running VM using ACPI shutdown
# $1 string The VM name
stop_vm() {
status=$(VBoxManage list runningvms | grep "\"$1\"" | wc -l | tr -d ' ')
if [[ $status = '0' ]]; then
echo "VM \"$1\" is not running"
else
echo "Stopping VM \"$1\" ..."
VBoxManage controlvm $1 acpipowerbutton
fi
}
# Powers off a running VM
# $1 string The VM name
off_vm() {
status=$(VBoxManage list runningvms | grep "\"$1\"" | wc -l | tr -d ' ')
if [[ $status = '0' ]]; then
echo "VM \"$1\" is not running"
else
echo "Stopping VM \"$1\" ..."
VBoxManage controlvm $1 poweroff
fi
}
# Restarts an existing VM
# $1 string The VM name
restart_vm() {
echo "Stopping VM \"$1\" ..."
VBoxManage controlvm $1 reset
}
# Shows a VM status, either runnning or stoped
# $1 string The VM name
status_vm() {
status=$(VBoxManage list runningvms | grep "\"$1\"" | wc -l | tr -d ' ')
if [[ $status = '0' ]]; then
echo -e "VM \"$1\" is \033[31mstopped\033[0m"
else
echo -e "VM \"$1\" is \033[32mrunning\033[0m"
fi
}
# Lists all VMs
list_vms() {
array=$(VBoxManage list vms | sed -e "s/^.*\"\(.*\)\".*$/\"\1\"/" | tr -d '"')
for item in ${array}; do
status=$(VBoxManage list runningvms | grep "\"$item\"" | wc -l | tr -d ' ')
if [[ $status = '0' ]]; then
echo -e "[\033[31mStopped\033[0m] \"$item\""
else
echo -e "[\033[32mRunning\033[0m] \"$item\""
fi
done
}
# List all running VMs
list_running_vms() {
running_vms=$(VBoxManage list runningvms | wc -l | tr -d ' ')
if [[ $running_vms = '0' ]]; then
echo "No VMs running"
else
echo "Running VMs ($running_vms):"
VBoxManage list runningvms | sed -e "s/^.*\"\(.*\)\".*$/\"\1\"/"
fi
}
# Displays the docbloc at the biginning of this script as
# the help message using some creative instrospection with grep
show_help() {
help=$(grep "^##" "${BASH_SOURCE[0]}" | cut -c 4-)
echo "$help"
}
if [ $# -gt 1 ]; then
case $2 in
start)
start_vm $1
;;
stop)
stop_vm $1
;;
off)
off_vm $1
;;
status)
status_vm $1
;;
restart)
restart_vm $1
;;
*)
show_help
;;
esac
else
case $1 in
-h)
show_help
;;
list)
list_vms
;;
listr)
list_running_vms
;;
*)
show_help
;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment