> ## Documentation Index
> Fetch the complete documentation index at: https://axe-engine.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Firecracker

> Run axed as a Firecracker microVM's PID 1 over vsock, and keep processes running across snapshot and restore.

This setup was tested with Firecracker v1.18.0 and a 6.1 guest kernel.

You need a Linux host with read-write access to `/dev/kvm`, Docker, `curl`, `python3` and `mkfs.ext4`, the `firecracker` binary from a [Firecracker release](https://github.com/firecracker-microvm/firecracker/releases), and an uncompressed guest kernel (`vmlinux`), such as one of the 6.1 kernels Firecracker publishes for its own testing.

## Build the root filesystem

The guest needs the static binaries, a shell for the commands you run, and an init that mounts the kernel filesystems and execs axed so it becomes PID 1. This example uses static busybox:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
docker build --target static -t axe:static .
mkdir -p rootfs
cid=$(docker create busybox:1.37.0-musl); docker export "$cid" | tar -x -C rootfs; docker rm "$cid"
cid=$(docker create axe:static); mkdir -p rootfs/usr/local/bin
docker cp "$cid:/axed" rootfs/usr/local/bin/; docker cp "$cid:/axectl" rootfs/usr/local/bin/; docker rm "$cid"
mkdir -p rootfs/{proc,sys,dev,tmp,workspace}
```

Write `rootfs/init`:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
#!/bin/sh
mount -t proc proc /proc
mount -t sysfs sys /sys
mountpoint -q /dev || mount -t devtmpfs dev /dev
mkdir -p /dev/pts && mount -t devpts devpts /dev/pts
mount -t cgroup2 cgroup2 /sys/fs/cgroup
mount -t tmpfs tmp /tmp
mount -t tmpfs workspace /workspace
exec /usr/local/bin/axed --listen 'vsock://any:1024?auth=none' --root-dir /workspace \
  --agent-sandbox=false --log-format text
```

Then make it executable and build an ext4 image:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
chmod +x rootfs/init
mkfs.ext4 -q -F -d rootfs -E root_owner=0:0 rootfs.ext4 128M
```

What the axed flags do:

* `--listen 'vsock://any:1024?auth=none'` listens on vsock port 1024 without a token. Outside the guest, only the host reaches the vsock channel through the VMM, so the channel is the trust boundary. Inside the guest, any process could connect over the loopback CID, so an `auth=none` vsock listener refuses connections from the guest itself and accepts only the host. Passing `--listen` replaces the default TCP gRPC listener.
* `--agent-sandbox=false` turns off the sandboxd API. Without it, axed also opens the REST API on TCP port 8080.
* The init mounts `/dev/pts` so terminal sessions work, and `/sys/fs/cgroup` so axed can use cgroups.

## Boot the VM

Start Firecracker, wait for its API socket to appear, then point the boot source at your kernel with `init=/init`, attach the rootfs, and add a vsock device:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
api() { curl -fsS --unix-socket /tmp/fc.sock -X "$1" "http://localhost/$2" \
  -H 'Content-Type: application/json' -d "$3"; }

rm -f /tmp/fc.sock /tmp/fc-vsock
firecracker --api-sock /tmp/fc.sock & fc=$!
while [ ! -S /tmp/fc.sock ]; do sleep 0.05; done
api PUT boot-source '{"kernel_image_path":"vmlinux","boot_args":"console=ttyS0 reboot=k panic=1 pci=off init=/init"}'
api PUT drives/rootfs '{"drive_id":"rootfs","path_on_host":"rootfs.ext4","is_root_device":true,"is_read_only":false}'
api PUT machine-config '{"vcpu_count":2,"mem_size_mib":512}'
api PUT vsock '{"guest_cid":3,"uds_path":"/tmp/fc-vsock"}'
api PUT actions '{"action_type":"InstanceStart"}'
```

## Connect from the host

Firecracker exposes the guest's vsock as a Unix socket on the host (`uds_path`). A host client connects to it, writes `CONNECT 1024\n`, reads `OK <n>\n`, and then the stream reaches the guest listener.

`axectl` dials plain Unix sockets, so put a small proxy in between that performs the handshake for each connection. The repository has one you can use or adapt, [`tests/firecracker/vsock_proxy.py`](https://github.com/vincent0426/axe/blob/main/tests/firecracker/vsock_proxy.py):

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
python3 tests/firecracker/vsock_proxy.py /tmp/axe.sock /tmp/fc-vsock 1024 &
export AXE_ADDR=unix:///tmp/axe.sock
axectl info
```

Use the `axectl` binary you copied into `rootfs/usr/local/bin`, or put a copy on your `PATH`. `axectl info` shows `pid1 true` and the guest's cgroup. In your own platform, perform the same handshake in your host agent's dialer.

## Cgroups in the guest

As PID 1 with the cgroup v2 hierarchy mounted, axed takes over the guest's cgroups with `--cgroups auto`. Each process gets its own cgroup, so whole-tree kill works, and memory limits are enforced when the kernel has the `memory` controller (an over-limit process is reported `oom-killed`). CPU limits need the `cpu` controller. axed enables whichever of `cpu`, `memory` and `pids` the guest kernel provides; check `/sys/fs/cgroup/cgroup.subtree_control` in the guest to see which ones yours has.

## Snapshot and restore

Pause the VM, take a full snapshot, and later load it into a new Firecracker process:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
api PATCH vm '{"state":"Paused"}'
api PUT snapshot/create '{"snapshot_type":"Full","snapshot_path":"snap","mem_file_path":"mem"}'
kill "$fc"    # stop this firecracker process

# later, start a new one and load the snapshot:
rm -f /tmp/fc.sock /tmp/fc-vsock
firecracker --api-sock /tmp/fc.sock & fc=$!
while [ ! -S /tmp/fc.sock ]; do sleep 0.05; done
api PUT snapshot/load '{"snapshot_path":"snap","mem_backend":{"backend_type":"File","backend_path":"mem"},"resume_vm":true}'
```

After restore, detached processes are still running and their output replays intact from any retained offset. Client connections die with the old VMM, so clients reconnect through the restored vsock socket and continue from the last offset they saw. The `instance_id` stays the same, because the process table survived.

### Call axectl resumed after every restore

Firecracker freezes the guest clocks across the snapshot, so timeouts stay correct without help. But the clocks do not show the gap, so axed does not detect the resume on its own, and the guest's wall clock falls behind. After every restore, report the restore with `System.Configure`:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl resumed
```

It sends the host's wall time and 64 bytes of fresh entropy, and prints the new generation, how far the clock was stepped, and whether the entropy was added, in the form `generation N, clock stepped S.SSSs, entropy added`. axed checks the request, then handles it in this order:

1. Steps the guest's wall clock to the host's time if it is more than 50 ms off. This needs `CAP_SYS_TIME`; without it the call fails here and nothing else happens.
2. Adds the entropy to the guest's random pool, so clones of one snapshot stop sharing random state. This needs `CAP_SYS_ADMIN`. It is best effort: without the capability the entropy is skipped and the output says `entropy not added`.
3. Records the resume, which increments the generation.

axed has both capabilities as root PID 1, as in this setup. `--no-entropy` skips step 2.

To quiesce processes before the snapshot, run `axectl freeze` first and `axectl freeze --thaw` after restore. Freezing needs cgroups. See [Snapshots](https://axe-engine.mintlify.site/snapshots.md).

## What was tested

With Firecracker v1.18.0 and a 6.1 guest kernel, using the setup on this page:

* axed runs as PID 1 and takes the guest's cgroup v2 hierarchy.
* Offset replay, PTYs, memory limits (`oom-killed`) and whole-tree kill work inside the microVM.
* After pause, snapshot, 15 seconds stopped and restore: the instance id is unchanged, the detached process is still running and producing output, replay from offset 0 is intact, and `axectl resumed` records the resume.
* A 20-second timeout fires after about 20 seconds of running time, not counting the 15 seconds stopped.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.