using lowercase
[tests.git] / system / template-qemu / env-qemu
1 #!/bin/bash
2
3
4 # Establishment of a runtime environment for a
5 # virtual  machine  under QEMU, This script permits
6 # to the virtual machine to share the
7 # network connection with a host machine under FC6.
8
9 #Author: Amine chaoui
10
11 # Default Value
12 IP_GATEWAY=0.0.0.0
13 IP_HOST=0.0.0.0
14 IP_BROADCAST=0.0.0.0
15 IP_NETMASK=0.0.0.0
16
17 INTERFACE_LAN=eth0
18 INTERFACE_BRIDGE=br0
19
20 # Fonction de mise en place du pont
21 start () {
22     #if we have already configured the same host_box no need to do it again
23     set $(/sbin/ifconfig | grep $INTERFACE_BRIDGE) >/dev/null
24     if [ -n "$1" ]; then
25         echo "Interface bridge $INTERFACE_BRIDGE already exist."
26         exit 0
27     fi
28     if [ -n "$1" ]; then
29         INTERFACE_LAN=$1
30         shift
31     fi
32     set $(/sbin/ifconfig | grep $INTERFACE_LAN) >/dev/null
33     if [ -z "$1" ]; then
34         echo "Interface réseau $IF_HOTE non trouvée."
35         exit 1
36     fi
37     shift $(($# - 1))
38     echo "Using the interface" $INTERFACE_LAN
39         
40     #Restarting the udev
41     echo "Starting the udev ..."
42     /sbin/udevd restart
43     echo "Starting the kqemu patch module ..."
44     modprobe kqemu
45     #Loding the tun/tap model
46     echo "Loading the kqemu patch module ..."
47     modprobe kqemu
48     #Loding the tun/tap model
49     echo "Loading the tun module ..."
50     modprobe tun
51     set $(lsmod | grep tun) >/dev/null
52     if [ -z "$1" ]; then
53         echo "Module tun/tap not activated"
54         exit 1
55     fi
56     shift $(($# - 1))
57
58     #Giving acces in Read/Write to the tun module
59     echo "Granting the Read/Write acces to the tun module..."
60     chmod 666 /dev/net/tun
61
62
63     ##Get The BROADCAST ip @
64     set $(/sbin/ip addr show $INTERFACE_LAN | grep inet) >/dev/null 2>&1
65     if [ -n "$2" ]; then
66         IP_BROADCAST=$4
67     fi
68     shift $(($# - 1))
69     
70     #Getting the GATEWAY IP @
71     set $(netstat -rn | grep UG ) >/dev/null 2>&1
72     if [ -n "$2" ]; then
73         IP_GATEWAY=$2
74     fi
75     shift $(($# - 1))
76
77     #Getting the host IP
78     set $(ifconfig $INTERFACE_LAN 2> /dev/null | grep "inet addr:" | \
79         sed -e "s/.*addr:\([^ ]*\).*/\1/")
80     if [ -n "$1" ]; then
81         IP_HOST=$1
82     fi
83     shift $(($# - 1))
84  
85     ##Getting the Netmask address
86     set $(ifconfig $INTERFACE_LAN 2> /dev/null | grep "inet addr:" | \
87         sed -e "s/.*Mask:\([^ ]*\).*/\1/")
88     if [ -n "$1" ]; then
89         IP_NETMASK=$1
90     fi
91     shift $(($# - 1))
92  
93     # Création et paramétrage du pont
94     echo "Configure $INTERFACE_BRIDGE bridge..."
95     /usr/sbin/brctl addbr $INTERFACE_BRIDGE
96     #/usr/sbin/brctl stp $INTERFACE_BRIDGE yes
97     /usr/sbin/brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
98     echo "Activating promiscuous mode  $INTERFACE_LAN..."
99     /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
100     sleep 2
101     echo "IP address on $INTERFACE_BRIDGE..."
102     # static
103     /sbin/ifconfig $INTERFACE_BRIDGE $IP_HOST  broadcast $IP_BROADCAST  netmask $IP_NETMASK up
104     sleep 1
105         
106     #Reconfigure the Bridge IP @ in the host machine
107     echo "Configuring  the IP  Gateway @:" $IP_GATEWAY
108     route add default gw $IP_GATEWAY
109
110     
111     #wipe the host firewall otherwise the guest qemu can't acces to the LAN
112     echo "Wiping the firewall..." 
113     iptables -F
114     
115 }
116
117
118 #Adding a new interface to the bridge
119 add () {
120         /sbin/ifconfig $1 0.0.0.0 promisc up
121         /usr/sbin/brctl addif $INTERFACE_BRIDGE $1
122 }
123
124
125 #Stop the actual bridged network  and Restore the original network
126 stop () {
127         if [ -n "$1" ]; then
128                 INTERFACE_LAN=$1
129         fi
130         TESTPONT=$(/sbin/ifconfig | grep $INTERFACE_BRIDGE)
131         if [ -z "$TESTPONT" ]; then
132                 echo "Attention : pont réseau non trouvé. Vérifier la config réseau ..."
133                 exit 1
134         fi
135         /usr/sbin/brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
136         /sbin/ifconfig $INTERFACE_BRIDGE down
137         /usr/sbin/brctl delbr $INTERFACE_BRIDGE
138         /sbin/service network restart
139 }
140
141
142 case $1 in
143         start)
144                 start $2
145         ;;
146         stop)
147                 stop $2
148         ;;
149         add)
150                 add $2
151         ;;
152         *)
153                 echo $"Use: env-qemu {start|add|stop} [interface]"
154                 exit 1
155 esac
156
157 exit 0
158
159
160
161