Files
leleka/crates/deploy/src/supervisor/sparse.rs
T

74 lines
3.4 KiB
Rust

//! The sparse archive: shard stamps, the pre-verification process and the served extents.
use super::*;
impl Supervisor {
/// Are both shards stamped as verified in their present state? (Then an engine start hashes neither.)
pub(super) fn shard_stamps_current(&self) -> bool {
(0..2).all(|index| {
let digest = {
let mut out = [0u8; 32];
hex::decode_to_slice(crate::profile::SHARD_SHA256_HEX[index], &mut out)
.is_ok()
.then_some(out)
};
let identity = std::fs::File::open(self.paths.shard(index))
.and_then(|f| f.metadata())
.ok()
.and_then(|m| FileIdentity::of(&m));
matches!((digest, identity), (Some(d), Some(id)) if self.stamps.trusts("gguf", &d, &id))
})
}
/// While the supervisor fills the sealed memfd (reading 55 GB), a CPU-only `leleka-inference --verify-only`
/// hashes the two shards and records their stamps, so the engine that starts right after finds them verified.
/// The two reads overlap instead of following each other. Same verification, same refusals: a shard that does
/// not match its pin fails the start here, and a shard that changes before the engine opens it is hashed again
/// by the engine because the stamp no longer matches.
pub(super) fn spawn_shard_preverify(&self) -> Option<Child> {
if !self.config.verify_stamps || self.shard_stamps_current() {
return None;
}
let program = self.paths.bin("leleka-inference");
let args = self.config.verify_args(false);
match self.spawn("preverify", &program, &args, &BTreeMap::new(), None) {
Ok(child) => {
self.log.line(
"hashing the shards while the archive is copied (stamps are not current)",
);
Some(child)
}
Err(_) => None,
}
}
/// The pin of the extents when the profile asks for a sparse memfd (`None` for the full copy); refuses to start without one.
pub(super) fn sparse_pin(&self) -> Result<Option<[u8; 32]>, Flow> {
if self.config.profile.memfd_extents != MemfdExtents::PleOnly {
return Ok(None);
}
match extents_sha256(&|key| std::env::var(key).ok()) {
Ok((pin, source)) => {
self.log.line(&format!(
"memfd_extents=ple_only: only the byte ranges the serve path reads are in the sealed memfd; the engine verifies them against the extents digest {}{}",
pin.iter().map(|b| format!("{b:02x}")).collect::<String>(),
match source {
PinSource::Compiled => " (pinned in the binary)",
PinSource::Operator => " (supplied by LELEKA_KAR_EXTENTS_SHA256, not pinned in the binary)",
}
));
Ok(Some(pin))
}
Err(message) => fail(1, message),
}
}
/// The page-aligned ranges of the archive file the serve path reads, from its header and directory (about a megabyte of reads).
pub(super) fn serve_extents(kar: &Path) -> Result<Vec<leleka_model_format::kar::Extent>, Flow> {
match sealed::archive_serve_extents(kar, ARCHIVE_BYTES) {
Ok(extents) => Ok(extents),
Err(message) => fail(1, message),
}
}
}