Blob Blame History Raw
From c9fd1e80d3c87e1f844edcd86e9d36ae499ce717 Mon Sep 17 00:00:00 2001
From: Gris Ge <fge@redhat.com>
Date: Sun, 23 Apr 2023 16:01:29 +0800
Subject: [PATCH] cli: Do nothing for `persist-nic-names` when got
 `net.ifnames=0`

When `net.ifnames=0` is defined in kernel argument, systemd will disable
the predicable network interface name which make no sense for nmstate to
pin the interface names. Hence we do nothing in `persist-nic-names` in
that case.

Manually tested in CentOS stream 9 VM.

Signed-off-by: Gris Ge <fge@redhat.com>
---
 rust/src/cli/persist_nic.rs | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/rust/src/cli/persist_nic.rs b/rust/src/cli/persist_nic.rs
index 6b126d58..61b07dbb 100644
--- a/rust/src/cli/persist_nic.rs
+++ b/rust/src/cli/persist_nic.rs
@@ -7,6 +7,8 @@
 //!
 //! The logic currently is:
 //!
+//!  - Do nothing if kernel argument contains `net.ifnames=0` which disabled the
+//!    predicable network interface name, hence not fit our use case here.
 //!  - Iterate over all active NICs
 //!  - Pin every ethernet interface to its MAC address (prefer permanent MAC
 //!    address)
@@ -70,6 +72,14 @@ pub(crate) fn run_persist_immediately(
         PersistAction::CleanUpDryRun => return clean_up(root, true),
     };
 
+    if is_prediable_ifname_disabled() {
+        log::info!(
+            "Systemd predicable network interface name is disabled \
+            by kernel argument `net.ifnames=0`, will do nothing"
+        );
+        return Ok("".to_string());
+    }
+
     let stamp_path = Path::new(root)
         .join(SYSTEMD_NETWORK_LINK_FOLDER)
         .join(NMSTATE_PERSIST_STAMP);
@@ -317,3 +327,12 @@ fn is_nmstate_generated_systemd_link_file(file_path: &PathBuf) -> bool {
         .map(|_| buff == PERSIST_GENERATED_BY.as_bytes())
         .unwrap_or_default()
 }
+
+const KERNEL_CMDLINE_FILE: &str = "/proc/cmdline";
+
+fn is_prediable_ifname_disabled() -> bool {
+    std::fs::read(KERNEL_CMDLINE_FILE)
+        .map(|v| String::from_utf8(v).unwrap_or_default())
+        .map(|c| c.contains("net.ifnames=0"))
+        .unwrap_or_default()
+}
-- 
2.40.0