Skip to content

Commit f5fdd93

Browse files
initramfs: Allow passing target for mounting
`target` field in Args was not being used. Use it if it is passed in the args. Also helps us mount the new root at `/run/nextroot` Also, use Cmdline struct instead of String to represent the kernel command line Signed-off-by: Pragyan Poudyal <[email protected]>
1 parent 77a9cab commit f5fdd93

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/initramfs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ composefs.workspace = true
1515
composefs-boot.workspace = true
1616
toml.workspace = true
1717
fn-error-context.workspace = true
18+
bootc-kernel-cmdline = { path = "../kernel_cmdline", version = "0.0.0" }
1819

1920
[lints]
2021
workspace = true

crates/initramfs/src/lib.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use composefs_boot::cmdline::get_cmdline_composefs;
3131

3232
use fn_error_context::context;
3333

34+
use bootc_kernel_cmdline::utf8::Cmdline;
35+
3436
// mount_setattr syscall support
3537
const MOUNT_ATTR_RDONLY: u64 = 0x00000001;
3638

@@ -74,13 +76,17 @@ fn set_mount_readonly(fd: impl AsFd) -> Result<()> {
7476
mount_setattr(fd, libc::AT_EMPTY_PATH, &attr)
7577
}
7678

77-
// Config file
79+
/// Types of mounts supported by the configuration
7880
#[derive(Clone, Copy, Debug, Deserialize)]
7981
#[serde(rename_all = "lowercase")]
80-
enum MountType {
82+
pub enum MountType {
83+
/// No mount
8184
None,
85+
/// Bind mount
8286
Bind,
87+
/// Overlay mount
8388
Overlay,
89+
/// Transient mount
8490
Transient,
8591
}
8692

@@ -90,11 +96,14 @@ struct RootConfig {
9096
transient: bool,
9197
}
9298

99+
/// Configuration for mount operations
93100
#[derive(Debug, Default, Deserialize)]
94-
struct MountConfig {
95-
mount: Option<MountType>,
101+
pub struct MountConfig {
102+
/// The type of mount to use
103+
pub mount: Option<MountType>,
96104
#[serde(default)]
97-
transient: bool,
105+
/// Whether this mount should be transient (temporary)
106+
pub transient: bool,
98107
}
99108

100109
#[derive(Deserialize, Default)]
@@ -138,7 +147,7 @@ pub struct Args {
138147

139148
#[arg(long, help = "Kernel commandline args (for testing)")]
140149
/// Kernel commandline args (for testing)
141-
pub cmdline: Option<String>,
150+
pub cmdline: Option<Cmdline<'static>>,
142151

143152
#[arg(long, help = "Mountpoint (don't replace sysroot, for testing)")]
144153
/// Mountpoint (don't replace sysroot, for testing)
@@ -260,8 +269,9 @@ pub fn mount_composefs_image(sysroot: &OwnedFd, name: &str, insecure: bool) -> R
260269
Ok(rootfs)
261270
}
262271

272+
/// Mounts a subdirectory with the specified configuration
263273
#[context("Mounting subdirectory")]
264-
fn mount_subdir(
274+
pub fn mount_subdir(
265275
new_root: impl AsFd,
266276
state: impl AsFd,
267277
subdir: &str,
@@ -325,12 +335,11 @@ pub fn setup_root(args: Args) -> Result<()> {
325335
let sysroot = open_dir(CWD, &args.sysroot)
326336
.with_context(|| format!("Failed to open sysroot {:?}", args.sysroot))?;
327337

328-
let cmdline = match &args.cmdline {
329-
Some(cmdline) => cmdline,
330-
// TODO: Deduplicate this with composefs branch karg parser
331-
None => &std::fs::read_to_string("/proc/cmdline")?,
332-
};
333-
let (image, insecure) = get_cmdline_composefs::<Sha512HashValue>(cmdline)?;
338+
let cmdline = args
339+
.cmdline
340+
.unwrap_or(Cmdline::from_proc().context("Failed to read cmdline")?);
341+
342+
let (image, insecure) = get_cmdline_composefs::<Sha512HashValue>(&cmdline)?;
334343

335344
let new_root = match args.root_fs {
336345
Some(path) => open_root_fs(&path).context("Failed to clone specified root fs")?,
@@ -342,11 +351,13 @@ pub fn setup_root(args: Args) -> Result<()> {
342351

343352
set_mount_readonly(&sysroot_clone)?;
344353

354+
let mount_target = args.target.unwrap_or(args.sysroot.clone());
355+
345356
// Ideally we build the new root filesystem together before we mount it, but that only works on
346357
// 6.15 and later. Before 6.15 we can't mount into a floating tree, so mount it first. This
347358
// will leave an abandoned clone of the sysroot mounted under it, but that's OK for now.
348359
if cfg!(feature = "pre-6.15") {
349-
mount_at_wrapper(&new_root, CWD, &args.sysroot)?;
360+
mount_at_wrapper(&new_root, CWD, &mount_target)?;
350361
}
351362

352363
if config.root.transient {
@@ -366,7 +377,7 @@ pub fn setup_root(args: Args) -> Result<()> {
366377
if cfg!(not(feature = "pre-6.15")) {
367378
// Replace the /sysroot with the new composed root filesystem
368379
unmount(&args.sysroot, UnmountFlags::DETACH)?;
369-
mount_at_wrapper(&new_root, CWD, &args.sysroot)?;
380+
mount_at_wrapper(&new_root, CWD, &mount_target)?;
370381
}
371382

372383
Ok(())

0 commit comments

Comments
 (0)