This repository contains the code of urunit, a simple init process designed
for Linux guests running over urunc. It
acts as a reaper and correctly prepares and forwards command-line arguments
to the target application.
The key features of urunit are:
- Parsing and grouping multi-word arguments from Linux kernel boot parameters.
- Launching and waiting for the target application until it terminates.
- Reaping all zombie processes.
- Setting the default route to eth0, if
URUNIT_DEFROUTEenvironment variable is set. - Reading and setting the environment variables for an application from a file.
- Reading and setting the execution environment configuration for a process from a file.
- Reading and mounting attached block devices defined in the configuration file.
- Serving
urunc execsessions with a built-in agent, started as a child ofurunitbefore the application when a container boot staged the binary at/run/urunc/urunit. Once the application exits the agent getsSIGTERM. - Running as the early init of a urunc generic container boot (Linux only).
- Stopping the application with
SIGTERMwhen the ACPI power button of the VM is pressed (x86), and then powering the VM off instead of restarting it.
urunit picks its role from the name it runs under (argv[0]), or from
URUNIT_MODE (init, agent, supervise or signal) when that is set:
| Name | Role |
|---|---|
urunit |
the supervisor of the application (PID 1) |
init |
the early init of a container boot: it mounts the container root, switches to it and becomes the supervisor |
urunit-agent |
the exec agent alone |
URUNIT_MODE=signal |
sends the signal number in argv[1] to PID 1. urunc runs /run/urunc/urunit this way through the exec agent to stop a guest |
The supervisor forwards SIGTERM, SIGINT, SIGHUP, SIGQUIT, SIGUSR1 and SIGUSR2 to the application. As PID 1 it would otherwise drop them. When the application exits, the supervisor syncs and reboots or powers off the guest.
In the early init a failed step that the workload depends on ends the boot; the clock, the host name, the embedded program, the network files and cgroup2 are logged and skipped. The root is the first of:
/urunc-rootfs-lowerin the initrd: the read-only disk it names (block:<serial>:<fstype>). Disks are found by their virtio-blk serial.root=,rootfstype=androotflags=on the kernel command line. hull on macOS names the virtiofs tagrootfsthis way.- hull's block rootfs
/dev/vda, when hull's/urunc-cmdor/urunc-envis in the initrd.
The root is mounted under an overlay with /urunc-rootfs-lower,
/urunc-overlay or /urunc-rootfs-upper, and directly otherwise.
/urunc-rootfs-upper names the upper layer (block[:<serial>] or
virtiofs); without it the upper is a tmpfs. A /urunit.conf in the initrd
is staged, and the supervisor reads it through URUNIT_CONFIG.
A container boot initrd needs only /urunit, with /init as a symlink to
it. On FreeBSD urunit is always the supervisor.
make static EMBED=<path> builds a static program for the target architecture
into urunit. The early init starts it before the root is mounted, from a
memfd, so the initrd stays one file and the root mount and the workload run
after it: a tracing or monitoring helper that has to be in place first. It is a
child of PID 1, reaped like every other, with its output on /dev/kmsg, and
tracefs is mounted at /sys/kernel/tracing before it starts.
The build fixes when and how it runs:
| Variable | Default | Effect |
|---|---|---|
EMBED_NAME |
embedded |
its argv[0] |
EMBED_WHEN |
empty: always | a path that must exist for it to start, such as a device the kernel offers only when the feature is there |
EMBED_READY |
empty: no wait | a flag that takes a file descriptor, such as --ready-fd; the program gets the write end of a pipe through it, and the init waits up to three seconds for one byte, so the workload starts after the program is ready. A program that exits first or stalls is logged and the boot goes on. |
A build without EMBED starts nothing.
Building urunit is as simple as running make, but make sure that make and
a C compiler is installed. In particular:
make static: buildsurunitas a statically-linked binarymake static_debug: buildsurunitas a statically-linked binary enabling debug messagesmake dynamic: buildsurunitas a dynamically-linked binarymake dynamic_debug: buildsurunitas a dynamically-linked binary enabling debug messagesmake check: builds and runs the unit tests (Linux only)
NOTE: The default build target is
make static, hence runningmakewill buildurunitstatically.
To use urunit, prefix it before the application you want to run.
For instance, to run a ls command:
urunit ls
There are no arguments specifically for urunit. The first argument is treated
as the application to execute with the rest passed as arguments to that
application.
One of the main purposes of urunit is to support multi-word CLI arguments
passed via urunc to an application in a Linux VM. Since the Linux kernel
cannot differentiate multi-word arguments in boot parameters (treating each
word as a separate argument), urunc follows a convention: multi-word arguments
are enclosed in single quotes. urunit then reassembles them properly before
execution.
For example, running:
urunit echo `hello world`
will result in echo receiving a single argument: hello world.
In order to configure the execution environment for an application, urunit
accepts a specific configuration file. The configuration file can contain the
following information:
- The list of the environment variables to set for the application
- The configuration for the process execution environment.
- The application command to execute, when
urunitis started without a command line (optional). - The list of mounts of block devices. Each block device is defined by its serial id and it will get mounted in the defined mountpoint.
- The network configuration (IP address, gateway and netmask) for guests that can not get it from the kernel command line (optional).
The file can be specified to urunit setting the URUNIT_CONFIG
environment variable with the path to the configuration file.
The configuration file consists of records. Every record is a NUL-terminated
string (\0), section markers included. Since neither environment variables
nor arguments can contain a NUL character, every value is stored verbatim, even
if it contains new lines (e.g. a certificate). For example:
printf 'UES\0PATH=/bin\0CERT=line1\nline2\0UEE\0' > urunit.conf
urunit also reads the legacy format, where the records are lines. The first
record tells the two apart: a NUL before the first new line means
NUL-terminated records. A leading URUNIT1 record, which an earlier writer
put first, is skipped.
Empty records are ignored, so the file can be padded with zeros (e.g. when it is
exposed as a raw block device). A PAD record also ends the configuration and
anything after it is ignored. Section markers (UES, UEE, ...) must match
the whole record and fields must match the whole key, including :. Hence, an
environment variable such as UEE_MODE=1 does not end the list.
The supported records of the configuration file are the following, one per line for readability:
URUNIT1
UES
/* list of environment variables */
UEE
UCS
UID: <uid_for_the_application>
GID: <gid_for_the_application>
WD: <working_directory>
ARC: <number_of_application_arguments>
ARV: <application_argument>
...
UCE
UBS
ID: <serial_id>
MP: <mountpoint>
...
UBE
UNS
IP: <ipv4_address>
GW: <gateway>
MSK: <netmask>
UNE
Inside the UCS section, the application command is optional: ARC holds the
number of arguments and is followed by exactly that many ARV lines, one per
argument, each taken verbatim. It is used when urunit is started without a
command line, in which case the command from the configuration is executed.
The UNS section is optional and provides the network configuration for
guests that can not get it from the kernel command line: IP is the IPv4
address, GW the gateway and MSK the netmask. An empty value (e.g. IP:)
leaves the field unset.
A configuration file that does not begin with the URUNIT1 record is parsed in
the legacy format, where every record is a line instead. It is still supported
for compatibility, but it can not store values that contain new lines.
Using one of the following methods, we can install urunit either in a
container image or in any other supported target.
Simply running:
make
make install
will build and install urunit at /usr/local/bin/. You can override the installation path using the PREFIX` variable.
Each release includes statically linked binaries for x86_64 and aarch64. Download and install with:
wget -O /usr/local/bin/urunit https://github.com/nubificus/urunit/releases/download/v0.1.0/urunit_x86_64
chmod +x /usr/local/bin/urunit
For easier distribution of urunit, the
harbor.nbfc.io/nubificus/urunit:latest contains a statically built version of urunit at /urunit. To extract it into another container:
FROM harbor.nbfc.io/nubificus/urunit:latest AS urunit
...
COPY --from=urunit /urunit /urunit