Fixed mounting of /dev /sys /proc what is needed for other stuff to work.

This commit is contained in:
2026-07-20 23:21:48 +02:00
parent b90544bac5
commit 1bf3ef0e15
4 changed files with 120 additions and 58 deletions
+50 -16
View File
@@ -18,6 +18,7 @@
Functions fot system interaction
*/
use nix;
use std::fs;
use std::process::Command;
@@ -101,9 +102,7 @@ impl std::error::Error for ExecCommandError {}
/// exec given command with arguments, capturing stderr in case or failure
pub fn exec_command(cmd: &str, args: &Vec<&str>) -> Result<(), ExecCommandError> {
// For testing shake lets assume all binaries is in /usr/bin
let ex_cmd = format!("/usr/bin/{}", cmd);
let out = Command::new(ex_cmd).args(args).output()?;
let out = Command::new(cmd).args(args).output()?;
if out.status.success() {
Ok(())
} else {
@@ -149,18 +148,24 @@ pub fn bcachefs_unlock(pass: String, dev: &str) -> Result<bool, ExecCommandError
use std::process::Stdio;
// "-k", "session" is important as it stores key in sessyin keyring what allows
// system to mount filesystem after switch_root
let mut child = Command::new("bcachefs")
.args(&vec!["unlock", "-k", "session", dev])
let mut child = Command::new("/usr/bin/bcachefs")
.args(&vec!["unlock", "-k", "session", dev])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
let mut c_stderr = child.stderr.take().ok_or_else(||{"Unable to get child process STDERR".to_string()})?;
let mut c_stderr = child
.stderr
.take()
.ok_or_else(|| "Unable to get child process STDERR".to_string())?;
// Write password to STDIN in a limited scope
{
let mut c_stdin = child.stdin.take().ok_or_else(||{"Unable to get child process STDIN".to_string()})?;
let mut c_stdin = child
.stdin
.take()
.ok_or_else(|| "Unable to get child process STDIN".to_string())?;
// neccessery to use thread or we may be deadlocking
c_stdin.write_all(pass.as_bytes())?;
let bytes_written = c_stdin.write(b"\n")?;
@@ -181,12 +186,12 @@ pub fn bcachefs_unlock(pass: String, dev: &str) -> Result<bool, ExecCommandError
// Safe to wait now; child will see EOF
let exit_status = child.wait()?;
// if exit status is SUCCESS - unloking is successful
Ok(exit_status.success())
Ok(exit_status.success())
}
/// Cheking if device is encripted
pub fn bzzpss_dev_encrypted(dev_name: &str) -> Result<bool, ExecCommandError> {
match exec_command("bcachefs", &vec!["unlock", "--check", dev_name]) {
match exec_command("/usr/bin/bcachefs", &vec!["unlock", "--check", dev_name]) {
Ok(_) => Ok(true), // if status OK - dev encrypted
Err(ExecCommandError::CommandFailed(_)) => Ok(false), // if bcachefs returned error - unencrypted
Err(ex) => Err(ex), // othervice its error
@@ -218,11 +223,40 @@ pub fn list_bzzpss_snaps(snaps_dir: &str) -> Result<Vec<String>, ExecCommandErro
// mount -t proc none /proc
// mount -t sysfs none /sys
// modprobe bcachefs
pub fn prepare_env() -> Result<(), ExecCommandError> {
pub fn prepare_env() -> Result<(), String> {
use nix::mount::{mount, MsFlags};
use std::path::Path;
// Mount proc filesystem
// mount -t proc none /proc
exec_command("mount", &vec!["-t", "proc", "none", "/proc"])?;
mount(
Some("proc"),
Path::new("/proc"),
Some("proc"),
MsFlags::empty(),
None::<&str>,
)
.map_err(|ex| format!("Error mounting /proc: {}", ex.to_string()))?;
// mount -t sysfs none /sys
exec_command("mount", &vec!["-t", "sysfs", "none", "/sys"])?;
mount(
Some("sysfs"),
Path::new("/sys"),
Some("sysfs"),
MsFlags::empty(),
None::<&str>,
)
.map_err(|ex| format!("Error mounting /sys: {}", ex.to_string()))?;
mount(
Some("devtmpfs"),
"/dev",
Some("devtmpfs"),
MsFlags::empty(),
None::<&str>,
)
.map_err(|ex| format!("Error mounting /dev: {}", ex.to_string()))?;
// modprobe bcachefs
// exec_command("modprobe", &vec!["bcachefs"])?;
@@ -241,12 +275,12 @@ pub fn set_env_vars_unsafe() -> () {
// 3. Construct new path strings
let new_path = match env::var("PATH") {
Err(_) => custom_bin.to_string(),
Ok(v) => format!("{}:{}", v, custom_bin)
Ok(v) => format!("{}:{}", custom_bin, v),
};
let new_ld_path = match env::var("LD_LIBRARY_PATH") {
Err(_) => custom_lib.to_string(),
Ok(v) => format!("{}:{}", v, custom_lib)
Ok(v) => format!("{}:{}", custom_lib, v),
};
// 4. Set environment variables (UNSAFE in Rust 2024+)
@@ -259,7 +293,7 @@ pub fn set_env_vars_unsafe() -> () {
// # Mount the real root filesystem
pub fn mount_bee_root(mount_point: &str, dev_name: &str) -> Result<(), ExecCommandError> {
exec_command("mount", &vec!["-o", "ro", dev_name, mount_point])?;
exec_command("/usr/bin/mount", &vec!["-o", "ro", dev_name, mount_point])?;
Ok(())
}
@@ -281,7 +315,7 @@ pub fn hand_off_control(mnt_root: &str, init_path: &str) -> Result<(), ExecComma
// 3. Execute switch_root.
// This call replaces the current Rust process.
// It only returns if an error occurs.
let err = Command::new("switch_root")
let err = Command::new("/usr/bin/switch_root")
.arg(mnt_root)
.arg(init_path)
.exec();