libkrun uses ffier to generate its C ABI surface and Rust clients directly from Rust source annotations.
libkrun exposes two distinct FFI libraries:
libkrun(krun): VMM builder, device management, payload loading, execution control.libkrun_init(krun_init): Guest init configuration builder, OCI conversion, and overlay injection.
Building with --features ffi generates machine-readable JSON schemas in target/ffier-<lib>.json. The schemas are checked in and used by ffier-gen-c-header to generate C headers (include/) and by ffier-gen-rust-client (invoked by dependent crate build.rs scripts) to generate dynamic Rust clients in Cargo's OUT_DIR.
- Declaration:
src/libkrun/src/api/mod.rsregisters exported types viaffier::library_definition!("krun", ...)andsrc/libkrun/src/lib.rsinvokesffier::generate_bridge!. - Schema:
bindings/libkrun-via-libc/ffier-krun.json. - C Header:
include/libkrun.h. - Rust Client:
bindings/libkrun-via-libc/build.rsreads the schema and generates the Rust client into Cargo'sOUT_DIR.
- Declaration:
init/init-blob/src/lib.rsregisters types viaffier::library_definition!("krun_init", ...)and invokesffier::generate_bridge!. - Schema:
bindings/init-blob-via-cdylib/ffier-krun_init.json. - C Header:
include/libkrun_init.h. - Rust Client:
bindings/init-blob-via-cdylib/build.rsgenerates the Rust client intoOUT_DIR.
Rust applications can consume libkrun and krun-init-blob in two ways:
-
Direct Static Linking (In-Process Rust Crates):
- Depend directly on the
kruncrate. - Depend on
krun-init-blobwith its defaultkrunfeature for compile-time type integration withkrun::FsOverlayandkrun::Payload. - Zero FFI overhead and full compiler type safety.
- Depend directly on the
-
Shared-Library Clients:
libkrun-via-libcusesffier-dynlookupfor runtime symbol lookup orffier-strongfor linking againstlibkrun.so. Consumers must select a mode; neither is enabled by default.krun-init-blob-via-cdylibuses the same features forlibkrun_init.so. Select thelibkrun-via-libcmode separately for its foreign handle types.
makedefaults toFFI=1, buildinglibkrunwith--features ffiand compiling the C ABI export bridge intolibkrun.so/libkrun.dylib.make FFI=0omits thelibkrunC ABI bridge.make testruns the dynamically linked tests by default;make test FFI=0runs the statically linked tests.
Binding generation and C ABI export are gated behind the ffi feature:
libkrun: Building with--features ffi(the default formake) compiles the C ABI bridge symbols intolibkrun.so/libkrun.dyliband generatestarget/ffier-krun.json.krun-init-blob: Building with--features fficompileslibkrun_init.soand generatestarget/ffier-krun_init.json.
Install the ffier-gen-c-header binary:
cargo install --git https://github.com/mtjhrc/ffier.git --tag v0.3.0 --locked ffier-gen-c-headermake gen-libkrun-bindings generates one libkrun.h for all supported hosts.
Platform predicates become C preprocessor guards automatically, so each
consumer sees declarations for its target platform.
TEE and Nitro change which API exists, so generation uses
--guard-features tee,aws-nitro. When compiling a consumer, define
KRUN_CFG_FEATURE_TEE and KRUN_CFG_FEATURE_AWS_NITRO only for features
enabled in its library build; leave them undefined otherwise. These macros
select header declarations, not the library's build features.
Other optional features, such as net, blk, gpu, input and vhost-user,
are assumed enabled during generation. The header can therefore declare
functions absent from a narrower build. Override GEN_HEADER_FFIER_FLAGS
with --disable-features to omit those declarations.
# Regenerate all bindings
make gen-libkrun-bindings
make gen-init-blob-bindings
# Verify working tree has no uncommitted binding or schema diffs
git diff --exit-code bindings/ include/The Rust annotations and ffier::library_definition! calls in src/libkrun/src/api/ and init/init-blob/src/ are the sole source of truth.
Never hand-edit anything generated by make gen-libkrun-bindings / make gen-init-blob-bindings: the schemas in bindings/, include/libkrun.h, include/libkrun_init.h, or any code generated in OUT_DIR by build.rs.