From 4fc0b3e09ad892e2b2da975b6786998729511ac6 Mon Sep 17 00:00:00 2001 From: Gris Ge Date: Sun, 23 Apr 2023 17:37:00 +0800 Subject: [PATCH 1/2] dep: Upgrade to nix 0.26.2 Signed-off-by: Gris Ge --- rust/src/lib/Cargo.toml | 2 +- rust/src/lib/nispor/hostname.rs | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/rust/src/lib/Cargo.toml b/rust/src/lib/Cargo.toml index 29fe4c34..1a4e96c4 100644 --- a/rust/src/lib/Cargo.toml +++ b/rust/src/lib/Cargo.toml @@ -55,7 +55,7 @@ default-features = false features = ["derive"] [dependencies.nix] -version = "0.24.1" +version = "0.26.2" optional = true default-features = false features = ["feature", "hostname"] diff --git a/rust/src/lib/nispor/hostname.rs b/rust/src/lib/nispor/hostname.rs index 517bf7c7..b96fb3f3 100644 --- a/rust/src/lib/nispor/hostname.rs +++ b/rust/src/lib/nispor/hostname.rs @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 + use std::io::Read; use crate::{ErrorKind, HostNameState, NmstateError}; @@ -5,17 +7,16 @@ use crate::{ErrorKind, HostNameState, NmstateError}; const HOST_NAME_MAX: usize = 64; pub(crate) fn get_hostname_state() -> Option { - let mut buffer = [0u8; HOST_NAME_MAX]; - let running = match nix::unistd::gethostname(&mut buffer) { - Ok(hostname_cstr) => match hostname_cstr.to_str() { - Ok(h) => Some(h.to_string()), - Err(e) => { - log::error!("Failed to convert hostname to String: {}", e); + let running = match nix::unistd::gethostname() { + Ok(hostname_cstr) => match hostname_cstr.into_string() { + Ok(h) => Some(h), + Err(s) => { + log::error!("Failed to convert hostname to String: {:?}", s); None } }, Err(e) => { - log::error!("Failed to get hostname: {}", e); + log::error!("Failed to get hostname {}", e); None } }; -- 2.40.0 From c9fd1e80d3c87e1f844edcd86e9d36ae499ce717 Mon Sep 17 00:00:00 2001 From: Gris Ge Date: Sun, 23 Apr 2023 16:01:29 +0800 Subject: [PATCH 2/2] 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 --- 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