xref: /kvmtool/util/set_private_br.sh (revision 1c87dc459e97a17ea81c9e4f60b9c338a66bffd9)
1#!/bin/bash
2#
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.
7#
8# @ ./set_private_br.sh $bridge_name $subnet_prefix
9# @ ./set_private_br.sh vbr0 192.168.33
10
11brname='vbr0'
12subnet='192.168.33'
13
14add_br()
15{
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
29}
30
31del_br()
32{
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
38}
39
40
41if [ $# = 0 ]; then
42    del_br 2>/dev/null
43    exit
44fi
45if [ $# > 1 ]; then
46    brname="$1"
47fi
48if [ $# = 2 ]; then
49    subnet="$2"
50fi
51add_br
52