summaryrefslogtreecommitdiff
path: root/setup.sh
blob: 64d51428d123c80162c10d9ee95f1514e744c21d (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/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 all; };
}

protocol kernel {
  ipv6 { export all; };
}

protocol device {
}

protocol direct {
  ipv4;
  ipv6;
}

protocol static {
  ipv4;
}

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)\" {"
            echo "      type ptp;"
            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] && net ~ [$(getval "Subnet" | grep -v ':' | while read -r line; do printf "%s+," "$line"; done | sed 's/,$//')] then accept; else reject; };
  };
$interfacelist
}
protocol ospf v3 ospf6 {
  ipv6 {
    import all;
    export filter { if source ~ [RTS_DEVICE, RTS_INHERIT] && net ~ [$(getval "Subnet" | grep ':' | while read -r line; do printf "%s+," "$line"; done | sed 's/,$//')] then accept; else reject; };
  };
$interfacelist
}
EOF
    fi  
) > /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