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
7 changes: 4 additions & 3 deletions src/dmverity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use nix::libc::dev_t;
use nix::sys::stat::minor;

use crate::cmdline::CmdlineOptions;
use crate::{read_file, Result};
use crate::{read_file, wait_for_device, Result};

const DM_VERSION_MAJOR: u32 = 4;

Expand Down Expand Up @@ -120,8 +120,9 @@ pub fn prepare_dmverity(options: &mut CmdlineOptions) -> Result<bool> {
return Ok(false);
}
let root_device = options.root.as_ref().ok_or("No root device")?;
if !Path::new(&root_device).exists() {
return Ok(false);
match options.rootfstype.as_deref() {
Some("nfs") | Some("9p") => return Ok(false),
_ => wait_for_device(root_device)?,
}

let mut data_blocks = "";
Expand Down
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use std::os::fd::{AsFd, AsRawFd, RawFd};
use std::os::unix::ffi::OsStrExt;
use std::panic::set_hook;
use std::path::Path;
use std::thread;
use std::time;

use cmdline::{parse_cmdline, CmdlineOptions};
#[cfg(feature = "dmverity")]
Expand Down Expand Up @@ -51,6 +53,21 @@ fn read_file(filename: &str) -> std::result::Result<String, String> {
read_to_string(filename).map_err(|e| format!("Failed to read {filename}: {e}"))
}

fn wait_for_device(root_device: &str) -> Result<()> {
let duration = time::Duration::from_millis(5);
let path = Path::new(&root_device);

for _ in 0..1000 {
if path.exists() {
return Ok(());
}

thread::sleep(duration);
}

Err("timout reached while waiting for the device".into())
}

/*
* Setup stdout/stderr. The kernel will create /dev/console in the
* initramfs, so we can use that.
Expand Down
13 changes: 9 additions & 4 deletions src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use log::debug;
use nix::mount::{mount, MsFlags};

use crate::cmdline::CmdlineOptions;
use crate::{mkdir, Result};
use crate::{mkdir, wait_for_device, Result};

pub fn do_mount(
src: Option<&str>,
Expand Down Expand Up @@ -45,10 +45,15 @@ pub fn mount_apivfs(dst: &str, fstype: &str) -> Result<()> {
}

pub fn mount_root(options: &CmdlineOptions) -> Result<()> {
if options.root.is_none() {
return Err("root= not found in /proc/cmdline".into());
let root = options
.root
.as_ref()
.ok_or("root= not found in /proc/cmdline")?;

match options.rootfstype.as_deref() {
Some("nfs") | Some("9p") => (),
_ => wait_for_device(root)?,
}

mkdir("/root")?;

debug!(
Expand Down