2 Commits
4 changed files with 71 additions and 7 deletions
Generated
+1 -1
View File
@@ -305,7 +305,7 @@ checksum = "6aa2c4e539b869820a2b82e1aef6ff40aa85e65decdd5185e83fb4b1249cd00f"
[[package]]
name = "nyanit"
version = "0.1.6"
version = "0.1.8"
dependencies = [
"nix",
"ratatui",
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "nyanit"
version = "0.1.6"
version = "0.1.8"
edition = "2024"
[dependencies]
+19 -3
View File
@@ -41,7 +41,7 @@ const BEE_CAT: &str = include_str!("bee_cat.txt");
//const ROOT_PARTLABEL: &str = "bee-root";
const ROOT_PARTLABEL: &str = "bee-root";
const TMP_ROOT: &str = "/mnt/bee_root";
const TMP_ROOT_SNAPS: &str = "/mnt/bee-root/bzz_snaps";
const TMP_ROOT_SNAPS: &str = "/mnt/bee_root/bzz_snaps";
const SYS_INIT: &str = "/sbin/init";
// fn bee_root_is_present() -> bool {
@@ -90,12 +90,26 @@ fn mount_and_check_snaps(bee_root_dev: &str) -> Result<NyanitState, ExecCommandE
Ok(NyanitState::HandOff(TMP_ROOT.into()))
}
fn init_stage() -> 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 = nyan_system::find_partlabel(ROOT_PARTLABEL)?;
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
@@ -105,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> {
@@ -133,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))
+50 -2
View File
@@ -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<u8>,
@@ -115,6 +119,7 @@ pub fn exec_command(cmd: &str, args: &Vec<&str>) -> Result<(), ExecCommandError>
pub fn find_partlabel(partlabel: &str) -> Result<String, ExecCommandError> {
let out = Command::new("/usr/bin/blkid")
.args([
"--probe",
"-s",
"PARTLABEL",
"--match-token",
@@ -138,6 +143,19 @@ pub fn find_partlabel(partlabel: &str) -> Result<String, ExecCommandError> {
}
}
pub fn list_partlabel() -> Result<String, ExecCommandError> {
let out = Command::new("/usr/bin/blkid")
.args(["--probe", "-s", "PARTLABEL"])
.output()?;
if out.status.success() {
let out_text = String::from_utf8(out.stdout)?;
Ok(out_text)
} else {
Err(ExecCommandError::Other("Oh no".to_string()))
}
}
/// exec given command with arguments, returning stdout parsed to UTF-8 string
/// capturing stderr in case or failure
// pub fn exec_command_out(cmd: &str, args: &Vec<&str>) -> Result<String, ExecCommandError> {
@@ -246,6 +264,29 @@ pub fn list_bzzpss_snaps(snaps_dir: &str) -> Result<Vec<String>, 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
@@ -277,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(())