kvm tools, setup: Create private directory
[linux-2.6/next.git] / tools / kvm / util / set_private_br.sh
blob49867ddca6a737619e9a72099e11e4550cf9dbb5
1 #!/bin/bash
3 # Author: Amos Kong <kongjianjun@gmail.com>
4 # Date: Apr 14, 2011
5 # Description: this script is used to create/delete a private bridge,
6 # launch a dhcp server on the bridge by dnsmasq.
8 # @ ./set_private_br.sh $bridge_name $subnet_prefix
9 # @ ./set_private_br.sh vbr0 192.168.33
11 brname='vbr0'
12 subnet='192.168.33'
14 add_br()
16 echo "add new private bridge: $brname"
17 /usr/sbin/brctl addbr $brname
18 echo 1 > /proc/sys/net/ipv6/conf/$brname/disable_ipv6
19 echo 1 > /proc/sys/net/ipv4/ip_forward
20 /usr/sbin/brctl stp $brname on
21 /usr/sbin/brctl setfd $brname 0
22 ifconfig $brname $subnet.1
23 ifconfig $brname up
24 # Add forward rule, then guest can access public network
25 iptables -t nat -A POSTROUTING -s $subnet.254/24 ! -d $subnet.254/24 -j MASQUERADE
26 /etc/init.d/dnsmasq stop
27 /etc/init.d/tftpd-hpa stop 2>/dev/null
28 dnsmasq --strict-order --bind-interfaces --listen-address $subnet.1 --dhcp-range $subnet.1,$subnet.254 $tftp_cmd
31 del_br()
33 echo "cleanup bridge setup"
34 kill -9 `pgrep dnsmasq|tail -1`
35 ifconfig $brname down
36 /usr/sbin/brctl delbr $brname
37 iptables -t nat -D POSTROUTING -s $subnet.254/24 ! -d $subnet.254/24 -j MASQUERADE
41 if [ $# = 0 ]; then
42 del_br 2>/dev/null
43 exit
45 if [ $# > 1 ]; then
46 brname="$1"
48 if [ $# = 2 ]; then
49 subnet="$2"
51 add_br