Commit 0e2ffa48 authored by Jan Moskyto Matejka's avatar Jan Moskyto Matejka

Birdlab: Great rework of rc.local

closes #6
parent ddc06a4f
......@@ -69,6 +69,9 @@ if [ "$1" = "--install" ]; then
shift 2
fi
RCGFILE=$ROOT/nfs/rc/rc-gen-$HOSTNAME
truncate -s0 $RCGFILE
declare -a HOSTNICVLAN
if [ -n "$HOSTNIC" ]; then
declare -a HOSTNIC=( $HOSTNIC )
......@@ -76,34 +79,38 @@ if [ -n "$HOSTNIC" ]; then
IFS=';' read NETNAME MAC TAG IP4 IP4PEER <<<"$N"
TAPID=$(mactotap $MAC)
HOSTNICDEV="$HOSTNICDEV -netdev tap,id=$TAPID,ifname=$TAPID,script=no -device e1000,netdev=$TAPID,mac=$MAC"
TAPFILE=$ROOT/nfs/net/$TAPID
echo "NAME $NETNAME" > $TAPFILE
echo "IP4 $IP4" >> $TAPFILE
if [ -n "$IP4PEER" ]; then echo "IP4PEER $IP4PEER" >> $TAPFILE; fi
echo "N=\$(ifname \${IFBYMAC[$MAC]})" >>$RCGFILE
echo "IFBYMAC[$MAC]=\$N" >>$RCGFILE
if [ -n "$IP4PEER"]; then
echo "ip4ptp \$N $IP4 $IP4PEER" >>$RCGFILE
else
echo "ip4 \$N $IP4" >>$RCGFILE
fi
HOSTNICVLAN+=( $TAPID,$TAG )
done
fi
GREFILE=$ROOT/nfs/net/gr-${HOSTMAC//:}
truncate -s0 $GREFILE
if [ -n "$HOSTVARS" ]; then
declare -a HOSTVARS=( $HOSTVARS )
for V in ${HOSTVARS[@]}; do
echo "export $V" >> $GREFILE
echo "export $V" >> $RCGFILE
done
declare -a HOSTVARNAMES
for V in ${HOSTVARS[@]}; do
HOSTVARNAMES+=( ${V%%=*} )
done
echo "declare -a VARNAMES=(${HOSTVARNAMES[@]})" >> $GREFILE
echo "declare -a VARNAMES=(${HOSTVARNAMES[@]})" >> $RCGFILE
fi
if [ -n "$HOSTDUMMY" ]; then
declare -a HOSTDUMMY=( $HOSTDUMMY )
for D in ${HOSTDUMMY[@]}; do
IFS=';' read DNAME IP4 IP6 <<<"$D"
echo dummy $DNAME $IP4 $IP6 >> $GREFILE
echo "N=\$(dummy $DNAME)" >>$RCGFILE
echo "INTERFACES+=(\$N)" >>$RCGFILE
echo "ip4 \$N $IP4" >>$RCGFILE
echo "ip6 \$N $IP6" >>$RCGFILE
done
fi
......@@ -111,7 +118,9 @@ if [ -n "$HOSTGRE" ]; then
declare -a HOSTGRE=( $HOSTGRE )
for G in ${HOSTGRE[@]}; do
IFS=';' read GRENAME PEER IP4 IP4PEER <<<"$G"
echo gre $GRENAME $HOSTIP4 ${CONF[$PEER;IP4]} $IP4 $IP4PEER >> $GREFILE
echo "N=\$(gre $GRENAME $HOSTIP4 ${CONF[$PEER;IP4]})" >>$RCGFILE
echo "INTERFACES+=(\$N)" >>$RCGFILE
echo "ip4ptp \$N $IP4 $IP4PEER" >>$RCGFILE
done
fi
......@@ -121,14 +130,17 @@ if [ -n "$HOSTSOCK" ]; then
IFS=';' read SOCKNAME APPEND MAC IP4 <<<"$S"
TAPID=$(mactotap $MAC)
HOSTNICDEV="$HOSTNICDEV -netdev socket,$APPEND,id=$SOCKNAME -device e1000,netdev=$SOCKNAME,mac=$MAC"
TAPFILE=$ROOT/nfs/net/$TAPID
echo "NAME $NETNAME" > $TAPFILE
echo "IP4 $IP4" >> $TAPFILE
if [ -n "$IP4PEER" ]; then echo "IP4PEER $IP4PEER" >> $TAPFILE; fi
echo "N=\$(ifname \${IFBYMAC[$MAC]})" >>$RCGFILE
echo "IFBYMAC[$MAC]=\$N" >>$RCGFILE
if [ -n "$IP4PEER"]; then
echo "ip4ptp \$N $IP4 $IP4PEER" >>$RCGFILE
else
echo "ip4 \$N $IP4" >>$RCGFILE
fi
done
fi
echo -e "hostname $HOSTNAME\nexport HOSTNAME=$HOSTNAME" > $ROOT/nfs/net/hostname-${HOSTMAC//:}
echo -e "hostname $HOSTNAME\nexport HOSTNAME=$HOSTNAME" > $ROOT/nfs/rc/hostname-${HOSTMAC//:}
$ROOT/bin/omapi add $NAME $HOSTMAC $HOSTIP4
......
#!/bin/bash
UNAME=$(uname)
UNAME=${UNAME,,}
declare -a INTERFACES_OLD
declare -a INTERFACES
GRECOUNT=0
DUMMYCOUNT=100
function dummy() {
DN=$1
case $UNAME in
linux)
ip link add $DN type dummy
ip link set $DN up
ip addr add $2 dev $DN
ip -6 addr add $3 dev $DN
;;
netbsd|openbsd)
ifconfig lo$DUMMYCOUNT create
ifconfig lo$DUMMYCOUNT inet $2
ifconfig lo$DUMMYCOUNT inet6 $3
DUMMYCOUNT=$((DUMMYCOUNT+1))
;;
freebsd)
ifconfig lo$DUMMYCOUNT create
ifconfig lo$DUMMYCOUNT inet $2
ifconfig lo$DUMMYCOUNT inet6 $3
ifconfig lo$DUMMYCOUNT name $DN
DUMMYCOUNT=$((DUMMYCOUNT+1))
;;
*)
echo "Dummy ifaces not implemented yet!"
;;
esac
INTERFACES+=( "$DN;00:00:00:00:00:00" )
}
function gre() {
# Call: gre grename localtunip peertunip localip peerip
GN=$1
case $UNAME in
linux)
ip tunnel add $GN mode gre remote $3 local $2
ip link set $GN up
ip a add $4 peer $5 dev $GN
;;
freebsd)
GIF=gre$GRECOUNT
ifconfig $GIF create
ifconfig $GIF $4 ${5%%/*}
ifconfig $GIF tunnel $2 $3
ifconfig $GIF name $GN
GRECOUNT=$((GRECOUNT+1))
;;
openbsd)
sysctl -w net.inet.gre.allow=1
GIF=gre$GRECOUNT
ifconfig $GIF create
ifconfig $GIF $4 ${5%%/*}
ifconfig $GIF tunnel $2 $3
ifconfig $GIF inet6 eui64 # OpenBSD needs to explicitly add link-local address for IPv6
GN=$GIF
GRECOUNT=$((GRECOUNT+1))
;;
netbsd)
GIF=gre$GRECOUNT
ifconfig $GIF create
ifconfig $GIF $4 ${5%%/*}
ifconfig $GIF tunnel $2 $3
GN=$GIF
GRECOUNT=$((GRECOUNT+1))
;;
*)
echo "GRE tunnels not implemented yet!"
;;
esac
INTERFACES+=( "$GN;00:00:00:00:00:00" )
}
case $UNAME in
linux)
INTERFACES_OLD+=( $( ( ip link ; echo "0: eof" ) | sed -rn '/^[0-9]/{s/^[0-9]+: //;s/:.*//;x;s/\n /,/;p;}; /ether/{s/.*ether (..:..:..:..:..:..).*/ \1/;H;}') )
;;
*)
INTERFACES_OLD+=( $( ( ifconfig ; echo "eof" ) | sed -rn '/^[a-z]/{s/:.*//;x;s/\n /,/;p;}; /(ether|lladdr|address:)/{s/.*(ether|lladdr|address:) (..:..:..:..:..:..).*/ \2/;H;}') )
;;
esac
for I in ${INTERFACES_OLD[@]}; do
NAME=${I%%,*}
MAC=${I##*,}
MAC=${MAC,,}
if [ "$NAME" = "$MAC" ]; then continue; fi
echo "$NAME -> $MAC"
MACFILE=/mnt/nfs/net/vl-${MAC//:}
GREFILE=/mnt/nfs/net/gr-${MAC//:}
if [ -f $MACFILE ]; then
IP4=$(sed -rn 's/^IP4 //p;' < $MACFILE)
IP4PEER=$(sed -rn 's/^IP4PEER //p;' <$MACFILE)
NN=$(sed -rn 's/^NAME //p;' < $MACFILE)
case $UNAME in
linux)
echo "Renaming $NAME to $NN"
ip link set $NAME name $NN
NAME=$NN
echo "Adding $IP4 to $NAME ($MAC)${IP4PEER:+,peer $IP4PEER}"
ip link set $NAME up
ip addr add $IP4 dev $NAME ${IP4PEER:+peer $IP4PEER}
;;
freebsd)
echo "Renaming $NAME to $NN"
ifconfig $NAME name $NN
NAME=$NN
echo "Adding $IP4 to $NAME ($MAC)${IP4PEER:+,peer $IP4PEER}"
ifconfig $NAME up
ifconfig $NAME inet $IP4 ${IP4PEER%%/*}
if [ ! -z "$IP4PEER" ]; then route add ${IP4PEER%%/*} -iface $NAME; fi
;;
openbsd)
echo "Can't rename $NAME to $NN"
ifconfig $NAME up
echo "Adding $IP4 to $NAME ($MAC)${IP4PEER:+,peer $IP4PEER}"
ifconfig $NAME up
ifconfig $NAME inet $IP4 ${IP4PEER%%/*}
if [ ! -z "$IP4PEER" ]; then route add ${IP4PEER%%/*} -iface $NAME; fi
;;
*)
echo "Unknown system $UNAME"
;;
esac
INTERFACES+=( "$NAME,$MAC" )
fi
if [ -f $GREFILE ]; then
. $GREFILE
fi
done
......@@ -2,16 +2,12 @@
export PATH=$PATH:/usr/local/bin:/usr/pkg/bin
. /mnt/nfs/net/rc.local.network
HOSTNAME=$(hostname)
if [ "${HOSTNAME%%-install}" != "$HOSTNAME" ]; then
echo "Install machine. Aborting tests."
exit 0
fi
UNAME=$(uname)
UNAME=${UNAME,,}
case $UNAME in
linux) AUTOCONF=autoconf
linux)
AUTOCONF=autoconf
MAKE=make
;;
freebsd|netbsd)
......@@ -37,6 +33,200 @@ case $UNAME in
;;
esac
if [ -f "/mnt/nfs/net/rc-${HOSTNAME}" ]; then
. /mnt/nfs/net/rc-${HOSTNAME}
declare -a INTERFACES
declare -A IFBYMAC
function dbg {
echo "D: " "$@"
}
function ifup() {
case $UNAME in
linux)
ip link set $1 up
;;
netbsd|openbsd|freebsd)
ifconfig $1 up
;;
esac
}
function ip4() {
case $UNAME in
linux)
ip addr add $2 dev $1
;;
netbsd|openbsd|freebsd)
ifconfig $1 inet $2
;;
esac
}
function ip6() {
case $UNAME in
linux)
ip -6 addr add $2 dev $1
;;
netbsd|openbsd|freebsd)
ifconfig $1 inet6 $2
;;
esac
}
function ip4ptp() {
case $UNAME in
linux)
ip addr add $2 peer $3 dev $1
;;
netbsd|openbsd|freebsd)
ifconfig $1 inet $2 ${3%%/*}
;;
esac
}
function cnt() {
local OUT
if [ ! -r /tmp/${1}count ]; then
OUT=0
else
OUT=$(</tmp/${1}count)
fi
echo $((OUT+1)) > /tmp/${1}count
echo $OUT
}
function grecount() {
cnt gre
}
function locount() {
cnt lo
}
function gre() {
case $UNAME in
linux)
ip tunnel add $1 mode gre remote $3 local $2
ip link set $1 up
echo $1
INTERFACES+=( $1, )
;;
freebsd)
GIF=gre$(grecount)
ifconfig $GIF create
ifconfig $GIF tunnel $2 $3
ifconfig $GIF name $1
echo $1
INTERFACES+=( $1, )
;;
openbsd)
GIF=gre$(grecount)
ifconfig $GIF create
ifconfig $GIF tunnel $2 $3
ifconfig $GIF inet6 eui64 # OpenBSD needs to explicitly add link-local address for IPv6
ifconfig $GIF description $1
echo $GIF
INTERFACES+=( $GIF, )
;;
netbsd)
GIF=gre$(grecount)
ifconfig $GIF create
ifconfig $GIF tunnel $2 $3
echo $GIF
INTERFACES+=( $GIF, )
;;
esac
}
function dummy() {
case $UNAME in
linux)
ip link add $1 type dummy
ip link set $1 up
echo $1
INTERFACES+=( $1, )
;;
netbsd)
LIF=lo$(locount)
ifconfig $LIF create
echo $LIF
INTERFACES+=( $LIF, )
;;
openbsd)
LIF=lo$(locount)
ifconfig $LIF create
ifconfig $LIF description $1
echo $LIF
INTERFACES+=( $LIF, )
;;
freebsd)
LIF=lo$(locount)
ifconfig $LIF create
ifconfig $LIF name $1
echo $1
INTERFACES+=( $1, )
;;
esac
}
function ifname() {
case $UNAME in
linux)
ip link set $1 name $2
echo $2
;;
freebsd)
ifconfig $1 name $2
echo $2
;;
openbsd)
ifconfig $1 description $2
echo $1
;;
netbsd)
echo $1
;;
esac
}
# Get list of physical interfaces, together with their MACs.
function ifaces() {
case $UNAME in
linux)
( ip link ; echo "0: eof" ) | sed -rn '/^[0-9]/{s/^[0-9]+: //;s/:.*//;x;s/\n /,/;p;}; /ether/{s/.*ether (..:..:..:..:..:..).*/ \1/;H;}' | grep ','
;;
*)
( ifconfig ; echo "eof" ) | sed -rn '/^[a-z]/{s/:.*//;x;s/\n /,/;p;}; /(ether|lladdr|address:)/{s/.*(ether|lladdr|address:) (..:..:..:..:..:..).*/ \2/;H;}' | grep ','
;;
esac
}
for I in $(ifaces); do
NAME=${I%%,*}
MAC=${I##*,}
MAC=${MAC,,}
HOSTNAMEFILE=/mnt/nfs/rc/hostname-${MAC//:}
if [ -f $HOSTNAMEFILE ]; then
. $HOSTNAMEFILE
fi
done
for I in $(ifaces); do
NAME=${I%%,*}
MAC=${I##*,}
MAC=${MAC,,}
IFBYMAC[$MAC]=$NAME
INTERFACES+=( $I )
done
RCGFILE=/mnt/nfs/rc/rc-gen-$HOSTNAME
if [ -f $RCGFILE ]; then
. $RCGFILE
fi
RCFILE=/mnt/nfs/rc/rc-loc-$HOSTNAME
if [ -f $RCFILE ]; then
. $RCFILE
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment