SHA256
Basically initial commit, lots of work.
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
// <Nyanit - init program fot initrd.>
|
||||
// Copyright (C) <2026> <Kira Foundation>
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
/*
|
||||
Functions fot system interaction
|
||||
*/
|
||||
|
||||
use std::fs;
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CommandFailed {
|
||||
pub stderr: Vec<u8>,
|
||||
pub cmd: String,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for CommandFailed {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"Command failed! Command: {}; Stderr: {}",
|
||||
self.cmd,
|
||||
String::from_utf8(self.stderr.clone())
|
||||
.unwrap_or_else(|_| { "STDEER IS NOT VALID UTF-8".to_string() })
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandFailed {
|
||||
pub fn new(cmd: &str, args: &Vec<&str>, stderr: Vec<u8>) -> Self {
|
||||
Self {
|
||||
stderr: stderr,
|
||||
cmd: format!("{} {}", cmd, args.join(" ")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ExecCommandError {
|
||||
IoError(std::io::Error),
|
||||
FromUtf8Error(std::string::FromUtf8Error),
|
||||
CommandFailed(CommandFailed),
|
||||
Other(String),
|
||||
}
|
||||
|
||||
// 2. Implement From for each wrapped error type
|
||||
impl From<std::io::Error> for ExecCommandError {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
ExecCommandError::IoError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::string::FromUtf8Error> for ExecCommandError {
|
||||
fn from(err: std::string::FromUtf8Error) -> Self {
|
||||
ExecCommandError::FromUtf8Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CommandFailed> for ExecCommandError {
|
||||
fn from(err: CommandFailed) -> Self {
|
||||
ExecCommandError::CommandFailed(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for ExecCommandError {
|
||||
fn from(err: String) -> Self {
|
||||
ExecCommandError::Other(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ExecCommandError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
ExecCommandError::IoError(e) => write!(f, "IO Error: {}", e),
|
||||
ExecCommandError::FromUtf8Error(e) => {
|
||||
write!(f, "IO streem to String parse Error: {}", e)
|
||||
}
|
||||
ExecCommandError::CommandFailed(ex) => {
|
||||
write!(f, "{}", ex)
|
||||
}
|
||||
ExecCommandError::Other(msg) => write!(f, "Error: {}", msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Implement std::error::Error
|
||||
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> {
|
||||
let out = Command::new(cmd).args(args).output()?;
|
||||
if out.status.success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(CommandFailed::new(cmd, args, out.stderr).into())
|
||||
}
|
||||
}
|
||||
|
||||
/// 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> {
|
||||
let out = Command::new(cmd).args(args).output()?;
|
||||
if out.status.success() {
|
||||
Ok(String::from_utf8(out.stdout)?)
|
||||
} else {
|
||||
Err(CommandFailed::new(cmd, args, out.stderr).into())
|
||||
}
|
||||
}
|
||||
|
||||
// ls /dev/disk/by-partlabel/
|
||||
// here we can find gpt part labels
|
||||
pub fn list_gpt_labeled_devs() -> Result<Vec<String>, ExecCommandError> {
|
||||
use std::os::unix::fs::FileTypeExt;
|
||||
let mut res: Vec<String> = Vec::new();
|
||||
for maybe_entry in fs::read_dir("/dev/disk/by-partlabel/")? {
|
||||
let entry = maybe_entry?;
|
||||
if entry.file_type()?.is_block_device() {
|
||||
res.push(entry.path().to_str().unwrap().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Cheking if device is encripted
|
||||
pub fn bzzpss_dev_encrypted(dev_name: &str) -> Result<bool, ExecCommandError> {
|
||||
match exec_command("bcachefs", &vec!["unlock", "--check"]) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
//const TMP_ROOT: &str = "/mnt/bee-root";
|
||||
pub fn list_bzzpss_snaps(snaps_dir: &str) -> Result<Vec<String>, ExecCommandError> {
|
||||
use regex::Regex;
|
||||
// root snapshoot in a form of bzz-yyyy-mm-ddTHH-MM-SS
|
||||
let re = Regex::new(r"^bzz-[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}-[0-9]{2}-[0-9]{2}$").unwrap();
|
||||
|
||||
let mut res: Vec<String> = Vec::new();
|
||||
for maybe_entry in fs::read_dir(snaps_dir)? {
|
||||
let entry = maybe_entry?;
|
||||
if entry.file_type()?.is_dir() {
|
||||
if let Some(dir_name) = entry.file_name().to_str()
|
||||
&& re.is_match(dir_name)
|
||||
{
|
||||
res.push(dir_name.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
// mounting process
|
||||
//# Mount essential virtual filesystems
|
||||
// mount -t proc none /proc
|
||||
// mount -t sysfs none /sys
|
||||
// modprobe bcachefs
|
||||
pub fn prepare_env() -> Result<(), ExecCommandError> {
|
||||
// mount -t proc none /proc
|
||||
exec_command("mount", &vec!["-t", "proc", "none", "/proc"])?;
|
||||
// mount -t sysfs none /sys
|
||||
exec_command("mount", &vec!["-t", "sysfs", "none", "/sys"])?;
|
||||
|
||||
// modprobe bcachefs
|
||||
exec_command("modprobe", &vec!["bcachefs"])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// # 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])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// finish boot
|
||||
pub fn hand_off_control(mnt_root: &str, init_path: &str) -> Result<(), ExecCommandError> {
|
||||
use std::env;
|
||||
use std::os::unix::process::CommandExt;
|
||||
|
||||
// 2. Change directory to the new root (Mandatory for switch_root)
|
||||
env::set_current_dir(mnt_root)?;
|
||||
|
||||
// # Clean up virtual filesystems
|
||||
// !!! do not do this, switch_root handle this stuff and fail if thay not present!
|
||||
// exec_command("umount", &vec!["/proc"])?;
|
||||
// exec_command("umount", &vec!["/sys"])?;
|
||||
|
||||
// # Hand off control to the real system's init
|
||||
// exec switch_root /mnt/root /sbin/init
|
||||
// 3. Execute switch_root.
|
||||
// This call replaces the current Rust process.
|
||||
// It only returns if an error occurs.
|
||||
let err = Command::new("switch_root")
|
||||
.arg(mnt_root)
|
||||
.arg(init_path)
|
||||
.exec();
|
||||
|
||||
// If execution reaches here, exec() failed.
|
||||
Err(format!("switch_root failed: {}", err).into())
|
||||
}
|
||||
Reference in New Issue
Block a user