Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions crates/osmodifier/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Centralized constants for the osmodifier crate.
//!
//! GRUB variable names and kernel command-line arg names are defined here
//! once to avoid magic-literal duplication across modules.

/// GRUB variable name for the primary kernel command line.
pub(crate) const GRUB_VAR_CMDLINE_LINUX: &str = "GRUB_CMDLINE_LINUX";

/// GRUB variable name for the default (non-recovery) kernel command line.
pub(crate) const GRUB_VAR_CMDLINE_LINUX_DEFAULT: &str = "GRUB_CMDLINE_LINUX_DEFAULT";

/// GRUB variable name for the boot device.
pub(crate) const GRUB_VAR_DEVICE: &str = "GRUB_DEVICE";

/// Kernel args to extract from grub.cfg and sync back to /etc/default/grub.
///
/// Used by both `grub_cfg::extract_boot_args_from_grub_cfg` (to pick which
/// args to capture) and `update_default_grub` (to specify which existing
/// args to replace).
pub(crate) const SYNC_ARG_NAMES: &[&str] = &[
"rd.overlayfs",
"roothash",
"root",
"security",
"selinux",
"enforcing",
];

/// Kernel command-line arg names managed by SELinux configuration.
///
/// Used when updating or replacing SELinux-related boot args in
/// GRUB_CMDLINE_LINUX.
pub(crate) const SELINUX_CMDLINE_ARG_NAMES: &[&str] = &["security", "selinux", "enforcing"];
11 changes: 7 additions & 4 deletions crates/osmodifier/src/default_grub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{fs, path::PathBuf};
use anyhow::{Context, Error};
use log::{debug, trace};

use crate::constants::{GRUB_VAR_CMDLINE_LINUX, GRUB_VAR_CMDLINE_LINUX_DEFAULT};
use crate::OsModifierContext;

const DEFAULT_GRUB_PATH: &str = "/etc/default/grub";
Expand Down Expand Up @@ -93,7 +94,9 @@ impl DefaultGrub {
old_keys: &[&str],
new_args: &[String],
) -> Result<(), Error> {
let current = self.get_variable("GRUB_CMDLINE_LINUX").unwrap_or_default();
let current = self
.get_variable(GRUB_VAR_CMDLINE_LINUX)
.unwrap_or_default();

let mut args: Vec<String> = current
.split_whitespace()
Expand All @@ -107,7 +110,7 @@ impl DefaultGrub {
args.extend(new_args.iter().cloned());

let new_value = args.join(" ");
self.set_variable("GRUB_CMDLINE_LINUX", &new_value);
self.set_variable(GRUB_VAR_CMDLINE_LINUX, &new_value);

Ok(())
}
Expand All @@ -127,7 +130,7 @@ impl DefaultGrub {
}

let current = self
.get_variable("GRUB_CMDLINE_LINUX_DEFAULT")
.get_variable(GRUB_VAR_CMDLINE_LINUX_DEFAULT)
.unwrap_or_default();

let mut args: Vec<String> = if current.is_empty() {
Expand All @@ -139,7 +142,7 @@ impl DefaultGrub {
args.extend(extra.iter().cloned());

let new_value = args.join(" ");
self.set_variable("GRUB_CMDLINE_LINUX_DEFAULT", &new_value);
self.set_variable(GRUB_VAR_CMDLINE_LINUX_DEFAULT, &new_value);
}
}

Expand Down
4 changes: 1 addition & 3 deletions crates/osmodifier/src/grub_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ use anyhow::{bail, Context, Error};
use log::{debug, info, trace};
use osutils::dependencies::Dependency;

use crate::constants::SYNC_ARG_NAMES;
use crate::OsModifierContext;

/// Possible grub.cfg locations, tried in order.
const GRUB_CFG_PATHS: &[&str] = &["/boot/grub2/grub.cfg", "/boot/grub/grub.cfg"];

/// The grub.cfg args we want to extract for syncing to /etc/default/grub.
const SYNC_ARG_NAMES: &[&str] = &["rd.overlayfs", "roothash", "root", "selinux", "enforcing"];

/// Extract boot arguments from the generated grub.cfg.
///
/// Returns a tuple of (args_to_sync, optional_root_device).
Expand Down
12 changes: 5 additions & 7 deletions crates/osmodifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! directory (defaulting to `/`).

pub mod config;
mod constants;
mod default_grub;
mod grub_cfg;
mod hostname;
Expand Down Expand Up @@ -126,7 +127,7 @@ pub fn modify_os(ctx: &OsModifierContext, config: &OSModifierConfig) -> Result<(
///
/// This replaces the Go `osmodifier --update-grub` codepath:
/// 1. Reads the generated grub.cfg
/// 2. Extracts overlayfs, verity, root, selinux, enforcing args
/// 2. Extracts the args listed in [`constants::SYNC_ARG_NAMES`]
/// 3. Stamps those values into /etc/default/grub
/// 4. Runs grub2-mkconfig to regenerate
pub fn update_default_grub(ctx: &OsModifierContext) -> Result<(), Error> {
Expand All @@ -137,13 +138,10 @@ pub fn update_default_grub(ctx: &OsModifierContext) -> Result<(), Error> {

let mut default_grub = default_grub::DefaultGrub::read(ctx)?;

default_grub.update_cmdline_args(
&["rd.overlayfs", "roothash", "root", "selinux", "enforcing"],
&args,
)?;
default_grub.update_cmdline_args(constants::SYNC_ARG_NAMES, &args)?;

if let Some(root) = root_device {
default_grub.set_variable("GRUB_DEVICE", &root);
default_grub.set_variable(constants::GRUB_VAR_DEVICE, &root);
}
Comment thread
bfjelds marked this conversation as resolved.

default_grub.write()?;
Expand Down Expand Up @@ -219,7 +217,7 @@ pub fn modify_boot(ctx: &OsModifierContext, config: &BootConfig) -> Result<(), E

if let Some(ref root_device) = config.root_device {
debug!("Setting root device to '{root_device}'");
default_grub.set_variable("GRUB_DEVICE", root_device);
default_grub.set_variable(constants::GRUB_VAR_DEVICE, root_device);
changed = true;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/osmodifier/src/selinux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use anyhow::{bail, Context, Error};
use log::debug;
use trident_api::config::SelinuxMode;

use crate::{default_grub::DefaultGrub, OsModifierContext};
use crate::{constants::SELINUX_CMDLINE_ARG_NAMES, default_grub::DefaultGrub, OsModifierContext};

const SELINUX_CONFIG_PATH: &str = "/etc/selinux/config";

Expand Down Expand Up @@ -91,7 +91,7 @@ pub fn update_grub_cmdline(
SelinuxMode::Disabled => vec!["selinux=0".to_string()],
};

default_grub.update_cmdline_args(&["security", "selinux", "enforcing"], &new_args)
default_grub.update_cmdline_args(SELINUX_CMDLINE_ARG_NAMES, &new_args)
}

#[cfg_attr(not(test), allow(unused_imports, dead_code))]
Expand Down
Loading