another round of rework
[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
12 # constant
13 INTERFACE_BRIDGE=br0
14 # Default Value
15 INTERFACE_LAN=eth0
16
17 # Fonction de mise en place du pont
18 start () {
19
20     # take extra arg for ifname, if provided
21     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
22
23     ### Checking
24     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
25
26     #if we have already configured the same host_box no need to do it again
27     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
28         echo "Interface bridge $INTERFACE_BRIDGE already exist."
29         exit 0
30     }
31     /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
32         echo "Cannot use interface $INTERFACE_LAN - exiting"
33         exit 1
34     }
35
36     #Getting host IP/masklen
37     address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $2;}')
38     [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
39     
40     broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $4;}')
41     [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
42
43     gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
44     [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
45
46     ### do it
47     #Restarting udev
48     echo "Starting udev ..."
49     /sbin/udevd restart
50     if modprobe kqemu ; then
51         echo "kqemu loadded"
52     else
53         echo "WARNING : Could not modprobe kqemu"
54     fi
55     #Loding the tun/tap model
56     if modprobe tun ; then
57         echo "tun loaded"
58         # Giving read/write access
59         echo "Granting read/write acces to the tun device"
60         chmod 666 /dev/net/tun
61     else
62         echo "Could not modprobe tun - exiting"
63         exit 1
64     fi
65
66     # creating the bridge
67     echo "Creating bridge $INTERFACE_BRIDGE"
68     brctl addbr $INTERFACE_BRIDGE
69     #brctl stp $INTERFACE_BRIDGE yes
70     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
71     echo "Activating promiscuous mode  $INTERFACE_LAN..."
72     /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
73     sleep 2
74     echo "Setting bridge $address $broadcast"
75     # static
76     /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
77     sleep 1
78         
79     #Reconfigure the routing table
80     echo "Configuring  the IP  Gateway @:" $gateway
81     route add default gw $gateway
82
83     # xxx this is *wrong*
84     # wipe the host firewall otherwise the guest qemu can't access the LAN
85     echo "Wiping the firewall..." 
86     iptables -F
87     
88 }
89
90 #Adding a new interface to the bridge: this is used by qemu-ifup 
91 add () {
92
93     [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
94     INTERFACE_LAN=$1; shift
95
96     echo "Activating link for $INTERFACE_LAN..."
97     /sbin/ip link set $INTERFACE_LAN up
98     sleep 1
99     echo "Adding $INTERFACE_BRIDGE to $INTERFACE_BRIDGE"
100     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
101     # putting a virtual interface in promisc mode seems like an odd thing to do
102     # echo "Activating promiscuous mode on $INTERFACE_LAN ..."
103     # /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
104     # sleep 1
105     echo "Done."
106
107 }
108
109 #Stop the bridge and restore the original setting
110 stop () {
111     # take extra arg for ifname, if provided
112     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
113
114     ### Checking
115     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
116
117     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
118         echo "Interface bridge $INTERFACE_BRIDGE does not exist."
119         exit 0
120     }
121     brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
122     /sbin/ifconfig $INTERFACE_BRIDGE down
123     brctl delbr $INTERFACE_BRIDGE
124     /sbin/service network restart
125 }
126
127
128 case "$1" in
129     start)
130         shift; start "$@" ;;
131     stop)
132         shift; stop "$@" ;;
133     add)
134         shift; add "$@" ;;
135     *)
136         echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
137 esac
138
139 exit 0