pid1#
Minimal OCaml PID 1 for Linux VMs.
Minimal init process for LinuxKit-style VMs with no shell or busybox. Mounts essential filesystems (devtmpfs, proc, sysfs), handles signals for zombie reaping and graceful shutdown, reads the superblock from virtio-blk devices, and provides a service supervisor state machine with exponential backoff.
The library uses C stubs for mount(2) and reboot(2) syscalls which have no
Eio equivalent. The companion spaceos-init binary is the actual init process:
it sets up filesystems, reads the superblock, manages parameters and event logs,
communicates with the host via virtio-serial ports, and runs FDIR integrity
monitoring.
Installation#
opam install pid1
Usage#
(* Library usage for building a custom init *)
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun _sw ->
let fs = Eio.Stdenv.fs env in
Pid1.setup_filesystems ~fs;
Pid1.setup_signals ();
match Pid1.read_superblock Eio.Path.(fs / "/dev/vda") with
| Ok sb when Pid1.check_magic sb && Pid1.check_crc sb ->
Fmt.pr "%a@." Pid1.pp_superblock sb
| Ok _sb -> Fmt.epr "Invalid superblock@."
| Error e -> Fmt.epr "Error: %s@." e
API#
- Filesystem setup --
mountwraps themount(2)syscall.setup_filesystemsmounts devtmpfs, proc, and sysfs at standard locations. - Signal handling --
setup_signalsinstalls SIGCHLD (zombie reaping), SIGTERM, and SIGINT handlers.shutdown_requestedreports whether a termination signal was received. - Superblock --
read_superblockreads a 48-byte superblock from a block device.check_magicvalidates the magic number (0x53504F53 / "SPOS").check_crcvalidates the CRC-32C. A Wire codec is provided for encoding/decoding. - System control --
poweroffandhaltinvokereboot(2). - Child exit tracking --
drain_child_exitsatomically collects child process exit records accumulated by the SIGCHLD handler. - Service supervisor -- Pure state machine (
svc_step) that drives service lifecycle through Pending, Starting, Running, Backoff, and Failed states with configurable exponential backoff and dependency tracking. - Watchdog --
pet_watchdogwrites to/dev/watchdogto prevent hardware watchdog resets.