- OCaml 86%
- C 14%
pid1 #
PID 1 primitives for Linux VM init processes.
On Linux, the first process has work that nobody else will do. It mounts the
kernel filesystems and reaps orphaned children. It confines the daemons it
starts. When everything is over, it powers the machine off. A minimal VM in
the LinuxKit style has no shell and no busybox to lean on, so its init must
do all of this itself, and pid1 gives you the pieces to write that init in
OCaml with Eio.
The filesystem side covers mounts, idmapped mounts, detached mount hand-off,
switch_root, and the device-mapper verity, linear and crypt targets. On
the process side you get signal handling and child reaping. A child can be
spawned under a profile that sets its capabilities, rlimits, identity,
seccomp filter and hidden paths, and cgroup ceilings are computed beside it.
For the machine itself there are reboot(2), kexec and a hardware
watchdog to keep fed. Built from these are a service supervisor (a state
machine with exponential backoff) and a relay that passes the guest's obs
traces to a collector on the host. Where Eio does not expose a system call,
such as mount(2) or reboot(2), a C stub wraps it.
Some background on the kernel features involved. dm-verity checks a
read-only block device against a hash tree. Each data block is hashed with
the algorithm the table names, usually SHA-256. Those hashes are hashed
again level by level, up to a single root hash supplied when the target is
set up. The kernel checks every block against the tree before the reader
sees it, so a modified block gives an I/O error instead of its data.
Confinement draws on four mechanisms. Capabilities split root's power into
about forty bits such as CAP_NET_ADMIN, and an rlimit caps one resource of
a process, for example the number of open files. A cgroup accounts for the
CPU, memory and I/O of a group of processes and can limit them. A seccomp
filter is a BPF program that the kernel runs on every system call to decide,
from the call's number and arguments, whether to allow it.
Installation #
Install with opam:
$ opam install pid1
If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install pid1
Usage #
let or_die = function
| Ok () -> ()
| Error e -> failwith (Format.asprintf "%a" Pid1.pp_error e)
let init env =
Eio.Switch.run @@ fun _sw ->
let fs = Eio.Stdenv.fs env in
or_die (Pid1.setup_filesystems ~fs);
Pid1.setup_signals ();
(* ... run your services, drain child exits, pet the watchdog ... *)
or_die (Pid1.poweroff ())
(* Entry point: [Eio_main.run init] *)
API #
- Filesystem setup.
mountandmount_flagswrapmount(2).setup_filesystemsmounts devtmpfs, proc and sysfs where they usually go.dm_verity_create,dm_linear_create,dm_linear_mapanddm_crypt_createset up device-mapper targets, andswitch_rootmoves to a new root.open_tree_cloneandmove_mount_fdhand a mount to a confined child as a descriptor. - Signal handling.
setup_signalsinstalls a SIGCHLD handler that reaps children, and handlers for SIGTERM and SIGINT.shutdown_requestedtells you whether a termination signal came in. - System control.
poweroff,haltandrestartcallreboot(2).kexec_file_loadfollowed bykexec_rebootboots a new kernel. - Least-privilege profiles.
spawn_confinedstarts a child under aprofile: its capabilities, rlimits, identity, seccomp policy and hidden paths are applied between fork and exec. The policy is aSeccomp.tfrom theseccomppackage, so its rules can read a syscall's arguments. If the child cannot be confined, it does not run.Cgroup.writeslists the files that put a process under cgroup v2 ceilings, for the caller to write. - Child exit tracking.
wait_forwaits for one child without racing the SIGCHLD reaper, anddrain_child_exitsreturns the exit records the reaper has collected. - Service supervisor.
svc_stepis a pure state machine. It moves a service through Pending, Starting, Running, Backoff and Failed, with an exponential backoff between attempts.deps_satisfiedchecks that the services a service depends on are running. - Watchdog.
open_watchdog ~sw ~device ()opens the device once under the switch that owns it. Pass the returned watchdog topet_watchdogfor each keepalive; the flow stays open between calls and closes when the owner switch ends. An absent device is inert while its owner lives. Opening or writing a present device, or petting after its owner ends, returns a typed error. UseEio.Path.(fs / "/dev/watchdog")for the usual device, or select a numbered one when the machine has more than one. - Observability.
Obs_relaymakes init theobscollector of the guest and forwards what every producer sends to the host.
License #
ISC. See LICENSE.md.