meta-google: gbmc-ncsi-config: Remove dhcp-relay from small images

We provide a new variable called `GBMC_NCSI_DHCP_RELAY` to indicate
whether or not our image should support DHCP relaying. By default as a
heuristic, we use the FLASH_SIZE for the value as only 64MB and bigger
images will need it in practice.

In the future it will be tuned per-platform to be enabled only where
needed and eventually disabled by default.

Change-Id: I08bc07074cada79fcf7b18522d1219ffcc14d1be
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/meta-google/recipes-google/ncsi/files/gbmc-ncsi-smartnic-wa.sh.in b/meta-google/recipes-google/ncsi/files/gbmc-ncsi-smartnic-wa.sh.in
index 533888c..88f52d3 100644
--- a/meta-google/recipes-google/ncsi/files/gbmc-ncsi-smartnic-wa.sh.in
+++ b/meta-google/recipes-google/ncsi/files/gbmc-ncsi-smartnic-wa.sh.in
@@ -13,15 +13,19 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-override=/run/systemd/system/gbmc-ncsi-dhcrelay.service.d/10-nosp.conf
-mkdir -p "$(dirname "$override")"
-echo '[Service]' >"$override"
-echo 'ExecStart=' >>"$override"
-# Remove the source relay port from the relay, bringing it back to run on
-# the default port 547. Our relays don't support the source port option needed
-# to run on 3967 for our legacy NICs.
-grep '^ExecStart=' /lib/systemd/system/gbmc-ncsi-dhcrelay.service | \
-  sed 's, -rp 3967,,' >>"$override"
+HAS_DHCRELAY='@GBMC_NCSI_DHCP_RELAY@'
+
+if [ "$HAS_DHCRELAY" = 1 ]; then
+  override=/run/systemd/system/gbmc-ncsi-dhcrelay.service.d/10-nosp.conf
+  mkdir -p "$(dirname "$override")"
+  echo '[Service]' >"$override"
+  echo 'ExecStart=' >>"$override"
+  # Remove the source relay port from the relay, bringing it back to run on
+  # the default port 547. Our relays don't support the source port option needed
+  # to run on 3967 for our legacy NICs.
+  grep '^ExecStart=' /lib/systemd/system/gbmc-ncsi-dhcrelay.service | \
+    sed 's, -rp 3967,,' >>"$override"
+fi
 
 override=/run/systemd/system/gbmc-br-dhcp.service.d/10-direct.conf
 mkdir -p "$(dirname "$override")"
@@ -33,8 +37,10 @@
   sed 's, -i gbmcbr, -i @NCSI_IF@,' >>"$override"
 
 systemctl daemon-reload
-systemctl reset-failed gbmc-ncsi-dhcrelay
-systemctl restart --no-block gbmc-ncsi-dhcrelay
+if [ "$HAS_DHCRELAY" = 1 ]; then
+  systemctl reset-failed gbmc-ncsi-dhcrelay
+  systemctl restart --no-block gbmc-ncsi-dhcrelay
+fi
 systemctl reset-failed gbmc-br-dhcp
 systemctl restart --no-block gbmc-br-dhcp
 
diff --git a/meta-google/recipes-google/ncsi/gbmc-ncsi-config.bb b/meta-google/recipes-google/ncsi/gbmc-ncsi-config.bb
index 607f1b7..217a932 100644
--- a/meta-google/recipes-google/ncsi/gbmc-ncsi-config.bb
+++ b/meta-google/recipes-google/ncsi/gbmc-ncsi-config.bb
@@ -5,16 +5,17 @@
 
 inherit systemd
 
+GBMC_NCSI_DHCP_RELAY ??= "${@'' if int(d.getVar('FLASH_SIZE')) < 65536 else '1'}"
 GBMC_NCSI_IF_OLD ??= ""
 GBMC_NCSI_PURGE_ETC ??= ""
 
 SRC_URI += " \
-  file://-bmc-gbmcbrncsidhcp.netdev \
-  file://-bmc-gbmcbrncsidhcp.network \
-  file://-bmc-gbmcncsidhcp.netdev \
-  file://-bmc-gbmcncsidhcp.network \
+  ${@'' if d.getVar('GBMC_NCSI_DHCP_RELAY') != '1' else 'file://-bmc-gbmcbrncsidhcp.netdev'} \
+  ${@'' if d.getVar('GBMC_NCSI_DHCP_RELAY') != '1' else 'file://-bmc-gbmcbrncsidhcp.network'} \
+  ${@'' if d.getVar('GBMC_NCSI_DHCP_RELAY') != '1' else 'file://-bmc-gbmcncsidhcp.netdev'} \
+  ${@'' if d.getVar('GBMC_NCSI_DHCP_RELAY') != '1' else 'file://-bmc-gbmcncsidhcp.network'} \
   file://50-gbmc-ncsi.rules.in \
-  file://gbmc-ncsi-dhcrelay.service.in \
+  ${@'' if d.getVar('GBMC_NCSI_DHCP_RELAY') != '1' else 'file://gbmc-ncsi-dhcrelay.service.in'} \
   file://gbmc-ncsi-ip-from-ra.service.in \
   file://gbmc-ncsi-ip-from-ra.sh.in \
   file://gbmc-ncsi-smartnic-wa.sh.in \
@@ -34,7 +35,7 @@
 
 RDEPENDS:${PN} += " \
   bash \
-  dhcp-relay \
+  ${@'' if d.getVar('GBMC_NCSI_DHCP_RELAY') != '1' else 'dhcp-relay'} \
   gbmc-ip-monitor \
   ncsid \
   network-sh \
@@ -50,7 +51,7 @@
   "
 
 SYSTEMD_SERVICE:${PN} += " \
-  gbmc-ncsi-dhcrelay.service \
+  ${@'' if d.getVar('GBMC_NCSI_DHCP_RELAY') != '1' else 'gbmc-ncsi-dhcrelay.service'} \
   gbmc-ncsi-sslh.service \
   gbmc-ncsi-sslh.socket \
   gbmc-ncsi-set-nicenabled.service \
@@ -72,15 +73,17 @@
   echo "net.ipv6.conf.$if_name.dad_transmits=0" \
     >>${D}${sysconfdir}/sysctl.d/25-gbmc-ncsi.conf
 
-  install -d -m0755 ${D}${systemd_unitdir}/network
-  install -m0644 ${WORKDIR}/-bmc-gbmcbrncsidhcp.netdev \
-    ${D}${systemd_unitdir}/network/
-  install -m0644 ${WORKDIR}/-bmc-gbmcbrncsidhcp.network \
-    ${D}${systemd_unitdir}/network/
-  install -m0644 ${WORKDIR}/-bmc-gbmcncsidhcp.netdev \
-    ${D}${systemd_unitdir}/network/
-  install -m0644 ${WORKDIR}/-bmc-gbmcncsidhcp.network \
-    ${D}${systemd_unitdir}/network/
+  if [ "${GBMC_NCSI_DHCP_RELAY}" = 1 ]; then
+    install -d -m0755 ${D}${systemd_unitdir}/network
+    install -m0644 ${WORKDIR}/-bmc-gbmcbrncsidhcp.netdev \
+      ${D}${systemd_unitdir}/network/
+    install -m0644 ${WORKDIR}/-bmc-gbmcbrncsidhcp.network \
+      ${D}${systemd_unitdir}/network/
+    install -m0644 ${WORKDIR}/-bmc-gbmcncsidhcp.netdev \
+      ${D}${systemd_unitdir}/network/
+    install -m0644 ${WORKDIR}/-bmc-gbmcncsidhcp.network \
+      ${D}${systemd_unitdir}/network/
+  fi
 
   netdir=${D}${systemd_unitdir}/network/00-bmc-$if_name.network.d
   install -d -m0755 "$netdir"
@@ -132,8 +135,10 @@
   sed "s,@NCSI_IF@,$if_name,g" ${WORKDIR}/gbmc-ncsi-set-nicenabled.service.in \
     >${D}${systemd_system_unitdir}/gbmc-ncsi-set-nicenabled.service
 
-  sed "s,@NCSI_IF@,$if_name,g" ${WORKDIR}/gbmc-ncsi-dhcrelay.service.in \
-    >${D}${systemd_system_unitdir}/gbmc-ncsi-dhcrelay.service
+  if [ "${GBMC_NCSI_DHCP_RELAY}" = "1" ]; then
+    sed "s,@NCSI_IF@,$if_name,g" ${WORKDIR}/gbmc-ncsi-dhcrelay.service.in \
+      >${D}${systemd_system_unitdir}/gbmc-ncsi-dhcrelay.service
+  fi
 
   if [ -n "${GBMC_NCSI_IF_OLD}" ]; then
     sed -e "s,@NCSI_IF@,$if_name,g" -e "s,@OLD_IF@,${GBMC_NCSI_IF_OLD},g" ${WORKDIR}/gbmc-ncsi-old.service.in \
@@ -153,8 +158,8 @@
   install -d -m0755 ${D}${libexecdir}
   install -m0755 ${WORKDIR}/gbmc-ncsi-ip-from-ra.sh ${D}${libexecdir}/
 
-  sed "s,@NCSI_IF@,$if_name,g" ${WORKDIR}/gbmc-ncsi-smartnic-wa.sh.in \
-    >${WORKDIR}/gbmc-ncsi-smartnic-wa.sh
+  sed -e "s,@NCSI_IF@,$if_name,g" -e "s,@GBMC_NCSI_DHCP_RELAY@,${GBMC_NCSI_DHCP_RELAY},g" \
+    ${WORKDIR}/gbmc-ncsi-smartnic-wa.sh.in >${WORKDIR}/gbmc-ncsi-smartnic-wa.sh
   install -d -m0755 ${D}${bindir}
   install -m0755 ${WORKDIR}/gbmc-ncsi-smartnic-wa.sh ${D}${bindir}/
 }