From 840129097631611fe110bbe01b1707f9db000865 Mon Sep 17 00:00:00 2001
From: Riccardo Pittau <elfosardo@gmail.com>
Date: Tue, 8 Feb 2022 10:58:06 +0100
Subject: [PATCH] Fallback to persistent netifs names with systemd

The dhcp-all-interfaces element does not work with the predictable names
scheme, fallback to the persistent names scheme as workaround.

Bug: 1960301

Change-Id: I117964a60615a5b7e9984f52f02cd018d1a48ed0
---
 .../install.d/50-dhcp-all-interfaces                 |  1 +
 .../install.d/udev-net-setup-link.rules              |  0
 .../persistent-netifs-fallback-1b861e037dd8a27c.yaml | 12 ++++++++++++
 3 files changed, 13 insertions(+)
 create mode 100644 diskimage_builder/elements/dhcp-all-interfaces/install.d/udev-net-setup-link.rules
 create mode 100644 releasenotes/notes/persistent-netifs-fallback-1b861e037dd8a27c.yaml

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..ee8de0c95 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
@@ -64,6 +64,7 @@ elif [ "$DIB_INIT_SYSTEM" == "systemd" ]; then
     install -D -g root -o root -m 0644 ${SCRIPTDIR}/dhcp-interface@.service /usr/lib/systemd/system/dhcp-interface@.service
     install -D -g root -o root -m 0644 ${SCRIPTDIR}/dhcp-all-interfaces-udev.rules /etc/udev/rules.d/99-dhcp-all-interfaces.rules
     sed -i "s/TimeoutStartSec=DIB_DHCP_TIMEOUT/TimeoutStartSec=$(( ${DIB_DHCP_TIMEOUT:-30} * 2 ))s/" /usr/lib/systemd/system/dhcp-interface@.service
+    install -D -g root -o root -m 0644 ${SCRIPTDIR}/udev-net-setup-link.rules /etc/udev/rules.d/80-net-setup-link.rules
 elif [ "$DIB_INIT_SYSTEM" == "sysv" ]; then
     install -D -g root -o root -m 0755 ${SCRIPTDIR}/dhcp-all-interfaces.init /etc/init.d/dhcp-all-interfaces
     update-rc.d dhcp-all-interfaces defaults
diff --git a/diskimage_builder/elements/dhcp-all-interfaces/install.d/udev-net-setup-link.rules b/diskimage_builder/elements/dhcp-all-interfaces/install.d/udev-net-setup-link.rules
new file mode 100644
index 000000000..e69de29bb
diff --git a/releasenotes/notes/persistent-netifs-fallback-1b861e037dd8a27c.yaml b/releasenotes/notes/persistent-netifs-fallback-1b861e037dd8a27c.yaml
new file mode 100644
index 000000000..21fd21907
--- /dev/null
+++ b/releasenotes/notes/persistent-netifs-fallback-1b861e037dd8a27c.yaml
@@ -0,0 +1,12 @@
+---
+fixes:
+  - |
+    The dhcp-all-interfaces element does not work correctly with the new
+    "predictable names" scheme for network interfaces.
+    During boot the name of the interfaces is changed from ethX to the new
+    scheme, for example enpXsX; this name change is not detected by udev de
+    facto making the udev rule of the dhcp-all-interfaces element useless;
+    this causes the machine booted with the image including the dhcp-all-interfaces
+    element to not get any ip and so being unreachable from the network.
+    This patch includes a workaround to fallback to the "persistent names"
+    scheme allowing the dhcp-all-interfaces element to work again as intended.