#!/bin/bash
#   $Id: kvm-start.sh 1.2 2008/07/23 17:26:24 root Exp root $

# Start the kvm so virtual machines can be used
bash -xv <<XX
  # put this default firewal define early
  FW1="put your default router IP address here"

  lsmod | grep -q kvm ||
  modprobe -v kvm-intel
  if [ -e /dev/kvm ]; then
    chgrp kvm /dev/kvm
    chmod g+rw /dev/kvm; else
    echo "/dev/kvm not present" >&2
  fi
  # now load the tun module
  lsmod | grep -q '^tun ' ||
  modprobe -v tun
  if [ -e /dev/net/tun ]; then
    chgrp kvm /dev/net/tun
    chmod g+rw /dev/net/tun
  else
    echo "load of \"tun\" module failed to create /dev/net/tun" >&2
  fi
  # change to using the bridge if you haven't
  if ! ifconfig br0 &>/dev/null; then
    # save the eth0 IP
    eth0IP=$(ifconfig eth0 | sed -n '/inet addr:/s/[^:]*:\([0-9.]*\).*/\1/p')
    # add the bridge
    brctl addbr br0 &&
    ifconfig br0 \${eth0IP} &&
    brctl addif br0 eth0 &&
    {
      ifconfig eth0 0.0.0.0
      route add default gw \${FW1} dev br0
    }
  fi
  # add the FORWARD rule
  if ! iptables -L FORWARD | grep -q br0; then
    # pass anything from the bridge
    iptables -I FORWARD 1 -i br0 -j ACCEPT
  fi
  # wait a little and perform the final test
  sleep 10 # for DHCP to set up again
  ping -c3 -w5 \$FW1 ||
  echo "Bridge setup not right" >&2
XX

# In the main bash for the start-VM script
alias KVM=/usr/local/bin/qemu-system-x86_64
MAC_webster="52:54:00:12:00:01"
MAC_partygirl="52:54:00:12:00:02"

