From 2ba9e9ce6d2c85534a5a5e5afa798c9bbbc30ac6 Mon Sep 17 00:00:00 2001 From: Piyush Jena Date: Fri, 17 Jan 2025 23:59:31 +0000 Subject: [PATCH] prairiedog: extend reboot-to-reconcile to support marker files --- packages/systemd/systemd-tmpfiles.conf | 1 + sources/api/prairiedog/src/bootconfig.rs | 31 +++++++++++++++++++++++- 2 files changed, 31 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..61810a5a3 100644 --- a/sources/api/prairiedog/src/bootconfig.rs +++ b/sources/api/prairiedog/src/bootconfig.rs @@ -13,6 +13,7 @@ use std::{fs, io}; // Boot config related consts const BOOTCONFIG_INITRD_PATH: &str = "/var/lib/bottlerocket/bootconfig.data"; const PROC_BOOTCONFIG: &str = "/proc/bootconfig"; +const REBOOT_REQD_MARKER_DIR: &str = "/run/prairiedog"; const DEFAULT_BOOTCONFIG_STR: &str = r#" kernel = "" init = "" @@ -295,6 +296,30 @@ 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 dentry = entry.unwrap(); + let path = dentry.path(); + + // If the dentry is a subdirectory then call this function for that directory + if path.is_dir() { + // path will never be ".." or "." + ans |= contains_files(&path); + } else { + // file found + return true; + } + } + } + + 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 +332,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 |= has_files; Ok(reboot_required) }