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