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
diskshold, not only pulled images - What a VM does at power-on: UEFI firmware finds
EFI/BOOT/BOOTAA64.EFIon a disk and runs it - The serial console, the console log and the display, with nothing but your own code inside
Topology
| Node | Disks | CPUs / memory | Addresses | Role |
|---|---|---|---|---|
hello | hello.img from ./build.sh, read-only | 1 / 512MiB | none | Prints 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-uefiBuild the program and its disk:
bash
cd examples/31-hello-os
./build.sh # writes hello.imgRun it
bash
vz validate
vz up
vz node console hello # shows "hi"; close the window to detachTry 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 NICsChange 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.imgand callsefi_main. The program never returns, so the firmware stays in charge of memory and devices. - Screen. Text written with the firmware's
OutputStringappears on the display. - Serial console. The firmware does not write to the virtio console that
vz node consoleshows, 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 downwaits 60 seconds for a shutdown that never comes and then powers the VM off.vz down --forcepowers it off right away. vz validatedoes not check thathello.imgexists. Without it,vz upstops withnode '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