summaryrefslogtreecommitdiff
path: root/netjail/netjail-init.sh
blob: 7fd0dd53a7555fcc5a17afea393665341eda6e0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/sh

# This file is in the public domain.
#
# Shell script for the general setup of network namespaces.
# This script should be run once at boot time (as root).
# Afterwards, the netjail.sh can be used by anyone (with
# the right permissions) to create their own network namespace.
#
set -eu

# Be extra safe, even though sudo should already do this.
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Setup virtual bridge which we will use to connect all
# of the builder networks to.

BRIDGE=builderbridge

brctl addbr $BRIDGE
brctl stp $BRIDGE off
ip link set dev $BRIDGE up

# Connect bridge to host network
ip link add tap0 type veth peer name br-tap0
brctl addif $BRIDGE br-tap0
ip link set dev tap0 up
ip link set dev br-tap0 up

# Configure bridge on host
ip addr add 10.42.42.1/24 dev tap0

# Configure IP masquerading/NAT
# Obtain list of our network interfaces from /etc/network/interfaces
IFCS=`cat /etc/network/interfaces | grep iface | grep -v loopback | awk '{print $2}'`
for IFC in $IFCS
do
    iptables --table nat --append POSTROUTING --out-interface $IFC -j MASQUERADE
done
echo 1 > /proc/sys/net/ipv4/ip_forward