From f249cec9f3c897f72b9bc3e69b1310714819e192 Mon Sep 17 00:00:00 2001
From: Lenny Verkhovsky <lennyb@mellanox.com>
Date: Sun, 24 Dec 2017 12:57:56 +0000
Subject: [PATCH] Checking link status according to DIB_DHCP_TIMEOUT

In slow networks like Infiniband it takes much time for the
interface to get the carrier. This patch enables this service
to run more then 20 seconds and limited by DIB_DHCP_TIMEOUT.

Change-Id: I8a6015567ac25e37b5a5aba4b1fda71170cc144a
---
 diskimage_builder/elements/dhcp-all-interfaces/README.rst     | 2 +-
 .../dhcp-all-interfaces/install.d/50-dhcp-all-interfaces      | 1 +
 .../dhcp-all-interfaces/install.d/dhcp-all-interfaces.sh      | 4 ++--
 3 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/diskimage_builder/elements/dhcp-all-interfaces/README.rst b/diskimage_builder/elements/dhcp-all-interfaces/README.rst
index 7ecd387c7..eb56a378d 100644
--- a/diskimage_builder/elements/dhcp-all-interfaces/README.rst
+++ b/diskimage_builder/elements/dhcp-all-interfaces/README.rst
@@ -25,5 +25,5 @@ DIB_DHCP_TIMEOUT
   :Required: No
   :Default: 30
   :Description: Amount of time in seconds that the systemd service will
-   wait to get an address.
+   wait to get an address. Should be increased in networks such as Infiniband.
   :Example: DIB_DHCP_TIMEOUT=300
diff --git a/diskimage_builder/elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces b/diskimage_builder/elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces
index 121f8d28c..25018929f 100755
--- a/diskimage_builder/elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces
+++ b/diskimage_builder/elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces
@@ -11,6 +11,7 @@ SCRIPTDIR=$(dirname $0)
 # this script is not needed on Gentoo.
 if [ "$DISTRO_NAME" != "gentoo" ]; then
     install -D -g root -o root -m 0755 ${SCRIPTDIR}/dhcp-all-interfaces.sh /usr/local/sbin/dhcp-all-interfaces.sh
+    sed -i "s/DIB_DHCP_TIMEOUT/${DIB_DHCP_TIMEOUT:-30}/" /usr/local/sbin/dhcp-all-interfaces.sh
 fi
 
 if [ "$DIB_INIT_SYSTEM" == "upstart" ]; then
diff --git a/diskimage_builder/elements/dhcp-all-interfaces/install.d/dhcp-all-interfaces.sh b/diskimage_builder/elements/dhcp-all-interfaces/install.d/dhcp-all-interfaces.sh
index 7e29e0e71..25113b710 100755
--- a/diskimage_builder/elements/dhcp-all-interfaces/install.d/dhcp-all-interfaces.sh
+++ b/diskimage_builder/elements/dhcp-all-interfaces/install.d/dhcp-all-interfaces.sh
@@ -97,8 +97,8 @@ function inspect_interface() {
         echo "Device has generated MAC, skipping."
     else
         local has_link
-        local tries
-        for ((tries = 0; tries < 20; tries++)); do
+        local tries=DIB_DHCP_TIMEOUT
+        for ((; tries > 0; tries--)); do
             # Need to set the link up on each iteration
             ip link set dev $interface up &>/dev/null
             has_link=$(get_if_link $interface)