Scripts to use in Headless Mode
Posted: 16. Sep 2007, 03:17
Since I have 5 servers with only 1 monitor and doing alot of work with the CLI. I decided to create some scripts. I thought I would share these in the community, as to be honest. Im very new to bash. Perphaps others working with CLI and headless mode could take my work and further it.
I've put these scripts in /root/.VirtualBox/Scripts and created symlinks to /usr/bin
vboxiso.sh - a script to control the dvd/cd image, even whilst the guest is on.
vboxdelete.sh - a script to completely remove a guest from the system!
These set of scripts, are to allow me to dynamically create TAP devices and assign them to the relevant bridge. It works on an 1:1 basis for interface to brige! Also, since I want to RDP into the guest immediately, it takes the VRDP IP and assigns it to the first TAP interface.
Unlike in the manual, I dont assign any network interfaces to the host interfaces created. vboxup.sh does this for me. However, I still use cleanuptap.sh as directed for the interfaces that I have. As when the VM shutsdown, its able to delete the TAP devices.
Unfortunately, the script doesnt take into account that a guest could crash and then you need to use the existing TAP devices. Or if you run the startup script again, it will create new TAP devices. I will fix this later.
vboxup.sh - Create the TAP devices per network interface
vboxstart.sh - script to start a guest
vboxstop.sh - script to stop a guest
cleanuptap.sh - remove TAP interfaces assigned to each network interface (taken from the manual)
I've put these scripts in /root/.VirtualBox/Scripts and created symlinks to /usr/bin
vboxiso.sh - a script to control the dvd/cd image, even whilst the guest is on.
Code: Select all
#!/bin/sh
#ensure that the server name is entered
if [ -z "$1" ] ; then
echo "Please enter the server you wish to start"
echo "usage cmd <server>"
exit 1
fi
if [ "$2" == "none" ] ; then
echo Unmounting DVD/CD-ROM
elif [ -z "$2" ] ; then
echo "Please enter the iso image you wish to mount"
exit 1
elif [ -e $2 ] ; then
donothing=0
else
echo "$2 doesnt seem to exist, please check the path or filename"
exit 1
fi
VBoxManage controlvm $1 dvdattach $2
Code: Select all
#!/bin/sh
#integer for number of drives
num=1
# ensure that the server name is entered
if [ -z "$1" ] ; then
echo "Please enter the server you wish to start"
echo "usage cmd <server>"
exit 1
fi
# loop through available hard drives
for vint in `grep hardDisk /root/.VirtualBox/Machines/"$1"/"$1".xml | cut -d\" -f6`; do
len=${#vint}
len=$(($len-2))
uuid=`expr substr $vint 2 $len`
path=`VBoxManage showvdiinfo $uuid`
files=`echo $path | cut -d" " -f44`
#remove hd's from vm guest
case $num in
1 )
echo --- Unmounting hda ---
VBoxManage modifyvm $1 -hda none
echo --- Unregistering drive ---
VBoxManage unregisterimage disk $uuid
;;
2 )
echo --- Unmounting hdd ---
VBoxManage modifyvm $1 -hdd none
echo --- Unregistering drive ---
VBoxManage unregisterimage disk $uuid
;;
esac
rm -rf $files
echo --- Deleted $files ---
num=$(($num+1))
done
rm -rf "/root/.VirtualBox/Machines/"$1
VBoxManage unregistervm $1
echo --- Deleted VM Guest: $1 ---
Unlike in the manual, I dont assign any network interfaces to the host interfaces created. vboxup.sh does this for me. However, I still use cleanuptap.sh as directed for the interfaces that I have. As when the VM shutsdown, its able to delete the TAP devices.
Unfortunately, the script doesnt take into account that a guest could crash and then you need to use the existing TAP devices. Or if you run the startup script again, it will create new TAP devices. I will fix this later.
vboxup.sh - Create the TAP devices per network interface
Code: Select all
#!/bin/sh
# Create variable for Tap interfaces
tap=0
# create array to store vbox interfaces
declare -a vbr
# create variables for checking bridges
array=0
flag=0
# check for how many bridged interfaces are present
for i in `service network status`; do
#check using regular expression:
if [[ "$i" =~ "br[0..9]*" ]] ; then
# array is empty, add in card
if [[ "$array" == 0 ]] ; then
vbr=( ${vbr[@]} $i )
array=$(($array+1))
else
# check array for duplications
for a in ${vbr[@]}; do
if [ $i != $a ] ; then
# set flag when dup found
flag=$(($flag+1))
fi
done
# flag is up, add it to the array
if [ "$flag" == 1 ] ; then
vbr=( ${vbr[@]} $i )
fi
fi
fi
done
#No bridges configured, terminate script
if [ "$array" == 0 ] ; then
echo You have no bridges configured!
exit 1
fi
# Create Tap interfaces per network interfaces
for vint in `grep HostInterface /root/.VirtualBox/Machines/"$1"/"$1".xml | cut -d\" -f2`; do
#Create the Tap Interface
interface=`VBoxTunctl -b -u root`
if [ -z "$interface" ]; then
echo couldnt setup a TAP Device
exit 1
fi
if [ "$tap" == 0 ]; then
# get the netmask from br0
ifcfg=`ifconfig br0`
netmask=`echo $ifcfg | cut -d":" -f10`
mask=`echo $netmask | cut -d" " -f1`
# set an IP to the vbox interface
# get the server.xml to get the RDP address
rdpip=`grep RemoteDisplay /root/.VirtualBox/Machines/"$1"/"$1".xml | cut -d\" -f6`
/sbin/ifconfig $interface $rdpip netmask $mask up
tap=$(($tap+1))
fi
/sbin/ifconfig $interface up
/usr/sbin/brctl addif ${vbr[$vint]} $interface
# modify the guest vm to assign the host interface to that
# virtual interface
VBoxManage modifyvm $1 "-hostifdev"${#vint[@]} $interface
done
exit 0
Code: Select all
#!/bin/sh
#ensure that the server name is entered
if [ -z $1 ] ; then
echo "Please enter the server you wish to start"
echo "usage cmd <server>"
exit 1
fi
showvminfo=`VBoxManage showvminfo $1`
showvminfoline=`echo $showvminfo | cut -d":" -f13`
status=`echo $showvminfoline | cut -d" " -f1`
if [[ "$status" = "running" ]] ; then
echo VM Guest $1 is already powered on!
exit 1
else
VBoxUp $1
VBoxVRDP -startvm "$1" &
fi
Code: Select all
#!/bin/sh
#ensure that the server name is entered
if [ -z $1 ] ; then
echo "Please enter the server you wish to stop"
echo "usage cmd <server>"
exit 1
fi
showvminfo=`VBoxManage showvminfo $1`
showvminfoline=`echo $showvminfo | cut -d":" -f13`
power=`echo $showvminfoline | cut -d" " -f1`
status=`echo $showvminfoline | cut -d" " -f2`
vm="$power $status"
if [[ "$vm" = "powered off" ]] ; then
echo VM Guest $1 is already powered off!
exit 1
else
VBoxManage controlvm "$1" poweroff
#VBoxDown "$1"
fi
Code: Select all
#!/bin/bash
VBoxTunctl -d $2