Skip to content

Run machines with Firecracker

This guide follows one machine on a server with the node role. It makes the machine, finds its systemd unit and its files, restarts hopboxd with the machine running, and deletes the machine. The Firecracker driver describes each step of the driver.

The node also connects each machine to its node network. Check the network of a machine follows the network of a machine.

hopbox new box1

The CLI waits until the machine runs. The first machine of an image takes longer, because the node unpacks the image first. On the tested server, it takes about 25 s. The log of hopboxd shows each step:

sudo journalctl -u hopboxd -o cat | grep -E 'unpacked|booted|state changed'
{"level":"INFO","msg":"unpacked the disk of an image","component":"firecracker","digest":"sha256:e591c258...","duration":22400000000}
{"level":"INFO","msg":"booted a microVM","component":"firecracker","machine":"01a1610f-...","unit":"hopbox-machine-01a1610f-....service","jail_id":1879048192,"tap":"hbt3f2c9a0b1d4e","duration":76000000}
{"level":"INFO","msg":"machine state changed","component":"executor","machine":"01a1610f-...","from":"starting","to":"running","boot_generation":0}

Run a command in the machine:

hopbox exec box1 uname -r
6.1.188-hopbox1

Each machine runs in a systemd unit of its own, in the slice hopbox-machines.slice:

systemctl list-units 'hopbox-machine-*'
UNIT LOAD ACTIVE SUB DESCRIPTION
hopbox-machine-01a1610f-26b5-71c8-abbd-93be11b94c53.service loaded active running hopbox machine 01a1610f-26b5-71c8-abbd-93be11b94c53

During the repair of the root file system of a machine before its boot, the list also has the unit hopbox-machine-<id>-fsck.service of e2fsck (the Firecracker driver).

The main process of the unit is Firecracker. It runs as the jail user of the machine, in the jail of the machine. The journal of the unit holds the serial console of the guest and the log of Firecracker:

sudo journalctl -u hopbox-machine-01a1610f-26b5-71c8-abbd-93be11b94c53.service

The disk of the machine is a reflink copy of the image, as large as the disk size of the machine. Only the blocks that the machine writes take space of their own:

sudo du -h --apparent-size /var/lib/hopbox/machines/01a1610f-26b5-71c8-abbd-93be11b94c53/disk.raw
sudo du -h /var/lib/hopbox/machines/01a1610f-26b5-71c8-abbd-93be11b94c53/disk.raw

The first command prints the size of the disk, 94G for 100 GB. The second prints the space of the file system that the file uses, at most the size of the image. The blocks that the disk shares with the image count in it too.

A restart or an upgrade of hopboxd stops only the processes of hopboxd.service. The unit of the machine is not one of them:

systemctl show -P MainPID hopbox-machine-01a1610f-26b5-71c8-abbd-93be11b94c53.service
sudo systemctl restart hopboxd
systemctl show -P MainPID hopbox-machine-01a1610f-26b5-71c8-abbd-93be11b94c53.service
hopbox exec box1 uptime

Both commands print the same PID, and uptime shows that the machine did not boot again. The new hopboxd logs attached to a microVM for the machine. Then it gives the machine a new link key, and the guest agent connects again.

A reboot or a shutdown of the server shuts each guest down cleanly. You do not need to stop the machines first. The unit of each machine runs hopboxd stop-guest at its stop, which asks the guest to shut down. Look at the stop command of the unit:

systemctl show -P ExecStop hopbox-machine-01a1610f-26b5-71c8-abbd-93be11b94c53.service

The output names /opt/hopbox/current/hopboxd stop-guest, with the API socket of the machine. At a shutdown, systemd stops hopboxd first and then the machines. Each guest writes its data to the disk, and unmounts its file systems. If a guest does not end within 30 s, systemd kills its Firecracker.

After the boot of the server, hopboxd boots each machine with the target running again. A machine that ran before the reboot thus runs again, with its disk:

sudo systemctl reboot

After the reboot, hopbox exec box1 uptime shows a new boot of the machine. A microVM that an older release booted can have no ExecStop. Stop and start such a machine once, so that its unit gets one.

Do not stop a machine with systemctl stop on the server. hopboxd did not ask for that stop, so the restart policy applies (the Firecracker driver). Use hopbox machine stop instead.

hopbox machine stop box1
hopbox machine start box1
hopbox machine delete box1

A stop asks the guest to shut down, and kills the microVM after 30 s. A start boots the machine from its disk, so the files of the machine stay. A delete stops the microVM, and removes its unit, its disk, its jail, and its tap device.

A machine whose boot fails gets the state failed (the node executor). The log of hopboxd holds the error of the boot, in the record a machine action failed of the executor:

sudo journalctl -u hopboxd -o cat | grep 'a machine action failed'

These are the common causes:

Error What to do
the node has no guest kernel with the sha256 <sha256>, which the image needs Install the release that shipped the image. Its install adds its kernel to the kernel cache.
the image cache has no image <digest>, and the image has no URL Install the release of the image, or register the image with --url.
the file system of <dir> has no reflinks Put /var/lib/hopbox on XFS made with mkfs.xfs -m reflink=1.
the jailer or Firecracker ended at its start Read the journal of the unit. The error names it.

After a failed boot, no microVM of the machine runs. The restart policy always boots the machine again after a backoff.

make test-kvm runs the same steps on a test host, with names of its own for the units and the tap devices.