- OCaml 90%
- C 9%
- WebAssembly <1%
- Raku <1%
mrg #
Cross-build, run and publish unikernels.
Warning. This is an experimental fork of mirage. Do not use it in production; use mirage instead.
Overview #
A unikernel is one application linked with the operating-system library code
it uses into a single image that boots directly on a hypervisor. A MirageOS
unikernel is described twice: once in its source, and once in a config.ml
file. Upstream MirageOS uses the functoria library to read that file:
mirage configure runs it and writes a main.ml that applies the unikernel's
functors to the device implementations of the chosen target, plus the build
rules for that target. mrg removes the second description. The unikernel's
source holds one capability term, written with the combinators of the
nox-mirage.eio library, and that term is the whole configuration. A
capability term is the list of devices passed to Mirage_eio.run, such as
[ Net.listener ~port:80 (netif "eth0") ]: each entry names an external
resource the unikernel may use, and the body receives one typed value per
entry and can reach nothing else. The same term builds for every backend, and
the dune build context picks the implementation. mrg is the command-line
tool around those combinators: it drives dune, the cross toolchains and
ocaml-oci, and it generates no code.
mrg works on a unikernel project inside the existing dune workspace, from
any directory in it, and resolves paths against the dune root. It builds no
separate environment.
mrg init my-app # scaffold a hello-world project
mrg up -t solo5 my-app # install the solo5 cross toolchain
mrg build -t solo5 my-app # build it (mrg run does this for you)
mrg run -t solo5 my-app # boot it under tender-hvf (macOS) or tender-kvm (Linux)
mrg run -t solo5 my-app -- --help # the boot arguments the unikernel accepts
mrg push -t solo5 my-app -n registry.example/my-app:latest
Installation #
Install with opam:
$ opam install mrg
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 mrg
Commands #
mrg init scaffolds a self-contained project: a dune-project, a
hello-world capability term in unikernel.ml, and a dune file holding the
native (unix) executable, which plain dune build builds. The generated
cross-build rules go into an mrg/ subdirectory, so the project root holds
only your code, as with mirage configure. mrg/cross.dune, included by the
root dune, holds the solo5, unikraft and xen executables, each gated
on the dune context of its toolchain; mrg/dune holds the rule that, on
dune test, regenerates mrg/cross.dune from the root dune and fails when
the two differ. A Solo5 tender is the small host process that loads the
unikernel into a hardware-virtualised guest (through KVM on Linux or
Hypervisor.framework on macOS, the hvt target), attaches the devices the
image declares and services the guest's few calls for console, clock,
network and block I/O.
mrg dune DIR [TARGET] prints the cross-build rules for the unikernel in
DIR, read from its native executable in DIR/dune: the whole set, or one
target's.
mrg up -t <target> sets up a target, like mirage configure -t. It
regenerates mrg/cross.dune from the project's current dependencies, then
installs the target's cross toolchain through opam-x toolchain up (for
solo5, unikraft and xen) and the unikernel's dependencies built for
it. The unix and wasi targets need no toolchain.
mrg build -t <target> cross-builds a unikernel and prints the path
of the artefact. A unikernel is a dune project, so this is dune build for
the unix target and dune build -x <toolchain> for a freestanding one,
which puts the artefact under _build/default.<toolchain>/. mrg run and
mrg push run the same build first, so mrg build is for CI and for
checking that a target compiles without running it. The target defaults to
solo5.
mrg run builds the unikernel and runs it under the tender, engine or
toolstack its target needs: a Solo5 tender with slirp networking for
solo5 (tender-hvf on macOS, tender-kvm on Linux), qemu-system-<arch>
for unikraft, wasmtime for wasi, xl create for xen, and the
binary itself for unix. Slirp is a user-space TCP/IP stack that turns the
guest's packets into ordinary host sockets, so the guest has a network
without root or a tap device. For solo5, the bindings are checked against
the Solo5 manifest inside the artefact before boot. The manifest is a
table in an ELF note of the unikernel binary listing each device the guest
expects, by name and type (NET_BASIC or BLOCK_BASIC). Devices.plan
refuses a run whose bindings do not match it, and the tender attaches no
device the manifest does not name. The network defaults to
the slirp gateway when a network device is declared (--net overrides
it), and each declared block device needs a --disk IMAGE, bound in
manifest order. -p HOST:GUEST publishes a port, on the unix target as
well, where the native backend parses it itself, and --host-forward
proxies connections from the guest to the host. Everything after -- goes
to the unikernel's own boot arguments; --help there prints the boot flags
the capability term contributes, such as --ipv4, or --port for a
listener.
A unikernel that declares an RNG seeds it at boot from the sources
--entropy=SOURCES allows, a comma-separated list of cpu, host and
jitter, and stops booting when no allowed source other than jitter answers.
cpu is the processor's RNG instruction: x86 RDSEED and RDRAND, aarch64 RNDRRS
and RNDR where the core implements FEAT_RNG (Apple silicon does not). host is
a seed the host hands the guest: on solo5 the tender draws 32 bytes from the
host's getentropy for every boot and passes them in guest memory, never on a
host command line, and the guest's startup code takes them out before the
unikernel sees its arguments; on unix and wasi it is the operating system's
generator the backend already draws from; on xen it is the device tree's
/chosen/rng-seed, which xl fills with fresh bytes for every domain from Xen
4.16 and a dom0less domain lacks; unikraft has no host seed yet, so there
only cpu can answer. jitter is execution-time jitter,
mixed in but never a source alone, because a host that hides the RNG
instruction and controls the cycle counter can predict it. The default is
cpu,host. A confidential VM, whose host is the adversary, boots with
--entropy=cpu, so on a processor without an RNG instruction it does not boot
at all.
mrg push uploads the built unikernel as an OCI image that urunc can run,
using ocaml-oci. An OCI image is the container image format of the Open
Container Initiative: a JSON manifest listing a configuration blob and
file-system layers (tar archives), each named by its SHA-256. A registry is
the HTTP server images are pushed to and pulled from by name. urunc is a
container runtime that, instead of starting a Linux container, boots the
unikernel found in such an image under the monitor its annotations name. The
image has one root file system layer carrying the binary at /unikernel and
the base64 urunc.json, the com.urunc.unikernel.* manifest annotations
(mirage and hvt for the Solo5 target, unikraft and qemu for the
Unikraft one), and the artefact's Solo5 device manifest as
com.urunc.unikernel.devices, so a registry shows what the image must be
given. --layout DIR writes the same image as an OCI layout (the oci:DIR of
skopeo and crane) instead of pushing it. Registry credentials come from
--username and --password-stdin, or from the MIRAGE_REGISTRY_USERNAME
and MIRAGE_REGISTRY_PASSWORD environment variables. The image covers one
platform, the artefact's own architecture; a multi-architecture index will
follow once more than one target architecture cross-builds here.
mrg registry DIR walks a tree of unikernels laid out as
<category>/<name>/ and prints the dune file that keeps the build rules
of every unikernel in it in step.
Pulling a pushed image and running it in production is the next step after
mrg push, and a separate piece of work.
Devices #
A device is glue over its package's Eio adapter. A module under
lib/eio wires capabilities, bridges a netif or frame channel to the
adapter, and owns the device's lifecycle. It holds no protocol state and
does not re-implement the adapter's driver loop, state cell, lock or retry
cadence over the I/O-free core. When a device needs a transport shape the
<pkg>-eio adapter lacks, such as frame channels instead of Eio flows, the
entry point goes into that adapter and the device keeps only the carrier
bridging. A device drives a core directly only when no adapter exists and
doing so holds no protocol state.
Cross builds #
dune build -x <toolchain>succeeds and builds nothing when the toolchain'sfindlib.conf.d/<toolchain>.confis missing: ocamlfind falls back to the host compiler.mrg buildchecks for the file first; a plain dune invocation does not, so check that the artefact exists and thatfilereports an ELF image, not Mach-O.- An object the cross linker pulls in by a flag, such as the Solo5 binding
object chosen by
-z solo5-abi=xen, is outside dune's graph: reinstalling it does not relink an image, and the dune cache can serve the stale link. Build cross targets withDUNE_CACHE=disabled, and change a tracked input to force the relink. - A
Configurator.V1probe compiles its test with theocamlconPATH, the host one, unless its rule passes-ocamlc %{ocamlc}; without it a feature test passes for the host and fails in the cross context.
Backends #
The same capability term runs on every nox-mirage.eio backend, and the
-t target picks the dune build context that links it. Each target is
named after the backend it compiles to (the -x toolchain, or unix and
wasi). The name of a tender or runner (hvt, qemu, wasm) is not a
target, and passing one prints the target to use instead.
| Backend | Target | What it is |
|---|---|---|
nox-mirage.eio.native |
unix |
A host binary on Eio_main. Host networking and file systems are adapted to the Solo5 device shapes, so development keeps the usual tools. |
nox-mirage.eio.solo5 |
solo5 |
A freestanding Solo5 ELF, run under a tender (tender-hvf on macOS, tender-kvm on Linux) with the unprivileged slirp gateway. |
nox-mirage.eio.unikraft |
unikraft |
A Unikraft kernel booted under qemu-system-<arch>. |
nox-mirage.eio.wasi |
wasi |
The Eio scheduler compiled to WebAssembly (wasm_of_ocaml --enable=wasi), run under wasmtime or Node's WASI. Networking uses host sockets (wasi:sockets, with TLS through wasi-tls) rather than a stack inside the guest. |
nox-mirage.eio.xen |
xen |
A Xen/ARM aarch64 guest. mrg run boots it as a dom0less domU under the Xen hypervisor named by $XEN, inside qemu-system-aarch64, with a device tree it generates; on a Xen host the same image runs as a domU under xl create. |
solo5 builds with the Solo5 cross toolchain and unikraft with the
Unikraft one, and mrg up -t <target> installs whichever is missing
through opam-x.
doc/image-size.md explains what keeps code in a unikernel image and how to find the units that cost the most.