turn verbose on when setting up qemu bridge and stuff
[tests.git] / system / template-qemu / qemu-bridge-init
1 #!/bin/bash
2
3 # Establishment of a runtime environment for a
4 # virtual  machine  under QEMU, This script allows the host box
5 # to share its network connection with qemu-based guests
6 #
7 # Author: Amine chaoui
8 #
9
10 COMMAND=$(basename $0)
11 cd $(dirname $0)
12
13 # turn on verbosity
14 set -x
15
16 # constant
17 INTERFACE_BRIDGE=br0
18 # Default Value
19 INTERFACE_LAN=eth0
20
21 # Fonction de mise en place du pont
22 start () {
23
24     # take extra arg for ifname, if provided
25     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
26
27     ### Checking
28     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
29
30     #if we have already configured the same host_box no need to do it again
31     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
32         echo "Interface bridge $INTERFACE_BRIDGE already exist."
33         exit 0
34     }
35     /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
36         echo "Cannot use interface $INTERFACE_LAN - exiting"
37         exit 1
38     }
39
40     #Getting host IP/masklen
41     address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $2;}')
42     [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
43     
44     broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $4;}')
45     [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
46
47     gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
48     [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
49
50     ### do it
51     #Restarting udev
52     echo "Starting udev ..."
53     /sbin/udevd restart
54     if modprobe kqemu &> /dev/null ; then
55         echo "kqemu loadded"
56     else
57         echo "WARNING : Could not modprobe kqemu"
58     fi
59     #Loading the tun/tap model
60     if modprobe tun ; then
61         echo "tun loaded"
62         # Giving read/write access
63         echo "Granting read/write acces to the tun device"
64         chmod 666 /dev/net/tun
65     else
66         echo "Could not modprobe tun - exiting"
67         exit 1
68     fi
69
70     # creating the bridge
71     echo "Creating bridge $INTERFACE_BRIDGE"
72     brctl addbr $INTERFACE_BRIDGE
73     #brctl stp $INTERFACE_BRIDGE yes
74     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
75     echo "Activating promiscuous mode  $INTERFACE_LAN..."
76     /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
77     sleep 2
78     echo "Setting bridge $address $broadcast"
79     # static
80     /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
81     sleep 1
82         
83     #Reconfigure the routing table
84     echo "Configuring  the IP  Gateway @:" $gateway
85     route add default gw $gateway
86
87 }
88
89 #Adding a new interface to the bridge: this is used by qemu-ifup 
90 add () {
91
92     [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
93     INTERFACE_LAN=$1; shift
94
95     echo "Activating link for $INTERFACE_LAN..."
96     /sbin/ip link set $INTERFACE_LAN up
97     sleep 1
98     echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
99     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
100
101     # turn off filtering on this interface
102     ########## from the test environment
103     # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
104     CONFIG=qemu.conf
105     [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
106     . $CONFIG
107
108     echo "Tweaking iptables"
109     iptables-save > iptables.pre
110     # rewrite a new config - quick and dirty
111     ./iptables.py iptables.pre iptables.post $IP
112     iptables-restore < iptables.post
113
114     echo "Installed iptables"
115     iptables-save
116     
117 }
118
119 #Stop the bridge and restore the original setting
120 stop () {
121     # take extra arg for ifname, if provided
122     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
123
124     ### Checking
125     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
126
127     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
128         echo "Interface bridge $INTERFACE_BRIDGE does not exist."
129         exit 0
130     }
131     brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
132     /sbin/ifconfig $INTERFACE_BRIDGE down
133     brctl delbr $INTERFACE_BRIDGE
134     /sbin/service network restart
135     /sbin/service iptables restart
136 }
137
138 function main () {
139
140     case "$1" in
141         start)
142             shift; start "$@" ;;
143         stop)
144             shift; stop "$@" ;;
145         add)
146             shift; add "$@" ;;
147         *)
148             echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
149     esac
150     exit 0
151 }
152
153 # redirect stderr as well
154 main "$@" 2>&1