reorganize the qemu configuration template file
[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
23     if [ -n "$1" ]; then
24         INTERFACE_LAN=$1
25         shift
26     fi
27     set $(/sbin/ifconfig | grep $INTERFACE_LAN) >/dev/null
28     if [ -z "$1" ]; then
29         echo "Interface réseau $IF_HOTE non trouvée."
30         exit 1
31     fi
32     shift $(($# - 1))
33     echo "Using the interface" $INTERFACE_LAN
34         
35     #Restarting the udev
36     echo "Starting the udev ..."
37     /sbin/udevd restart
38     #Loding the tun/tap model
39     echo "Loading the tun module ..."
40     modprobe tun
41     set $(lsmod | grep tun) >/dev/null
42     if [ -z "$1" ]; then
43         echo "Module tun/tap not activated"
44         exit 1
45     fi
46     shift $(($# - 1))
47
48     #Giving acces in Read/Write to the tun module
49     echo "Granting the Read/Write acces to the tun module..."
50     chmod 666 /dev/net/tun
51
52
53     ##Get The BROADCAST ip @
54     set $(/sbin/ip addr show $INTERFACE_LAN | grep inet) >/dev/null 2>&1
55     if [ -n "$2" ]; then
56         IP_BROADCAST=$4
57     fi
58     shift $(($# - 1))
59     
60     #Getting the GATEWAY IP @
61     set $(netstat -rn | grep UG ) >/dev/null 2>&1
62     if [ -n "$2" ]; then
63         IP_GATEWAY=$2
64     fi
65     shift $(($# - 1))
66
67     #Getting the host IP
68     set $(ifconfig $INTERFACE_LAN 2> /dev/null | grep "inet addr:" | \
69         sed -e "s/.*addr:\([^ ]*\).*/\1/")
70     if [ -n "$1" ]; then
71         IP_HOST=$1
72     fi
73     shift $(($# - 1))
74  
75     ##Getting the Netmask address
76     set $(ifconfig $INTERFACE_LAN 2> /dev/null | grep "inet addr:" | \
77         sed -e "s/.*Mask:\([^ ]*\).*/\1/")
78     if [ -n "$1" ]; then
79         IP_NETMASK=$1
80     fi
81     shift $(($# - 1))
82  
83     # Création et paramétrage du pont
84     echo "Configure $INTERFACE_BRIDGE bridge..."
85     /usr/sbin/brctl addbr $INTERFACE_BRIDGE
86     #/usr/sbin/brctl stp $INTERFACE_BRIDGE yes
87     /usr/sbin/brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
88     echo "Activating promiscuous mode  $INTERFACE_LAN..."
89     /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
90     sleep 2
91     echo "IP address on $INTERFACE_BRIDGE..."
92     # static
93     /sbin/ifconfig $INTERFACE_BRIDGE $IP_HOST  broadcast $IP_BROADCAST  netmask $IP_NETMASK up
94     sleep 1
95         
96     #Reconfigure the Bridge IP @ in the host machine
97     echo "Configuring  the IP  Gateway @:" $IP_GATEWAY
98     route add default gw $IP_GATEWAY
99
100     
101     #wipe the host firewall otherwise the guest qemu can't acces to the LAN
102     echo "Wiping the firewall..." 
103     iptables -F
104     
105     #preparing the hard disk image for qemu install
106     if [  -e "hda_5.raw" ];then
107         rm -rf hda_5.raw
108     fi
109     echo "Creating hard disk for Qemu install..."
110     set $(qemu-img create hda_5.raw 5G) >/dev/null
111     if [ -z "$1" ];then
112         echo "Can't Create disk image..."
113     fi
114     shift $(($# - 1))
115     
116         
117     
118 }
119
120
121 #Adding a new interface to the bridge
122 add () {
123         /sbin/ifconfig $1 0.0.0.0 promisc up
124         /usr/sbin/brctl addif $INTERFACE_BRIDGE $1
125 }
126
127
128 #Stop the actual bridged network  and Restore the original network
129 stop () {
130         if [ -n "$1" ]; then
131                 INTERFACE_LAN=$1
132         fi
133         TESTPONT=$(/sbin/ifconfig | grep $INTERFACE_BRIDGE)
134         if [ -z "$TESTPONT" ]; then
135                 echo "Attention : pont réseau non trouvé. Vérifier la config réseau ..."
136                 exit 1
137         fi
138         /usr/sbin/brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
139         /sbin/ifconfig $INTERFACE_BRIDGE down
140         /usr/sbin/brctl delbr $INTERFACE_BRIDGE
141         /sbin/service network restart
142 }
143
144
145 case $1 in
146         start)
147                 start $2
148         ;;
149         stop)
150                 stop $2
151         ;;
152         add)
153                 add $2
154         ;;
155         *)
156                 echo $"Use: env-qemu {start|add|stop} [interface]"
157                 exit 1
158 esac
159
160 exit 0
161
162
163
164