How myugan/firecracker-python works
Project type and purpose
• This is an open-source Python client library (wrapper) that manages Firecracker microVM instances via the Firecracker VMM binary’s REST API.
• It provides classes and helper scripts to create, configure, launch, inspect, and tear down microVMs, and to wire up networking (taps, port forwarding).
Main components
a. User layer
• CLI or user script (e.g., examples/create_vm.py) or any client code that imports firecracker.MicroVM.
b. Python library modules (all living under firecracker/)
• microvm.py – the high-level MicroVM abstraction
• api.py – the HTTP client for Firecracker’s REST API over a Unix socket
• vmm.py & process.py – code to spawn and manage the Firecracker VMM binary process
• network.py & configure_vm_network.py – logic to create tap devices, iptables rules, port forwarding
• config.py – defaults and configuration objects
• utils.py, logger.py, exceptions.py, scripts.py – helpers, logging, error handling
c. External Firecracker VMM binary
• The firecracker executable that runs under KVM. Exposes a REST API over a Unix socket.
d. Host OS / Kernel
• KVM module, cgroups, TAP interfaces, iptables
e. Guest microVM
• The isolated Firecracker microVM with its own kernel and filesystem.
Relationships and interactions
• User Script → Python Library: calls MicroVM(...), create(), delete(), port_forward(), list().
• Python Library → Firecracker VMM
• Uses process.py/vmm.py to launch the binary.
• Uses api.py to issue HTTP PUT/PATCH/GET requests (JSON) over the VM socket.
• Python Library → Host OS Networking
• Calls configure_vm_network.py or functions in network.py to create a TAP device, move it into the VM’s net namespace, set up NAT/iptables & port forwarding.
• Firecracker VMM → Host Kernel
• Through KVM syscalls to allocate and run the microVM, attach TAP, manage memory & vCPUs.
• Guest VM → External Network
• Packets NAT’d/forwarded through the host TAP.
Architectural patterns & design principles
• Layered Architecture:
• Presentation (User scripts/CLI)
• Client Library (domain logic in microvm.py)
• Infrastructure glue (process management + HTTP API client + networking code)
• Platform & OS (Firecracker binary + KVM + Linux kernel)
• Separation of Concerns: each submodule handles a distinct responsibility
• Adapter Pattern: api.py adapting Python calls into HTTP calls to Firecracker
• Factory/Builder: config.py + scripts.py to assemble JSON configurations
1. User Layer (scripts & examples)
2. Python Library Layer (modules grouped logically)
3. Firecracker VMM Process
4. Host OS / Kernel (KVM, network stack)
5. Guest microVM
b. Within the Python Library layer, group modules:
• Core API: microvm.py
• REST Adapter: api.py
• Process Management: vmm.py, process.py
• Network Management: network.py, configure_vm_network.py
• Support: config.py, utils.py, logger.py, exceptions.py
• From User → Core API
• Core API → Process Mgmt (spawn binary)
• Core API → REST Adapter → Firecracker socket
• Core API → Network Mgmt → Host OS net stack
• Firecracker → Host OS KVM
• Host OS net stack → Guest NIC
• Python 3, requests or stdlib HTTP client
• Firecracker VMM binary
• Linux KVM, TAP, iptables
Essential details not to omit
• The Unix socket REST API bridge (api.py ↔ Firecracker binary).
• The process launch (how vmm.py spawns Firecracker).
• The network namespace and tap creation code.
• The MicroVM abstraction as the main entry point.