diff options
Diffstat (limited to 'src/rootfs.rs')
| -rw-r--r-- | src/rootfs.rs | 188 |
1 files changed, 129 insertions, 59 deletions
diff --git a/src/rootfs.rs b/src/rootfs.rs index 659032e..8b89efe 100644 --- a/src/rootfs.rs +++ b/src/rootfs.rs @@ -2,20 +2,49 @@ use std::fs; use std::path::Path; use std::collections::HashSet; use std::process::Command; +use std::os::unix::fs::symlink; use colored::Colorize; use crate::config; use crate::group::Group; +use crate::progress::ProgressBar; use crate::install::{ Dependency, Package, load_package, download_and_verify, extract_native, install_files_with_root, select_binary_for_arch, }; +fn setup_usr_merge(target_dir: &Path) -> Result<(), String> { + for dir in &["bin", "lib", "lib64", "sbin"] { + let path = target_dir.join(dir); + if path.symlink_metadata().is_ok() { + if path.is_dir() && !path.is_symlink() { + fs::remove_dir_all(&path) + .map_err(|e| format!("Failed to remove directory {}: {}", dir, e))?; + } else { + fs::remove_file(&path) + .map_err(|e| format!("Failed to remove {}: {}", dir, e))?; + } + } + } + for dir in &["usr/bin", "usr/lib", "usr/lib64", "usr/sbin"] { + fs::create_dir_all(target_dir.join(dir)) + .map_err(|e| format!("Failed to create {}: {}", dir, e))?; + } + for (link, target) in &[ + ("bin", "usr/bin"), + ("lib", "usr/lib"), + ("lib64", "usr/lib64"), + ("sbin", "usr/sbin"), + ] { + symlink(target, target_dir.join(link)) + .map_err(|e| format!("Failed to create symlink {} -> {}: {}", link, target, e))?; + } + Ok(()) +} fn mount_virtual_fs(root_dir: &Path) -> Result<(), String> { - let mounts: &[(&str, &str, &str, bool)] = &[ ("/dev", "dev", "devtmpfs", true), ("/dev/pts", "dev/pts", "devpts", true), @@ -48,7 +77,6 @@ fn mount_virtual_fs(root_dir: &Path) -> Result<(), String> { Ok(()) } - fn umount_virtual_fs(root_dir: &Path) { for rel in &["dev/pts", "dev/shm", "dev", "proc", "sys"] { let target = root_dir.join(rel); @@ -58,49 +86,71 @@ fn umount_virtual_fs(root_dir: &Path) { } } +const SHELL_CANDIDATES: &[(&str, Option<&str>)] = &[ + ("/bin/sh", None), + ("/usr/bin/sh", None), + ("/bin/dash", None), + ("/usr/bin/dash", None), + ("/bin/bash", None), + ("/usr/bin/bash", None), + ("/bin/busybox", Some("sh")), + ("/usr/bin/busybox", Some("sh")), + ("/bin/ash", None), + ("/usr/bin/ash", None), +]; + +fn find_working_shell(root_dir: &Path) -> Option<(&'static str, Option<&'static str>)> { + for &(shell_path, subcmd) in SHELL_CANDIDATES { + if !root_dir.join(shell_path.trim_start_matches('/')).exists() { + continue; + } + let mut cmd = Command::new("chroot"); + cmd.arg(root_dir).arg(shell_path); + if let Some(sub) = subcmd { + cmd.arg(sub); + } + cmd.arg("-c").arg("exit 0"); - - - - -fn find_shell_in_rootfs(root_dir: &Path) -> Option<(&'static str, Option<&'static str>)> { - let candidates: &[(&str, Option<&str>)] = &[ - ("/bin/sh", None), - ("/usr/bin/sh", None), - ("/bin/bash", None), - ("/usr/bin/bash", None), - ("/bin/busybox", Some("sh")), - ("/usr/bin/busybox", Some("sh")), - ]; - candidates.iter() - .find(|(p, _)| root_dir.join(p.trim_start_matches('/')).exists()) - .copied() + match cmd.output() { + Ok(out) if out.status.success() => { + return Some((shell_path, subcmd)); + } + Ok(out) => { + let stderr = String::from_utf8_lossy(&out.stderr); + eprintln!( + "{} Shell {} failed in chroot ({}), trying next...", + "[ror]".yellow(), + shell_path, + stderr.lines().next().unwrap_or("unknown error").trim() + ); + } + Err(e) => { + eprintln!("{} Cannot test shell {}: {}, trying next...", "[ror]".yellow(), shell_path, e); + } + } + } + None } - - - - -fn run_install_steps_in_chroot(pkg_name: &str, steps: &str, root_dir: &Path) -> Result<(), String> { - let (shell, subcmd) = find_shell_in_rootfs(root_dir) - .ok_or_else(|| format!( - "No shell found in rootfs for '{}'. Checked: /bin/sh, /bin/bash, /bin/busybox", - pkg_name - ))?; - - println!("{} Using shell: {}{}", ">>>".cyan(), shell, - subcmd.map(|s| format!(" {}", s)).unwrap_or_default()); - +fn run_install_steps_in_chroot( + pkg_name: &str, + steps: &str, + root_dir: &Path, + shell: &str, + subcmd: Option<&str>, +) -> Result<(), String> { let script_rel = "/.ror-install-steps.sh"; let script_host = root_dir.join(script_rel.trim_start_matches('/')); - + fs::write(&script_host, format!("#!/bin/sh\nset -e\n{}", steps)) .map_err(|e| format!("Failed to write install script: {}", e))?; let mut cmd = Command::new("chroot"); cmd.arg(root_dir).arg(shell); - if let Some(sub) = subcmd { cmd.arg(sub); } + if let Some(sub) = subcmd { + cmd.arg(sub); + } cmd.arg(script_rel); let status = cmd.status() @@ -112,28 +162,25 @@ fn run_install_steps_in_chroot(pkg_name: &str, steps: &str, root_dir: &Path) -> Ok(()) } else { Err(format!( - "install_steps for '{}' failed with exit code {:?}", + "install_steps for \'{}\' failed with exit code {:?}", pkg_name, status.code() )) } } - - - - fn process_dependencies_chroot( pkg: &Package, cfg: &config::Config, root_dir: &Path, installing: &mut HashSet<String>, target_arch: &str, + progress: &mut ProgressBar, ) -> Result<(), String> { for dep in &pkg.depends { match dep { Dependency::Single(name) => { if !installing.contains(name) { - install_package_in_chroot(name, cfg, root_dir, installing, target_arch)?; + install_package_in_chroot(name, cfg, root_dir, installing, target_arch, progress)?; } } Dependency::Any(alternatives) => { @@ -142,7 +189,7 @@ fn process_dependencies_chroot( } let chosen = alternatives.first() .ok_or_else(|| format!("Empty alternative list in package {}", pkg.name))?; - install_package_in_chroot(chosen, cfg, root_dir, installing, target_arch)?; + install_package_in_chroot(chosen, cfg, root_dir, installing, target_arch, progress)?; } } } @@ -155,9 +202,14 @@ pub fn install_package_in_chroot( root_dir: &Path, installing: &mut HashSet<String>, target_arch: &str, + progress: &mut ProgressBar, ) -> Result<(), String> { if installing.contains(pkg_name) { - eprintln!("{} Circular dependency detected: {}", "[ror]".red().bold(), pkg_name); + println!( + "{} {} already queued, dependencies are already installed", + "[ror]".green(), + pkg_name.green() + ); return Ok(()); } installing.insert(pkg_name.to_string()); @@ -165,15 +217,16 @@ pub fn install_package_in_chroot( let pkg = load_package(pkg_name) .ok_or_else(|| format!("Package '{}' not found", pkg_name))?; - process_dependencies_chroot(&pkg, cfg, root_dir, installing, target_arch)?; + process_dependencies_chroot(&pkg, cfg, root_dir, installing, target_arch, progress)?; let binary = select_binary_for_arch(&pkg, target_arch) .map_err(|e| format!("Failed to select binary for {}: {}", pkg_name, e))?; + progress.inc(&format!("Installing {}", pkg_name)); + let mut last_err = None; for mirror in &binary.mirrors { let url = mirror.replace("{filename}", &binary.filename); - println!("{} Trying mirror: {}", ">>>".yellow(), url); let archive_path = match download_and_verify(&url, &binary.sha256, cfg) { Ok(p) => p, @@ -205,10 +258,6 @@ pub fn install_package_in_chroot( Err(last_err.unwrap_or_else(|| "All mirrors failed".into())) } - - - - fn run_all_install_steps( group_order: &[String], all_installed: &HashSet<String>, @@ -216,34 +265,47 @@ fn run_all_install_steps( ) -> Result<(), String> { let mut done: HashSet<String> = HashSet::new(); - let mut queue: Vec<&str> = group_order.iter().map(|s| s.as_str()).collect(); - + let mut extras: Vec<String> = all_installed.iter() .filter(|p| !group_order.contains(p)) .cloned() .collect(); - extras.sort(); + extras.sort(); for e in &extras { queue.push(e.as_str()); } + let steps_pkgs: Vec<String> = queue.iter() + .filter(|&&pkg_name| { + all_installed.contains(pkg_name) && + load_package(pkg_name).map(|p| !p.install_steps.trim().is_empty()).unwrap_or(false) + }) + .map(|s| s.to_string()) + .collect(); + + let total_steps = steps_pkgs.len(); + let mut pb = ProgressBar::new(total_steps.max(1), "Running install steps..."); + + let (shell, subcmd) = find_working_shell(target_dir) + .ok_or_else(|| "No working shell found in rootfs. Tried: sh, dash, bash, busybox, ash".to_string())?; + + println!("{} Using shell: {}{}", ">>>".cyan(), shell, + subcmd.map(|s| format!(" {}", s)).unwrap_or_default()); + for pkg_name in queue { if done.contains(pkg_name) || !all_installed.contains(pkg_name) { continue; } done.insert(pkg_name.to_string()); if let Some(pkg) = load_package(pkg_name) { if !pkg.install_steps.trim().is_empty() { - println!("{} install_steps: {}", ">>>".cyan(), pkg_name); - run_install_steps_in_chroot(pkg_name, &pkg.install_steps, target_dir)?; + pb.inc(&format!("install_steps: {}", pkg_name)); + run_install_steps_in_chroot(pkg_name, &pkg.install_steps, target_dir, shell, subcmd)?; } } } + pb.finish("All install steps done"); Ok(()) } - - - - pub fn build_rootfs( group_name: &str, target_dir: &Path, @@ -274,12 +336,20 @@ pub fn build_rootfs( println!("{} {}", "Description:".yellow(), desc); } - + println!("\n{} Phase 0: UsrMerge...", ">>>".cyan().bold()); + setup_usr_merge(target_dir)?; + println!("{} UsrMerge done: bin/lib/lib64/sbin are now symlinks into usr/", ">>>".green()); + println!("\n{} Phase 1: installing files...", ">>>".cyan().bold()); + + let estimated_total = group.packages.len() * 3; + let mut pb = ProgressBar::new(estimated_total.max(1), "Starting..."); + let mut installing: HashSet<String> = HashSet::new(); for pkg in &group.packages { - install_package_in_chroot(pkg, cfg, target_dir, &mut installing, target_arch)?; + install_package_in_chroot(pkg, cfg, target_dir, &mut installing, target_arch, &mut pb)?; } + pb.finish(&format!("Phase 1 complete: {} packages installed", installing.len())); println!("{} Phase 1 complete: {} packages.", ">>>".green(), installing.len()); if run_ldconfig { |