meta-google: gbmc-bridge: Rework IP address persistence

This consolidates all of the mechanisms which write out a persistent IP
into a single place. It also transitions to writing a very simple
persistent file instead of systemd style network units.

Change-Id: Ib99d7646178d2c5383cf23b09248bf24544c1d9e
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-dhcp.sh b/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-dhcp.sh
index 19fa7b1..9c61036 100644
--- a/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-dhcp.sh
+++ b/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-dhcp.sh
@@ -15,27 +15,20 @@
 
 # A list of functions which get executed for each bound DHCP lease.
 # These are configured by the files included below.
+# Shellcheck does not understand how this gets referenced
+# shellcheck disable=SC2034
 GBMC_BR_DHCP_HOOKS=()
 
-# Load configurations from a known location in the filesystem to populate
-# hooks that are executed after each event.
-shopt -s nullglob
-for conf in /usr/share/gbmc-br-dhcp/*.sh; do
-  # SC doesn't like dynamic source loading
-  # shellcheck disable=SC1090
-  source "$conf"
-done
-
-gbmc_br_dhcp_run_hooks() {
-  local hook
-  for hook in "${GBMC_BR_DHCP_HOOKS[@]}"; do
-    "$hook" || return
-  done
-}
-
 # SC can't find this path during repotest
 # shellcheck disable=SC1091
 source /usr/share/network/lib.sh || exit
+# SC can't find this path during repotest
+# shellcheck disable=SC1091
+source /usr/share/gbmc-br-lib.sh || exit
+
+# Load configurations from a known location in the filesystem to populate
+# hooks that are executed after each event.
+gbmc_br_source_dir /usr/share/gbmc-br-dhcp || exit
 
 # Write out the current PID and cleanup when complete
 trap 'rm -f /run/gbmc-br-dhcp.pid' EXIT
@@ -51,57 +44,25 @@
   # Ensure we are a BMC and have a suffix nibble, the 0th index is reserved
   if (( pfx_bytes[8] != 0xfd || pfx_bytes[9] & 0xf == 0 )); then
     echo "Invalid address" >&2
-    exit
+    exit 1
   fi
   # Ensure we don't have more than a /80 address
   for (( i = 10; i < 16; ++i )); do
     if (( pfx_bytes[i] != 0 )); then
       echo "Invalid address" >&2
-      exit
+      exit 1
     fi
   done
 
   pfx="$(ip_bytes_to_str pfx_bytes)"
-  (( pfx_bytes[9] &= 0xf0 ))
-  stateless_pfx="$(ip_bytes_to_str pfx_bytes)"
-  read -r -d '' contents <<EOF
-[Network]
-Address=$pfx/128
-IPv6PrefixDelegation=yes
-[IPv6PrefixDelegation]
-RouterLifetimeSec=60
-[IPv6Prefix]
-Prefix=$stateless_pfx/80
-PreferredLifetimeSec=60
-ValidLifetimeSec=60
-[IPv6RoutePrefix]
-Route=$pfx/80
-LifetimeSec=60
-[Route]
-Destination=$stateless_pfx/76
-Type=unreachable
-Metric=1024
-EOF
-
-  for file in /etc/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf; do
-    mkdir -p "$(dirname "$file")"
-    printf '%s' "$contents" >"$file"
-  done
-
-  # Ensure that systemd-networkd performs a reconfiguration as it doesn't
-  # currently check the mtime of drop-in files.
-  touch -c /lib/systemd/network/*-bmc-gbmcbr.network
-
-  if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
-    networkctl reload && networkctl reconfigure gbmcbr
-  fi
+  gbmc_br_set_ip "$pfx" || exit
 
   if [ -n "${fqdn-}" ]; then
     echo "Using hostname $fqdn" >&2
     hostnamectl set-hostname "$fqdn" || true
   fi
 
-  gbmc_br_dhcp_run_hooks || exit
+  gbmc_br_run_hooks GBMC_BR_DHCP_HOOKS || exit
 
   # Ensure that the installer knows we have completed processing DHCP by
   # running a service that reports completion