Gerir as máquinas virtuais

Eis os comandos necessários para compilar o VirtualBox:

cd /root
bat/vboxes stop
cd /usr/local/
VBoxManage extpack uninstall "Oracle VM VirtualBox Extension Pack"
reboot
exit
cd /usr/local/
chmod +x ./VirtualBox-X.X.XX-XXXXX-Linux_amd64.run
./VirtualBox-X.X.XX-XXXXX-Linux_amd64.run
VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-X.X.XX-XXXXX.vbox-extpack
cd -
bat/vboxes start

Nota : o ficheiro bat/vboxes é um ficheiro de comandos que criei para gerir as máquinas virtuais do VirtualBox. Já agora, este script serve para lançar as máquinas virtuais em modo headless e tem o conteúdo seguinte:

#!/bin/sh
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/sbin:/usr/bin:/sbin:/bin
SCRIPTNAME=/root/bat/vboxes
MANAGE_CMD=/root/bat/vboxcase "$1" in
start|stop|shutdown|restart|force-reload)
echo "-- $1 virtual boxes --"
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|shutdown|restart|force-reload}" >&2
exit 3
;;
esac

for i in `VBoxManage list vms | grep "^\"" | sed -e "s/{.*}//g" | sed -e "s/\"//g"`;
do
echo "$MANAGE_CMD $i $1"
$MANAGE_CMD $i $1
done

No script anterior faço referência a um outro script bat/vbox cujo conteúdo se segue e serve para gerir uma máquina virtual de cada vez (ver código original aqui)):

#!/bin/sh
### BEGIN INIT INFO
# Provides: vbox
# Required-Start:$local_fs $remote_fs vboxdrv vboxnet
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: MAQUINA virtual machine
# Description: MAQUINA virtual machine hosted by VirtualBox
### END INIT INFO# Author: Brendan Kidwell <brendan@glump.net>#
# Based on /etc/init.d/skeleton from Ubuntu 8.04. Updated for Ubuntu 9.10.
# If you are using Ubuntu <9.10, you might need to change "Default-Stop" # above to "S 0 1 6". # Do NOT "set -e" # O nome da máquina tem que vir como 1º parâmetro # O nome do comando tem que vir como 2º parâmetro MAQUINA="$1" COMANDO="$2" # PATH should only include /usr/* if it runs after the mountnfs.sh script PATH=/usr/sbin:/usr/bin:/sbin:/bin DESC="$MAQUINA virtual machine" NAME=vbox SCRIPTNAME=/root/bat/$NAME MANAGE_CMD=VBoxManage VM_OWNER=root VM_NAME="$MAQUINA" #This has to be the name exactly as it appears in your VirtualBox GUI control panel. # Read configuration variable file if it is present [ -r /etc/default/$NAME ] && . /etc/default/$NAME # Load the VERBOSE setting and other rcS variables [ -f /etc/default/rcS ] && . /etc/default/rcS # Define LSB log_* functions. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
#. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started

sudo -H -u $VM_OWNER $MANAGE_CMD showvminfo "$VM_NAME"|grep "^State:\s*running" >/dev/null && {
echo "$VM_NAME" is already running.
return 1
}

sudo -H -u $VM_OWNER $MANAGE_CMD startvm "$VM_NAME" -type headless >/dev/null || {
echo Failed to start "$VM_NAME".
return 2
}

echo "$VM_NAME" started or resumed.
return 0
}

#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred

sudo -H -u $VM_OWNER $MANAGE_CMD showvminfo "$VM_NAME"|grep "^State:\s*running" >/dev/null || {
echo "$VM_NAME" is already stopped.
return 1
}

sudo -H -u $VM_OWNER $MANAGE_CMD controlvm "$VM_NAME" savestate || {
echo Failed to stop "$VM_NAME".
return 2
}

echo "$VM_NAME" suspended.
return 0
}

#
#
# Function that powers off the daemon/service
#
do_shutdown()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred

sudo -H -u $VM_OWNER $MANAGE_CMD showvminfo "$VM_NAME"|grep "^State:\s*running" >/dev/null || {
echo "$VM_NAME" is already stopped.
return 1
}

sudo -H -u $VM_OWNER $MANAGE_CMD controlvm "$VM_NAME" acpipowerbutton || {
echo Failed to stop "$VM_NAME".
return 2
}

echo "$VM_NAME" powered off.
return 0
}

#
# Display "State" field from showinfo action
#
do_status()
{
sudo -H -u $VM_OWNER $MANAGE_CMD showvminfo "$VM_NAME"|grep "^State:\s*.*$"
}

case "$COMANDO" in
start)
[ "$VERBOSE" != no ] && logger "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && logger 0 ;;
2) [ "$VERBOSE" != no ] && logger 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && logger "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && logger 0 ;;
2) [ "$VERBOSE" != no ] && logger 1 ;;
esac
;;
shutdown)
[ "$VERBOSE" != no ] && logger "Powering off $DESC" "$NAME"
do_shutdown
case "$?" in
0|1) [ "$VERBOSE" != no ] && logger 0 ;;
2) [ "$VERBOSE" != no ] && logger 1 ;;
esac
;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
logger "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) logger 0 ;;
1) logger 1 ;; # Old process is still running
*) logger 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
logger 1
;;
esac
;;
status)
do_status
;;
*)
#echo "Usage: $SCRIPTNAME nome-maquina {start|stop|shutdown|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME nome-maquina {start|stop|shutdown|restart|force-reload|status}" >&2
exit 3
;;
esac

Usei outro script logo no arranque da máquina (host) para criar placas de rede virtuais para serem usadas pelas máquinas virtuais. São criadas como uma ponte (bridge) para uma placa física no host que só serve para rencaminhar o tráfego das máquinas virtuais para a rede e vice-versa. O seu conteúdo é o seguinte:

#!/bin/sh
# set PATH for the case we are called via sudo or su root
# PATH=/sbin:/usr/bin:/bin:/usr/binUSER=root

NUMBER_OF_VM=9
# create the bridge
brctl addbr br0

# create the taps and insert them into the bridge

NB=1
while [ $NB -le $NUMBER_OF_VM ]
do
tunctl -t tap$NB -u $USER
ip link set up dev tap$NB
brctl addif br0 tap$NB
let NB=$NB+1
done

ip route add 192.168.40.0/24 dev eth1

# set the IP address and routing
ip link set up dev br0
ip addr add 192.168.40.1/24 dev br0
##ip route add 192.168.40.0/24 dev br0

brctl addif br0 eth1

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *

Este site utiliza o Akismet para reduzir spam. Fica a saber como são processados os dados dos comentários.