diff --git a/Cargo.lock b/Cargo.lock index 5b2c081..c099a94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -305,7 +305,7 @@ checksum = "6aa2c4e539b869820a2b82e1aef6ff40aa85e65decdd5185e83fb4b1249cd00f" [[package]] name = "nyanit" -version = "0.1.7" +version = "0.1.8" dependencies = [ "nix", "ratatui", diff --git a/Cargo.toml b/Cargo.toml index 4d10e61..39fb921 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nyanit" -version = "0.1.7" +version = "0.1.8" edition = "2024" [dependencies] diff --git a/src/main.rs b/src/main.rs index 09d012b..1d0d09f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,17 +90,26 @@ fn mount_and_check_snaps(bee_root_dev: &str) -> Result Result<(String, NyanitState), ExecCommandError> { // mount sysfss nyan_system::prepare_env().map_err(|ex| format!("prepare_env error: {}", ex))?; // get bee-root device name aka /dev/sda1 - let bee_root_dev = match nyan_system::find_partlabel(ROOT_PARTLABEL) { - Err(_ex) => { - return Err(ExecCommandError::Other(nyan_system::list_partlabel().unwrap())) - }, - Ok(v)=>v - }; + + let mut maybe_bee_root_dev = nyan_system::find_partlabel(ROOT_PARTLABEL); + if maybe_bee_root_dev.is_err() { + for _i in 0u32..5 { + thread::sleep(Duration::from_millis(100)); + maybe_bee_root_dev = nyan_system::find_partlabel(ROOT_PARTLABEL); + if maybe_bee_root_dev.is_ok() { + break ; + } + } + } + + let bee_root_dev= maybe_bee_root_dev?; + // check if device is encrypted if nyan_system::bzzpss_dev_encrypted(&bee_root_dev).map_err(|ex| format!("Error checking if device is encrypted: {}", ex))? { // if encrypted return EnterPassword so used get promted for pass @@ -110,6 +119,7 @@ fn init_stage() -> Result<(String, NyanitState), ExecCommandError> { // if unencrypted let check_res = mount_and_check_snaps(&bee_root_dev)?; Ok((bee_root_dev, check_res)) + } struct NyanitTUI<'a> { @@ -138,7 +148,8 @@ impl NyanitTUI<'_> { self.state = match init_stage() { Ok(res) => { self.bee_root_dev = res.0; - res.1 + //res.1 + NyanitState::Error(format!("bee_root_dev={} END EVERYTHING IS READY", self.bee_root_dev)) }, Err(ex) => { NyanitState::Error(format!("Error while trying to bzzz-pss things:\n{}", ex)) diff --git a/src/nyan_system.rs b/src/nyan_system.rs index 76c2e46..a758ebc 100644 --- a/src/nyan_system.rs +++ b/src/nyan_system.rs @@ -22,6 +22,10 @@ use nix; use std::fs; use std::process::Command; + +// timeout for block deices population +const WAIT_FOR_BLK_DEVS: u64 = 5u64; + #[derive(Debug)] pub struct CommandFailed { pub stderr: Vec, @@ -260,6 +264,29 @@ pub fn list_bzzpss_snaps(snaps_dir: &str) -> Result, ExecCommandErro Ok(res) } + +// pulls /sys/class/block +fn wait_for_block_devices(timeout_secs: u64) -> bool { + use std::path::Path; + use std::{thread, time::Duration}; + + let start = std::time::Instant::now(); + while start.elapsed().as_secs() < timeout_secs { + // Check if any block device exists (e.g., sda, nvme0n1, vda) + // You can refine this to check for a specific device needed for root + if Path::new("/sys/class/block").read_dir().ok() + .and_then(|mut d| d.next().is_some().then_some(true)) + .unwrap_or(false) + { + // Optional: Verify specific device needed for root exists + // if Path::new("/dev/nvme0n1").exists() { return true; } + return true; + } + thread::sleep(Duration::from_millis(100)); + } + false +} + // mounting process //# Mount essential virtual filesystems // mount -t proc none /proc @@ -291,15 +318,22 @@ pub fn prepare_env() -> Result<(), String> { ) .map_err(|ex| format!("Error mounting /sys: {}", ex.to_string()))?; + if wait_for_block_devices(WAIT_FOR_BLK_DEVS) == false { + return Err("Waiting for block devices to get populated timed out.".to_string()); + } + + let dev_mount_flags = MsFlags::MS_NOSUID | MsFlags::MS_RELATIME; mount( Some("devtmpfs"), "/dev", Some("devtmpfs"), - MsFlags::empty(), - None::<&str>, + dev_mount_flags, + Some("mode=0755"), ) .map_err(|ex| format!("Error mounting /dev: {}", ex.to_string()))?; + + // modprobe bcachefs // exec_command("modprobe", &vec!["bcachefs"])?; Ok(())