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