Skip to content

Instantly share code, notes, and snippets.

@rkmax
Created January 31, 2012 15:34
Show Gist options
  • Save rkmax/1711103 to your computer and use it in GitHub Desktop.
Save rkmax/1711103 to your computer and use it in GitHub Desktop.
Admin VM on VirtualBOx
#!/bin/bash
VM_CMD=VBoxManage
VM_NAME=$2
usage() {
C_NAME=`basename $0`
cat << EOF
VMadmin Utility
===============
Envoltorio del comando "$VM_CMD" para la administracion de de maquinas
virtuales instaladas con VirtualBox.
Modo de uso:
$C_NAME [-h user@host] -m nombre-maquina-virtual start|stop
Inicia o detiene en modo headless una VM "nombre-maquina-virtual",
si la opcion "-h" es definida lo realiza en una maquina remota
$C_NAME [-h user@host] list
Muestra una lista de las maquinas virtuales creada en el host, si la
opcion "-h" es definida se muestran las maquinas instaladas en la
maquina remota
NOTA: la maquina remota debe proveer acceso SSH y debe tener instaladas
la clave publica en la maquina remota, para mas informacion consulte
"man ssh-copy-id".
NOTA2: la maquina debe tener VirtualBox instalado.
EOF
}
machine_error() {
if [[ -z $MACHINE ]]; then
echo -e "\n ** ERROR: La opcion "-m" es obligatoria **"
usage; exit 1
fi
}
accion_error() {
echo -e "\n ** ERROR: ejecute una accion start|stop|list **"
usage; exit 1
}
# Check if VBox NOT exists
if ! which $VM_CMD >/dev/null; then
cat << EOF
** ERROR: Necesita tener instalado virtualbox **
EOF
exit 1
fi
# Check if 1 params
if [[ $# -eq 0 ]]; then
usage
exit
fi
HOST=
MACHINE=
while getopts m:h: OPTION; do
case $OPTION in
m ) MACHINE=$OPTARG ;;
h ) HOST=$OPTARG ;;
? ) usage; exit ;;
esac
done
shift $(($OPTIND -1))
# Check if exist one action
if [[ $# -ne 1 ]]; then
accion_error
fi
# Organiza la instruccion
EXEC_CMD=
case $1 in
"start" )
machine_error
EXEC_CMD="$VM_CMD startvm $VM_NAME --type headless"
;;
"stop" )
machine_error
EXEC_CMD="$VM_CMD controlvm $VM_NAME poweroff"
;;
"list" )
EXEC_CMD="$VM_CMD list vms"
;;
*) accion_error ;;
esac
# Check if is remote or not
if [[ -z $HOST ]]; then
$EXEC_CMD
else
ssh $HOST "$EXEC_CMD"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment