#!/bin/sh # $Id$ # Qemu environment preparation script # --------------------------------------------------------------------------- # # After running this startup script, run a QEMU virtual machine in this way: # # vdeqemu [qemu_option [qemu_option], ...] # # The vdeqemu program will automatically connect # the QEMU virtual machine to the VDE switch. # # --------------------------------------------------------------------------- # The IP configuration for the tap device that will be used for # the virtual machine network: TAP_DEV=tap0 TAP_IP=10.111.111.254 TAP_MASK=255.255.255.0 TAP_BCAST=`/bin/ipmask ${TAP_MASK} ${TAP_IP} | cut -f 1 -d ' '` # Host interfaces that need to be NAT-ed (in case we're not bridging): NAT_IFS="eth+" # Definitions for the LAN segment the Qemu virtual machines will be in. # These definitions will be fed to dnsmasq - this program will provide DNS # and DHCP to the Qemu LAN. # The VM_IPLOW and VM_IPHIGH addresses must agree with the definitions for # the tap0 device above. These 'low' and 'high' values are the IP address # range for the DHCP server to use. VM_DOMAIN=qemu.lan VM_IPLOW=10.111.111.128 VM_IPHIGH=10.111.111.199 VM_BCAST=${TAP_BCAST} VM_MASK=${TAP_MASK} # For additional options to dnsmasq: #DNSMASQ_OPTIONS="--server /my.net/192.168.1.1" DNSMASQ_OPTIONS="" # See how we were called. case "$1" in start) echo -n "Starting VDE network for QEMU: " # Load tun module /sbin/modprobe tun 2>/dev/null # Wait for the module to be loaded while ! /bin/lsmod |grep -q "^tun"; do echo Waiting for tun device;sleep 1; done # Start tap switch vde_switch -tap ${TAP_DEV} -daemon sleep 1 # Bring tap interface up ifconfig ${TAP_DEV} ${TAP_IP} broadcast ${TAP_BCAST} netmask ${TAP_MASK} # Start IP Forwarding echo "1" > /proc/sys/net/ipv4/ip_forward for NIC in ${NAT_IFS}; do iptables -t nat -A POSTROUTING -o ${NIC} -j MASQUERADE done # Change pipe permission (vde2 uses a different pipe directory) #chmod 666 /tmp/vde.ctl chmod -R a+rwx /var/run/vde.ctl # If we are not running 2.6, apply workaround if uname -r | grep '^2.4'; then echo 1024 > /proc/sys/dev/rtc/max-user-freq fi # Start dnsmasq, the DNS/DHCP server # for our Virtual Machines behind the tap0 interface /usr/sbin/dnsmasq \ --log-queries \ --user=named \ --dhcp-leasefile=/var/state/dhcp/qemu-dhcpd.leases \ --dhcp-range=${VM_IPLOW},${VM_IPHIGH},${VM_MASK},${VM_BCAST},8h \ --interface=${TAP_DEV} \ --domain=${VM_DOMAIN} \ $DNSMASQ_OPTIONS echo ;; stop) echo -n "Stopping VDE network for QEMU: " { # Delete the NAT rules for NIC in ${NAT_IFS}; do iptables -t nat -D POSTROUTING -o ${NIC} -j MASQUERADE done # Bring tap interface down ifconfig ${TAP_DEV} down # Kill VDE switch pgrep -f vde_switch | xargs kill -TERM # Remove the control socket (vde2 uses a different location) #rm -f /tmp/vde.ctl rmdir /var/run/vde.ctl # Stop dnsmasq pgrep -f dnsmasq | xargs kill -TERM } >/dev/null 2>&1 echo ;; restart|reload) $0 stop $0 start ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac