Skip to main content

A Python client library to interact with Firecracker microVMs

Project description

firecracker-python

Firecracker

firecracker-python is a simple Python library that makes it easy to manage Firecracker microVMs. It provides a simple way to create, configure, and manage microVMs.

Some features are still being developed and will be added in the future. You can track these in the TODO.md file.

asciicast

Table of Contents

How to Install

To install from PyPI, you need to have a personal access token with read access to the repository.

pip3 install firecracker-python

Or install from source, by cloning the repository and installing the package using pip:

git clone https://github.com/myugan/firecracker-python.git
cd firecracker-python

# Using uv (recommended)
uv sync --dev

# Or using pip
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt
pip3 install -e .

Key Features

  • Easily create microVMs with default or custom settings
  • View a list of all running microVMs
  • Access and modify microVM settings
  • Remove one or all microVMs
  • Connect to microVMs using SSH
  • Set up port forwarding in microVMs

Getting Started

The easiest way to get started is to use the official Firecracker setup script:

# Run the official setup script (downloads official CI kernel and rootfs)
./assets/rootfs/setup-firecracker-official.sh

# Then run the sample script
./examples/sample.py

This will:

  1. Download the latest Firecracker kernel from official CI (tested with Ubuntu)
  2. Download the official Ubuntu rootfs from Firecracker CI
  3. Set up SSH keys for root access
  4. Create a properly configured ext4 rootfs image

The official setup uses Firecracker's CI kernel and rootfs which are proven to work together, avoiding kernel compatibility issues.

Manual Setup (Alternative):

If you prefer to build your own rootfs, see FIRECRACKER_SETUP.md for detailed instructions.

Prerequisites

Before running the setup script, ensure you have:

  • Firecracker binary installed at /usr/local/bin/firecracker or /usr/bin/firecracker
  • KVM enabled on your system: lsmod | grep kvm
  • Docker installed and running (for rootfs setup)
  • Python 3.9+ installed

Enable IP Forwarding

To enable networking for Firecracker VMs, run:

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -P FORWARD ACCEPT

Quick Start Example

# Activate virtual environment and run sample
source .venv/bin/activate
./examples/sample.py

The sample script includes:

  • Automatic IP conflict detection
  • VM creation with verified files
  • SSH connection capability
  • Cleanup instructions

To get started with firecracker-python, check out the getting started guide

Usage

Here are some examples of how to use the library.

Create a microVM with custom configuration and list them all

from firecracker import MicroVM

# Create a new microVM with custom configuration
vm = MicroVM(vcpu=2, memory="4096")
# Or
vm = MicroVM(vcpu=2, memory="4G")

vm.create()

# List all running microVMs
vms = MicroVM.list()  # Static method to list all VMs
for vm in vms:
    print(f"VM with id {vm['id']} has IP {vm['ip_addr']} and is in state {vm['state']}")

Delete a microVM by id or all microVMs

from firecracker import MicroVM

# Create a new microVM
vm = MicroVM()
vm.create()

# Delete the microVM just created
vm.delete()

# Delete a specific microVM by ID
vm.delete(id="<specific_id>")

# Delete all microVMs
vm.delete(all=True)

Enable port forwarding

During initialization:

from firecracker import MicroVM

# Single port
vm = MicroVM(expose_ports=True, host_port=10222, dest_port=22)
# Multiple ports
# vm = MicroVM(expose_ports=True, host_port=[10222, 10280], dest_port=[22, 80])

vm.create()

After creation you can also expose ports using the port_forward function:

from firecracker import MicroVM

vm = MicroVM()
vm.create()

# Forward a single port
vm.port_forward(host_port=10222, dest_port=22)
# 'Port forwarding added successfully'

# Forward multiple ports
vm.port_forward(host_port=[10222, 10280], dest_port=[22, 80])
# 'Port forwarding added successfully'

# Remove port forwarding
vm.port_forward(host_port=10222, dest_port=22, remove=True)
# 'Port forwarding removed successfully'

Note: When using port forwarding, you need to specify both host_port and dest_port. The number of host ports must match the number of destination ports.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a Pull Request (PR).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hysn_firecracker_python-1.0.5.tar.gz (283.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hysn_firecracker_python-1.0.5-py3-none-any.whl (42.4 kB view details)

Uploaded Python 3

File details

Details for the file hysn_firecracker_python-1.0.5.tar.gz.

File metadata

  • Download URL: hysn_firecracker_python-1.0.5.tar.gz
  • Upload date:
  • Size: 283.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for hysn_firecracker_python-1.0.5.tar.gz
Algorithm Hash digest
SHA256 7db5e9b4d2f7503cbb8720fae60ce81fae27baa3f86d6b223b33566b79a6e9a8
MD5 49e211524b30a1d56b74d020611602fe
BLAKE2b-256 8896f3d54363e5e7a7ff52aa4cb5dd7c17f5c4c1e22c764038fa6b5328794215

See more details on using hashes here.

Provenance

The following attestation bundles were made for hysn_firecracker_python-1.0.5.tar.gz:

Publisher: pypi-release.yml on hysnsec/firecracker-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hysn_firecracker_python-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for hysn_firecracker_python-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 94f7b77568efaf0672543893f92964bee1cf112850fe3b9ee442bf909417fd17
MD5 3ac494b736c3f3830f050f6e3e49b87b
BLAKE2b-256 dece97adc1146025954253afd33d70b7b2c9c339fd218b4b3f596969aa115282

See more details on using hashes here.

Provenance

The following attestation bundles were made for hysn_firecracker_python-1.0.5-py3-none-any.whl:

Publisher: pypi-release.yml on hysnsec/firecracker-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page