Skip to content

Commit 47885fe

Browse files
composefs/bls: Get cmdline from usr/lib/bootc/kargs.d
Parse toml files in usr/lib/bootc/kargs.d and append them to kernel cmdline on install and upgrade/switch. Also, copy over current deployment's cmdline args on upgrade/switch to another deployment Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>
1 parent 99123ba commit 47885fe

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

crates/lib/src/bootc_composefs/boot.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::Path;
55

66
use anyhow::{anyhow, Context, Result};
77
use bootc_blockdev::find_parent_devices;
8-
use bootc_kernel_cmdline::utf8::Cmdline;
8+
use bootc_kernel_cmdline::utf8::{Cmdline, Parameter};
99
use bootc_mount::inspect_filesystem_of_dir;
1010
use bootc_mount::tempmount::TempMount;
1111
use camino::{Utf8Path, Utf8PathBuf};
@@ -33,6 +33,7 @@ use rustix::{mount::MountFlags, path::Arg};
3333
use schemars::JsonSchema;
3434
use serde::{Deserialize, Serialize};
3535

36+
use crate::bootc_kargs::kargs_from_composefs_filesystem;
3637
use crate::composefs_consts::{TYPE1_ENT_PATH, TYPE1_ENT_PATH_STAGED};
3738
use crate::parsers::bls_config::{BLSConfig, BLSConfigType};
3839
use crate::parsers::grub_menuconfig::MenuEntry;
@@ -51,7 +52,6 @@ use crate::{
5152
BOOT_LOADER_ENTRIES, COMPOSEFS_CMDLINE, ORIGIN_KEY_BOOT, ORIGIN_KEY_BOOT_DIGEST,
5253
STAGED_BOOT_LOADER_ENTRIES, STATE_DIR_ABS, USER_CFG, USER_CFG_STAGED,
5354
},
54-
install::RW_KARG,
5555
spec::{Bootloader, Host},
5656
};
5757

@@ -384,7 +384,7 @@ pub(crate) fn setup_composefs_bls_boot(
384384
) -> Result<String> {
385385
let id_hex = id.to_hex();
386386

387-
let (root_path, esp_device, cmdline_refs, fs, bootloader) = match setup_type {
387+
let (root_path, esp_device, mut cmdline_refs, fs, bootloader) = match setup_type {
388388
BootSetupType::Setup((root_setup, state, postfetch, fs)) => {
389389
// root_setup.kargs has [root=UUID=<UUID>, "rw"]
390390
let mut cmdline_options = Cmdline::new();
@@ -415,16 +415,53 @@ pub(crate) fn setup_composefs_bls_boot(
415415
let sysroot_parent = get_sysroot_parent_dev(&storage.physical_root)?;
416416
let bootloader = host.require_composefs_booted()?.bootloader.clone();
417417

418+
let current_cfg = match bootloader {
419+
Bootloader::Grub => {
420+
let boot_dir = storage
421+
.physical_root
422+
.open_dir("boot")
423+
.context("Opening boot")?;
424+
425+
get_booted_bls(&boot_dir)?
426+
}
427+
428+
Bootloader::Systemd => {
429+
let esp = get_esp_partition(&sysroot_parent)?.0;
430+
let esp_mnt = mount_esp(&esp)?;
431+
432+
get_booted_bls(&esp_mnt.fd)?
433+
}
434+
};
435+
436+
let mut cmdline = match current_cfg.cfg_type {
437+
BLSConfigType::NonEFI { options, .. } => {
438+
let options = options
439+
.ok_or_else(|| anyhow::anyhow!("No 'options' found in BLS Config"))?;
440+
441+
Cmdline::from(options)
442+
}
443+
444+
_ => anyhow::bail!("Found NonEFI config"),
445+
};
446+
447+
// Copy all cmdline args, replacing only `composefs=`
448+
let param = format!("{COMPOSEFS_CMDLINE}={id_hex}");
449+
let param =
450+
Parameter::parse(&param).context("Failed to create 'composefs=' parameter")?;
451+
cmdline.add_or_modify(&param);
452+
418453
(
419454
Utf8PathBuf::from("/sysroot"),
420455
get_esp_partition(&sysroot_parent)?.0,
421-
Cmdline::from(format!("{RW_KARG} {COMPOSEFS_CMDLINE}={id_hex}")),
456+
cmdline,
422457
fs,
423458
bootloader,
424459
)
425460
}
426461
};
427462

463+
kargs_from_composefs_filesystem(fs, &repo, &mut cmdline_refs)?;
464+
428465
let is_upgrade = matches!(setup_type, BootSetupType::Upgrade(..));
429466

430467
let (entry_paths, _tmpdir_guard) = match bootloader {

crates/lib/src/bootc_kargs.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ use cap_std_ext::cap_std::fs::Dir;
66
use cap_std_ext::cap_std::fs_utf8::Dir as DirUtf8;
77
use cap_std_ext::dirext::CapStdExtDirExt;
88
use cap_std_ext::dirext::CapStdExtDirExtUtf8;
9+
use composefs::fs::read_file;
10+
use composefs::generic_tree::Inode;
911
use ostree::gio;
1012
use ostree_ext::ostree;
1113
use ostree_ext::ostree::Deployment;
1214
use ostree_ext::prelude::Cast;
1315
use ostree_ext::prelude::FileEnumeratorExt;
1416
use ostree_ext::prelude::FileExt;
1517
use serde::Deserialize;
18+
use std::ffi::OsStr;
1619

1720
use crate::deploy::ImageState;
1821
use crate::store::Storage;
22+
use crate::store::{ComposefsFilesystem, ComposefsRepository};
1923

2024
/// The relative path to the kernel arguments which may be embedded in an image.
2125
const KARGS_PATH: &str = "usr/lib/bootc/kargs.d";
@@ -45,6 +49,48 @@ impl Config {
4549
}
4650
}
4751

52+
/// Looks for files in usr/lib/bootc/kargs.d and parses cmdline agruments
53+
pub(crate) fn kargs_from_composefs_filesystem(
54+
fs: &ComposefsFilesystem,
55+
repo: &ComposefsRepository,
56+
cmdline: &mut Cmdline,
57+
) -> Result<()> {
58+
let kargs_d = fs
59+
.root
60+
.get_directory_opt(OsStr::new(KARGS_PATH))
61+
.with_context(|| format!("Getting {KARGS_PATH}"))?;
62+
63+
let Some(kargs_d) = kargs_d else {
64+
return Ok(());
65+
};
66+
67+
for (fname, inode) in kargs_d.sorted_entries() {
68+
let Inode::Leaf(..) = inode else { continue };
69+
70+
let fname_str = fname
71+
.to_str()
72+
.ok_or_else(|| anyhow::anyhow!("Failed to get filename as string"))?;
73+
74+
if !Config::filename_matches(fname_str) {
75+
continue;
76+
}
77+
78+
let file = kargs_d
79+
.get_file(fname)
80+
.with_context(|| format!("Getting file {fname_str}"))?;
81+
82+
let contents = read_file(&file, &repo)?;
83+
let contents = std::str::from_utf8(&contents)
84+
.with_context(|| format!("File {fname_str} is not a valid UTF-8"))?;
85+
86+
if let Some(kargs) = parse_kargs_toml(contents, std::env::consts::ARCH)? {
87+
cmdline.extend(&kargs);
88+
};
89+
}
90+
91+
Ok(())
92+
}
93+
4894
/// Load and parse all bootc kargs.d files in the specified root, returning
4995
/// a combined list.
5096
pub(crate) fn get_kargs_in_root(d: &Dir, sys_arch: &str) -> Result<CmdlineOwned> {

0 commit comments

Comments
 (0)