blob: 59d82e626709969c165de155e561a3577b3071ba [file] [log] [blame]
termie76ce2312011-09-28 16:29:25 -05001#!/bin/bash
2
3
4# Print some usage info
5function usage {
6 echo "Usage: $0 [OPTION] [host_ip]"
7 echo "Set up networking for LXC"
8 echo ""
9 echo " -n, --dry-run Just print the commands that would execute."
10 echo " -h, --help Print this usage message."
11 echo ""
12 exit
13}
14
15# Allow passing the ip address on the command line.
16function process_option {
17 case "$1" in
18 -h|--help) usage;;
19 -n|--dry-run) dry_run=1;;
20 *) host_ip="$1"
21 esac
22}
23
24# Set up some defaults
25host_ip=
26dry_run=0
27bridge=br0
28DRIER=
29
30# Process the args
31for arg in "$@"; do
32 process_option $arg
33done
34
35if [ $dry_run ]; then
36 DRIER=echo
37fi
38
39if [ "$UID" -ne "0" ]; then
40 echo "This script must be run with root privileges."
41 exit 1
42fi
43
44# Check for bridge-utils.
45BRCTL=`which brctl`
46if [ ! -x "$BRCTL" ]; then
47 echo "This script requires you to install bridge-utils."
48 echo "Try: sudo apt-get install bridge-utils."
49 exit 1
50fi
51
52# Scare off the nubs.
53echo "====================================================="
54echo
55echo "WARNING"
56echo
57echo "This script will modify your current network setup,"
58echo "this can be a scary thing and it is recommended that"
59echo "you have something equivalent to physical access to"
60echo "this machine before continuing in case your network"
61echo "gets all funky."
62echo
63echo "If you don't want to continue, hit CTRL-C now."
64
65if [ -z "$host_ip" ];
66then
67 echo "Otherwise, please type in your host's ip address and"
68 echo "hit enter."
69 echo
70 echo "====================================================="
71 read host_ip
72else
73 echo "Otherwise hit enter."
74 echo
75 echo "====================================================="
76 read accept
77fi
78
79
80# Add a bridge interface, this will choke if there is already
81# a bridge named $bridge
82$DRIER $BRCTL addbr $bridge
83$DRIER ip addr add 192.168.1.1/24 dev $bridge
84if [ $dry_run ]; then
85 echo "echo 1 > /proc/sys/net/ipv4/ip_forward"
86else
87 echo 1 > /proc/sys/net/ipv4/ip_forward
88fi
89$DRIER ifconfig $bridge up
90
91# Set up the NAT for the instances
92$DRIER iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source $host_ip
93$DRIER iptables -I FORWARD -s 192.168.1.0/24 -j ACCEPT
94