KraftKit Documentation

repository·staging·Indexed 19 days ago

https://github.com/unikraft/kraftkit

A Go-based framework and suite of tools for building, running, and packaging custom, minimal, and immutable Unikernel virtual machines based on the Unikraft library operating system. It supports multiple hypervisors including QEMU, Firecracker, Xen, and Hyperlight, and provides OCI image packaging, a Docker LLB plugin for builds, and integration with GitHub Actions.

Tokens
57.4K
Snippets
229
Records
274
Agent score
63%

What's inside KraftKit

  1. What is libmocktainer?

    staging
    libmocktainer is a stripped-down version of libcontainer designed to preserve only the bare minimum functionalities required for running Linux Virtual Machine Monitors (VMMs) such as QEMU and Firecracker. It remains compliant with the OCI (Open Container Initiative) runtime flow. Its primary purpose is to serve as the backend for Unikraft's runu OCI runtime CLI.
  2. Package Unikraft unikernels as OCI images

    staging

    KraftKit allows you to package and distribute Unikraft unikernels using the Open Container Image (OCI) Image Specification. This enables the use of existing container infrastructure for distributing pre-built unikernel images.

    In the KraftKit OCI implementation, the top-level element is an index containing a list of manifests. Each manifest represents a pre-built unikernel image paired with its accompanying root filesystem.

    kraft pkg --name helloworld:latest --with-kconfig --plat qemu
  3. Choose an OCI backend for KraftKit

    staging

    KraftKit supports two backends for manipulating and storing OCI images:

    1. directory (default): Represents the OCI image as a directory structure on the host system. Artifacts are stored under /root/.local/share/kraftkit/runtime/oci (or the user's local share directory), organized into indexes/ and digests/ folders.
    2. containerd: Uses the containerd content storage system. This requires specific configuration in the KraftKit config file.
  4. How Xen integration versioning works in KraftKit

    staging

    KraftKit's Xen functionality relies on a specific relationship between the Xen hypervisor and the KraftKit library. While they are decoupled, they must remain within an acceptable version distance to function correctly.

    Currently, KraftKit has confirmed integration with Xen 4.19 and potentially 4.20-rc versions. If you need to use a different version, KraftKit must be recompiled specifically for that version.

  5. Install KraftKit

    staging

    You can install KraftKit using an interactive shell script or by downloading binaries directly from the GitHub releases page. The shell script is the quickest way to get started on most systems.

    curl --proto '=https' --tlsv1.2 -sSf https://get.kraftkit.sh | sh
  6. Build your first unikernel with a Kraftfile

    staging

    You can build a unikernel by creating a Kraftfile in your repository that points to an existing Dockerfile. This allows you to leverage standard container definitions to produce a unikernel.

    1. Create a Kraftfile with the following structure:
    spec: v0.6
    
    runtime: base:latest
    
    rootfs: ./Dockerfile
    
    cmd: ["/path/to/my-server-app"]
    1. Run the build and execution command from the directory containing the Kraftfile:
    kraft run .
    spec: v0.6
    
    runtime: base:latest
    
    rootfs: ./Dockerfile
    
    cmd: ["/path/to/my-server-app"]
    kraft run .
  7. Use Unikraft C-Go bindings

    staging

    The unikraft/export package provides C-bindings for Unikraft, allowing Go applications built on top of Unikraft to interact with Unikraft internal structures.

    This package serves two primary purposes:

    1. It allows KraftKit internals to reference Unikraft structures during unikernel manifestation (compile-time or runtime).
    2. It enables Go developers to directly reference Unikraft internals, bypassing general-purpose syscall boundaries.

    Note on Stability: This package is currently a work-in-progress. All exported libraries, constants, variables, and methods are prefixed with a v0 suffix to indicate they are unstable and subject to change. Once Unikraft reaches v1.0, these exports will transition to stable APIs.

    import "kraftkit.sh/unikraft/export/v0"
  8. Use the KraftKit LLB Plugin with Docker

    staging

    The KraftKit LLB Plugin allows you to run Unikraft builds using standard Docker commands. This approach executes builds inside containers, removing the requirement to install KraftKit or Unikraft-specific dependencies on your local host. The final output is a Unikraft image stored in your local Docker registry.

    Prerequisites

    • Docker with BuildKit enabled (default in Docker v23.0+)
    • Go v1.26 or later (if building the plugin manually)

    Setup and Execution

    1. Build the plugin image: Create the Docker image containing the plugin:

      docker build . -t kraftkit.sh/llb
    2. Configure your Kraftfile: Add the following syntax directive to the very top of your kraft.yaml file to instruct Docker to use the KraftKit LLB frontend:

      #syntax=kraftkit.sh/llb:latest
    3. Run the build: Execute the build using the docker build command, pointing to your Kraftfile and the application directory:

      docker build -f test/apps/app-helloworld/kraft.yaml test/apps/app-helloworld
    docker build . -t kraftkit.sh/llb
    
    # In your kraft.yaml:
    #syntax=kraftkit.sh/llb:latest
    
    docker build -f test/apps/app-helloworld/kraft.yaml test/apps/app-helloworld
  9. Use the KraftKit container build environment

    staging

    If you prefer not to install dependencies directly on your host, you can use the KraftKit container build environment. This environment includes the kraft binary and all necessary tools/libraries for building Unikraft unikernels.

    To use it, run the kraftkit.sh/base:latest image and mount your local working directory to /workspace inside the container.

    docker run -it --rm -v $(pwd):/workspace --entrypoint bash kraftkit.sh/base:latest
  10. Recompile KraftKit for an older Xen version

    staging

    If you need to use a Xen version older than 4.19 (e.g., 4.18), you must recompile the KraftKit build environment and the KraftKit binary itself.

    Note: You must use sudo when interacting with Xen during runtime.

    ```console
    # 1. Update version references in the source
    find . -type f -exec sed -i 's/4.19/4.18/g' {} +
    
    # 2. Fetch the corresponding library version
    go get -u xenbits.xenproject.org/git-http/xen.git/tools/golang/xenlight@RELEASE-4.18.0 && go mod tidy
    
    # 3. Rebuild the KraftKit build environment
    make buildenv-xen buildenv-myself-full buildenv-myself
    
    # 4. Start the build container
    docker run --rm -it --entrypoint /bin/bash -v .:/tmp/kraftkit kraftkit.sh/myself-full:latest
    
    # 5. Rebuild KraftKit inside the container
    cd /tmp/kraftkit && make kraft

    After completion, your binary will be located at dist/kraft.

  11. Install the Hyperlight Machine Driver

    staging

    To use the Hyperlight machine driver in KraftKit, you must satisfy host requirements and install the hyperlight-unikraft binary on your $PATH.

    Requirements

    • Linux: Requires read/write access to /dev/kvm.
    • Windows: Requires the Windows Hypervisor Platform (WHP) to be enabled.
    • Kernel: Unikraft kernels must be built specifically for the hyperlight platform target.

    Installation

    Install the hyperlight-unikraft binary using Cargo:

    cargo install --git https://github.com/hyperlight-dev/hyperlight-unikraft \
        --branch main hyperlight-unikraft-host --bin hyperlight-unikraft
  12. Select the Hyperlight platform in KraftKit

    staging

    The Hyperlight driver registers two platform names that can be used with the --plat flag during kraft build or kraft run commands:

    • hyperlight (canonical name)
    • hl (shorthand)

    Examples

    # Build for hyperlight
    kraft build --plat hyperlight --arch x86_64
    
    # Run using the shorthand
    kraft run --plat hl ...
    kraft build --plat hyperlight --arch x86_64
    kraft run   --plat hl ...