Skip to content

Port the init binary code to Rust#670

Open
jakecorrenti wants to merge 24 commits into
containers:mainfrom
jakecorrenti:port-init
Open

Port the init binary code to Rust#670
jakecorrenti wants to merge 24 commits into
containers:mainfrom
jakecorrenti:port-init

Conversation

@jakecorrenti
Copy link
Copy Markdown
Member

@jakecorrenti jakecorrenti commented May 7, 2026

This PR ports the init binary code to Rust. It acts like any of the other crates that we have within the project.

To run the examples or with Podman, you would build the project as usual: make BLK=1 NET=1 && sudo make BLK=1 NET=1 install and continue with business as usual.

In my testing I've found the init binary to still remain small:

libkrun/init port-init ≡
❯ ll ../target/release/krun-init
.rwxr-xr-x@ 657k jcorrent  4 Jun 11:16 -I ../target/release/krun-init

NOTE: the AWS Nitro init binary is still written in C and will be ported next.

Fixes: #632

Comment thread src/init-blob/build.rs
@jakecorrenti jakecorrenti changed the title WIP: Port the init binary code to Rust Port the init binary code to Rust Jun 4, 2026
@jakecorrenti jakecorrenti marked this pull request as ready for review June 4, 2026 15:13
@jakecorrenti
Copy link
Copy Markdown
Member Author

@nohajc if you could give this a try on FreeBSD that would be awesome :)

@nohajc
Copy link
Copy Markdown
Contributor

nohajc commented Jun 4, 2026

@jakecorrenti I'll see what I can do. :)

Copy link
Copy Markdown
Contributor

@nohajc nohajc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, passing the command-line on FreeBSD is broken. See my comment.

This also reveals an issue in integration tests because you can see they TIMEOUT but the test run is marked as successful anyway. Not sure why...

Comment thread init/src/config.rs Outdated
}

pub fn load(#[cfg(target_os = "linux")] is_mount_point: impl Fn(&str) -> bool) -> Config {
let path = env::var("KRUN_CONFIG").unwrap_or_else(|_| CONFIG_FILE_PATH.to_string());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already found an issue when I noticed in the github action log that ISO_CONFIG_PATH is unused. It needs to be used instead of CONFIG_FILE_PATH on FreeBSD.

Copy link
Copy Markdown
Member Author

@jakecorrenti jakecorrenti Jun 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like things are running ok so far and added the fix you suggested timed out again, but not sure what's going on. i don't have a FreeBSD machine so i will have to figure it out

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try to play with it more tomorrow. Anyway, you should be able to run the full integration test including FreeBSD guest cases locally on either macOS or Linux. It downloads the sysroot and all other dependencies automatically.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FreeBSD tests are skipped by default but they should run once init-freebsd is first compiled.

Add init/src/config.rs, replacing the hand-rolled jsmn-based parser
with serde_json. Parses /.krun_config.json (or KRUN_CONFIG env var) and
returns a Config struct with:

- argv: Entrypoint ++ (args | Cmd), or None if absent
- workdir: WorkingDir or Cwd
- tmpfs: first tmpfs mount destination not already mounted

Environment variables from the Env array are applied during parsing,
with HOME and TERM always overwritten, all others set only if unset.
A missing or unparseable config file is silently ignored.

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Assisted-by: Claude Code:claude-sonnet-4.6
Add setup_network() and setup_dhcp() to env.rs.

setup_network() brings up lo unconditionally. setup_dhcp() checks that
the interface exists before calling do_dhcp(), and logs a warning on
failure rather than aborting (DHCP failure is non-fatal — the VM may be
IPv6-only or have no network).

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Assisted-by: Claude Code:claude-sonnet-4.6
Extend env.rs with:
- apply_hostname(): sets hostname from HOSTNAME env var, defaulting
  to "localhost"
- apply_env(): maps KRUN_HOME -> HOME and KRUN_TERM -> TERM
- apply_rlimits(): parses the KRUN_RLIMITS comma-separated list of
  id,cur,max triples and applies each via setrlimit(2)

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Assisted-by: Claude Code:claude-sonnet-4.6
Add exec.rs with:
- setup_redirects(): walks /sys/class/virtio-ports and dup2s
  krun-stdin/stdout/stderr onto the corresponding file descriptors
- set_exit_code(): reports the workload exit code to the host via
  KRUN_EXIT_CODE_IOCTL, only when the root fs is virtiofs
- run_workload(): forks so PID 1 can reap children; the child calls
  exec_workload() which sets up redirects and execvp's the argv.
  Parent waits for the child, reports exit code, syncs, and reboots.
  KRUN_INIT_PID1=1 skips the fork and exec_workload directly as PID 1.

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Assisted-by: Claude Code:claude-sonnet-4.6
Connect all modules in main() in order:
  1. mount_block_root()          [amd-sev | tdx]
  2. mount_filesystems()
  3. mount_block_root_device()   [KRUN_BLOCK_ROOT_DEVICE]
  4. mount_shared_root()
  5. setsid + TIOCSCTTY
  6. setup_network()
  7. config::load()
  8. mount_tmpfs()               [config tmpfs mount]
  9. apply_env / apply_hostname / apply_rlimits
 10. chdir to workdir
 11. run_workload(argv)

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Assisted-by: Claude Code:claude-sonnet-4.6
Add init/src/freebsd.rs with:
- kenv_get(): reads a variable from the FreeBSD kernel environment via
  kenv(2), which is the source of env vars for init before the process
  environment is set up
- populate_env_from_kenv(): imports the known KRUN_* variables from
  kenv into std::env at startup so the rest of the code can use
  std::env::var uniformly on both platforms
- open_console(): replicates login_tty(3) without linking libutil —
  revokes existing opens of /dev/console, opens it, creates a new
  session via setsid(2), sets the controlling terminal via TIOCSCTTY,
  and dup2s it onto stdio; falls back to /dev/null + /init.log
- mount_config_iso() / unmount_config_iso(): mounts the KRUN_CONFIG
  ISO 9660 image at /mnt via nmount(2) so the JSON config file can be
  read, then unmounts it afterwards

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Assisted-by: Claude Code:claude-sonnet-4.6
Connect the FreeBSD helpers into the boot sequence:
- open_console() and populate_env_from_kenv() are called at the very
  start of main() before anything else
- setsid/TIOCSCTTY are Linux-only; open_console() handles session setup
  on FreeBSD
- setlogin("root") is called on FreeBSD after console setup
- KRUN_DHCP and DHCP setup are Linux-only
- If KRUN_CONFIG is not set, mount_config_iso() is attempted; the ISO
  is unmounted immediately after config::load() returns
- fs::* mounts and mount_shared_root are Linux-only
- exec_workload() calls open_console() on FreeBSD instead of
  setup_redirects(), giving the child process a fresh controlling
  terminal before execvp

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Assisted-by: Claude Code:claude-sonnet-4.6
Replace the C-based BSD init build rule (which referenced the now-deleted
init/init.c) with a cargo build rule targeting the correct Rust triple.

Makefile:
- Remove dead INIT_SRC = init/init.c variable.
- Derive FREEBSD_RUST_TARGET from the host ARCH with arm64→aarch64
  substitution to get the correct Rust triple.
- Set CARGO_BSD_RUSTFLAGS with the clang cross-linker flags (mirroring
  the existing CC_BSD setup) so cargo can link for FreeBSD.
- aarch64-unknown-freebsd is a Tier 3 target with no prebuilt std;
  use +nightly -Z build-std for that case.

setup-build-env:
- Add rustup target add x86_64-unknown-freebsd (Tier 2, prebuilt std).
- Install nightly toolchain + rust-src for the aarch64 FreeBSD case.

cross-compilation.yml:
- Add clang to the Linux cross-compilation dependencies so the
  FreeBSD linker flags resolve correctly on Linux runners.

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Assisted-by: Claude Code:claude-sonnet-4.6
Implements the timesync feature behind the `timesync` cargo feature flag.
Receives host-side nanosecond timestamps over AF_VSOCK/SOCK_DGRAM on port
123 and applies them via clock_settime when the delta exceeds 100ms.

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Assisted-by: Claude Code:claude-sonnet-4.6
Delete init/init.c, init/dhcp.c, init/dhcp.h, init/jsmn.h, and the
entire init/tee/ directory (snp_attest.c/h and the KBS client).

The amd-sev feature no longer performs LUKS unlock or KBS attestation —
it mounts /dev/vda as ext4 like the tdx path does.

Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Assisted-by: Claude Code:claude-sonnet-4.6
Port of cd8b2be. The temporary root
directory hack has been replaced by NullFs, so the ioctl that cleaned
it up is no longer needed.

Assisted-by: Claude Code: claude-sonnet-4-6
Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Port of 2593acc. When TSI is active,
brings up dummy0 and assigns it 10.0.0.1/8 so applications that probe
for network availability see a configured interface. Silently skips
setup if the dummy driver is absent in the kernel.

Assisted-by: Claude Code: claude-sonnet-4-6
Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
The non-Linux setup_network() stub was empty, so the lo interface was
never raised inside FreeBSD guests. The C init unconditionally brought up
lo on all platforms (the #if __linux__ guard covered only the DHCP block,
not lo setup).

Use nix::sys::socket to open an AF_INET/SOCK_DGRAM socket and issue
SIOCSIFFLAGS / IFF_UP, matching the C behaviour.

Assisted-by: Claude Code: claude-sonnet-4-6
Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
The C init accepted "Cmd", "Env", "WorkingDir"/"Cwd", and "Entrypoint"
keys via case-insensitive comparison; the Rust port only handled OCI
runtime-spec keys ("args", "env", "cwd" inside "process").

Add serde aliases so RawConfig's flat fields also accept the Docker image
config capitalisation:
  - "Cmd" aliases "args"
  - "Env" aliases "env"
  - "WorkingDir"/"Cwd" alias "cwd"
  - new "Entrypoint" field (top-level, Docker format only)

When Entrypoint is present it is prepended to the resolved args vector,
matching the C init's concat_entrypoint_argv() behaviour.

Assisted-by: Claude Code: claude-sonnet-4-6
Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Two issues with apply_rlimits():

1. The C init parsed KRUN_RLIMITS with strtoull() and a single-char skip,
   so any separator character between ID, CUR, and MAX worked (e.g.
   "7:1024:4096").  The Rust code required "ID=CUR:MAX" and silently
   skipped entries using the historical colon-only format.

2. krun_set_rlimits() wraps the entire value in double-quotes
   (format!("\"{}\"", ...)), so the env var received by init is
   "\"7=1024:4096\"".  Neither the old Rust nor the C parser handled
   this correctly.

Fix both by extracting parse_rlimit_entry() which strips outer '"' chars
and splits on the first two occurrences of '=' or ':' via splitn(3).
Both formats and the quoted form now parse correctly.

Assisted-by: Claude Code: claude-sonnet-4-6
Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
The C init replaced argv[0] with "/bin/sh" when neither KRUN_INIT nor
a config file was present, forwarding remaining cmdline tokens as shell
arguments.  The Rust init instead treats proc_args[1] as the executable
directly.

Add a comment explaining the rationale: callers that omit both KRUN_INIT
and a config file intend the cmdline argument to be the command, not a
shell script path, making the Rust behaviour more intuitive.

Assisted-by: Claude Code: claude-sonnet-4-6
Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
A thread is destroyed when the parent calls execvp() (in PID1 mode).
The C init ran clock_worker() in a forked child process, which survives
exec.  Match that behaviour: create the vsock socket, fork, and run the
recv loop in the child; the parent closes its copy of the socket and
returns immediately.

Also switch to nix wrappers throughout: socket::socket(), socket::recv(),
time::clock_gettime(), and time::clock_settime() replace the equivalent
unsafe libc calls.  Add the nix "time" feature to support the clock
functions.

Assisted-by: Claude Code: claude-sonnet-4-6
Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
The C init called exit(125) if setup_redirects() returned a negative
value (which it did when opendir("/sys/class/virtio-ports") failed).
The Rust port returned silently, letting the workload run with
unredirected stdio and no diagnostic.

Match the C behaviour: print an error and exit(125) so callers get a
visible signal that the redirects could not be set up.

Assisted-by: Claude Code: claude-sonnet-4-6
Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
The C init checked *env_init_pid1 == '1' (first-byte comparison),
accepting any value starting with '1' — including "10" or "1\n" (which
can appear when the value originates from a file read).  The Rust port
used exact equality with "1", silently ignoring those variants.

Replace with is_ok_and(|v| v.starts_with('1')).

Assisted-by: Claude Code: claude-sonnet-4-6
Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Port of upstream commit 378e524 ("init/dhcp: only overwrite
resolv.conf with DNS"). Only write /etc/resolv.conf when the DHCP
server provides nameservers, preserving any pre-existing content.

Assisted-by: Claude Code: claude-sonnet-4-6
Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewrite init in Rust

4 participants