Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/openshell-driver-vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ sha2 = { version = "0.10", optional = true }
toml = { workspace = true, optional = true }
zstd = { version = "0.14", optional = true }

[build-dependencies]
# The guest runtime identity is hashed once, at build time (build.rs).
sha2 = { workspace = true }

[features]
default = ["compute-driver", "telemetry"]
## Build the standalone compute driver and its host runtime implementation.
Expand Down
43 changes: 40 additions & 3 deletions crates/openshell-driver-vm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! This crate embeds the sandbox, host supervisor, and minimal libkrun runtime
//! artifacts it needs to boot VMs without a separate VM runtime binary.

use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use std::{env, fs};

Expand All @@ -14,6 +15,43 @@ fn main() {
return;
}

let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR not set"));
embed_runtime_artifacts(&out_dir);
write_guest_runtime_identity(&out_dir);
}

/// The guest runtime identity (`rootfs::sandbox_guest_runtime_identity`):
/// SHA-256 over the embedded sandbox, vm-init and umoci blobs and the guest
/// init script, exactly the bytes the crate embeds. It keys the image
/// cache, so a changed guest runtime invalidates cached root disks. Hashed
/// here once instead of in every image preparation worker, a fresh process
/// per sandbox launch.
fn write_guest_runtime_identity(out_dir: &Path) {
const GUEST_INIT_SCRIPT: &str = "scripts/openshell-vm-sandbox-init.sh";
println!("cargo:rerun-if-changed={GUEST_INIT_SCRIPT}");
let manifest_dir =
PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"));
let mut hasher = Sha256::new();
for path in [
out_dir.join("openshell-sandbox.zst"),
out_dir.join("openshell-vm-init.zst"),
out_dir.join("umoci.zst"),
manifest_dir.join(GUEST_INIT_SCRIPT),
] {
let bytes = fs::read(&path).unwrap_or_else(|e| {
panic!(
"read {} for the guest runtime identity: {e}",
path.display()
)
});
hasher.update(&bytes);
}
let identity = format!("{:x}", hasher.finalize());
let path = out_dir.join("guest-runtime-identity");
fs::write(&path, identity).unwrap_or_else(|e| panic!("write {}: {e}", path.display()));
}

fn embed_runtime_artifacts(out_dir: &Path) {
println!("cargo:rerun-if-env-changed=OPENSHELL_VM_RUNTIME_COMPRESSED_DIR");

if let Ok(dir) = env::var("OPENSHELL_VM_RUNTIME_COMPRESSED_DIR") {
Expand All @@ -32,7 +70,6 @@ fn main() {
}
}

let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR not set"));
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();

Expand All @@ -42,7 +79,7 @@ fn main() {
_ => {
println!("cargo:warning=VM runtime not available for {target_os}-{target_arch}");
generate_stub_resources(
&out_dir,
out_dir,
&[
"libkrun",
"libkrunfw",
Expand All @@ -62,7 +99,7 @@ fn main() {
println!("cargo:warning=OPENSHELL_VM_RUNTIME_COMPRESSED_DIR not set");
println!("cargo:warning=Run: mise run vm:setup && mise run vm:supervisor");
generate_stub_resources(
&out_dir,
out_dir,
&[
&format!("{libkrun_name}.zst"),
&format!("{libkrunfw_name}.zst"),
Expand Down
Loading