You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Go bindings for libkrun, a dynamic library for creating lightweight microVMs using KVM (Linux) or HVF (macOS/ARM64).
Installation
go get github.com/mishushakov/libkrun-go/krun
libkrun must be installed on your system. The bundled header in libkrun/include/libkrun.h is used at build time when the submodule is present. Otherwise, pkg-config is used to locate headers and the shared library (libkrun.so or libkrun.dylib), and you can override paths with CGO_CFLAGS/CGO_LDFLAGS.
If you prefer, you can also build and install libkrun from source.
brew tap slp/krun
brew install libkrun pkg-config
pkg-config is used on macOS to locate libkrun headers and libraries from Homebrew.
If you installed libkrun via Homebrew, make sure pkg-config can find libkrun.pc (Homebrew usually handles this automatically). If not, set:
On macOS, binaries include an rpath to /opt/homebrew/lib so Homebrew installs work out of the box. If the runtime loader still cannot find the installed libraries, set a library search path before running your binary:
package main
import (
"fmt""os""github.com/mishushakov/libkrun-go/krun"
)
funcmain() {
// Create a new VM configuration context.ctx, err:=krun.CreateContext()
iferr!=nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
// Configure 2 vCPUs and 512 MiB of RAM.ctx.SetVMConfig(krun.VMConfig{NumVCPUs: 2, RAMMiB: 512})
// Use a host directory as the root filesystem.ctx.SetRoot("/path/to/rootfs")
// Set the command to run inside the VM.ctx.SetExec(krun.ExecConfig{
Path: "/bin/uname",
Args: []string{"/bin/uname", "-a"},
})
// Start the VM. Does not return on success.iferr:=ctx.StartEnter(); err!=nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
Build Tags
Some libkrun features are optional and gated behind Go build tags. Without the corresponding tag, calls to those functions return syscall.ENOSYS.
Tag
Feature
krun_blk
Block device / disk support (AddDisk, SetRootDiskRemount)