Skip to content

Instantly share code, notes, and snippets.

@tetafro
Last active September 8, 2016 10:44
Show Gist options
  • Save tetafro/242cc8389746b1165b0fd5373874fc65 to your computer and use it in GitHub Desktop.
Save tetafro/242cc8389746b1165b0fd5373874fc65 to your computer and use it in GitHub Desktop.
Shortcuts for managing VirtualBox machines

Description

Shortcuts for managing VirtualBox machines

Setup

  1. Make vm.sh globally visible as vm (rename and add it's directory to $PATH).
  2. Add source /full/path/to/autocomplete.sh to ~/.bashrc.
  3. Relogin.
_autocomplete() {
_commands='run stop ssh list'
_vm_states='running stopped all'
local cur prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ $prev == 'vm' ]]; then
COMPREPLY=( $(compgen -W "${_commands}" -- ${cur}) )
elif [[ $prev == 'list' ]]; then
COMPREPLY=( $(compgen -W "${_vm_states}" -- ${cur}) )
elif [[ $prev == 'run' ]]; then
_vms=$(vm list stopped | tr '\n' ' ')
COMPREPLY=( $(compgen -W "${_vms}" -- ${cur}) )
elif [[ $prev == 'stop' || $prev == 'ssh' ]]; then
_vms=$(vm list running | tr '\n' ' ')
COMPREPLY=( $(compgen -W "${_vms}" -- ${cur}) )
fi
return 0
}
complete -F _autocomplete vm
#!/bin/bash
# Shortcuts for managing VirtualBox machines
function error {
echo $1
echo 'Usage:'
echo ' vm [run|stop|ssh|list] [vm_name]'
exit 1
}
if [[ $# -gt 2 || $# -lt 1 ]]
then
error 'Wrong number of arguments'
fi
command=$1
target=$2
case $command-$target in
# One-line list of running VMs
list-running)
VBoxManage list runningvms | cut -f 1 -d ' ' | tr -d '"' | tr '[:upper:]' '[:lower:]'
;;
# One-line list of VMs that are not currently running
list-stopped)
run_vms=($(VBoxManage list runningvms | cut -f 1 -d ' ' | tr -d '"' | tr '[:upper:]' '[:lower:]' | tr '\n' ' '))
all_vms=($(VBoxManage list vms | cut -f 1 -d ' ' | tr -d '"' | tr '[:upper:]' '[:lower:]' | tr '\n' ' '))
for vm in "${all_vms[@]}"; do
is_running=false
for run_vm in "${run_vms[@]}"; do
if [[ $run_vm == $vm ]]; then
is_running=true
break
fi
done
if [[ !is_running ]]; then
echo "$vm "
fi
done
;;
# User-friendly output of all VMs and running VMs
list-all|list-)
# Remove IDs and quotes, make lowercase, add spaces before output
all_vms=$(VBoxManage list vms | cut -f 1 -d ' ' | tr -d '"' | tr '[:upper:]' '[:lower:]')
run_vms=$(VBoxManage list runningvms | cut -f 1 -d ' ' | tr -d '"' | tr '[:upper:]' '[:lower:]' | sed 's/^/ /')
echo 'List of available machines:'
if [[ $all_vms != '' ]]; then
echo $all_vms | sed 's/ /\n/g' | sed 's/^/ /'
else
echo '-- empty --'
fi
echo 'List of currently running machines:'
if [[ $run_vms != '' ]]; then
for vm in "${run_vms[@]}"; do
echo $run_vms | sed 's/ /\n/g' | sed 's/^/ /'
done
else
echo ' -- empty --'
fi
;;
list-*)
error 'Unknown command'
;;
run-*)
name=${target^} # capitalize first letter
VBoxHeadless -s $name &
;;
stop-*)
name=${target^} # capitalize first letter
VBoxManage controlvm $name acpipowerbutton
;;
ssh-*)
ssh user@$target.local
;;
*)
error 'Unknown command'
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment