use crate::error::{Error, Result};
use crate::manifest::{RetryConfig, validate_name};
use crate::supply::{backoff_from_config, download_file, fetch_text, url_exists};
use chrono_machines::BackoffStrategy;
use rand::rng;
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::thread;
use std::time::Duration;
use tar::Archive;
use xz2::read::XzDecoder;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Arch {
Amd64,
Arm64,
I386,
}
impl Arch {
pub fn current() -> Result<Self> {
let arch = std::env::consts::ARCH;
match arch {
"x86_64" => Ok(Self::Amd64),
"aarch64" => Ok(Self::Arm64),
"x86" => Ok(Self::I386),
_ => Err(Error::UnsupportedArch(arch.to_string())),
}
}
pub const fn freebsd_name(self) -> &'static str {
match self {
Self::Amd64 => "amd64",
Self::Arm64 => "arm64",
Self::I386 => "i386",
}
}
}
#[derive(Debug)]
pub struct Release {
pub name: String,
pub path: PathBuf,
pub arch: Arch,
}
pub struct Provisioner {
mirror_url: String,
releases_dir: PathBuf,
cache_dir: PathBuf,
archives: Vec<String>,
arch: Arch,
retry_config: RetryConfig,
}
impl Provisioner {
pub fn new(
mirror_url: String,
releases_dir: PathBuf,
cache_dir: PathBuf,
archives: Vec<String>,
retry_config: RetryConfig,
) -> Result<Self> {
Ok(Self {
mirror_url,
releases_dir,
cache_dir,
archives,
arch: Arch::current()?,
retry_config,
})
}
pub fn from_config(config: &crate::manifest::GlobalConfig) -> Result<Self> {
Self::new(
config.mirror_url.clone(),
config.releases_dir.clone(),
config.cache_dir.clone(),
config.bootstrap_archives.clone(),
config.retry.clone(),
)
}
fn archive_url(&self, release: &str, archive: &str) -> String {
format!(
"{}/{}/{}/{}.txz",
self.mirror_url,
self.arch.freebsd_name(),
release,
archive
)
}
fn manifest_url(&self, release: &str) -> String {
format!(
"{}/{}/{}/MANIFEST",
self.mirror_url,
self.arch.freebsd_name(),
release
)
}
fn parse_manifest(content: &str) -> HashMap<String, String> {
let mut checksums = HashMap::new();
for line in content.lines() {
let parts: Vec<&str> = line.split('\t').collect();
if parts.len() >= 2 {
let archive = parts[0].trim_end_matches(".txz");
let sha256 = parts[1];
checksums.insert(archive.to_string(), sha256.to_string());
}
}
checksums
}
pub fn release_path(&self, release: &str) -> Result<PathBuf> {
validate_name("release", release)?;
Ok(self.releases_dir.join(release))
}
pub fn is_bootstrapped(&self, release: &str) -> Result<bool> {
let path = self.release_path(release)?;
Ok(path.exists() && path.join("bin").exists() && path.join("usr").exists())
}
pub fn list_releases(&self) -> Result<Vec<Release>> {
let mut releases = Vec::new();
if !self.releases_dir.exists() {
return Ok(releases);
}
for entry in fs::read_dir(&self.releases_dir).map_err(Error::Io)? {
let entry = entry.map_err(Error::Io)?;
let path = entry.path();
if path.is_dir() {
if path.join("bin").exists()
&& path.join("usr").exists()
&& let Some(name) = path.file_name().and_then(|n| n.to_str())
{
releases.push(Release {
name: name.to_string(),
path: path.clone(),
arch: self.arch,
});
}
}
}
releases.sort_by(|a, b| a.name.cmp(&b.name));
Ok(releases)
}
pub fn bootstrap_pkgbase(&self, release: &str, force: bool) -> Result<PathBuf> {
let release_path = self.release_path(release)?;
if self.is_bootstrapped(release)? && !force {
return Err(Error::ReleaseAlreadyExists(release.to_string()));
}
let version = crate::sys::OsVersion::parse(release)?;
let host = crate::sys::OsVersion::detect_kernel()?;
if version.major > host.major {
return Err(Error::JailOperation(format!(
"Cannot bootstrap {release} on a FreeBSD {host} host: jail userlands must not be newer than the kernel"
)));
}
let repo = match version.release_type {
crate::sys::ReleaseType::Release => format!("base_release_{}", version.minor),
_ => "base_latest".to_string(),
};
let abi = format!("FreeBSD:{}:{}", version.major, self.arch.freebsd_name());
let versioned_keys = format!("/usr/share/keys/pkgbase-{}", version.major);
let fingerprints = if Path::new(&versioned_keys).exists() {
versioned_keys
} else {
"/usr/share/keys/pkg".to_string()
};
std::fs::create_dir_all(&release_path)?;
std::fs::create_dir_all(release_path.join("etc"))?;
let keys_in_root = release_path.join(fingerprints.trim_start_matches('/'));
copy_tree(Path::new(&fingerprints), &keys_in_root)?;
let repo_dir = self.cache_dir.join("pkgbase-repo");
std::fs::create_dir_all(&repo_dir)?;
std::fs::write(
repo_dir.join("FreeBSD-base.conf"),
format!(
"FreeBSD-base: {{\n url: \"pkg+https://pkg.freebsd.org/${{ABI}}/{repo}\",\n mirror_type: \"srv\",\n signature_type: \"fingerprints\",\n fingerprints: \"{fingerprints}\",\n enabled: yes\n}}\n"
),
)?;
eprintln!("Bootstrapping {release} via pkgbase ({abi} / {repo})");
for pkg_args in [
vec!["update"],
vec!["install", "-y", "FreeBSD-set-base-jail"],
] {
let status = std::process::Command::new("/usr/sbin/pkg")
.arg("--rootdir")
.arg(&release_path)
.arg("--repo-conf-dir")
.arg(&repo_dir)
.args(["-o", &format!("ABI={abi}")])
.args(["-o", "IGNORE_OSVERSION=yes"])
.args(&pkg_args)
.status()
.map_err(|e| Error::JailOperation(format!("Failed to run pkg: {e}")))?;
if !status.success() {
return Err(Error::JailOperation(format!(
"pkg {} failed for pkgbase bootstrap of {}",
pkg_args.join(" "),
release
)));
}
}
let rc_conf = release_path.join("etc/rc.conf");
if !rc_conf.exists()
&& let Err(e) = std::fs::write(&rc_conf, "")
{
eprintln!("Warning: could not create {}: {e}", rc_conf.display());
}
let _ = std::fs::write(release_path.join("root/.hushlogin"), "");
eprintln!("Bootstrap complete: {}", release_path.display());
Ok(release_path)
}
pub fn bootstrap(&self, release: &str, force: bool) -> Result<PathBuf> {
let release_path = self.release_path(release)?;
for archive in &self.archives {
validate_name("archive", archive)?;
}
if self.is_bootstrapped(release)? && !force {
return Err(Error::ReleaseAlreadyExists(release.to_string()));
}
let manifest_url = self.manifest_url(release);
if !url_exists(&manifest_url, &self.retry_config) {
return Err(Error::ReleaseNotFound(release.to_string()));
}
eprintln!("Bootstrapping {} for {}", release, self.arch.freebsd_name());
eprintln!("Fetching MANIFEST...");
let manifest_content = fetch_text(&manifest_url, &self.retry_config)?;
let checksums = Self::parse_manifest(&manifest_content);
for dir in [&self.cache_dir, &release_path] {
let mut check = dir.as_path();
while let Some(parent) = check.parent() {
if let Ok(meta) = fs::symlink_metadata(check)
&& meta.file_type().is_symlink()
{
return Err(Error::ExtractionFailed(format!(
"Path component '{}' is a symlink, refusing to proceed",
check.display()
)));
}
if check == parent {
break;
}
check = parent;
}
}
fs::create_dir_all(&self.cache_dir).map_err(Error::Io)?;
fs::create_dir_all(&release_path).map_err(Error::Io)?;
for archive in &self.archives {
let url = self.archive_url(release, archive);
let cache_file = self.cache_dir.join(format!("{release}-{archive}.txz"));
let expected_sha256 = checksums.get(archive.as_str());
let backoff = backoff_from_config(&self.retry_config);
let mut rng = rng();
let mut attempt: u8 = 0;
loop {
attempt += 1;
let needs_download = if cache_file.exists() {
if let Some(expected) = expected_sha256 {
match crate::supply::sha256_file(&cache_file) {
Ok(actual) => actual != *expected,
Err(_) => true,
}
} else {
false
}
} else {
true
};
let result: Result<()> = (|| {
if needs_download {
eprintln!("Downloading {archive}.txz...");
download_file(
&url,
&cache_file,
expected_sha256.map(std::string::String::as_str),
&self.retry_config,
)?;
} else {
eprintln!("Using cached {archive}.txz");
}
eprintln!("Extracting {archive}.txz...");
Self::extract_txz(&cache_file, &release_path)?;
Ok(())
})();
match result {
Ok(()) => break,
Err(e) => {
if let Some(delay_ms) = backoff.delay(attempt, &mut rng) {
eprintln!(
"Archive {archive} attempt {attempt} failed, retrying in {delay_ms}ms..."
);
let _ = fs::remove_file(&cache_file);
thread::sleep(Duration::from_millis(delay_ms));
} else {
return Err(e);
}
}
}
}
}
eprintln!("Bootstrap complete: {}", release_path.display());
Ok(release_path)
}
fn extract_txz(archive_path: &Path, dest: &Path) -> Result<()> {
let file = File::open(archive_path).map_err(|e| {
Error::ExtractionFailed(format!(
"Failed to open archive {}: {}",
archive_path.display(),
e
))
})?;
let reader = BufReader::new(file);
let xz = XzDecoder::new(reader);
let mut archive = Archive::new(xz);
archive.set_preserve_permissions(true);
archive.set_preserve_ownerships(true);
if let Ok(meta) = std::fs::symlink_metadata(dest)
&& meta.file_type().is_symlink()
{
return Err(Error::ExtractionFailed(format!(
"Extraction destination '{}' is a symlink, refusing to unpack into it",
dest.display()
)));
}
archive.unpack(dest).map_err(|e| {
Error::ExtractionFailed(format!(
"Failed to extract {}: {}",
archive_path.display(),
e
))
})?;
Ok(())
}
pub fn verify(&self, release: &str) -> Result<bool> {
if !self.is_bootstrapped(release)? {
return Err(Error::ReleaseNotFound(release.to_string()));
}
let release_path = self.release_path(release)?;
let essential_paths = ["bin/sh", "usr/bin/env", "lib/libc.so.7"];
for path in essential_paths {
if !release_path.join(path).exists() {
eprintln!("Missing essential file: {path}");
return Ok(false);
}
}
Ok(true)
}
}
pub fn copy_tree(src: &Path, dest: &Path) -> Result<()> {
use std::os::unix::fs::PermissionsExt;
let meta = fs::symlink_metadata(src)
.map_err(|e| Error::ExtractionFailed(format!("stat {}: {}", src.display(), e)))?;
if meta.file_type().is_symlink() {
let target = fs::read_link(src)
.map_err(|e| Error::ExtractionFailed(format!("readlink {}: {}", src.display(), e)))?;
let _ = fs::remove_file(dest); std::os::unix::fs::symlink(&target, dest)
.map_err(|e| Error::ExtractionFailed(format!("symlink {}: {}", dest.display(), e)))?;
} else if meta.is_dir() {
fs::create_dir_all(dest)
.map_err(|e| Error::ExtractionFailed(format!("mkdir {}: {}", dest.display(), e)))?;
fs::set_permissions(dest, fs::Permissions::from_mode(meta.permissions().mode()))
.map_err(|e| Error::ExtractionFailed(format!("chmod {}: {}", dest.display(), e)))?;
for entry in fs::read_dir(src)
.map_err(|e| Error::ExtractionFailed(format!("readdir {}: {}", src.display(), e)))?
{
let entry = entry.map_err(|e| {
Error::ExtractionFailed(format!("readdir {}: {}", src.display(), e))
})?;
copy_tree(&entry.path(), &dest.join(entry.file_name()))?;
}
} else {
fs::copy(src, dest).map_err(|e| {
Error::ExtractionFailed(format!(
"copy {} -> {}: {}",
src.display(),
dest.display(),
e
))
})?;
}
Ok(())
}
pub fn unbootstrapped_releases(config: &crate::manifest::BlackshipConfig) -> Vec<String> {
let Ok(provisioner) = Provisioner::from_config(&config.config) else {
return Vec::new();
};
config
.jails
.iter()
.filter_map(|jail| {
let release = jail.release.as_deref()?;
(!provisioner.is_bootstrapped(release).unwrap_or(true)).then(|| {
format!(
"jail `{}`: release `{release}` not bootstrapped in {} (blackship bootstrap {release})",
jail.name,
config.config.releases_dir.display()
)
})
})
.collect()
}
#[cfg(test)]
mod tests;