[ARMedslack] qemu-network-tun.sh modified script

John O'Donnell unixjohn1969 at gmail.com
Fri Apr 29 08:23:31 UTC 2011


On 04/29/2011 04:06 AM, Davide wrote:
> If anyone else finds this handy this works with dhcp client on both host and guest and also gets rid of all rc.local requirements.
> On the guest os you will need to config interface once system is up (static or via dhcp)
> Needs fixing for static ip reconfiguration/rerouting on host system after bridge creation.
>
> I apologize for my non standard indentation ...

Also if anyone cares, I wrote a script like that over a year ago to lanuch a 
private network for many VMs (all Linux distros - many versions) to compile 
software for a company I was at.  It is configured a private network so some 
assembly is required.  But I made most of it configurable up top so it could be 
on a public adapter.  I developed it on Slack (of course) then deployed it to a 
Suse production box.  I had set up NFS on the host with the source code.  The 
source would get copied to its own branch for the machine it was compiling for, 
fire up the VM, compile over NFS, create package on NFS host 
(rpm/tgz/txz/deb/etc), then die, rinse and repeat.

#!/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

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

-- 
=== Never ask a geek why, just nod your head and slowly back away.===
+================================+==================================+
|  John O'Donnell                |                                  |
|  (Sr. Systems Engineer,        |    http://juanisan.homeip.net    |
|  Net Admin, Programmer, etc.)  |  E-Mail: unixjohn1969 at gmail.com  |
+================================+==================================+
No man is useless who has a friend, and if we are loved we are
indispensable.  -- Robert Louis Stevenson


More information about the ARMedslack mailing list