feat(bootstrap): implement bubblewrap sandbox argument builder and priority helper

This commit is contained in:
katR 2026-09-29 08:05:10 +05:30
parent f209c7ad64
commit b4be13d9f2
3 changed files with 90 additions and 0 deletions

View file

@ -4,3 +4,4 @@ pub mod cdn;
pub mod error;
pub mod fflags;
pub mod mods;
pub mod sandbox;

View file

@ -0,0 +1,69 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use crate::error::MolotovError;
use std::path::{Path, PathBuf};
use std::process::Command;
#[derive(Debug, Clone)]
pub struct SandboxConfig {
pub instance_id: String,
pub data_dir: PathBuf,
pub config_dir: PathBuf,
pub nice_priority: i32,
}
pub struct SandboxRunner {
config: SandboxConfig,
}
impl SandboxRunner {
pub fn new(config: SandboxConfig) -> Self {
Self { config }
}
pub fn build_bwrap_args(&self, executable_path: &Path) -> Vec<String> {
let mut args = vec![
"--unshare-pid".to_string(),
"--unshare-ipc".to_string(),
"--ro-bind".to_string(),
"/usr".to_string(),
"/usr".to_string(),
"--ro-bind".to_string(),
"/lib".to_string(),
"/lib".to_string(),
"--ro-bind".to_string(),
"/lib64".to_string(),
"/lib64".to_string(),
"--dev".to_string(),
"/dev".to_string(),
"--proc".to_string(),
"/proc".to_string(),
"--tmpfs".to_string(),
"/tmp".to_string(),
];
// Isolated XDG_DATA_HOME
args.push("--bind".to_string());
args.push(self.config.data_dir.to_string_lossy().to_string());
args.push(self.config.data_dir.to_string_lossy().to_string());
// Isolated XDG_CONFIG_HOME
args.push("--bind".to_string());
args.push(self.config.config_dir.to_string_lossy().to_string());
args.push(self.config.config_dir.to_string_lossy().to_string());
args.push("--".to_string());
args.push(executable_path.to_string_lossy().to_string());
args
}
pub fn check_bwrap_installed() -> Result<(), MolotovError> {
match Command::new("bwrap").arg("--version").output() {
Ok(output) if output.status.success() => Ok(()),
_ => Err(MolotovError::Sandbox(
"bubblewrap ('bwrap') is not installed. Please install it via your system package manager."
.to_string(),
)),
}
}
}

View file

@ -0,0 +1,20 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use molotov_bootstrap::sandbox::{SandboxConfig, SandboxRunner};
use std::path::PathBuf;
#[test]
fn test_bwrap_arguments_contain_isolated_data_dir() {
let config = SandboxConfig {
instance_id: "test-acc-1".to_string(),
data_dir: PathBuf::from("/tmp/molotov_data"),
config_dir: PathBuf::from("/tmp/molotov_config"),
nice_priority: -5,
};
let runner = SandboxRunner::new(config);
let args = runner.build_bwrap_args(&PathBuf::from("/usr/bin/molotov"));
assert!(args.contains(&"--unshare-pid".to_string()));
assert!(args.contains(&"--bind".to_string()));
assert!(args.iter().any(|a| a.contains("/tmp/molotov_data")));
}