Skip to content

Commit 5e190af

Browse files
committed
mount: enable bind mounting folders from initrd
1 parent 607dfad commit 5e190af

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/cmdline.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct CmdlineOptions {
1414
pub nfsroot: Option<String>,
1515
pub init: String,
1616
pub cleanup: bool,
17+
pub bind_mount: Option<String>,
1718
}
1819

1920
const SBIN_INIT: &str = "/sbin/init";
@@ -28,6 +29,7 @@ impl Default for CmdlineOptions {
2829
nfsroot: None,
2930
init: SBIN_INIT.into(),
3031
cleanup: true,
32+
bind_mount: None,
3133
}
3234
}
3335
}
@@ -45,6 +47,7 @@ fn parse_option(key: &str, value: Option<&str>, options: &mut CmdlineOptions) ->
4547
"rw" => options.rootfsflags.remove(MsFlags::MS_RDONLY),
4648
"nfsroot" => options.nfsroot = Some(ensure_value(key, value)?.to_string()),
4749
"init" => options.init = ensure_value(key, value)?.into(),
50+
"rsinit.bind" => options.bind_mount = Some(ensure_value(key, value)?.to_string()),
4851
_ => (),
4952
}
5053
Ok(())
@@ -236,4 +239,19 @@ mod tests {
236239

237240
assert_eq!(options, expected);
238241
}
242+
243+
#[test]
244+
fn test_rsinit_bind() {
245+
let cmdline = "root=/dev/root rsinit.bind=/lib/modules\n";
246+
247+
let expected = CmdlineOptions {
248+
root: Some("/dev/root".into()),
249+
bind_mount: Some("/lib/modules".into()),
250+
..Default::default()
251+
};
252+
253+
let options = parse_cmdline(cmdline).expect("failed");
254+
255+
assert_eq!(options, expected);
256+
}
239257
}

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ use systemd::{mount_systemd, shutdown};
3030
#[cfg(feature = "usb9pfs")]
3131
use usbg_9pfs::prepare_9pfs_gadget;
3232

33+
use crate::mount::mount_bind;
34+
3335
mod cmdline;
3436
#[cfg(feature = "dmverity")]
3537
mod dmverity;
@@ -96,6 +98,7 @@ fn start_root(options: &mut CmdlineOptions) -> Result<()> {
9698
unlink(exe.as_path())?;
9799
}
98100

101+
mount_bind(options)?;
99102
mount_move_special(options)?;
100103

101104
chdir("/root")?;

src/mount.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::fs::remove_dir;
55
use std::path::Path;
66

7-
use log::debug;
7+
use log::{debug, error};
88
use nix::mount::{mount, MsFlags};
99

1010
use crate::cmdline::CmdlineOptions;
@@ -105,3 +105,19 @@ pub fn mount_move_special(options: &CmdlineOptions) -> Result<()> {
105105
mount_move("/proc", "/root/proc", options.cleanup)?;
106106
Ok(())
107107
}
108+
109+
pub fn mount_bind(options: &CmdlineOptions) -> Result<()> {
110+
if let Some(src) = &options.bind_mount {
111+
if !Path::new(src).exists() {
112+
error!("Can't bind mount {} as it doesn't exist", *src);
113+
return Ok(());
114+
}
115+
116+
let dst = format!("/root{src}");
117+
118+
debug!("Bind mounting {src} to {dst}");
119+
120+
do_mount(Some(src), &dst, None, MsFlags::MS_BIND, None)?;
121+
}
122+
Ok(())
123+
}

0 commit comments

Comments
 (0)