#!/bin/sh # Start/stop qemu's private network # Revised: 12/16/2009 JJO ### BEGIN INIT INFO # Provides: qemunet # Required-Start: $network # Required-Stop: $network # Default-Start: 3 5 # Default-Stop: 0 1 2 6 # Description: Start the qemu private network ### END INIT INFO # start qemu with: -net nic -net tap,ifname=tap0,script=no # You can then assign any VM any static IP on the $ETHIP network ETHIP=10.10.10.1 GATEWAY= ETHBC=10.10.10.255 BRIDGE=br0 ETH=dummy0 TAP=tap0 # A little SuSE sanity check [ -f /etc/SuSE-release ] && /sbin/rmmod dummy0 >/dev/null 2>&1 # Start the qemu private network qemunet_start() { # Make sure the qemu private network isnt already running PROBLEM=FALSE for IF in $ETH $TAP $BRIDGE; do /sbin/ifconfig | grep $IF >/dev/null 2>&1 [ $? = 0 ] && PROBLEM=TRUE done if [ $PROBLEM = TRUE ]; then echo "All or part of the qemu private network is already running!" echo "Cowardly refusing to start it again! BYE!" exit 1 fi echo "Starting the qemu private network..." # Make sure the kernel module is loaded /sbin/lsmod | grep dummy >/dev/null 2>&1 [ $? = 1 ] && /sbin/modprobe dummy # First take interface down, then bring it up with IP 0.0.0.0 /sbin/ifconfig $ETH down /sbin/ifconfig $ETH 0.0.0.0 promisc up # Bring up the tap device (name specified as first argument, by QEMU) /usr/sbin/openvpn --mktun --dev $TAP /sbin/ifconfig $TAP 0.0.0.0 promisc up # create the bridge between eth0 and the tap device /sbin/brctl addbr $BRIDGE /sbin/brctl addif $BRIDGE $ETH /sbin/brctl addif $BRIDGE $TAP # only a single bridge so loops are not possible, turn off spanning tree protocol /sbin/brctl stp $BRIDGE off # Bring up the bridge with ETHIP and add the default route /sbin/ifconfig br0 $ETHIP netmask 255.255.255.0 broadcast $ETHBC [ -n "$GATEWAY" ] && /sbin/route add default gw $GATEWAY } # Stop the qemu private network qemunet_stop() { echo "Stopping the qemu private network..." # Bring down interface and br0 /sbin/ifconfig $ETH down /sbin/ifconfig $BRIDGE down /sbin/rmmod dummy # Delete the bridge /sbin/brctl delbr $BRIDGE # delete the tap device /usr/sbin/openvpn --rmtun --dev $TAP } # Restart the qemu private network qemunet_restart() { qemunet_stop sleep 1 qemunet_start } # Check if the qemu private network is up and running qemunet_status() { echo -n "Checking the qemu private network: " # Check the qemu private inetwork for IF in $ETH $TAP $BRIDGE; do /sbin/ifconfig | grep $IF >/dev/null 2>&1 if [ $? = 1 ]; then echo -n "$IF is down" else echo -n "$IF is up" fi if [ $IF = $BRIDGE ]; then echo "." else echo -n ", " fi done } case "$1" in 'start') qemunet_start ;; 'stop') qemunet_stop ;; 'restart') qemunet_restart ;; 'status') qemunet_status ;; *) echo "usage $0 start|stop|restart|status" esac