#!/bin/bash # Create a virtual network using VirtualBox virtual machines # # Example usage: # vn-createtopology 1 # creates a virtual network with topology 1. The topology is defined by # the interface files in virtnet/data/topologies. See that directory # for the available topologies, e.g. 1, 2, 3, ... # # Design: # 1. For each node that has an interface file, create that node using # vn-newnode # # For more info see: http://sandilands.info/sgordon/virtnet # # $Revision$ # $Author$ # $Date$ # $URL$ # Input parameters # Topology number, e.g. 1, 2, 3, ... See virtnet/data/topologies for # available topologies topology=$1 # If there is no topology, return an error if [ -z ${topology} ]; then echo "Error. You must supply a topology number, e.g." echo " vn-createtopology 1" echo "For available topologies see http://sandilands.info/virtnet/topologies" echo "For further instructions see http://sandilands.info/sgordon/virtnet" exit fi # If topology number is 1 to 9, then add a 0, e.g. 00 to 09 if [ ${topology} -lt 10 -a `echo ${topology} | wc -c` -eq 2 ]; then topology="0${topology}" fi # Script parameters # Path for virtnet directory on host, e.g. /home/user/svn/virtnet/ virtnethostpath="`dirname $BASH_SOURCE`/../../" # Path for virtnet directory on guest, e.g. /home/network/virtnet virtnetpath="/home/network/virtnet" # Location of interface files used: local or base interfacelocation="local" # Set permissions on id_rsa file chmod u+rw,go-rwx ${virtnethostpath}/data/defaults/root/.ssh/id_rsa chmod u+rw,go-rwx ${virtnethostpath}/data/defaults/home/network/.ssh/id_rsa # For each node in the topology for iffile in `ls ${virtnethostpath}/data/topologies/interfaces-topology-${topology}-node-*`; do # Find the VM name (e.g. node1) and node id (e.g. 01) fn=`basename ${iffile}` nodeid=`echo ${fn} | cut -d "-" -f 5` # node1 instead of node01 if [ ${nodeid} -lt 10 ]; then nodeid=`echo ${nodeid} | cut -c 2` fi vmname="node${nodeid}" # Extract the VirtualBox network name from interface file # See data/topologies/README for required format ifoption=" " tmpfile=`mktemp tmp.XXXXX` grep "# VBoxNetwork" ${iffile} | cut -d ":" -f 2 | tr -d ' ' > ${tmpfile} for n in `cat ${tmpfile}`; do ifoption="${ifoption} --iface ${n}" done rm ${tmpfile} # Call vn-newnode to create a new virtual machine # Can use the local copy of topology, rather than that on base if [ "${interfacelocation}" = "local" ]; then bash vn-newnode --node ${nodeid} --file ${virtnethostpath}/data/topologies/`basename ${iffile}` ${ifoption} nodecreation=$? else bash vn-newnode --node ${nodeid} --file ${virtnetpath}/data/topologies/`basename ${iffile}` ${ifoption} nodecreation=$? fi # Check the status of node creation if [ "${nodecreation}" = "1" ]; then echo "vn: Creation of new VM node${nodeid} failed." echo "vn: View the messages above to find the reason. Commonly" echo "vn: it is due nodes with the name node${nodeid} already " echo "vn: existing in VirtualBox. Try renaming them (or removing" echo "vn: them if not needed) and then create the topology again." echo "vn: ERROR. Exiting." exit 1 fi # Start VM # VBoxManage startvm --type headless ${vmname} done