use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::io::{Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Provenance {
pub builder_version: String,
pub build_commit: String,
pub build_dirty: bool,
pub built_at_epoch: u64,
pub source_iso: IsoIdentity,
pub host: HostSpecs,
pub deps: DependencyPins,
pub guest: GuestParams,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IsoIdentity {
pub file_name: String,
pub size_bytes: u64,
pub sha256: String,
pub pvd_creation: Option<String>,
pub wim_images: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HostSpecs {
pub os: String,
pub os_version: String,
pub arch: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DependencyPins {
#[serde(alias = "qemu_version")]
pub qemu_tag: String,
#[serde(alias = "wimlib_version")]
pub wimlib_tag: String,
pub virtio_iso_name: String,
pub virtio_sha256: Option<String>,
pub openssh_zip_name: String,
pub openssh_release_tag: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GuestParams {
pub arch: String,
pub username: String,
pub locale: String,
#[serde(default = "unknown_accel")]
pub accel: String,
}
fn unknown_accel() -> String {
"unknown".to_string()
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestRecord {
pub tested_at_epoch: u64,
pub outcome: String,
pub ssh_banner: Option<String>,
pub provenance: Provenance,
}
impl TestRecord {
pub const LOG: &str = "tested-isos.jsonl";
pub fn append(provenance: Provenance, banner: &str) -> Result<PathBuf> {
let record = Self {
tested_at_epoch: now_epoch(),
outcome: "ssh-banner".to_string(),
ssh_banner: Some(banner.trim().to_string()),
provenance,
};
let path = PathBuf::from(Self::LOG);
let mut line = serde_json::to_string(&record)?;
line.push('\n');
use std::io::Write;
std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&path)?
.write_all(line.as_bytes())?;
eprintln!("proven: {}", path.display());
Ok(path)
}
pub fn load_sidecar(image: &Path) -> Result<Provenance> {
let path = Provenance::sidecar_path(image);
let json = std::fs::read_to_string(&path)
.with_context(|| format!("read sidecar {}", path.display()))?;
serde_json::from_str(&json).with_context(|| format!("parse sidecar {}", path.display()))
}
}
pub fn now_epoch() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
pub fn sha256_file(path: &Path) -> Result<String> {
use sha2::Digest;
let mut f = std::fs::File::open(path)
.with_context(|| format!("open {} for hashing", path.display()))?;
let mut h = sha2::Sha256::new();
let mut buf = vec![0u8; 1024 * 1024];
loop {
let n = f.read(&mut buf)?;
if n == 0 {
break;
}
h.update(&buf[..n]);
}
Ok(hex::encode(h.finalize()))
}
pub fn pvd_creation_stamp(iso: &Path) -> Result<Option<String>> {
let mut f = std::fs::File::open(iso)?;
f.seek(SeekFrom::Start(16 * 2048))?;
let mut sector = vec![0u8; 2048];
f.read_exact(&mut sector)?;
if sector[0] != 1 || §or[1..6] != b"CD001" {
return Ok(None);
}
let raw = §or[40..57];
if !raw.iter().all(|b| b.is_ascii_digit()) {
return Ok(None);
}
Ok(Some(String::from_utf8_lossy(raw).into_owned()))
}
pub fn wim_image_names(wimlib_bin: &Path, wim_path: &Path) -> Vec<String> {
let out = std::process::Command::new(wimlib_bin)
.args(["info", &wim_path.to_string_lossy()])
.output();
let Ok(out) = out else { return Vec::new() };
if !out.status.success() {
return Vec::new();
}
let text = String::from_utf8_lossy(&out.stdout);
text.lines()
.filter_map(|l| {
let (_, name) = l.trim_start().split_once("Name:")?;
Some(name.trim().trim_matches('"').to_string())
})
.filter(|n| !n.is_empty())
.collect()
}
fn host_specs() -> HostSpecs {
let os_version = match std::env::consts::OS {
"macos" => std::process::Command::new("sw_vers")
.arg("-productVersion")
.output()
.ok()
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
.filter(|s| !s.is_empty())
.unwrap_or_else(|| "unknown".to_string()),
"linux" => linux_pretty_name(),
_ => "unknown".to_string(),
};
HostSpecs {
os: std::env::consts::OS.to_string(),
os_version,
arch: std::env::consts::ARCH.to_string(),
}
}
fn linux_pretty_name() -> String {
std::fs::read_to_string("/etc/os-release")
.ok()
.and_then(|c| os_release_pretty_name(&c))
.unwrap_or_else(|| "unknown".to_string())
}
fn os_release_pretty_name(content: &str) -> Option<String> {
content.lines().find_map(|line| {
let value = line.strip_prefix("PRETTY_NAME=")?.trim();
let value = value
.strip_prefix('"')
.and_then(|v| v.strip_suffix('"'))
.unwrap_or(value);
(!value.is_empty()).then(|| value.to_string())
})
}
impl Provenance {
#[allow(clippy::too_many_arguments)]
pub fn gather(
win_iso: &Path,
wimlib_bin: &Path,
wim_path: &Path,
arch: crate::unattend::Architecture,
qemu_tag: &str,
wimlib_tag: &str,
virtio_iso: Option<&Path>,
username: &str,
locale: &str,
) -> Result<Self> {
let meta = std::fs::metadata(win_iso)?;
let file_name = win_iso
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| win_iso.display().to_string());
eprintln!("media: hashing source ISO (one pass, ~30 s) ...");
let sha256 = sha256_file(win_iso)?;
let pvd_creation = pvd_creation_stamp(win_iso).unwrap_or(None);
let wim_images = wim_image_names(wimlib_bin, wim_path);
let (virtio_iso_name, virtio_sha256) = match virtio_iso {
Some(p) => (
p.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default(),
sha256_file(p).ok(),
),
None => ("<drivers-dir>".to_string(), None),
};
Ok(Self {
builder_version: env!("CARGO_PKG_VERSION").to_string(),
build_commit: option_env!("BUILD_GIT_SHA")
.unwrap_or("unknown")
.to_string(),
build_dirty: option_env!("BUILD_GIT_DIRTY").is_some_and(|v| v == "true"),
built_at_epoch: now_epoch(),
source_iso: IsoIdentity {
file_name,
size_bytes: meta.len(),
sha256,
pvd_creation,
wim_images,
},
host: host_specs(),
deps: DependencyPins {
qemu_tag: qemu_tag.to_string(),
wimlib_tag: wimlib_tag.to_string(),
virtio_iso_name,
virtio_sha256,
openssh_zip_name: arch.openssh_zip_name().to_string(),
openssh_release_tag: crate::pins::OPENSSH_RELEASE_TAG.to_string(),
},
guest: GuestParams {
arch: arch.as_xml_str().to_string(),
username: username.to_string(),
locale: locale.to_string(),
accel: crate::unattend::resolve_accel().to_string(),
},
})
}
pub fn sidecar_path(image: &Path) -> PathBuf {
let mut s = image.as_os_str().to_owned();
s.push(".json");
PathBuf::from(s)
}
pub fn invalidate(image: &Path) {
let _ = std::fs::remove_file(Self::sidecar_path(image));
}
pub fn write_sidecar(&self, image: &Path) -> Result<PathBuf> {
let path = Self::sidecar_path(image);
let json = serde_json::to_string_pretty(self)?;
std::fs::write(&path, json)?;
eprintln!("media: provenance {}", path.display());
Ok(path)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn synthetic_pvd(stamp: &[u8; 17]) -> Vec<u8> {
let mut img = vec![0u8; 17 * 2048];
img[16 * 2048] = 1;
img[16 * 2048 + 1..16 * 2048 + 6].copy_from_slice(b"CD001");
img[16 * 2048 + 40..16 * 2048 + 57].copy_from_slice(stamp);
img
}
#[test]
fn pvd_stamp_parses() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("fake.iso");
std::fs::write(&p, synthetic_pvd(b"20260922093000000")).unwrap();
assert_eq!(
pvd_creation_stamp(&p).unwrap(),
Some("20260922093000000".to_string())
);
}
#[test]
fn pvd_absent_on_non_iso9660() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("fake.iso");
std::fs::write(&p, vec![0u8; 17 * 2048]).unwrap();
assert_eq!(pvd_creation_stamp(&p).unwrap(), None);
}
#[test]
fn os_release_pretty_name_parses() {
let s = "NAME=\"Ubuntu\"\nPRETTY_NAME=\"Ubuntu 26.04.1 LTS\"\nID=ubuntu\n";
assert_eq!(
os_release_pretty_name(s),
Some("Ubuntu 26.04.1 LTS".to_string())
);
assert_eq!(
os_release_pretty_name("PRETTY_NAME=Alpine Linux v3.20"),
Some("Alpine Linux v3.20".to_string())
);
}
#[test]
fn os_release_pretty_name_missing_is_none() {
assert_eq!(os_release_pretty_name("ID=ubuntu\n"), None);
assert_eq!(os_release_pretty_name("PRETTY_NAME=\n"), None);
}
#[test]
fn provenance_json_roundtrip() {
let prov = Provenance {
builder_version: "0.1.0-alpha".to_string(),
build_commit: "abc123def456".to_string(),
build_dirty: true,
built_at_epoch: 1_758_000_000,
source_iso: IsoIdentity {
file_name: "Win11.iso".to_string(),
size_bytes: 7_000_000_000,
sha256: "abc".to_string(),
pvd_creation: Some("20260922093000000".to_string()),
wim_images: vec!["Windows 11 Pro".to_string()],
},
host: HostSpecs {
os: "macos".to_string(),
os_version: "26.0".to_string(),
arch: "aarch64".to_string(),
},
deps: DependencyPins {
qemu_tag: "qemu-v11.1.1".to_string(),
wimlib_tag: "wimlib-v1.14.5-27".to_string(),
virtio_iso_name: "virtio-win.iso".to_string(),
virtio_sha256: Some("def".to_string()),
openssh_zip_name: "OpenSSH-ARM64.zip".to_string(),
openssh_release_tag: crate::pins::OPENSSH_RELEASE_TAG.to_string(),
},
guest: GuestParams {
arch: "arm64".to_string(),
username: crate::unattend::DEFAULT_USERNAME.to_string(),
locale: "en-US".to_string(),
accel: "hvf".to_string(),
},
};
let json = serde_json::to_string(&prov).unwrap();
assert!(
!json.contains(crate::unattend::DEFAULT_PASSWORD),
"no secrets in provenance"
);
let back: Provenance = serde_json::from_str(&json).unwrap();
assert_eq!(back.source_iso.file_name, "Win11.iso");
assert_eq!(back.deps.qemu_tag, "qemu-v11.1.1");
assert_eq!(back.deps.wimlib_tag, "wimlib-v1.14.5-27");
let old = json
.replace("qemu_tag", "qemu_version")
.replace("wimlib_tag", "wimlib_version");
let back_old: Provenance = serde_json::from_str(&old).unwrap();
assert_eq!(back_old.deps.qemu_tag, "qemu-v11.1.1");
assert_eq!(back_old.deps.wimlib_tag, "wimlib-v1.14.5-27");
}
}