From dac502002dbcecd91af4c972fb29439f733d4047 Mon Sep 17 00:00:00 2001 From: Piyush Jena Date: Mon, 11 Nov 2024 21:46:52 +0000 Subject: [PATCH] prairiedog: extend reboot-to-reconcile to support marker files --- packages/systemd/systemd-tmpfiles.conf | 1 + sources/api/prairiedog/src/bootconfig.rs | 30 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/systemd/systemd-tmpfiles.conf b/packages/systemd/systemd-tmpfiles.conf index 2781b74ee..ca45bd9fc 100644 --- a/packages/systemd/systemd-tmpfiles.conf +++ b/packages/systemd/systemd-tmpfiles.conf @@ -1,5 +1,6 @@ d /run/cache 0755 root root - d /run/lock 0755 root root - +d /run/prairiedog 0755 root root - L /var/lock - - - - /run/lock Z /var/lib/systemd 0755 root root - z /var/lib/systemd/random-seed 600 root root - diff --git a/sources/api/prairiedog/src/bootconfig.rs b/sources/api/prairiedog/src/bootconfig.rs index 6dc2b2d9d..8f56c2a45 100644 --- a/sources/api/prairiedog/src/bootconfig.rs +++ b/sources/api/prairiedog/src/bootconfig.rs @@ -22,6 +22,7 @@ const DEFAULT_BOOT_SETTINGS: BootSettings = BootSettings { kernel_parameters: None, init_parameters: None, }; +const REBOOT_REQD_MARKER_DIR: &str = "/run/prairiedog"; #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "kebab-case")] @@ -295,6 +296,29 @@ fn boot_config_to_boot_settings_json(bootconfig_str: &str) -> Result { serde_json::to_string(&boot_settings).context(error::OutputJsonSnafu) } +/// Given a path, check if the directory or its subdirectories contains files +fn contains_files(dir: &Path) -> bool { + let mut ans: bool = false; + + // Check if the path is a directory + if dir.is_dir() { + for entry in fs::read_dir(dir).unwrap() { + let entry = entry.unwrap(); + let path = entry.path(); + + // If the dentry is a subdirectory then call this function for that directory + if path.is_dir() { + ans |= contains_files(&path); + } else { + // file found + return true; + } + } + } + + return ans; +} + /// Decides whether the host should be rebooted to have its boot settings take effect pub(crate) fn is_reboot_required

(config_path: P) -> Result where @@ -307,12 +331,16 @@ where let new_boot_settings = get_boot_config_settings(config_path)?.unwrap_or(DEFAULT_BOOT_SETTINGS); - let reboot_required = if new_boot_settings.reboot_to_reconcile.unwrap_or(false) { + let mut reboot_required = if new_boot_settings.reboot_to_reconcile.unwrap_or(false) { boot_settings_change_requires_reboot(&old_boot_settings, &new_boot_settings) } else { false }; + let marker_dir = Path::new(REBOOT_REQD_MARKER_DIR); + let has_files = contains_files(marker_dir); + + reboot_required = reboot_required | has_files; Ok(reboot_required) }