termie | 76ce231 | 2011-09-28 16:29:25 -0500 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | |
| 3 | |
| 4 | # Print some usage info |
| 5 | function 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. |
| 16 | function 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 |
| 25 | host_ip= |
| 26 | dry_run=0 |
| 27 | bridge=br0 |
| 28 | DRIER= |
| 29 | |
| 30 | # Process the args |
| 31 | for arg in "$@"; do |
| 32 | process_option $arg |
| 33 | done |
| 34 | |
| 35 | if [ $dry_run ]; then |
| 36 | DRIER=echo |
| 37 | fi |
| 38 | |
| 39 | if [ "$UID" -ne "0" ]; then |
| 40 | echo "This script must be run with root privileges." |
| 41 | exit 1 |
| 42 | fi |
| 43 | |
| 44 | # Check for bridge-utils. |
| 45 | BRCTL=`which brctl` |
| 46 | if [ ! -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 |
| 50 | fi |
| 51 | |
| 52 | # Scare off the nubs. |
| 53 | echo "=====================================================" |
| 54 | echo |
| 55 | echo "WARNING" |
| 56 | echo |
| 57 | echo "This script will modify your current network setup," |
| 58 | echo "this can be a scary thing and it is recommended that" |
| 59 | echo "you have something equivalent to physical access to" |
| 60 | echo "this machine before continuing in case your network" |
| 61 | echo "gets all funky." |
| 62 | echo |
| 63 | echo "If you don't want to continue, hit CTRL-C now." |
| 64 | |
| 65 | if [ -z "$host_ip" ]; |
| 66 | then |
| 67 | echo "Otherwise, please type in your host's ip address and" |
| 68 | echo "hit enter." |
| 69 | echo |
| 70 | echo "=====================================================" |
| 71 | read host_ip |
| 72 | else |
| 73 | echo "Otherwise hit enter." |
| 74 | echo |
| 75 | echo "=====================================================" |
| 76 | read accept |
| 77 | fi |
| 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 |
| 84 | if [ $dry_run ]; then |
| 85 | echo "echo 1 > /proc/sys/net/ipv4/ip_forward" |
| 86 | else |
| 87 | echo 1 > /proc/sys/net/ipv4/ip_forward |
| 88 | fi |
| 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 | |