![Kerf Logo](logo.png) # Kerf: Multikernel Management System `kerf` manages multiple Linux kernel instances running side by side on a single host. It carves a pool of CPUs, memory and devices out of the host, hands slices of that pool to spawn kernels, and loads, boots and controls those kernels. Every resource is described in a device tree that the host kernel holds. `kerf` validates each change against the full state before it reaches the kernel, so conflicting or invalid allocations are rejected up front. ## Contents - [Features](#features) - [Installation](#installation) - [Quick Start](#quick-start) - [Command Guide](#command-guide) - [How It Works](#how-it-works) - [Validation Rules](#validation-rules) - [References](#references) ## Features - **Resource pool**: reserve CPUs, memory and devices for multikernel use, and reshape the pool while it is live - **Instance lifecycle**: create, update and delete kernel instances, each with its own slice of the pool - **Kernel loading and execution**: load a kernel image with an initrd, a Docker image or a rootfs directory, then boot and shut it down - **Mandatory validation**: every operation checks for CPU, memory and device conflicts; validation cannot be skipped - **Signing and lockdown**: sign spawn kernels, and lock them down per run - **Access to running instances**: run commands inside an instance or attach to its console - **Inspection and replay**: show instances, list CPUs, and dump the device tree to replay it later or on another host ## Installation ### Requirements - A host kernel with multikernel support, exposing `/sys/fs/multikernel/` - Python 3.9 or newer - `musl-gcc` when installing from source (e.g. `apt install musl-tools`), to build the static `kerf-init` Python dependencies are installed automatically: ```toml [tool.poetry.dependencies] python = "^3.9" pylibfdt = "^1.7.0" # Device tree parsing (from dtc project) click = "^8.0.0" # Command line interface pyyaml = "^6.0" pyudev = "^0.24.0" # Device discovery cryptography = ">=3.1" # Signing spawn kernels (kerf sign) zstandard = ">=0.15" # Unpacking zstd compressed kernels lz4 = ">=3.0" # Unpacking LZ4 compressed kernels ``` ### From PyPI The distribution is named `kerf-multikernel`; the installed command is still `kerf`. ```bash pip install kerf-multikernel # Or isolated with pipx, avoiding conflicts with other packages pipx install kerf-multikernel ``` ### From Source Recommended for development. Installing builds the static `kerf-init`, so rerunning `pip install` after a pull also picks up `kerf-init` changes. ```bash git clone https://github.com/multikernel/kerf.git cd kerf # Installs 'kerf' command to ~/.local/bin/kerf pip install -e . # Or install to the system Python's scripts directory # (typically /usr/local/bin/kerf, or /usr/bin/kerf if using system Python) sudo pip install . ``` Check the installation: ```bash kerf --help kerf init --help ``` ## Quick Start The life of an instance, from an empty host to cleanup: ```bash # 1. Find the physical IDs of the CPUs to hand over kerf cpus # 2. Carve a pool out of the host: 7 CPUs (by APIC ID) and 1GB on their NUMA node sudo kerf init --cpus=128-134 --memory=1GB --report # 3. Create an instance with a slice of the pool sudo kerf create web-server --cpus=128-131 --memory=512MB # 4. Load a kernel image into it sudo kerf load web-server --kernel=/boot/vmlinuz --initrd=/boot/initrd.img \ --cmdline="root=/dev/sda1 ro" # 5. Boot it, then run something inside sudo kerf start web-server sudo kerf exec web-server ls / # 6. Tear it down sudo kerf kill web-server sudo kerf unload web-server sudo kerf delete web-server # 7. Hand everything back to the host sudo kerf init --cpus=none --memory=none ``` ## Command Guide | Command | Purpose | |---------|---------| | `kerf init` | Initialize or reshape the resource pool | | `kerf cpus` | List CPUs with their physical IDs and who holds each | | `kerf create` | Create a kernel instance with a resource allocation | | `kerf update` | Change the resources of an existing instance | | `kerf delete` | Delete a kernel instance | | `kerf sign` | Sign a spawn kernel image | | `kerf load` | Load a kernel image into an instance | | `kerf unload` | Unload the kernel image from an instance | | `kerf start` | Boot an instance | | `kerf kill` | Shut down a running instance | | `kerf exec` | Run a command inside a running instance | | `kerf console` | Attach to the console of a running instance | | `kerf show` | Show instance information | | `kerf dump` | Dump the device tree the kernel holds | Use `kerf --help` and `kerf --help` for all options. ### Resource Pool ```bash # Initialize resource pool with CPUs and pool memory # A plain size lands on the NUMA node of the requested CPUs kerf init --cpus=4-7 --memory=2GB # Initialize with CPUs, per-node memory and devices kerf init --cpus=4-31 --memory=8GB@0,8GB@1 --devices=nvme0n1,enp9s0 # Re-run to reshape the live pool; "none" asks for none of a resource kerf init --cpus=4-15 --memory=4GB kerf init --cpus=4-15 --memory=none # Hand everything back to the host kerf init --cpus=none --memory=none ``` `--cpus` takes physical CPU IDs: APIC IDs on x86, MPIDRs on arm64. MPIDRs are written in hex, and `--cpus=0x100-0x103` takes the CPUs that exist in that range. `kerf cpus` lists the IDs and who holds each CPU. ### Instances ```bash # Create kernel instance with resource allocation kerf create web-server --cpus=4-7 --memory=2GB kerf create database --cpu-count=8 --memory=16GB # Change what an instance holds kerf update web-server --cpus=4-9 --memory=4GB # Delete a kernel instance kerf delete web-server ``` ### Kernel Images ```bash # Load kernel image with initrd and boot parameters kerf load --kernel=/boot/vmlinuz --initrd=/boot/initrd.img \ --cmdline="root=/dev/sda1 ro" --id=1 # Sign a spawn kernel where the signing key is kept, not on the host; # the host kernel must trust the certificate, for example as a MOK kerf sign /boot/vmlinuz -o vmlinux.signed --key signing_key.pem --cert signing_cert.pem kerf load --kernel=vmlinux.signed --id=1 # Unload kernel image from an instance kerf unload web-server ``` ### Running Instances ```bash # Boot a kernel instance kerf start web-server # Change the entrypoint or add boot parameters for one run, without # loading the image again kerf start web-server --entrypoint=/bin/sh --append=loglevel=7 # Run a command inside a running instance (docker exec style) kerf exec web-server ls / kerf exec -it web-server sh kerf exec -u nobody web-server id # Attach to the console of a running instance kerf console web-server # Shutdown a running kernel instance kerf kill web-server ``` ### Lockdown Lockdown is chosen per run. The next start without the option is not locked down. ```bash # Lock a spawn kernel down at either of the kernel's two levels, # integrity or confidentiality kerf start web-server --lockdown=integrity # Or pick individual confidentiality restrictions on top of integrity kerf start web-server --lockdown=integrity --deny=kcore,xfrm-secret ``` `--deny` restrictions are enforced by `kerf-init`, and the spawn kernel needs BPF LSM and BTF. For a spawn that boots its own initramfs, `kerf-init` runs first and root in the spawn is trusted not to lift the restrictions, which only stay pinned in a bpffs. ### Inspection ```bash # Show kernel instance information kerf show kerf show web-server # List the CPUs with the physical IDs that --cpus takes, and who holds each kerf cpus # Dump the device tree the kernel holds: DTB to replay, --dts to read kerf dump -o host.dtb kerf dump web-server -o web.dtb kerf dump web-server --dts ``` ### Reusing a Dumped Baseline `kerf dump` writes the device tree the kernel holds, byte for byte, and `kerf init --input` and `kerf create --input` accept it back. This is the supported way to capture a configuration and replay it later or on another host; hand-written device tree files are not supported. An instance dump carries its CPUs, memory size, devices and id; `--static` is given again on the command line. ```bash sudo kerf dump -o host.dtb # baseline sudo kerf dump web-server -o web.dtb # a running instance sudo kerf init --input=host.dtb # replay the baseline sudo kerf create --input=web.dtb # recreate the instance sudo kerf dump --dts # read it; text is not replayable ``` ## How It Works ### Design Principles 1. **Single source of truth**: the baseline device tree describes the hardware resources available for allocation 2. **Mandatory validation**: every operation validates the full configuration 3. **Fail fast**: resource conflicts are caught immediately, and invalid output is never produced 4. **Overlay-based management**: instance changes are applied as device tree overlays 5. **Developer friendly**: clear error messages with suggestions for fixing problems ### Baseline and Overlays The global device tree has two parts: 1. **Resources** (`/resources`): all physical resources in the pool. This is the baseline, written by `kerf init`. It contains no instances. 2. **Instances** (`/instances`): the resource assignment of each spawn kernel, including its device references. These are added by overlays from `kerf create` and `kerf update`. `kerf init` builds the baseline from its command line arguments, validates it and writes it to the kernel: ``` kerf init --cpus=... --memory=... --devices=... │ ▼ ┌─────────┐ │ kerf │ ← Builds and validates │ init │ the resource pool └─────────┘ │ ▼ Baseline DTB (resources only) → /sys/fs/multikernel/device_tree ``` Instance changes compute the delta between the current state and the modified state, validate the full result, and apply a minimal overlay (DTBO): ``` Current State (Baseline + Overlays) + Requested change │ ▼ ┌─────────────┐ │ kerf │ ← Validates full state │ (create/ │ before generating overlay │ update/ │ │ delete) │ └─────────────┘ │ ▼ DTBO Overlay → /sys/fs/multikernel/overlays/new │ ▼ Applied Overlay → /sys/fs/multikernel/overlays/tx_XXX/ ``` Each overlay is a transaction. The effective device tree is the baseline plus all applied overlays: ``` Baseline DTB (static) │ ├─── Overlay tx_101 (instance: web-server) ├─── Overlay tx_102 (instance: database) └─── Overlay tx_103 (update: web-server resources) │ ▼ Effective Device Tree (Baseline + All Applied Overlays) │ ▼ Kernel Instance Views /sys/fs/multikernel/instances/* ``` ### Kernel Interface The kernel exposes a filesystem interface, mounted at `/sys/fs/multikernel/`: ``` /sys/fs/multikernel/ ├── device_tree # Baseline DTB (resources only, writable via kerf init) ├── overlays/ # Overlay subsystem │ ├── new # Write DTBO here to apply overlay │ ├── tx_101/ # Applied overlay transaction │ │ ├── id # Transaction ID: "101" │ │ ├── status # "applied" | "failed" | "removed" │ │ ├── dtbo # Original overlay blob (binary) │ │ └── ... │ └── tx_102/ │ └── ... └── instances/ # Runtime kernel instances (read-only) ├── web-server/ │ ├── id # Instance ID │ ├── status # Instance status │ └── ... └── ... ``` - **Rollback**: removing an overlay transaction directory (`rmdir /sys/fs/multikernel/overlays/tx_XXX/`) rolls the change back - **Kernel generated**: instance directories are generated by the kernel from the baseline and the applied overlays - **Name-based addressing**: the instance node name in the device tree (`/instances/web-server`) is the directory name under `instances/`. The kernel assigns numeric IDs, but users reference instances by name, so no manual ID coordination is needed ## Validation Rules All `kerf` operations validate automatically, against the full state (baseline plus all overlays). Validation cannot be disabled or skipped. ### CPUs 1. CPUs must be defined in the baseline resource pool 2. No CPU can be allocated to multiple instances 3. CPU lists should be explicitly enumerated ### Memory 1. Memory regions must be within the baseline memory pool 2. Memory regions cannot overlap between instances 3. Sum of all allocations must not exceed baseline memory pool size 4. Memory base addresses must be page-aligned (4KB = 0x1000) ### Devices 1. Referenced devices must be defined in the baseline 2. Devices can only be allocated to one instance (exclusive access) 3. Device references must be valid (no dangling phandles) 4. SR-IOV VF numbers must be within device limits 5. Namespace IDs must be within device limits ### Global 1. Instance names must be unique 2. All phandle references must resolve 3. Baseline resource configuration must be complete and consistent ## References - **Device Tree Specification**: https://devicetree-specification.readthedocs.io/ - **libfdt Documentation**: https://git.kernel.org/pub/scm/utils/dtc/dtc.git/tree/Documentation