| Crates.io | firecracker-http-client |
| lib.rs | firecracker-http-client |
| version | 0.1.3 |
| created_at | 2024-12-26 12:22:42.812781+00 |
| updated_at | 2024-12-26 13:12:11.824827+00 |
| description | A comprehensive HTTP client for the Firecracker VMM API |
| homepage | |
| repository | https://github.com/CremeCrackerCloud/firecracker-http-client |
| max_upload_size | |
| id | 1495629 |
| size | 140,128 |
A comprehensive Rust HTTP client library for interacting with the Firecracker VMM API. This client provides a safe, ergonomic interface for managing Firecracker microVMs with full support for all Firecracker v1.11.0 features.
Add this to your Cargo.toml:
[dependencies]
firecracker-http-client = "0.1.3"
The client is organized into trait-based modules for different Firecracker operations:
BootSourceOperations: Manage kernel and boot parametersDriveOperations: Configure block devicesNetworkInterfaceOperations: Set up network interfacesMachineConfigOperations: Configure VM resourcesSnapshotOperations: Create and load VM snapshotsMetricsOperations: Configure metrics collectionLoggerOperations: Manage loggingInstanceOperations: Control VM lifecycleuse firecracker_http_client::{MachineConfig, machine::MachineConfigOperations};
let config = MachineConfig {
vcpu_count: Some(2),
mem_size_mib: Some(1024),
smt: Some(false),
track_dirty_pages: Some(true),
..Default::default()
};
client.put_machine_config(&config).await?;
use firecracker_http_client::{NetworkInterface, network::NetworkInterfaceOperations};
let network = NetworkInterface {
iface_id: "eth0".to_string(),
host_dev_name: "tap0".to_string(),
guest_mac: Some("AA:BB:CC:DD:EE:FF".to_string()),
..Default::default()
};
client.put_network_interface("eth0", &network).await?;
use firecracker_http_client::{Drive, drive::DriveOperations};
let drive = Drive {
drive_id: "rootfs".to_string(),
path_on_host: "/path/to/rootfs.ext4".to_string(),
is_root_device: true,
is_read_only: false,
..Default::default()
};
client.put_drive("rootfs", &drive).await?;
The basic_vm.rs example shows how to configure a simple microVM:
use firecracker_http_client::{
FirecrackerClient,
BootSource,
Drive,
MachineConfig,
NetworkInterface,
boot::BootSourceOperations,
drive::DriveOperations,
network::NetworkInterfaceOperations,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = FirecrackerClient::new("http://localhost:8080").await?;
// Configure machine resources
let machine_config = MachineConfig {
vcpu_count: Some(2),
mem_size_mib: Some(1024),
..Default::default()
};
client.put_machine_config(&machine_config).await?;
// Set up boot source
let boot_source = BootSource {
kernel_image_path: "/path/to/vmlinux".to_string(),
boot_args: Some("console=ttyS0".to_string()),
..Default::default()
};
client.put_boot_source(&boot_source).await?;
Ok(())
}
The vm_lifecycle.rs example demonstrates the full VM lifecycle:
// Start the VM
let start_action = InstanceActionInfo {
action_type: "InstanceStart".to_string(),
};
client.create_sync_action(&start_action).await?;
// Monitor VM state
let instance_info = client.describe_instance().await?;
println!("VM state: {}", instance_info.state);
// Graceful shutdown
let shutdown_action = InstanceActionInfo {
action_type: "SendCtrlAltDel".to_string(),
};
client.create_sync_action(&shutdown_action).await?;
The snapshot.rs example shows how to create and load VM snapshots:
// Create snapshot
let snapshot_params = SnapshotCreateParams {
snapshot_path: "/tmp/snapshot".to_string(),
mem_file_path: "/tmp/snapshot.mem".to_string(),
snapshot_type: Some("Full".to_string()),
version: Some("1.0".to_string()),
};
client.create_snapshot(&snapshot_params).await?;
// Load snapshot
let load_params = SnapshotLoadParams {
snapshot_path: "/tmp/snapshot".to_string(),
mem_file_path: "/tmp/snapshot.mem".to_string(),
enable_diff_snapshots: Some(true),
};
client.load_snapshot(&load_params).await?;
The client provides detailed error types for better error handling:
use firecracker_http_client::FirecrackerError;
match result {
Err(FirecrackerError::Api { status_code, message }) => {
eprintln!("API error {}: {}", status_code, message);
}
Err(FirecrackerError::Network(e)) => {
eprintln!("Network error: {}", e);
}
Err(FirecrackerError::Validation(e)) => {
eprintln!("Validation error: {}", e);
}
Ok(_) => println!("Operation successful"),
}
firecracker --api-sock /tmp/firecracker.sock
# Basic VM setup
cargo run --example basic_vm
# Complete VM lifecycle
cargo run --example vm_lifecycle
# Snapshot management
cargo run --example snapshot
# Run all tests
cargo test
# Run specific test
cargo test test_name
# Run with logs
RUST_LOG=debug cargo test
This project is licensed under the Apache License, Version 2.0.