Re: Multiple QEMU hosts networking

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Claudio Jeker
Date: Friday, October 5, 2007 - 2:31 am

On Fri, Oct 05, 2007 at 10:54:17AM +0200, Michael wrote:

I use this silly script plus a small C program to open up the the tun
devices and pass them to qemu (makes it possible for me to run qemu
without root privs).

The main trick is getmac() which generates "hopefully" unique mac
addresses per port.
-- 
:wq Claudio

#!/bin/sh
#
# stupid script to start multiple qemus on a single box

SUDO=/usr/bin/sudo
USER=cjeker

# qemu args
IMAGE=virt.hd
MEMORY=64
FLAGS="-snapshot -nographic"

NICFLAGS="-net nic,vlan=\$id,macaddr=\$mac -net tap,vlan=\$id,fd=\$fd"

usage() {
	echo "usage: $0 [-n] [-i image] [-f floppy.fs] instance" 1>&2
	exit 2
}

getmac() {
	mac="00:bd:`printf %02x $(($RANDOM % 256))`:"
	mac="$mac`printf %02x $(($RANDOM % 256))`:"
	mac="$mac`printf %02x $(($1 % 256))`:`printf %02x $(($2 % 255 + 1))`"
}

start() {
	for id in 0 1 2 3; do
		fd=$(($id + 3))
		tun=tun$(($1 * 10 + $id))
		getmac $1 id

		eval "nics=\"$nics $NICFLAGS\""
		fds="$fds fdpass -n $fd -f /dev/$tun"

		# make sure a tun interface is available
		ifconfig $tun > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			${SUDO} ifconfig $tun link0
		fi
	done

	${SUDO} $fds -u cjeker qemu -m ${MEMORY} ${FLAGS} $nics ${IMAGE}
}

args=`getopt f:i:n $*`
if [ $? -ne 0 ]; then
	usage
fi
set -- $args
while [ $# -gt 0 ]; do
	case "$1" in
	-f)	shift
		FLAGS="-fda $1 -boot a  -monitor stdio"
		;;
	-i)	shift
		IMAGE="$1"
		;;
	-n)	FLAGS="-nographic"
		echo "DISABLING SNAPSHOT MODE"
		;;
	--)	shift;
		break
		;;
	esac
	shift
done

if [ $# -ne 1 ]; then
	usage
fi

start $1
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Multiple QEMU hosts networking, Michael, (Fri Oct 5, 1:54 am)
Re: Multiple QEMU hosts networking, Claudio Jeker, (Fri Oct 5, 2:31 am)