# myugan/firecracker-python architecture

> Architecture diagram of the GitHub repository myugan/firecracker-python, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/myugan/firecracker-python
- Repository: https://github.com/myugan/firecracker-python

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Layer
    subgraph "User Layer"
        direction TB
        CLI["examples/create_vm.py"]:::docs
        Docs1["docs/getting-started.md"]:::docs
        Docs2["docs/api-reference.md"]:::docs
        Docs3["docs/network.md"]:::docs
        Test["tests/test_microvm.py"]:::docs
    end

    %% Python Library Layer
    subgraph "Python Library Layer"
        direction TB
        subgraph "Core API"
            MicroVM["firecracker/microvm.py"]:::lib
        end
        subgraph "REST Adapter"
            API["firecracker/api.py"]:::lib
        end
        subgraph "Process Management"
            VMM["firecracker/vmm.py"]:::lib
            Process["firecracker/process.py"]:::lib
        end
        subgraph "Network Management"
            Network["firecracker/network.py"]:::lib
            NetConfig["configure_vm_network.py"]:::lib
        end
        subgraph "Support Modules"
            Config["firecracker/config.py"]:::lib
            Utils["firecracker/utils.py"]:::lib
            Logger["firecracker/logger.py"]:::lib
            Exceptions["firecracker/exceptions.py"]:::lib
            Scripts["firecracker/scripts.py"]:::lib
        end
    end

    %% Firecracker VMM Process
    FirecrackerBin["firecracker (VMM binary)"]:::external

    %% Host OS / Kernel
    subgraph "Host OS / Kernel"
        direction TB
        KVM["Linux KVM"]:::host
        NetStack["Network Stack (TAP, iptables)"]:::host
    end

    %% Guest microVM
    Guest["Guest MicroVM"]:::guest

    %% Connections
    CLI -->|"imports & calls"| MicroVM
    Test -->|"uses API"| MicroVM
    Docs1 -.-> CLI
    Docs2 -.-> API
    Docs3 -.-> Network

    MicroVM -->|"spawns"| VMM
    MicroVM -->|"uses"| API
    MicroVM -->|"configures"| Network

    VMM -->|"launch"| FirecrackerBin
    Process --> FirecrackerBin
    API -->|"HTTP REST over Unix socket"| FirecrackerBin

    FirecrackerBin -->|"KVM syscalls"| KVM
    Network -->|"creates TAP & iptables rules"| NetStack
    NetStack -->|"forwards traffic"| Guest

    %% Click Events
    click CLI "https://github.com/myugan/firecracker-python/blob/main/examples/create_vm.py"
    click MicroVM "https://github.com/myugan/firecracker-python/blob/main/firecracker/microvm.py"
    click API "https://github.com/myugan/firecracker-python/blob/main/firecracker/api.py"
    click VMM "https://github.com/myugan/firecracker-python/blob/main/firecracker/vmm.py"
    click Process "https://github.com/myugan/firecracker-python/blob/main/firecracker/process.py"
    click Network "https://github.com/myugan/firecracker-python/blob/main/firecracker/network.py"
    click NetConfig "https://github.com/myugan/firecracker-python/blob/main/configure_vm_network.py"
    click Config "https://github.com/myugan/firecracker-python/blob/main/firecracker/config.py"
    click Utils "https://github.com/myugan/firecracker-python/blob/main/firecracker/utils.py"
    click Logger "https://github.com/myugan/firecracker-python/blob/main/firecracker/logger.py"
    click Exceptions "https://github.com/myugan/firecracker-python/blob/main/firecracker/exceptions.py"
    click Scripts "https://github.com/myugan/firecracker-python/blob/main/firecracker/scripts.py"
    click Docs1 "https://github.com/myugan/firecracker-python/blob/main/docs/getting-started.md"
    click Docs2 "https://github.com/myugan/firecracker-python/blob/main/docs/api-reference.md"
    click Docs3 "https://github.com/myugan/firecracker-python/blob/main/docs/network.md"
    click Test "https://github.com/myugan/firecracker-python/blob/main/tests/test_microvm.py"

    %% Styles
    classDef lib fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef external fill:#90EE90,stroke:#000,stroke-width:1px
    classDef host fill:#FFA500,stroke:#000,stroke-width:1px
    classDef guest fill:#D3D3D3,stroke:#000,stroke-width:1px
    classDef docs fill:#EEE8AA,stroke:#000,stroke-width:1px
```
