Lua at the Boot Loader
You think Lua is a scripting language for games and embedded systems.
FreeBSD uses it in the loader, before the kernel even clears its throat.
What runs first:
The FreeBSD boot sequence is layered:
- Firmware
- Boot loader (boot1, boot2, loader)
- Kernel
- init / userspace
The loader is the gatekeeper between bare metal and the kernel. It decides what to load, how to load it, and what environment the kernel will inherit.
And the loader speaks Lua.
Modern FreeBSD ships /boot/lua/loader.lua as the default loader script. It is executed by the loader to read configuration, check passwords, build the menu, and load the kernel and modules.
Why Lua at this stage:
- The loader needs real logic, not brittle shell.
- It must stay small and deterministic.
- It must run in a constrained environment with limited services.
Lua fits the shape. It is compact. It has tables. It does not demand an OS to exist before it runs.
The environment is different:
Loader Lua is not full userland Lua. It runs inside the boot loader, not inside a kernel or a process.
It cannot assume you have libc, threads, or anything that looks like your normal runtime.
It is a knife, not a kitchen.
What it does:
- Loads configuration
- Checks passwords
- Builds the boot menu
- Loads the kernel (and modules)
When you pick a boot option, you are speaking to Lua.
The point:
FreeBSD uses Lua in the loader because it needs control before the kernel exists. The loader is not a shell. It is a policy engine.
Lua gives it policy without bloat.
Remember this the next time you type at a boot prompt: your first command is executed in a script environment that exists outside the operating system itself.
The kernel is not awake yet.
The script still runs.
— Kim Jong Rails, Supreme Leader of the Republic of Derails