ForgeOS Booted on a Real Server: Linux Kernel Below, Only Our Own Code Above
DEV Community

ForgeOS Booted on a Real Server: Linux Kernel Below, Only Our Own Code Above

Tonight a small cloud server, one core and 667 MB of RAM, booted the Linux kernel and above it nothing but our own code. For fifteen minutes forgeos.crowdware.info was served by ForgeOS: /init as PID 1, a small shell, ForgeCMS and a TLS gateway. No systemd, no libc, no OpenSSL, no nginx.

Then the machine restarted itself into its normal system, as planned, with its disk cleanly unmounted. This post is about how we got there, what the pieces are, and what does not work yet.

Why build it like this

ForgeOS is meant to become a free, non-violent operating system: no ads, no forced updates, no lock-in. I want "free" to go all the way down. So the rule is: every program on the system is built with tools we can build ourselves, from a small seed, on the machine itself.

That rules out the usual stack. Not because GCC, LLVM or glibc are bad, but because they are far too big to understand, and because a system that can only be built with a toolchain of a few million lines is not really ours.

So there are three layers of our own:

  • Anvil, a small systems language. Its first compiler is a NASM program of a few thousand lines. That one compiles the real compiler, written in Anvil, which then compiles itself again. The last two binaries must be byte for byte identical (a fixpoint). Anvil makes static x86-64 Linux programs that talk to the kernel through raw syscalls. No libc.
  • smsc, the compiler for SMS, our application language. smsc is written in SMS and emits Anvil. It also bootstraps from a seed (its own output, checked into the repo) and must reproduce itself exactly. No LLVM, no clang.
  • The SMS runtime, written in Anvil: a conservative mark-and-sweep garbage collector, an event loop on poll, an HTTP server and client, TLS 1.2 and 1.3 (X25519, Ed25519, ECDSA, RSA verify, ChaCha20-Poly1305, AES-GCM), an ACME client, PNG and JPEG.

On top of that run two programs written in SMS: ForgeCMS, a content management system of about 22,000 lines, and forge-gate, the door to the web. forge-gate takes ports 80 and 443, gets its own certificate from Let's Encrypt, terminates TLS and passes requests on to ForgeCMS.

Stage 1: our programs on a normal Linux

The first step was the cautious one. On a plain AlmaLinux VPS I installed NASM, cloned the repos and let everything build itself there:

FIXPOINT OK: stage 2 and stage 3 are identical (260611 bytes)
FIXPOINT OK: smsc compiles itself (1890065 bytes)

Both compilers reached their fixpoint on hardware they had never seen. Then smsc compiled ForgeCMS in 9 seconds and Anvil turned that into a 6.2 MB static binary in 7. forge-gate is 1.1 MB.

Both run as systemd services under their own users; forge-gate only gets CAP_NET_BIND_SERVICE. Given a domain, forge-gate fetched its certificate by itself within seconds. TLS 1.3, our own implementation, and openssl s_client and curl are happy with it.

A first load test: 4000 requests, 20 at a time, with keep-alive. All answered, about 610 per second on the single core, with the load generator running on the same core. ForgeCMS sat at 14 MB resident after start and 54 MB after all the tests.

One thing that looked like a bug and was not: with a fresh TCP connection for every request, all from one address, 2 to 5 percent of the connects timed out. The server never stalled (a probe every half second always answered within 0.5 s). The small VM's TIME_WAIT table (4096 entries) overflowed. Browsers keep connections open, so this is a load generator effect. But it took me a while to prove it.

Stage 2: the Linux kernel, and above it only Anvil

Stage 2 keeps the Linux kernel and throws away everything above it. The system is a 4.6 MB initramfs whose /init is an Anvil program. That /init had to learn a few things for a real cloud server:

  • Kernel modules. A distribution kernel has the virtio network card, the virtio disk and XFS as modules. /init loads them from a list with finit_module, before anything else. The build copies them with their dependencies from /lib/modules and decompresses them.
  • A /32 network. The provider gives the machine 82.165.218.93/32 with a gateway outside that network, configured by DHCP. /init sets the same values statically through ioctl on a socket, and adds a host route to the gateway before the default route.
  • The server's own disk. ForgeOS mounts AlmaLinux's XFS root and uses the data of stage 1, with the same user ids. The site, its posts and forge-gate's certificate are the same in both systems.
  • Services without root. /etc/services.conf is a few lines:
needs /disk
user 990:990 data /disk/var/lib/forgeos/forgecms service forgecms /srv/site/bin/forgecms --host 127.0.0.1 --port 8080 --data /disk/var/lib/forgeos/forgecms
user 989:989 bind data /disk/var/lib/forgeos/gate service forge-gate //bin/forge-gate --backend 127.0.0.1:8080 --data /disk/var/lib/forgeos/gate --domain forgeos.crowdware.info

user drops root before exec; bind keeps exactly one capability, as an ambient capability so it survives the exec. data hands the directory to that user before the first start.

  • A clean shutdown. /init stops the services, syncs, unmounts the disk and only then restarts.
  • A safety net. With forgeos.reboot=900 on the kernel command line, /init restarts the machine itself after fifteen minutes. The boot entry is set with grub2-reboot, so GRUB uses it exactly once. If I lost the machine, it would come back to its normal system on its own.

Before touching the server I booted its own AlmaLinux kernel with exactly the image built on it in QEMU locally, with an XFS disk laid out like the server's. All eight modules loaded, the disk mounted, the services started, the timer fired, the disk unmounted. Only the network could not be tested that way.

Then the real boot. Nothing answered for a few minutes, and the remote console showed messages from AlmaLinux's systemd, not from ForgeOS. What exactly happened there I still have to find out. After a reset in the console, GRUB took the ForgeOS entry. HTTPS answered in 25 ms, with the certificate forge-gate had fetched in stage 1. ps in our shell showed forgecms and forge-gate running as 990 and 989. I posted to the guestbook.

Fifteen minutes later ForgeOS restarted the machine, and AlmaLinux came back and said:

XFS (vda4): Ending clean mount

The post written under ForgeOS was on the disk.

What does not work yet

To be honest about it:

  • No way to update a running ForgeOS. There is no SSH. Today an update means booting back into AlmaLinux. Next step: forge-gate accepts a signed image over HTTPS, writes it to /boot and restarts.
  • No SSH. The cryptography is already in the runtime (X25519, Ed25519, ChaCha20, Poly1305, SHA-2), so an SSH server in Anvil, "forge-ssh", is the plan after that.
  • x86-64 only. Anvil has no ARM64 backend yet. That is the next big compiler task.
  • Every Anvil program reserves about 200 MB of address space at start. Only what is used becomes real memory, but on a 256 MB machine the kernel refuses to start /init at all. The heap should grow on demand instead.
  • Speed. Our code generator is simple. Image scaling and JPEG encoding are about 3.4 times slower than the C++ version with stb (they were 5.2 times slower this morning). HTTP serving is fast enough not to matter on this machine.
  • The kernel is still Linux. Stage 3, our own kernel, is a long way off.

Try it

Everything is on Codeberg:

  • Anvil: the language and its compiler
  • smsc: the SMS compiler and runtime (ready-built tools in its releases)
  • ForgeOS: /init, the shell, stage1/setup.sh, stage2/install.sh, and test.sh, which boots the whole thing in QEMU in a few seconds
  • ForgeCMS: the CMS this post is served by

If you have built a system from a small seed yourself, or you know where the traps are on the way from "Linux kernel plus own userland" to "own kernel", I would like to hear about it.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.