omea_kernel_x86_64/lib.rs
1//! Pre-built x86_64 runtime assets for omea's **KVM** backend,
2//! packaged as a Rust crate so embedders never fetch binaries out of band.
3//!
4//! This is the current x86_64 implementation of the architecture-split asset
5//! contract. You normally don't depend on it directly — depend on the
6//! [`omea-kernel`] facade, which re-exports the right arch's crate by
7//! `target_arch`. Cargo only downloads the sub-crate matching the build
8//! target, so an x86_64 build pulls only this crate.
9//!
10//! Three assets are bundled:
11//!
12//! 1. **Linux kernel** ([`KERNEL_BYTES`]) — a minimal-config x86_64
13//! `bzImage` (Linux 6.12). Everything the microVM needs is built in
14//! (`MODULES=n`): virtio-MMIO (+`CMDLINE_DEVICES`), virtio-blk,
15//! virtio-vsock, squashfs, overlayfs, ext4, 8250 console. No module
16//! loading, no host-kernel coupling.
17//! 2. **busybox** ([`BUSYBOX_BYTES`]) — a static x86_64 busybox. The KVM
18//! bake stages it into the agent initramfs as the PID-1 init's toolbox
19//! (mount, switch_root, overlay setup).
20//! 3. **`omea-agent`** ([`OMEA_AGENT_BYTES`]) — the in-VM
21//! control agent (static x86_64-musl). Runs as PID 1 inside the
22//! container after `switch_root`, serving `exec`/control RPCs over vsock.
23//!
24//! The KVM backend builds the container rootfs (overlayfs + `switch_root`)
25//! directly in the agent initramfs's generated init.
26//!
27//! [`omea-kernel`]: https://crates.io/crates/omea-kernel
28
29/// Raw bytes of the x86_64 kernel — a minimal-config Linux `bzImage`.
30///
31/// The omea KVM backend loads this as the guest kernel. The bytes are
32/// staged into `OUT_DIR` by `build.rs` (env override or bundled `kernel.xz`).
33pub const KERNEL_BYTES: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/kernel"));
34
35/// Length of the kernel image in bytes — `KERNEL_BYTES.len()`, const-evaluable.
36pub const KERNEL_LEN: usize = KERNEL_BYTES.len();
37
38/// Raw bytes of the static x86_64 busybox staged into the agent initramfs.
39/// The generated PID-1 init uses it for the overlayfs + `switch_root` dance
40/// that builds the writable container rootfs from the read-only OCI squashfs.
41pub const BUSYBOX_BYTES: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/busybox"));
42
43/// Length of the busybox binary in bytes.
44pub const BUSYBOX_LEN: usize = BUSYBOX_BYTES.len();
45
46/// Raw bytes of the in-VM `omea-agent` (static x86_64-musl ELF). Runs
47/// as PID 1 inside the container post-`switch_root`, serving docker-style
48/// `exec` and other control RPCs over vsock.
49pub const OMEA_AGENT_BYTES: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/omea-agent"));
50
51/// Length of the omea-agent binary in bytes.
52pub const OMEA_AGENT_LEN: usize = OMEA_AGENT_BYTES.len();
53
54/// Write the bundled kernel image to `dest`. Overwrites any existing file;
55/// the parent dir must exist (use [`extract_kernel_to_with_parents`] to mkdir).
56pub fn extract_kernel_to(dest: &std::path::Path) -> std::io::Result<()> {
57 std::fs::write(dest, KERNEL_BYTES)
58}
59
60/// Like [`extract_kernel_to`] but `mkdir -p`'s the parent dir first.
61pub fn extract_kernel_to_with_parents(dest: &std::path::Path) -> std::io::Result<()> {
62 if let Some(parent) = dest.parent() {
63 std::fs::create_dir_all(parent)?;
64 }
65 extract_kernel_to(dest)
66}
67
68/// Write the bundled busybox to `dest`, executable (mode 0o755) on Unix.
69pub fn extract_busybox_to(dest: &std::path::Path) -> std::io::Result<()> {
70 std::fs::write(dest, BUSYBOX_BYTES)?;
71 set_executable(dest)
72}
73
74/// Like [`extract_busybox_to`] but `mkdir -p`'s the parent dir first.
75pub fn extract_busybox_to_with_parents(dest: &std::path::Path) -> std::io::Result<()> {
76 if let Some(parent) = dest.parent() {
77 std::fs::create_dir_all(parent)?;
78 }
79 extract_busybox_to(dest)
80}
81
82/// Write the bundled `omea-agent` to `dest`, executable on Unix.
83pub fn extract_omea_agent_to(dest: &std::path::Path) -> std::io::Result<()> {
84 std::fs::write(dest, OMEA_AGENT_BYTES)?;
85 set_executable(dest)
86}
87
88/// Like [`extract_omea_agent_to`] but `mkdir -p`'s the parent first.
89pub fn extract_omea_agent_to_with_parents(dest: &std::path::Path) -> std::io::Result<()> {
90 if let Some(parent) = dest.parent() {
91 std::fs::create_dir_all(parent)?;
92 }
93 extract_omea_agent_to(dest)
94}
95
96#[cfg(unix)]
97fn set_executable(dest: &std::path::Path) -> std::io::Result<()> {
98 use std::os::unix::fs::PermissionsExt;
99 let mut perms = std::fs::metadata(dest)?.permissions();
100 perms.set_mode(0o755);
101 std::fs::set_permissions(dest, perms)
102}
103
104#[cfg(not(unix))]
105fn set_executable(_dest: &std::path::Path) -> std::io::Result<()> {
106 Ok(())
107}