Skip to content

31 Hello OS ​

A tiny operating system written in Rust that prints hi on the console. The node boots a disk you build yourself: vz pulls nothing and runs exactly what is on that disk.

What you practice ​

  • A node is a generic UEFI machine that boots whatever its disks hold, not only pulled images
  • What a VM does at power-on: UEFI firmware finds EFI/BOOT/BOOTAA64.EFI on a disk and runs it
  • The serial console, the console log and the display, with nothing but your own code inside

Topology ​

NodeDisksCPUs / memoryAddressesRole
hellohello.img from ./build.sh, read-only1 / 512MiBnonePrints hi, then echoes keys

Before you start ​

Install Rust with rustup, then add the UEFI target. It links with Rust's own linker, so nothing else is needed:

bash
rustup target add aarch64-unknown-uefi

Build the program and its disk:

bash
cd examples/31-hello-os
./build.sh                   # writes hello.img

Run it ​

bash
vz validate
vz up
vz node console hello    # shows "hi"; close the window to detach

Try it ​

bash
vz node logs hello       # "hi", saved because of consoleLog: true
vz node display hello    # "hi" on the screen too; type here and it echoes on both
vz node ls               # running, no NICs

Change the text in os/src/main.rs, run ./build.sh, then vz down --force and vz up to boot the new version.

How it works ​

The whole OS is os/src/main.rs, about 400 lines of Rust with no crates, built into a UEFI application of about 10 KB:

  • Boot. The VM's firmware loads the program from the FAT partition of hello.img and calls efi_main. The program never returns, so the firmware stays in charge of memory and devices.
  • Screen. Text written with the firmware's OutputString appears on the display.
  • Serial console. The firmware does not write to the virtio console that vz node console shows, so the program carries a small virtio driver: it finds the device through UEFI's PCI I/O protocol, sets up one transmit queue in a page of memory, and hands the device each string to send.
  • Watchdog. UEFI resets a machine 5 minutes after starting a boot program unless the program turns the watchdog off, which is the first thing it does.

build.sh compiles it with cargo and packs it into a 16 MiB raw disk with hdiutil: a GPT disk with one FAT partition holding EFI/BOOT/BOOTAA64.EFI, the path UEFI boots when a disk has no boot entry of its own.

Notes ​

  • The console window only shows output. hello-os reads keys from the display window, not from the serial console.
  • hello-os does not handle the ACPI power button, so a plain vz down waits 60 seconds for a shutdown that never comes and then powers the VM off. vz down --force powers it off right away.
  • vz validate does not check that hello.img exists. Without it, vz up stops with node 'hello': disk file not found.
  • The disk is attached read-only, and the node has no overlay: hello-os never writes to it.

Clean up ​

bash
vz down --force
vz destroy --yes   # deletes the state of this lab
rm -rf hello.img os/target