#!/bin/sh # # Generate s6 network definitions set -ex header_eb() { echo '#!/bin/execlineb -P' } header_if() { local if="${1:?missing if}" echo "define INTERFACE $if" } header_addr() { local addr="${1:?missing addr}" echo "define ADDR $addr" } header_fam() { local fam="${1:?missing family}" echo "define FAMILY $fam" } header_vlan() { local vlan="${1:?missing vlan}" local parent="${2:?missing parent}" echo "define VLAN $vlan" echo "define PARENT $parent" } header_vrf() { local vrf="${1:?missing vrf}" echo "define VRF $vrf" } header_prop() { local prop="${1:?missing prop}" echo "define PROP $prop" } header_path() { cat </dev/null 2>&1 && echo "$addr" | grep -v ':' >/dev/null 2>&1; then fam='4' fi echo "$fam" } linkdel() { local if="${1:?missing if}" header_eb header_path header_if "$if" echo 'ip link del $INTERFACE' } depends() { echo "$@" local svcname="${1:?missing sname}" shift 1 local sname # God i fucking hate global contexts by default for sname in $@; do install -Dm644 /dev/null rc/"$svcname"/dependencies.d/"$sname" done } contains() { local svcname="${1:?missing sname}" shift 1 local sname # God i fucking hate global contexts by default for sname in $@; do install -Dm644 /dev/null rc/"$svcname"/contents.d/"$sname" done } # # Bundle management # bundle_deps() { local if="${1:?missing if}" local svcname="bundle.interface.$if" [ -r "rc/$svcname/type" ] || install -Dm644 <(echo bundle) rc/"$svcname"/type shift 1 contains "$svcname" $@ } # Currently used stages # > configure # - create # - enslave # - configure (anything that must be configured before interface is up) # > ready # - link # - configure (anything that may be configured after interface is up) bundle_stage_create() { local if="${1:?missing if}" local stage="${2:?missing stage}" local svcname="bundle.interface.$if.$stage" [ -r "rc/$svcname/type" ] || install -Dm644 <(echo bundle) rc/"$svcname"/type # Add to parent bundle bundle_deps "$if" "$svcname" shift 2 contains "$svcname" $@ } bundle_stage_depend_on() { local if="${1:?missing if}" local stage="${2:?missing stage}" local svcname="bundle.interface.$if.$stage" shift 2 local sname for sname in $@; do depends "$sname" "$svcname" done } bundle_stage_step_configure() { local if="$1" local stage="configure" shift 1 bundle_stage_create "$if" "$stage" $@ } bundle_stage_step_ready() { local if="$1" local stage="ready" shift 1 bundle_stage_create "$if" "$stage" $@ } # # Interface creation # new_link() { local if="${1:?missing if}" local sname="interface.$if.link" bundle_stage_step_ready "$if" "$sname" install -Dm644 <( header_eb header_path header_if "$if" echo 'ip link set dev $INTERFACE up' ) rc/"$sname"/up install -Dm644 <( header_eb header_path header_if "$if" echo 'ip link set dev $INTERFACE down' ) rc/"$sname"/down install -Dm644 <(echo oneshot) rc/"$sname"/type bundle_stage_depend_on "$if" "configure" "$sname" } new_if_bridge() { local if="${1:?missing if}" local sname="interface.$if.create" bundle_stage_step_configure "$if" "$sname" new_link "$if" install -Dm644 <( header_eb header_path header_if "$if" shift 1 echo 'ip link add $INTERFACE type bridge' ) rc/"$sname"/up install -Dm644 <(linkdel "$if") rc/"$sname"/down install -Dm644 <(echo oneshot) rc/"$sname"/type depends "$sname" module.bridge } new_if_phys() { local if="${1:?missing if}" local sname="interface.$if.create" bundle_stage_step_configure "$if" "$sname" new_link "$if" install -Dm644 <( header_eb header_path header_if "$if" echo 'bcnm-waitif 1 $INTERFACE' ) rc/"$sname"/up install -Dm644 <(echo oneshot) rc/"$sname"/type depends "$sname" mdevd.coldplug } new_if_wg() { local if="${1:?missing if}" # Main service for creating interface local sname="interface.$if.create" bundle_stage_step_configure "$if" "$sname" install -Dm644 <( header_eb header_path header_if "$if" echo 'ip link add $INTERFACE type wireguard' ) rc/"$sname"/up install -Dm644 <(linkdel "$if") rc/"$sname"/down install -Dm644 <(echo oneshot) rc/"$sname"/type depends "$sname" module.wireguard new_link "$if" # Secondary service for loading config if_wg_conf "$if" } new_if_vrf() { local if="${1:?missing if}" local table="${2:?missing table}" local sname="interface.$if.create" bundle_stage_step_configure "$if" "$sname" install -Dm644 <( header_eb header_path header_if "$if" cat <