diff options
Diffstat (limited to 'setup.sh')
-rwxr-xr-x | setup.sh | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..c03fb10 --- /dev/null +++ b/setup.sh @@ -0,0 +1,147 @@ +#!/bin/sh + +run() { + printf '$ \x1b[32;1m%s\x1b[0m\n' "$*" + "$@" +} +step() { + printf '\x1b[34;1m>> %s\x1b[0m\n' "$*" +} + +getval() { + /usr/local/bin/config.awk /config/inet2.conf "$@" +} + +haskey() { + getval interface | while read -r inter; do + if getval "interface $inter" "$1"; then + echo "true" + return + fi + done +} + +# ensure the /run/inet2 directory is empty (docker doesn't mount tmpfs to /run) +# /run/inet2 is used for storage during runtime - restarting the container should clear it +rm -rf /run/inet2 2>/dev/null +mkdir /run/inet2 + +# ensure the /var/lib/inet2 directory exists +# /var/lib/inet2 is used for storage for the entire lifetime of the container - restarting the container shouldn't clear it +if [ ! -d /var/lib/inet2 ]; then + mkdir -p /var/lib/inet2 +fi + +# these are disabled in the docker netns +step "Enabling IPv6" +run sysctl net.ipv6.conf.all.disable_ipv6=0 net.ipv6.conf.default.disable_ipv6=0 net.ipv6.conf.all.forwarding=1 + +ospf="$(haskey OSPF)" + +escapebird() { + sed -e 's/\\/\\\\/g;s/"/\\"/g' +} + +if [ -n "$ospf" ]; then + step "Creating Bird configuration" + + touch /var/log/bird.log + chown bird:bird /var/log/bird.log + + selfas=$(getval AS) + ( + cat <<EOF +log "/var/log/bird.log" all; + +$(getval RouterID | while read -r line; do echo "router id $line;"; done) + +protocol kernel { + ipv4 { + export filter { if source ~ [RTS_BGP, RTS_OSPF, RTS_OSPF_IA, RTS_OSPF_EXT1, RTS_OSPF_EXT2] then accept; else reject; }; + import all; + }; + learn; + scan time 10; +} +protocol kernel { + ipv6 { + export filter { if source ~ [RTS_BGP, RTS_OSPF, RTS_OSPF_IA, RTS_OSPF_EXT1, RTS_OSPF_EXT2] then accept; else reject; }; + import all; + }; + learn; + scan time 10; +} +protocol device { +} + +protocol direct { + ipv4; + ipv6; +} + + +EOF + if [ -n "$ospf" ]; then + interfacelist=$( + echo " area 0 {" + echo " interface \"lo\" { stub; };" + getval interface | while read -r inter; do + val="$(getval "interface $inter" OSPF)" + if [ "$?" = "0" ]; then + echo " interface \"$(printf "%s" "$inter" | escapebird)\" {" + if [ -n "$val" ]; then + echo " $val;"; + fi + echo " };" + fi + done + echo " };" + ) + + cat <<EOF +protocol ospf v3 ospf4 { + ipv4 { + import all; + export filter { if source ~ [RTS_DEVICE, RTS_INHERIT] then accept; else reject; }; + }; +$interfacelist +} +protocol ospf v3 ospf6 { + ipv6 { + import all; + export filter { if source ~ [RTS_DEVICE, RTS_INHERIT] then accept; else reject; }; + }; +$interfacelist +} +EOF + ) > /etc/bird.conf + chown root:bird /etc/bird.conf + chmod 640 /etc/bird.conf + + step "Enabling BIRD" + run rc-update add bird +fi + +if [ ! -f /var/lib/inet2/setupDone ]; then + if [ -f /config/setup.sh ]; then + step "Running /config/setup.sh" + /config/setup.sh + fi + touch /var/lib/inet2/setupDone +fi + +if [ -f /config/start.sh ]; then + step "Running /config/start.sh" + /config/start.sh +fi + +if [ "$#" = "0" ]; then + step "Starting OpenRC" + rm -rf /run/openrc 2>/dev/null + mkdir /run/openrc + touch /run/openrc/softlevel + exec /sbin/openrc +else + "$@" +fi + |