From c49bee218f4e8c4344bcd03da406ece9c69a79cd Mon Sep 17 00:00:00 2001 From: Xuewei Niu Date: Thu, 7 Aug 2025 10:46:54 +0800 Subject: [PATCH] systemd: Do not check systemd version The systemd version is purely informational and should not be parsed, as documented in the [1]. In practice, systemd version has different formats on OpenShift and Ubuntu. This commit skips the version check and allows all operations. The errors will be thrown from dbus when performing unsupported operations on obsolete versions of systemd. 1: https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.systemd1.html Signed-off-by: Xuewei Niu --- src/lib.rs | 15 ++------------- src/manager/systemd.rs | 26 +++++++------------------- src/systemd/cpu.rs | 12 +++--------- src/systemd/cpuset.rs | 16 +++------------- src/systemd/dbus/client.rs | 30 +----------------------------- src/systemd/dbus/error.rs | 3 --- src/systemd/error.rs | 3 --- src/systemd/mod.rs | 3 --- 8 files changed, 16 insertions(+), 92 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 362f4b8..a67208c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,22 +72,11 @@ pub mod tests { child } - pub fn systemd_version() -> Option { + pub fn systemd_version() -> Option { let output = Command::new("systemd").arg("--version").output().ok()?; // Return None if command execution fails - if !output.status.success() { return None; } - - let stdout = String::from_utf8_lossy(&output.stdout); - - // The first line is typically like "systemd 254 (254.5-1-arch)" - let first_line = stdout.lines().next()?; - let mut words = first_line.split_whitespace(); - - words.next()?; // Skip the "systemd" word - let version_str = words.next()?; // The version number as string - - version_str.parse::().ok() + Some(String::from_utf8_lossy(&output.stdout).to_string()) } } diff --git a/src/manager/systemd.rs b/src/manager/systemd.rs index ffb0140..6cb160d 100644 --- a/src/manager/systemd.rs +++ b/src/manager/systemd.rs @@ -91,31 +91,21 @@ impl SystemdManager<'_> { &self.unit } - fn set_cpuset( - &self, - props: &mut Vec, - linux_cpu: &LinuxCpu, - systemd_version: usize, - ) -> Result<()> { + fn set_cpuset(&self, props: &mut Vec, linux_cpu: &LinuxCpu) -> Result<()> { if let Some(cpus) = linux_cpu.cpus().as_ref() { - let (id, value) = cpuset::cpus(cpus, systemd_version)?; + let (id, value) = cpuset::cpus(cpus)?; props.push((id, value.into())); } if let Some(mems) = linux_cpu.mems().as_ref() { - let (id, value) = cpuset::mems(mems, systemd_version)?; + let (id, value) = cpuset::mems(mems)?; props.push((id, value.into())); } Ok(()) } - fn set_cpu( - &self, - props: &mut Vec, - linux_cpu: &LinuxCpu, - systemd_version: usize, - ) -> Result<()> { + fn set_cpu(&self, props: &mut Vec, linux_cpu: &LinuxCpu) -> Result<()> { if let Some(shares) = linux_cpu.shares() { let shares = if self.v2() { conv::cpu_shares_to_cgroup_v2(shares) @@ -130,7 +120,7 @@ impl SystemdManager<'_> { let quota = linux_cpu.quota().unwrap_or(0); if period != 0 { - let (id, value) = cpu::period(period, systemd_version)?; + let (id, value) = cpu::period(period)?; props.push((id, value.into())); } @@ -272,11 +262,9 @@ impl Manager for SystemdManager<'_> { fn set(&mut self, resources: &LinuxResources) -> Result<()> { let mut props = vec![]; - let systemd_version = self.systemd_client.systemd_version()?; - if let Some(linux_cpu) = resources.cpu() { - self.set_cpuset(&mut props, linux_cpu, systemd_version)?; - self.set_cpu(&mut props, linux_cpu, systemd_version)?; + self.set_cpuset(&mut props, linux_cpu)?; + self.set_cpu(&mut props, linux_cpu)?; } if let Some(linux_memory) = resources.memory() { diff --git a/src/systemd/cpu.rs b/src/systemd/cpu.rs index 2256866..75bf0e6 100644 --- a/src/systemd/cpu.rs +++ b/src/systemd/cpu.rs @@ -3,10 +3,8 @@ // SPDX-License-Identifier: Apache-2.0 or MIT // -use crate::systemd::error::{Error, Result}; -use crate::systemd::{ - CPU_QUOTA_PERIOD_US, CPU_QUOTA_PER_SEC_US, CPU_SHARES, CPU_SYSTEMD_VERSION, CPU_WEIGHT, -}; +use crate::systemd::error::Result; +use crate::systemd::{CPU_QUOTA_PERIOD_US, CPU_QUOTA_PER_SEC_US, CPU_SHARES, CPU_WEIGHT}; /// Returns the property for CPU shares. /// @@ -21,11 +19,7 @@ pub fn shares(shares: u64, v2: bool) -> Result<(&'static str, u64)> { } /// Returns the property for CPU period. -pub fn period(period: u64, systemd_version: usize) -> Result<(&'static str, u64)> { - if systemd_version < CPU_SYSTEMD_VERSION { - return Err(Error::ObsoleteSystemd); - } - +pub fn period(period: u64) -> Result<(&'static str, u64)> { Ok((CPU_QUOTA_PERIOD_US, period)) } diff --git a/src/systemd/cpuset.rs b/src/systemd/cpuset.rs index b11cbcc..7c09c1a 100644 --- a/src/systemd/cpuset.rs +++ b/src/systemd/cpuset.rs @@ -6,29 +6,19 @@ use bit_vec::BitVec; use crate::systemd::error::{Error, Result}; -use crate::systemd::{ALLOWED_CPUS, ALLOWED_MEMORY_NODES, CPUSET_SYSTEMD_VERSION}; +use crate::systemd::{ALLOWED_CPUS, ALLOWED_MEMORY_NODES}; const BYTE_IN_BITS: usize = 8; /// Returns the property for cpuset CPUs. -pub fn cpus(cpus: &str, systemd_version: usize) -> Result<(&'static str, Vec)> { - if systemd_version < CPUSET_SYSTEMD_VERSION { - return Err(Error::ObsoleteSystemd); - } - +pub fn cpus(cpus: &str) -> Result<(&'static str, Vec)> { let mask = convert_list_to_mask(cpus)?; - Ok((ALLOWED_CPUS, mask)) } /// Returns the property for cpuset memory nodes. -pub fn mems(mems: &str, systemd_version: usize) -> Result<(&'static str, Vec)> { - if systemd_version < CPUSET_SYSTEMD_VERSION { - return Err(Error::ObsoleteSystemd); - } - +pub fn mems(mems: &str) -> Result<(&'static str, Vec)> { let mask = convert_list_to_mask(mems)?; - Ok((ALLOWED_MEMORY_NODES, mask)) } diff --git a/src/systemd/dbus/client.rs b/src/systemd/dbus/client.rs index 8bbe245..c442b58 100644 --- a/src/systemd/dbus/client.rs +++ b/src/systemd/dbus/client.rs @@ -143,21 +143,6 @@ impl SystemdClient<'_> { Ok(()) } - /// Get the systemd version. - pub fn systemd_version(&self) -> Result { - let sys_proxy = systemd_manager_proxy()?; - - // Parse 249 from "249.11-0ubuntu3.16" - let version = sys_proxy.version()?; - let version = version - .split('.') - .next() - .and_then(|v| v.parse::().ok()) - .ok_or(Error::CorruptedSystemdVersion(version))?; - - Ok(version) - } - /// Check if the unit exists. pub fn exists(&self) -> bool { let sys_proxy = match systemd_manager_proxy() { @@ -216,7 +201,7 @@ pub mod tests { use crate::systemd::props::PropertiesBuilder; use crate::systemd::utils::expand_slice; use crate::systemd::{DEFAULT_DESCRIPTION, DESCRIPTION, PIDS}; - use crate::tests::{spawn_sleep_inf, spawn_yes, systemd_version}; + use crate::tests::{spawn_sleep_inf, spawn_yes}; const TEST_SLICE: &str = "cgroupsrs-test.slice"; @@ -504,19 +489,6 @@ pub mod tests { child.wait().unwrap(); } - #[test] - fn test_systemd_version() { - skip_if_no_systemd!(); - - let unit = test_unit(); - let props = PropertiesBuilder::default_cgroup(TEST_SLICE, &unit).build(); - let cgroup = SystemdClient::new(&unit, props).unwrap(); - let version = cgroup.systemd_version().unwrap(); - - let expected_version = systemd_version().unwrap(); - assert_eq!(version, expected_version, "Systemd version mismatch"); - } - #[test] fn test_exists() { skip_if_no_systemd!(); diff --git a/src/systemd/dbus/error.rs b/src/systemd/dbus/error.rs index d8cae94..c59034d 100644 --- a/src/systemd/dbus/error.rs +++ b/src/systemd/dbus/error.rs @@ -12,7 +12,4 @@ pub enum Error { #[error("dbus error: {0}")] Dbus(#[from] zbus::Error), - - #[error("corrupted systemd version: {0}")] - CorruptedSystemdVersion(String), } diff --git a/src/systemd/error.rs b/src/systemd/error.rs index 9189174..33e9ace 100644 --- a/src/systemd/error.rs +++ b/src/systemd/error.rs @@ -10,9 +10,6 @@ pub enum Error { #[error("invalid argument")] InvalidArgument, - #[error("obsolete systemd, please upgrade your systemd")] - ObsoleteSystemd, - #[error("resource not supported by cgroups v1")] CgroupsV1NotSupported, } diff --git a/src/systemd/mod.rs b/src/systemd/mod.rs index 66e35eb..2bd28b1 100644 --- a/src/systemd/mod.rs +++ b/src/systemd/mod.rs @@ -20,6 +20,3 @@ pub const DEFAULT_SLICE: &str = "system.slice"; pub const SLICE_SUFFIX: &str = ".slice"; pub const SCOPE_SUFFIX: &str = ".scope"; - -pub const CPU_SYSTEMD_VERSION: usize = 242; -pub const CPUSET_SYSTEMD_VERSION: usize = 244;