From e7c52139aa82b4add0d007c23ec5eb4ca7ca2695 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Harald=20Jens=C3=A5s?= <hjensas@redhat.com>
Date: Sun, 23 Jan 2022 23:09:11 +0100
Subject: [PATCH] dhcp-all-interfaces: opt let NetworkManager doit.

NetworkManager is quite capable to do automatic
interface configuration. NetworkManager will by default
try to auto-configure any interface with no configuration.
It will use DHCP for IPv4 and Router Advertisements to
decide how to initialize IPv6.

It will most likely do it just as good, or better than the
dhcp-all-interfaces.sh script.

Since dhcp-all-interfaces clean out all ifcfg files in
60-remove-cloud-image-interfaces it means NetworkManager will
by default attempt auto configuration for all interfaces.

This change add's and environment variable:
  DIB_DHCP_NETWORK_MANAGER_AUTO (default: false)

When DIB_DHCP_NETWORK_MANAGER_AUTO is set to `true` only the
NetworkManager config will be written. The dhcp-all-interfaces
service will not be installed. Hence dhcp-all-interfaces will
not write any config files, allowing NetworkManager to just do
it's thing.

Change-Id: Id6f8d6aaaf52a78175bb6c065ec88274c364834e
---
 .../elements/dhcp-all-interfaces/README.rst   | 11 +++++++++
 .../install.d/50-dhcp-all-interfaces          | 24 ++++++++++++-------
 ...orkManager-handle-it-f7c7f2d033b7f92d.yaml | 15 ++++++++++++
 3 files changed, 42 insertions(+), 8 deletions(-)
 create mode 100644 releasenotes/notes/dhcp-all-interfaces-add-opt-to-let-NetworkManager-handle-it-f7c7f2d033b7f92d.yaml

diff --git a/diskimage_builder/elements/dhcp-all-interfaces/README.rst b/diskimage_builder/elements/dhcp-all-interfaces/README.rst
index 4c6c7353d..a11a4cb38 100644
--- a/diskimage_builder/elements/dhcp-all-interfaces/README.rst
+++ b/diskimage_builder/elements/dhcp-all-interfaces/README.rst
@@ -28,3 +28,14 @@ DIB_DHCP_TIMEOUT
    will wait to get an address. Should be increased in networks such as
    Infiniband.
   :Example: DIB_DHCP_TIMEOUT=300
+
+DIB_DHCP_NETWORK_MANAGER_AUTO
+  :Required: No
+  :Default: false
+  :Description: When NetworkManager is detected, and this is set to true the
+   dhcp-all-interfaces service will not be installed. Only the NetworkManager
+   configuration will be added. NetworkManager is quite capable to do automatic
+   interface configuration. NetworkManager will by default try to
+   auto-configure any interface with no configuration, it will use DHCP for
+   IPv4 and Router Advertisements to decide how to initialize IPv6.
+  :Example: DIB_DHCP_NETWORK_MANAGER_AUTO=true
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 9b7223117..2b6e4ce6a 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
@@ -9,14 +9,6 @@ set -o pipefail
 SCRIPTDIR=$(dirname $0)
 
 if [ -e "/etc/redhat-release" ]; then
-    # TODO(hjensas): Once ndisc6 package is available in EPEL8 drop this,
-    # and add the package back in pkg-map.
-    # See: https://bugzilla.redhat.com/show_bug.cgi?id=1779134
-    if type dnf &>/dev/null; then
-        dnf -v -y install ndisc6 || true
-    else
-        yum -v -y install ndisc6 || true
-    fi
     # NOTE(TheJulia): While the internal client which every networkmanager user
     # should have is internal, dhclient has better behavior which includes
     # shutting down the port between retries which is critical for recovery
@@ -31,6 +23,22 @@ if [ -x "/etc/NetworkManager" ]; then
     install -D -g root -o root -m 0644 ${SCRIPTDIR}/NetworkManager-conf.d-00-main.conf /etc/NetworkManager/conf.d/00-main.conf
     sed -i "s/DIB_DHCP_CLIENT/${DIB_DHCP_CLIENT:-internal}/g" /etc/NetworkManager/conf.d/00-main.conf
     sed -i "s/DIB_DHCP_TIMEOUT/${DIB_DHCP_TIMEOUT:-30}/g" /etc/NetworkManager/conf.d/00-main.conf
+    if [ "${DIB_DHCP_NETWORK_MANAGER_AUTO:-false}" == true ]; then
+        # Use NetworkManager for auto configuration, it will behave just as
+        # good as dhcp-all-interfaces.sh in most cases.
+        exit 0
+    fi
+fi
+
+if [ -e "/etc/redhat-release" ]; then
+    # TODO(hjensas): Once ndisc6 package is available in EPEL8 drop this,
+    # and add the package back in pkg-map.
+    # See: https://bugzilla.redhat.com/show_bug.cgi?id=1779134
+    if type dnf &>/dev/null; then
+        dnf -v -y install ndisc6 || true
+    else
+        yum -v -y install ndisc6 || true
+    fi
 fi
 
 
diff --git a/releasenotes/notes/dhcp-all-interfaces-add-opt-to-let-NetworkManager-handle-it-f7c7f2d033b7f92d.yaml b/releasenotes/notes/dhcp-all-interfaces-add-opt-to-let-NetworkManager-handle-it-f7c7f2d033b7f92d.yaml
new file mode 100644
index 000000000..de2039855
--- /dev/null
+++ b/releasenotes/notes/dhcp-all-interfaces-add-opt-to-let-NetworkManager-handle-it-f7c7f2d033b7f92d.yaml
@@ -0,0 +1,15 @@
+---
+features:
+  - |
+    It is now possible to configure the dhcp-all-interfaces element not to
+    install the dhcp-all-interfaces service on systems with NetworkManager.
+
+    NetworkManager is quite capable to do automatic interface configuration.
+    NetworkManager will by default try to auto-configure any interface with
+    no configuration, it will use DHCP for IPv4 and Router Advertisements to
+    decide how to initialize IPv6.
+
+    The new environment variable ``DIB_DHCP_NETWORK_MANAGER_AUTO`` (defauls to:
+    ``false``) has been added, when set to ``true`` only the configuration for
+    NetworkManager is written.
+