runtime

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: AGPL-3.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package runtime owns the microVM processes. Firecracker is the only implementation, and this is the only package that names it.

Index

Constants

View Source
const (
	JailUID = 65534
	JailGID = 65534
)

JailUID and JailGID are the unprivileged ids the jailer drops to. TAP devices are owned by these ids so the jailed Firecracker can attach.

View Source
const (
	GuestIP      = "172.31.0.2"
	GuestGateway = "172.31.0.1"
	GuestNetmask = "255.255.255.252"
)

The fixed guest network configuration. Every Kiln VM sees the same addresses; only the host side differs.

View Source
const ConfigRoot = "/var/lib/kiln"

ConfigRoot is the default Kiln root.

Variables

This section is empty.

Functions

func CgroupDir

func CgroupDir(id string) string

CgroupDir is the cgroup directory whose limits cover one microVM.

func IsExecError

func IsExecError(err error, code string) bool

IsExecError reports whether err is a stable agent error with the given code.

func KVMReady

func KVMReady() bool

KVMReady reports whether /dev/kvm is present and usable.

func KillProcess

func KillProcess(pid int) error

KillProcess sends SIGKILL and waits until the process is gone. It is idempotent: a process that is already gone is not an error.

func ManagerCgroupDir

func ManagerCgroupDir() string

ManagerCgroupDir is the cgroup parent of every Kiln microVM. Jailer puts each VM in a cgroup named after its id below this directory.

func RemoveCgroup

func RemoveCgroup(id string) error

RemoveCgroup removes the cgroup of one microVM, including the child cgroup jailer creates for the process. It is idempotent.

func RestoreUffdSocket

func RestoreUffdSocket(root, id string) string

RestoreUffdSocket returns the host path of the UFFD socket a restoring microVM connects to. Firecracker sees the same socket at /run/uffd.sock.

func SandboxDir

func SandboxDir(root, id string) string

SandboxDir returns the host directory holding one sandbox's files.

func SandboxMemoryLimit

func SandboxMemoryLimit(memoryMiB int) int64

SandboxMemoryLimit is the cgroup memory limit of a sandbox with the given guest memory. The gate asserts the host value against it.

Types

type AdoptSpec

type AdoptSpec struct {
	ID  string
	PID int
}

AdoptSpec names a microVM whose process survived a daemon restart. The jail directory and both sockets are still in place.

type Config

type Config struct {
	Root        string
	Firecracker string
	Jailer      string
}

Config locates the Kiln root and the installed binaries.

type ExecError

type ExecError struct {
	Code    string
	Message string
}

ExecError is a stable error from the guest agent.

func (*ExecError) Error

func (e *ExecError) Error() string

type ExecRequest

type ExecRequest struct {
	Cmd            []string
	Cwd            string
	Env            map[string]string
	TimeoutSeconds int
}

ExecRequest is one command to run inside a microVM.

type ExecResult

type ExecResult struct {
	ExitCode int
	Stdout   string
	Stderr   string
	TimedOut bool
}

ExecResult is the buffered result of one command.

type Firecracker

type Firecracker struct {
	// contains filtered or unexported fields
}

Firecracker is the runtime implementation. Every VM runs under jailer.

func New

func New(cfg Config) (*Firecracker, error)

New returns the Firecracker runtime. Config paths default to the Kiln root.

func (*Firecracker) Adopt

func (f *Firecracker) Adopt(ctx context.Context, spec AdoptSpec) (*VM, error)

Adopt reattaches to a Firecracker process from a previous daemon. The jail directory and both sockets must still exist, and the process must answer on its API socket. The guest keeps running.

func (*Firecracker) Pause

func (f *Firecracker) Pause(ctx context.Context, vm *VM) error

Pause freezes the VM.

func (*Firecracker) Restore

func (f *Firecracker) Restore(ctx context.Context, spec RestoreSpec) (*VM, error)

Restore loads a snapshot into a new microVM and waits until the guest agent answers a fresh hello. The VM resumes before the call returns.

func (*Firecracker) Resume

func (f *Firecracker) Resume(ctx context.Context, vm *VM) error

Resume unfreezes the VM.

func (*Firecracker) Snapshot

func (f *Firecracker) Snapshot(ctx context.Context, vm *VM, dir string) (SnapshotFiles, error)

Snapshot pauses a VM, writes a full memory and state snapshot into dir, and resumes it.

func (*Firecracker) SnapshotPaused

func (f *Firecracker) SnapshotPaused(ctx context.Context, vm *VM, dir string) (SnapshotFiles, error)

SnapshotPaused writes a full memory and state snapshot of an already paused VM into dir. The VM stays paused.

func (*Firecracker) Start

func (f *Firecracker) Start(ctx context.Context, spec Spec) (*VM, error)

Start boots a microVM and waits until the guest agent answers.

func (*Firecracker) Stop

func (f *Firecracker) Stop(ctx context.Context, vm *VM) error

Stop asks the guest to power off, waits, and removes every host resource.

func (*Firecracker) StopPaused

func (f *Firecracker) StopPaused(ctx context.Context, vm *VM) error

StopPaused kills a paused VM without resuming it, then removes its host resources but keeps its directory. A sleeping sandbox uses it: the memory image and the writable drive stay for the wake.

type RestoreSpec

type RestoreSpec struct {
	Spec
	// StatePath is the host path of the Firecracker state file.
	StatePath string
	// UffdSocket is the host path of the UFFD socket that the page fault
	// source listens on.
	UffdSocket string
}

RestoreSpec describes a microVM loaded from a template snapshot instead of booting a kernel.

type Runtime

type Runtime interface {
	Start(ctx context.Context, spec Spec) (*VM, error)
	// Adopt reattaches to a running Firecracker process from a previous
	// daemon. It proves the process answers on its API socket.
	Adopt(ctx context.Context, spec AdoptSpec) (*VM, error)
	Pause(ctx context.Context, vm *VM) error
	Resume(ctx context.Context, vm *VM) error
	// Snapshot pauses the VM, writes a full snapshot into dir, and resumes it.
	Snapshot(ctx context.Context, vm *VM, dir string) (SnapshotFiles, error)
	// SnapshotPaused writes a full snapshot of an already paused VM and leaves
	// it paused. The caller resumes or stops it.
	SnapshotPaused(ctx context.Context, vm *VM, dir string) (SnapshotFiles, error)
	Stop(ctx context.Context, vm *VM) error
	// StopPaused kills an already paused VM without resuming it. The saved
	// memory image stays the truth of the sandbox.
	StopPaused(ctx context.Context, vm *VM) error
}

Runtime starts, pauses, resumes, snapshots and stops microVMs. A sleep leaves a paused VM stopped without letting it run again. Adopt reattaches to a VM whose process survived a daemon restart.

type SnapshotFiles

type SnapshotFiles struct {
	StatePath string
	MemPath   string
}

SnapshotFiles are the two files a full snapshot consists of.

type Spec

type Spec struct {
	ID             string
	KernelPath     string
	RootfsPath     string
	RootfsReadOnly bool
	// OverlayPath attaches a second writable drive as /overlay.ext4 when set.
	OverlayPath string
	VCPUs       int
	MemoryMiB   int
	VsockCID    uint32
	VsockPort   uint32
	// TAPName attaches a host TAP device as eth0 when set.
	TAPName string
	// BootArgs replaces the default kernel command line when set.
	BootArgs string
}

Spec describes one microVM to start.

type StartResult

type StartResult struct {
	ExitCode int
	// Output is the tail of what the application wrote. It is what explains
	// an exit; the terminal reads the rest inside the sandbox.
	Output string
}

StartResult is how a started application ended.

type Terminal

type Terminal struct {
	// contains filtered or unexported fields
}

Terminal is one interactive terminal in a running microVM. Read gives the shell's output, Write sends keystrokes, and Resize tells the guest how large the client's window is. Unlike an exec, the session lives until the client closes it or the shell exits.

func (*Terminal) Close

func (t *Terminal) Close() error

Close ends the session. The agent kills the shell when the connection goes.

func (*Terminal) Read

func (t *Terminal) Read(p []byte) (int, error)

Read returns the next of the shell's output. It returns io.EOF when the shell exits.

func (*Terminal) Resize

func (t *Terminal) Resize(size guestproto.Winsize) error

Resize tells the guest how large the client's window is, so the kernel signals the shell and a curses program redraws.

func (*Terminal) Write

func (t *Terminal) Write(p []byte) (int, error)

Write sends keystrokes to the shell.

type VM

type VM struct {
	ID          string
	PID         int
	Dir         string
	Socket      string
	VsockSocket string
	VsockPort   uint32
	// contains filtered or unexported fields
}

VM is one running microVM.

func (*VM) Await

func (vm *VM) Await(ctx context.Context, port int) (StartResult, error)

Await blocks until the started application ends. The caller runs it in its own goroutine for the life of the sandbox, so a crash reaches the event log without polling.

func (*VM) ConsoleTail

func (vm *VM) ConsoleTail() string

ConsoleTail returns the last lines of the VM console log, for errors. It is empty before the console file exists.

func (*VM) Exec

func (vm *VM) Exec(ctx context.Context, req ExecRequest) (ExecResult, error)

Exec runs one command in the VM and buffers its output. One call opens one agent connection and one guest process group.

func (*VM) ExecStream

func (vm *VM) ExecStream(ctx context.Context, req ExecRequest, onOutput func(typ byte, data []byte) error) (ExecResult, error)

ExecStream runs one command and calls onOutput for every stdout and stderr frame before the final result. With a callback the output is not buffered into the result; a nil callback buffers both streams.

func (*VM) LogTail

func (vm *VM) LogTail() string

LogTail returns the last lines of the Firecracker log, for errors.

func (*VM) OpenFile

func (vm *VM) OpenFile(ctx context.Context, path string) (io.ReadCloser, error)

OpenFile starts a guest file read and returns a reader for its contents. It fails before any data is read when the agent rejects the path or the file.

func (*VM) ResumeHooks

func (vm *VM) ResumeHooks(ctx context.Context, entropy []byte, unixNanos int64, hostname string, secrets map[string]string) error

Resume sends the resume hook data and waits for the agent's answer.

func (*VM) Start

func (vm *VM) Start(ctx context.Context, cmd []string, port, deadlineSeconds int, env map[string]string) error

Start runs the application of a template and returns when it accepts a connection on its port. The command keeps running after this call, unlike an exec, which kills its process group when the call ends.

func (*VM) Terminal

func (vm *VM) Terminal(ctx context.Context, size guestproto.Winsize, cwd string, env map[string]string) (*Terminal, error)

Terminal opens a terminal. It fails before any byte is exchanged when the agent refuses the request.

func (*VM) WriteFile

func (vm *VM) WriteFile(ctx context.Context, path string, r io.Reader) error

WriteFile writes r to a guest path. The agent creates parent directories and writes through a temporary file plus a rename.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL