QEMU Launch Script

From Internetworkpro

Jump to: navigation, search

To quickly start and stop QEMU olive images I have wrote the following script. It is incredibly simple and something should probably be written to replace it with better error checking.


This is the main script. It sources a file called route_conf will all the configuration and startup options in it

#!/bin/bash

source router_conf

function start_router {	
	echo "Starting router $1"
	run_$1 || echo "ERROR: No start configuration found for $1"
}

function stop_router {
	echo "Stoping router $1"
	stop_$1 || echo "ERROR: No stop configuration found for $1"
}

function start_all_routers {
	for router in `echo $ROUTERS`; do
		start_router $router
    done
}

function stop_all_routers {
    for router in `echo $ROUTERS`; do
		stop_router $router
	done
}

function killpidfile {
	if [ -e $1 ]; then
		kill `cat $1`
		rm $1
	else 
		echo "Pidfile $1 not found"
	fi
}

function print_help {
	echo "$0 [start|stop|restart] [<router>|all]"
}

case "$1"  in
	'start')
		if [ "$2" == "all" ]; then
			start_all_routers	
		else 
			start_router $2
		fi	
		;;
	'stop')
		if [ "$2" == "all" ]; then
			stop_all_routers
		else
			stop_router $2
		fi
		;;
	'restart')
		if ["$2" == "all" ]; then
			stop_all_routers
			start_all_routers
		else
			stop_router $2
			start_router $2
		fi
		;;
	*)
		print_help
		;;
esac

Here is a sample configuration file


#
# Custom varibles here
#
PIDDIR=$HOME/lab/test/working/pid

#
# List of routers (must exist!)
#
ROUTERS="P PE1 PE2 PE3 PE4 CEB1"

#
# P
# 
function run_P {
	nice -19 \
	qemu \
	P.img \
	-pidfile $PIDDIR/olive-P.pid \
	-m 96 -nographic -daemonize -localtime \
	-serial telnet::2001,server,nowait \
	-net nic,vlan=1,macaddr=00:aa:00:60:00:00,model=pemu_i82559 -net udp,vlan=1,sport=6000,dport=6100,daddr=127.0.0.1 \
	-net nic,vlan=2,macaddr=00:aa:00:60:01:00,model=pemu_i82559 -net udp,vlan=2,sport=6001,dport=6101,daddr=127.0.0.1 \
	-net nic,vlan=3,macaddr=00:aa:00:60:02:00,model=pemu_i82559 -net udp,vlan=3,sport=6002,dport=6102,daddr=127.0.0.1 \
	-net nic,vlan=4,macaddr=00:aa:00:60:03:00,model=pemu_i82559 -net udp,vlan=4,sport=6003,dport=6103,daddr=127.0.0.1 \
	&
}

function stop_P {
	killpidfile $PIDDIR/olive-P.pid
}

#
# PE1
#
function run_PE1 {
 	nice -19 \
	qemu \
	PE1.img \
	-pidfile $PIDDIR/olive-PE1.pid \
	-m 96 -nographic -daemonize -localtime \
	-serial telnet::2002,server,nowait \
	-net nic,vlan=1,macaddr=00:aa:00:61:00:01,model=pemu_i82559 -net udp,vlan=1,sport=6100,dport=6000,daddr=127.0.0.1 \
	-net nic,vlan=2,macaddr=00:aa:00:60:04:01,model=pemu_i82559 -net udp,vlan=2,sport=6004,dport=6104,daddr=127.0.0.1 \
	-net nic,vlan=3,macaddr=00:aa:00:60:05:01,model=pemu_i82559 -net udp,vlan=3,sport=6005,dport=6105,daddr=127.0.0.1 \
	 &
}

function stop_PE1 {
	killpidfile $PIDDIR/olive-PE1.pid
}

#
# PE2
#
function run_PE2 {
	nice -19 \
	qemu \
	PE2.img \
	-pidfile $PIDDIR/olive-PE2.pid \
	-m 96 -nographic -daemonize -localtime \
	-serial telnet::2003,server,nowait \
	-net nic,vlan=1,macaddr=00:aa:00:61:01:02,model=pemu_i82559 -net udp,vlan=1,sport=6101,dport=6001,daddr=127.0.0.1 \
	-net nic,vlan=2,macaddr=00:aa:00:61:04:02,model=pemu_i82559 -net udp,vlan=2,sport=6104,dport=6004,daddr=127.0.0.1 \
	-net nic,vlan=3,macaddr=00:aa:00:60:06:02,model=pemu_i82559 -net udp,vlan=3,sport=6006,dport=6106,daddr=127.0.0.1 \
	-net nic,vlan=3,macaddr=00:aa:00:60:07:02,model=pemu_i82559 -net udp,vlan=3,sport=6007,dport=6107,daddr=127.0.0.1 \
 	&
}

function stop_PE2 {
	killpidfile $PIDDIR/olive-PE2.pid
}

#
# PE3
#
function run_PE3 {
	nice -19 \
	qemu \
	PE3.img \
	pidfile $PIDDIR/olive-PE3.pid \
	-m 96 -nographic -daemonize -localtime \
	-serial telnet::2004,server,nowait \
	-net nic,vlan=1,macaddr=00:aa:00:61:02:03,model=pemu_i82559 -net udp,vlan=1,sport=6102,dport=6002,daddr=127.0.0.1 \
	-net nic,vlan=2,macaddr=00:aa:00:60:08:03,model=pemu_i82559 -net udp,vlan=2,sport=6008,dport=6108,daddr=127.0.0.1 \
	&
}

function stop_PE3 {
	killpidfile $PIDDIR/olive-PE3.pid
}

#
# PE4
# 
function run_PE4 {
	nice -19 \
	qemu \
	PE4.img \
	-pidfile $PIDDIR/olive-PE4.pid \
	-m 96 -nographic -daemonize -localtime \
	-serial telnet::2005,server,nowait \
	-net nic,vlan=1,macaddr=00:aa:00:61:03:04,model=pemu_i82559 -net udp,vlan=1,sport=6103,dport=6003,daddr=127.0.0.1 \
	-net nic,vlan=2,macaddr=00:aa:00:60:00:04,model=pemu_i82559 -net udp,vlan=2,sport=6009,dport=6109,daddr=127.0.0.1 \
	-net nic,vlan=3,macaddr=00:aa:00:60:10:04,model=pemu_i82559 -net udp,vlan=3,sport=6010,dport=6110,daddr=127.0.0.1 \
	&
}

function stop_PE4 {
	killpidfile $PIDDIR/olive-PE4.pid
}


#
# CEB1
#
function run_CEB1 {
	nice -19 \
	qemu \
	CEB1.img \
	-pidfile $PIDDIR/olive-CEB1.pid \
	-m 96 -nographic -daemonize -localtime \
	-serial telnet::2009,server,nowait \
	-net nic,vlan=1,macaddr=00:aa:00:61:07:11,model=pemu_i82559 -net udp,vlan=1,sport=6107,dport=6007,daddr=127.0.0.1 \
	&
}

function stop_CEB1 {
	killpidfile $PIDDIR/olive-CEB1.pid
}
Personal tools