All resources are used for demonstration purposes and are not intended for production. For a production deployment, follow the production host setup recommendations and run Firecracker under the jailer.
Once you have cloned the repository (see
Getting a Firecracker Binary), you can check if
your system meets the requirements by running tools/devtool checkenv.
An opinionated way to run Firecracker is to launch an
EC2 c5.metal instance with Ubuntu 24.04.
Firecracker requires the KVM Linux kernel module to perform its virtualization and emulation tasks.
We exclusively use .metal instance types, because EC2 only supports KVM on
.metal instance types.
Note
A Nitro Sandbox is the exception to both
statements. It is isolated by the AWS Nitro System rather than by KVM, needs
no /dev/kvm access, and runs on virtualized instance types that support AWS
Nitro Enclaves. See its
requirements.
The commands in this guide use curl, wget, jq, ssh and ssh-keygen,
unsquashfs (from squashfs-tools), mkfs.ext4 and e2fsck (from
e2fsprogs), ip (from iproute2) and nft (from nftables). Building
Firecracker from source additionally requires git and docker. On Amazon
Linux 2023, sudo dnf install -y squashfs-tools nftables git docker installs
the ones that are not part of the base image.
Firecracker supports x86_64 and aarch64 Linux, see specific supported kernels.
Firecracker requires read/write access to /dev/kvm, which the kernel creates
when KVM initialises, that is when the kernel has KVM support (built in or as
the kvm module) and the CPU exposes virtualization extensions.
The presence of KVM can be checked with:
ls -l /dev/kvmAn example output where it is enabled:
crw-rw---- 1 root kvm 10, 232 Nov 13 17:23 /dev/kvmIf the device is missing, KVM is not available on this host; on EC2 that means
the instance is neither a .metal type nor one with nested virtualization
enabled.
Some Linux distributions use the kvm group to manage access to /dev/kvm,
while others rely on access control lists. If you have the ACL package for your
distro installed, you can grant Read+Write access with:
sudo setfacl -m u:${USER}:rw /dev/kvmIf access is managed via the kvm group, check that the KVM group exists and
that /dev/kvm belongs to it in the ls -l output above:
getent group kvmYou can see if your current user is already in the kvm group by running:
groupsOtherwise, add your current user to the group by running:
[ $(stat -c "%G" /dev/kvm) = kvm ] && sudo usermod -aG kvm ${USER} \
&& echo "Access granted."If none of the above works, you will need to either install the file system ACL
package for your distro and use the setfacl command as above, or run
Firecracker as root (via sudo).
You can check if you have access to /dev/kvm with:
[ -r /dev/kvm ] && [ -w /dev/kvm ] && echo "OK" || echo "FAIL"In production, Firecracker is designed to be run securely inside an execution
jail, set up by the jailer binary, which applies cgroups,
namespaces (including a network namespace via --netns), resource limits and
drops privileges. This is how our
integration test suite does it. The host
itself should be configured following the
production host setup recommendations.
For simplicity, this guide will not use the jailer and runs the microVM in the
host's network namespace; see
Isolating the microVM network for how to
change that.
To successfully start a microVM, you will need an uncompressed Linux kernel binary, and an ext4 file system image (to use as rootfs). This guide uses the latest kernel image and Ubuntu rootfs available in our CI for the latest release.
The Ubuntu rootfs from our CI runs fcnet-setup.sh at boot, which assigns each
network interface whose MAC address starts with 06:00: the IP address encoded
in its last four bytes (06:00:AC:10:00:02 becomes 172.16.0.2). The MAC
address and TAP IP used later in this guide depend on that. If you bring your
own rootfs, configure the guest network as described in
network-setup instead.
ARCH="$(uname -m)"
S3="https://s3.amazonaws.com/spec.ccfc.min"
# Fetch latest CI artifact build. `sort` + `tail` returns artifacts with latest YYYYMMDD.
CI_ARTIFACTS_PREFIX=$(curl -fsSL "$S3?list-type=2&prefix=firecracker-ci/&delimiter=/" \
| grep -oP "(?<=<Prefix>)firecracker-ci/[0-9]{8}-[^/]+/(?=</Prefix>)" \
| sort \
| tail -1)
latest_kernel_key=$(curl -fsSL "$S3?list-type=2&prefix=${CI_ARTIFACTS_PREFIX}${ARCH}/vmlinux-" \
| grep -oP "(?<=<Key>)(${CI_ARTIFACTS_PREFIX}${ARCH}/vmlinux-[0-9]+\.[0-9]+\.[0-9]{1,3})(?=</Key>)" \
| sort -V \
| tail -1)
# Download a linux kernel binary
wget "$S3/${latest_kernel_key}"
latest_ubuntu_key=$(curl -fsSL "$S3?list-type=2&prefix=${CI_ARTIFACTS_PREFIX}${ARCH}/ubuntu-" \
| grep -oP "(?<=<Key>)(${CI_ARTIFACTS_PREFIX}${ARCH}/ubuntu-[0-9]+\.[0-9]+\.squashfs)(?=</Key>)" \
| sort -V \
| tail -1)
ubuntu_version=$(basename $latest_ubuntu_key .squashfs | grep -oE '[0-9]+\.[0-9]+')
# Download a rootfs from Firecracker CI
wget -O ubuntu-$ubuntu_version.squashfs.upstream "$S3/$latest_ubuntu_key"
# The rootfs in our CI doesn't contain SSH keys to connect to the VM
# For the purpose of this demo, let's create one and patch it in the rootfs
unsquashfs ubuntu-$ubuntu_version.squashfs.upstream
ssh-keygen -f id_rsa -N ""
cp -v id_rsa.pub squashfs-root/root/.ssh/authorized_keys
mv -v id_rsa ./ubuntu-$ubuntu_version.id_rsa
# create ext4 filesystem image
sudo chown -R root:root squashfs-root
truncate -s 1G ubuntu-$ubuntu_version.ext4
sudo mkfs.ext4 -d squashfs-root -F ubuntu-$ubuntu_version.ext4
# The extracted tree is owned by root now and no longer needed
sudo rm -rf squashfs-root
# Verify everything was correctly set up and print versions
echo
echo "The following files were downloaded and set up:"
KERNEL=$(ls vmlinux-* | tail -1)
[ -f $KERNEL ] && echo "Kernel: $KERNEL" || echo "ERROR: Kernel $KERNEL does not exist"
ROOTFS=$(ls *.ext4 | tail -1)
e2fsck -fn $ROOTFS &>/dev/null && echo "Rootfs: $ROOTFS" || echo "ERROR: $ROOTFS is not a valid ext4 fs"
KEY_NAME=$(ls *.id_rsa | tail -1)
[ -f $KEY_NAME ] && echo "SSH Key: $KEY_NAME" || echo "ERROR: Key $KEY_NAME does not exist"There are two options for getting a firecracker binary:
- Downloading an official firecracker release from our release page, or
- Building firecracker from source.
To download the latest firecracker release, run:
ARCH="$(uname -m)"
release_url="https://github.com/firecracker-microvm/firecracker/releases"
latest=$(basename $(curl -fsSLI -o /dev/null -w %{url_effective} ${release_url}/latest))
curl -L ${release_url}/download/${latest}/firecracker-${latest}-${ARCH}.tgz \
| tar -xz
# Rename the binary to "firecracker"
mv release-${latest}-$(uname -m)/firecracker-${latest}-${ARCH} firecrackerTo instead build firecracker from source, you will need to have docker
installed:
ARCH="$(uname -m)"
# Clone the firecracker repository
git clone https://github.com/firecracker-microvm/firecracker firecracker_src
# Start docker
sudo systemctl start docker
# Build firecracker
#
# It is possible to build for gnu, by passing the arguments '-l gnu'.
#
# This will produce the firecracker and jailer binaries under
# `./firecracker_src/build/cargo_target/${toolchain}/debug`.
#
sudo ./firecracker_src/tools/devtool build
# Rename the binary to "firecracker"
sudo cp ./firecracker_src/build/cargo_target/${ARCH}-unknown-linux-musl/debug/firecracker firecrackerSee Building Firecracker for release builds and other build options.
Running firecracker will require two terminals, the first one running the firecracker binary, and a second one for communicating with the firecracker process via HTTP requests:
API_SOCKET="/tmp/firecracker.socket"
# Remove API unix socket
sudo rm -f $API_SOCKET
# Run firecracker
sudo ./firecracker --api-sock "${API_SOCKET}" --enable-pciThe --enable-pci flag instructs Firecracker to create all VirtIO devices using
a PCI VirtIO transport. This flag is optional. If not passed, Firecracker will
create devices using the legacy MMIO transport. We suggest that users enable the
PCI transport, as it yields higher throughput and lower latency for VirtIO
devices. For more information regarding guest kernel requirements for using PCI
look at our kernel policy documentation.
In a new terminal (do not close the 1st one), set up the host side of the
network and configure the microVM. The firewall rules below use nft; if your
host does not have it, use the iptables-nft rules from
network-setup instead (legacy
iptables accepts the same syntax):
TAP_DEV="tap0"
TAP_IP="172.16.0.1"
GUEST_IP="172.16.0.2"
MASK_SHORT="/30"
# Setup network interface
sudo ip link del "$TAP_DEV" 2> /dev/null || true
sudo ip tuntap add dev "$TAP_DEV" mode tap user "$(id -u)" group "$(id -g)"
sudo ip addr add "${TAP_IP}${MASK_SHORT}" dev "$TAP_DEV"
sudo ip link set dev "$TAP_DEV" up
# Enable ip forwarding
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
# This tries to determine the name of the host network interface to forward
# VM's outbound network traffic through. If outbound traffic doesn't work,
# double check this returns the correct interface!
HOST_IFACE=$(ip -j route list default |jq -r '.[0].dev')
# Set up microVM internet access: NAT the guest's traffic out of the host
# interface. The table is recreated from scratch so that this block can be
# re-run safely.
sudo nft delete table firecracker 2> /dev/null || true
sudo nft add table firecracker
sudo nft "add chain firecracker postrouting { type nat hook postrouting priority srcnat; policy accept; }"
sudo nft add rule firecracker postrouting ip saddr "$GUEST_IP" oifname "$HOST_IFACE" counter masquerade
# Forwarded traffic is filtered in the standard FORWARD chain, which other
# software on the host (Docker, for example) shares. Create it with policy drop
# if it does not exist yet, so that the host only forwards what a rule allows.
if ! sudo nft list chain ip filter FORWARD > /dev/null 2>&1; then
sudo nft add table ip filter
sudo nft "add chain ip filter FORWARD { type filter hook forward priority filter; policy drop; }"
fi
# Allow traffic between the guest and the host interface. These rules only
# match on interfaces so that iptables, which manages this chain for Docker and
# others, can still read it.
sudo nft add rule ip filter FORWARD iifname "$TAP_DEV" oifname "$HOST_IFACE" accept comment \"firecracker\"
sudo nft add rule ip filter FORWARD iifname "$HOST_IFACE" oifname "$TAP_DEV" accept comment \"firecracker\"
# On top of that, only let replies to connections the guest opened reach it.
# This chain has policy accept because it must not decide for any other
# traffic: a packet has to pass both this chain and FORWARD.
sudo nft "add chain firecracker forward { type filter hook forward priority filter; policy accept; }"
sudo nft add rule firecracker forward oifname "$TAP_DEV" ct state established,related accept
sudo nft add rule firecracker forward oifname "$TAP_DEV" drop
API_SOCKET="/tmp/firecracker.socket"
LOGFILE="./firecracker.log"
# Set log file
sudo curl -X PUT --unix-socket "${API_SOCKET}" \
--data "{
\"log_path\": \"${LOGFILE}\",
\"level\": \"Debug\",
\"show_level\": true,
\"show_log_origin\": true
}" \
"http://localhost/logger"
# Set the number of vCPUs and the memory size. If this request is skipped, the
# microVM boots with 1 vCPU and 128 MiB.
sudo curl -X PUT --unix-socket "${API_SOCKET}" \
--data "{
\"vcpu_count\": 2,
\"mem_size_mib\": 1024
}" \
"http://localhost/machine-config"
KERNEL="./$(ls vmlinux* | tail -1)"
KERNEL_BOOT_ARGS="console=ttyS0 reboot=k panic=1"
# Set boot source
sudo curl -X PUT --unix-socket "${API_SOCKET}" \
--data "{
\"kernel_image_path\": \"${KERNEL}\",
\"boot_args\": \"${KERNEL_BOOT_ARGS}\"
}" \
"http://localhost/boot-source"
ROOTFS="./$(ls *.ext4 | tail -1)"
# Set rootfs
sudo curl -X PUT --unix-socket "${API_SOCKET}" \
--data "{
\"drive_id\": \"rootfs\",
\"path_on_host\": \"${ROOTFS}\",
\"is_root_device\": true,
\"is_read_only\": false
}" \
"http://localhost/drives/rootfs"
# The IP address of a guest is derived from its MAC address with
# `fcnet-setup.sh`, this has been pre-configured in the guest rootfs. It is
# important that `TAP_IP` and `FC_MAC` match this.
FC_MAC="06:00:AC:10:00:02"
# Set network interface
sudo curl -X PUT --unix-socket "${API_SOCKET}" \
--data "{
\"iface_id\": \"net1\",
\"guest_mac\": \"$FC_MAC\",
\"host_dev_name\": \"$TAP_DEV\"
}" \
"http://localhost/network-interfaces/net1"
# Start microVM
sudo curl -X PUT --unix-socket "${API_SOCKET}" \
--data "{
\"action_type\": \"InstanceStart\"
}" \
"http://localhost/actions"
# Wait for the guest to boot and for sshd to start before we attempt to SSH
# into it.
sleep 2s
KEY_NAME=./$(ls *.id_rsa | tail -1)
# Setup internet access in the guest
ssh -i $KEY_NAME root@172.16.0.2 "ip route add default via 172.16.0.1 dev eth0"
# Setup DNS resolution in the guest
ssh -i $KEY_NAME root@172.16.0.2 "echo 'nameserver 8.8.8.8' > /etc/resolv.conf"
# glibc resolves A/AAAA in parallel; on an IPv6-connected host the AAAA query
# can stall on host NAT, adding seconds per lookup.
ssh -i $KEY_NAME root@172.16.0.2 "echo 'options single-request-reopen' >> /etc/resolv.conf"
# SSH into the microVM
ssh -i $KEY_NAME root@172.16.0.2
# Use `root` for both the login and password.
# Run `reboot` to exit.Issuing a reboot command inside the guest will gracefully shutdown
Firecracker. This is due to the fact that Firecracker doesn't implement guest
power management.
The setup above creates the TAP device in the host's network namespace. We
recommend giving each microVM its own network namespace: it keeps per-VM
firewall rules, routes and TAP devices separate, it is required when several
snapshots of the same microVM (which restore with the same TAP device name and
IP address) are restored on the same host, and it is what the jailer's --netns
flag and our integration tests use.
The outline is: create a namespace, create the TAP device inside it, connect the
namespace to the host with a veth pair, and NAT inside the namespace:
sudo ip netns add fc0
sudo ip netns exec fc0 ip tuntap add dev tap0 mode tap
sudo ip netns exec fc0 ip addr add 172.16.0.1/30 dev tap0
sudo ip netns exec fc0 ip link set tap0 up
sudo ip link add name veth1 type veth peer name veth0 netns fc0
# ... assign addresses to veth1/veth0, add a default route and a NAT rule inside
# fc0, and enable forwarding on the hostThen either start Firecracker with sudo ip netns exec fc0 ./firecracker ...,
or pass --netns /var/run/netns/fc0 to the jailer. The full recipe, including
the veth addressing and the host-side rules, is in
Network Connectivity for Clones; the
jailer flags are described in jailer.md.
You can boot a guest without sending API requests by passing the parameter
--config-file to the Firecracker process. If you also pass --no-api,
Firecracker will not create the API socket at all. E.g.:
sudo ./firecracker --config-file <path_to_the_configuration_file> --no-api
path_to_the_configuration_file is the path to a JSON file with the
configuration for all of the microVM's resources. The JSON must contain the
configuration for the guest kernel and rootfs, all of the other resources are
optional. This configuration method will also start the microVM, as such you
need to specify all desired pre-boot configurable resources in the JSON. The
names of the resources can be seen in
firecracker.yaml and the names
of their fields are the same that are used in the API requests.
An example of configuration file is provided:
tests/framework/vm_config.json.
Once the guest is booted, refer network-setup to bring up the network in the guest machine.
If you omit --no-api and pass --api-sock as before, you can still use the
socket to send API requests for post-boot operations after the microVM has
started.
Once the microVM has shut down, remove the TAP device, the nftables table and the forwarding rules added above:
sudo ip link del tap0
sudo nft delete table firecracker
sudo nft -a list chain ip filter FORWARD | awk '/comment "firecracker"/ {print $NF}' \
| xargs -r -n1 sudo nft delete rule ip filter FORWARD handleIP forwarding stays enabled, and the FORWARD chain stays in place with policy
drop if it did not exist before. If nothing else on the host needs them (other
guests or Docker, for example), disable forwarding with
echo 0 | sudo tee /proc/sys/net/ipv4/ip_forward and then remove the chain with
sudo nft delete chain ip filter FORWARD.
See network-setup for the cleanup of the
iptables-nft based setup.
- Production host setup and the jailer: what to change before running untrusted workloads.
- Network setup:
nftrules, multiple guests, bridged networking and cleanup. - Custom rootfs and kernel images.
- Snapshotting and networking for restored clones.
- Rate limiting block and network devices.
- MMDS, vsock and seccomp.
Integration tests can be run with tools/devtool test.
The test suite is designed to ensure our SLA parameters
as measured on EC2 .metal instances, as such performance tests may fail when not
run on these machines. Specifically, don't be alarmed if you see
tests/integration_tests/performance/test_process_startup_time.py failing when
not run on an EC2 .metal instance. You can skip performance tests with:
./tools/devtool test -- --ignore integration_tests/performanceIf you run the integration tests on an EC2 .metal instance, and encounter failures such as the following
FAILED integration_tests/style/test_markdown.py::test_markdown_style - requests.exceptions.ReadTimeout: HTTPConnectionPool(host='169.254.169.254', port=80): Read timed out. (read timeout=2)
try running
aws ec2 modify-instance-metadata-options --instance-id i-<your instance id> --http-put-response-hop-limit 2.
The integration tests framework uses IMDSv2 to determine information such as
instance type. The additional hop is needed because the IMDS requests will pass
through docker.
Points to check to confirm the API socket is running and accessible:
- Check that the user running the Firecracker process and the user using
curlhave equivalent privileges. For example, if you run Firecracker with sudo that you runcurlwith sudo as well. - SELinux can regulate
access to sockets on RHEL based distributions. How user's permissions are
configured is environmentally specific, but for the purposes of
troubleshooting you can check if it is enabled in
/etc/selinux/config. - With the Firecracker process running using
--api-sock /tmp/firecracker.socket, confirm that the socket is open:ss -a | grep '/tmp/firecracker.socket'- If you have socat available, try
socat - UNIX-CONNECT:/tmp/firecracker.socketThis will throw an explicit error if the socket is inaccessible, or it will pause and wait for input to continue.