Skip to content

Commit 50f25cd

Browse files
author
Fabian Pfitzner
committed
waiting for device to exist
It might be possible that the storage device does not probe fast enough causing an error when trying to mount it. Thus, we should wait for it passively before proceeding with the code. Signed-off-by: Fabian Pfitzner <[email protected]>
1 parent 2bfb599 commit 50f25cd

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/dmverity.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use nix::libc::dev_t;
1212
use nix::sys::stat::minor;
1313

1414
use crate::cmdline::CmdlineOptions;
15-
use crate::{read_file, Result};
15+
use crate::{read_file, wait_for_device, Result};
1616

1717
const DM_VERSION_MAJOR: u32 = 4;
1818

@@ -120,9 +120,7 @@ pub fn prepare_dmverity(options: &mut CmdlineOptions) -> Result<bool> {
120120
return Ok(false);
121121
}
122122
let root_device = options.root.as_ref().ok_or("No root device")?;
123-
if !Path::new(&root_device).exists() {
124-
return Ok(false);
125-
}
123+
wait_for_device(root_device)?;
126124

127125
let mut data_blocks = "";
128126
let mut data_sectors = "";

src/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use std::os::fd::{AsFd, AsRawFd, RawFd};
1212
use std::os::unix::ffi::OsStrExt;
1313
use std::panic::set_hook;
1414
use std::path::Path;
15+
use std::thread;
16+
use std::time;
1517

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

56+
fn wait_for_device(root_device: &str) -> Result<()> {
57+
let counter = 0;
58+
while !Path::new(&root_device).exists() {
59+
let duration = time::Duration::from_millis(5);
60+
61+
if (counter > 1000) {
62+
println!("timout reached while waiting for the device");
63+
return Err(());
64+
}
65+
66+
thread::sleep(duration);
67+
counter += 1;
68+
}
69+
Ok(())
70+
}
71+
5472
/*
5573
* Setup stdout/stderr. The kernel will create /dev/console in the
5674
* initramfs, so we can use that.

src/mount.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use log::debug;
77
use nix::mount::{mount, MsFlags};
88

99
use crate::cmdline::CmdlineOptions;
10-
use crate::{mkdir, Result};
10+
use crate::{mkdir, wait_for_device, Result};
1111

1212
pub fn do_mount(
1313
src: Option<&str>,
@@ -49,6 +49,11 @@ pub fn mount_root(options: &CmdlineOptions) -> Result<()> {
4949
return Err("root= not found in /proc/cmdline".into());
5050
}
5151

52+
if (options.rootfstype != Some("nfs".to_string())) {
53+
let root_device = options.root.as_ref().ok_or("No root device argument")?;
54+
wait_for_device(root_device)?;
55+
}
56+
5257
mkdir("/root")?;
5358

5459
debug!(

0 commit comments

Comments
 (0)