freyanet/setup.sh

150 lines
3.3 KiB
Bash
Raw Normal View History

2023-11-11 00:44:48 +00:00
#!/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() {
2023-11-15 04:25:25 +00:00
sed -e 's/\\/\\\\/g;s/"/\\"/g'
2023-11-11 00:44:48 +00:00
}
if [ -n "$ospf" ]; then
step "Creating Bird configuration"
touch /var/log/bird.log
chown bird:bird /var/log/bird.log
2023-11-15 04:25:25 +00:00
2023-11-11 00:44:48 +00:00
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 {
2023-11-15 04:25:25 +00:00
ipv4 { export all; };
2023-11-11 00:44:48 +00:00
}
2023-11-15 04:25:25 +00:00
2023-11-11 00:44:48 +00:00
protocol kernel {
2023-11-15 04:25:25 +00:00
ipv6 { export all; };
2023-11-11 00:44:48 +00:00
}
2023-11-15 04:25:25 +00:00
2023-11-11 00:44:48 +00:00
protocol device {
}
protocol direct {
ipv4;
ipv6;
}
2023-11-15 04:25:25 +00:00
protocol static {
ipv4;
}
2023-11-11 00:44:48 +00:00
EOF
if [ -n "$ospf" ]; then
interfacelist=$(
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)\" {"
2023-11-15 04:25:25 +00:00
echo " type ptp;"
2023-11-11 00:44:48 +00:00
if [ -n "$val" ]; then
echo " $val;";
fi
echo " };"
fi
done
)
cat <<EOF
protocol ospf v3 ospf4 {
ipv4 {
import all;
2023-11-15 04:25:25 +00:00
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; };
2023-11-11 00:44:48 +00:00
};
2023-12-12 23:25:26 +00:00
area 0 {
$(getval "Stubnet" | grep -v ':' | while read -r net; do echo " stubnet $net {};"; done)
2023-11-11 00:44:48 +00:00
$interfacelist
2023-12-12 23:25:26 +00:00
};
2023-11-11 00:44:48 +00:00
}
protocol ospf v3 ospf6 {
ipv6 {
import all;
2023-11-15 04:25:25 +00:00
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; };
2023-11-11 00:44:48 +00:00
};
2023-12-12 23:25:26 +00:00
area 0 {
$(getval "Stubnet" | grep ':' | while read -r net; do echo " stubnet $net {};"; done)
2023-11-11 00:44:48 +00:00
$interfacelist
2023-12-12 23:25:26 +00:00
};
2023-11-11 00:44:48 +00:00
}
EOF
2023-11-15 04:25:25 +00:00
fi
) > /etc/bird.conf
2023-11-11 00:44:48 +00:00
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