How e2b-dev/firecracker works
Purpose and executables
Firecracker is a Rust/Linux microVM monitor for secure, multi-tenant container and function workloads. It runs one VMM process per microVM over Linux KVM. Primary binaries are Firecracker (src/firecracker/src/main.rs) and its production launcher/isolation wrapper, Jailer (src/jailer/src/main.rs). Workspace composition and dependencies are rooted in Cargo.toml.
Host API and lifecycle control
The Firecracker process exposes a host-side API defined by OpenAPI at src/firecracker/swagger/firecracker.yaml. The API server parses resource/action requests and routes them into VMM configuration and lifecycle operations: src/firecracker/src/api_server/mod.rs, src/firecracker/src/api_server/request/mod.rs, and src/firecracker/src/api_server_adapter.rs. This is the central boundary between an external orchestrator and in-process VM state; the guest does not directly access this API.
VMM construction and KVM execution
The VMM crate owns machine assembly, runtime state, and KVM-facing control. Its builder converts configured resources into a VM, memory map, vCPUs, buses, and devices (src/vmm/src/builder.rs, src/vmm/src/vstate/vm.rs, src/vmm/src/vstate/vcpu.rs). Architecture-specific KVM boot and register logic split across x86_64 and aarch64 (src/vmm/src/arch/x86_64/kvm.rs, src/vmm/src/arch/aarch64/kvm.rs). Linux KVM is therefore the fundamental external runtime dependency.
Configuration, resources, and CPU compatibility
API updates are represented as typed VMM configuration and resource state before or during VM creation (src/vmm/src/vmm_config/mod.rs, src/vmm/src/resources.rs, src/vmm/src/rpc_interface.rs). CPU templates normalize or constrain guest CPU exposure across host processors, with x86 CPUID and aarch64 implementations (src/vmm/src/cpu_config/x86_64/cpuid/normalize.rs, src/vmm/src/cpu_config/aarch64/custom_cpu_template.rs). The standalone CPU-template helper supports fingerprinting, generation, stripping, and verification (src/cpu-template-helper/src/main.rs).
Guest device and I/O plane
The device manager attaches legacy, MMIO, and PCI devices to guest buses (src/vmm/src/device_manager/mod.rs, src/vmm/src/device_manager/mmio.rs, src/vmm/src/device_manager/pci_mngr.rs). Virtio is the principal guest I/O boundary: block supports file-backed and vhost-user backends, net connects host TAP networking, and other devices include balloon, memory hotplug, RNG, PMEM, and vsock (src/vmm/src/devices/virtio/block/virtio/device.rs, src/vmm/src/devices/virtio/net/tap.rs, src/vmm/src/devices/virtio/vsock/device.rs). Block I/O can use asynchronous Linux io_uring (src/vmm/src/io_uring/mod.rs).
Isolation, syscall policy, and observability
Jailer establishes a production isolation boundary through chroot, cgroups, namespaces/environment setup, resource limits, and privilege reduction (src/jailer/src/chroot.rs, src/jailer/src/cgroup.rs, src/jailer/src/env.rs). Firecracker and VMM apply seccomp policies; the compiler and shipped architecture policies are separate (src/seccompiler/src/lib.rs, src/firecracker/src/seccomp.rs, resources/seccomp/x86_64-unknown-linux-musl.json). Logging and metrics flow through VMM logging/metrics components (src/vmm/src/logger/mod.rs, src/firecracker/src/metrics.rs).
Persistence and guest metadata
Snapshotting persists/restores VM state, device state, and memory-related metadata for cloning or rapid resume (src/vmm/src/snapshot/mod.rs, src/vmm/src/persist.rs, src/snapshot-editor/src/main.rs). MMDS is a guest-facing metadata datastore, exposed only when configured, with state persistence and token support (src/vmm/src/mmds/data_store.rs, src/vmm/src/mmds/persist.rs). These subsystems bridge host-provided state into a running or restored guest while remaining distinct from the host control API.
Main entry points: vm.rs KVM VM and vCPUs · builder.rs VMM builder · src/main.rs Firecracker · src/main.rs Jailer
Components 20
Host control plane
Host orchestration and VM lifecycle boundary.
- Firecracker · One monitor process runs one microVM.
src/firecracker/src/main.rs - Host API contract
src/firecracker/swagger/firecracker.yaml - API server · Accepts host resource and lifecycle requests.
src/firecracker/src/api_server/mod.rs - VMM API adapter · Translates API actions into in-process VMM operations.
src/firecracker/src/api_server_adapter.rs
VMM runtime
Machine construction and KVM execution.
- Configuration and resources · Represents configured resources before and during VM creation.
src/vmm/src/vmm_config/mod.rs - VMM builder · Builds memory, vCPUs, buses, and devices from resources.
src/vmm/src/builder.rs - KVM VM and vCPUs · Owns VM state and runs guest vCPUs through Linux KVM.
src/vmm/src/vstate/vm.rs - Linux KVM
- CPU compatibility · Normalizes or constrains guest CPU exposure across hosts.
src/vmm/src/cpu_config/x86_64/cpuid/normalize.rs - CPU template helper · Generates, fingerprints, strips, and verifies CPU templates.
src/cpu-template-helper/src/main.rs
Guest I/O and state
Guest-facing devices, metadata, and persistence.
- Device manager · Attaches legacy, MMIO, and PCI devices.
src/vmm/src/device_manager/mod.rs - Virtio devices · Provides block, network, vsock, and other guest devices.
src/vmm/src/devices/virtio/block/virtio/device.rs - io_uring
src/vmm/src/io_uring/mod.rs - Snapshots · Persists and restores VM, device, and memory-related state.
src/vmm/src/snapshot/mod.rs - MMDS · Configured guest-facing metadata with persistence and tokens.
src/vmm/src/mmds/data_store.rs
Isolation and operations
Production containment, syscall policy, and observability.
- Jailer · Prepares the isolated execution environment for Firecracker.
src/jailer/src/main.rs - Chroot and cgroups · Applies chroot, cgroups, namespaces, limits, and privilege reduction.
src/jailer/src/chroot.rs - Seccomp policy · Compiler and shipped policies restrict Firecracker and VMM syscalls.
src/seccompiler/src/lib.rs - Logging and metrics
src/vmm/src/logger/mod.rs
Other components
- Cargo workspace
Cargo.toml
Connections 21
- Cargo workspace → Firecracker: builds
- Cargo workspace → Jailer: builds
- Jailer → Chroot and cgroups: sets up
- Jailer → Firecracker: launches
- Host API contract → API server: defines
- API server → VMM API adapter: routes requests
- VMM API adapter → Configuration and resources: updates
- Configuration and resources → VMM builder: configures
- VMM builder → KVM VM and vCPUs: creates
- CPU compatibility → VMM builder: constrains CPU
- KVM VM and vCPUs → Linux KVM: uses
- VMM builder → Device manager: attaches devices
- Device manager → Virtio devices: manages
- Virtio devices → io_uring: uses for block I/O
- Snapshots → KVM VM and vCPUs: restores and saves state
- MMDS → Virtio devices: exposes to guest
- Seccomp policy → Firecracker: restricts syscalls
- Seccomp policy → KVM VM and vCPUs: restricts syscalls
- Firecracker → Logging and metrics: emits telemetry
- KVM VM and vCPUs → Logging and metrics: emits telemetry
- CPU template helper → CPU compatibility: produces and verifies