runtime

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 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.

View Source
const OverlayFile = "overlay.ext4"

OverlayFile is the name of a writable drive, in a jail and beside a snapshot.

Variables

This section is empty.

Functions

func CgroupDir

func CgroupDir(id string) string

CgroupDir is the cgroup directory whose limits cover one microVM.

func CopySparse added in v0.10.0

func CopySparse(ctx context.Context, src, dst string) error

CopySparse copies a drive and keeps its holes, so a large empty drive stays small. A filesystem with reflinks shares the blocks.

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
	MemoryMiB 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) CopyOverlay added in v0.10.0

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

CopyOverlay copies the writable drive of a paused VM into dir.

func (*Firecracker) Pause

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

Pause freezes the VM.

func (*Firecracker) RebaseMemory added in v0.13.0

func (f *Firecracker) RebaseMemory(ctx context.Context, memory, diff string) error

RebaseMemory writes the pages a diff holds over a memory file. The memory file can be a diff too, and its holes stay holes where the diff has one.

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. Firecracker answers one call at a time, so after a snapshot call that gave up, the resume waits for that write to end. The caller's context does not apply: a VM must not stay paused because its client left.

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) SnapshotDiff added in v0.13.0

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

SnapshotDiff pauses a VM, writes the pages changed since its last snapshot and its state into dir with a copy of its writable drive, and resumes it. The memory file is sparse: a hole is a page that did not change.

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. Firecracker cannot abandon the write, so the caller's cancel does not apply, only a deadline from the memory size.

func (*Firecracker) SnapshotWithDisk added in v0.10.0

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

SnapshotWithDisk pauses a VM, writes its memory and state into dir with a copy of its writable drive, and resumes it. The guest has the drive mounted, and its memory holds that mount, so only a drive from the same moment restores with it.

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 ProcessOutput added in v0.9.0

type ProcessOutput struct {
	guestproto.ProcessState
	Stdout string
	Stderr string
}

ProcessOutput is one read of a detached process.

type ProcessRequest added in v0.9.0

type ProcessRequest struct {
	Cmd []string
	Cwd string
	Env map[string]string
}

ProcessRequest is one detached process to start.

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
	// TrackDirty makes the VM record changed pages, so it can write a diff
	// snapshot. It adds a cost to every write the guest makes.
	TrackDirty bool
}

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)
	// SnapshotWithDisk is Snapshot with a copy of the writable drive in dir.
	SnapshotWithDisk(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)
	// SnapshotDiff is SnapshotWithDisk with only the pages changed since the
	// last snapshot of this VM. The VM must have been restored with TrackDirty.
	SnapshotDiff(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 ServiceRequest added in v0.10.0

type ServiceRequest struct {
	Name  string
	Cmd   []string
	Env   map[string]string
	Port  int
	Ready []string
}

ServiceRequest runs one service in the snapshot boot.

type SnapshotFiles

type SnapshotFiles struct {
	StatePath string
	MemPath   string
	// OverlayPath is the copy of the writable drive, when the snapshot took one.
	OverlayPath 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 StartOutput added in v0.10.0

type StartOutput struct {
	Output   string
	Exited   bool
	ExitCode int
	Services []guestproto.ServiceState
}

StartOutput is what the guest kept of the start command.

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
	MemoryMiB   int
	// 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) AwaitService added in v0.10.0

func (vm *VM) AwaitService(ctx context.Context, name string) (StartResult, error)

AwaitService blocks until one service ends. The caller runs it in its own goroutine for the life of the sandbox.

func (*VM) CheckUser added in v0.9.0

func (vm *VM) CheckUser(ctx context.Context, user string) error

CheckUser reports whether the image in this VM holds a user.

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) ReadProcess added in v0.9.0

func (vm *VM) ReadProcess(ctx context.Context, id string, stdoutOffset, stderrOffset int64) (ProcessOutput, error)

ReadProcess returns the state of a detached process and its output after each offset.

func (*VM) ResumeHooks

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

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

func (*VM) RunningProcesses added in v0.9.0

func (vm *VM) RunningProcesses(ctx context.Context) (int, error)

RunningProcesses counts the detached processes that run now.

func (*VM) SignalProcess added in v0.9.0

func (vm *VM) SignalProcess(ctx context.Context, id, signal string) error

SignalProcess sends a signal to the process group of a detached process.

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) StartOutput added in v0.10.0

func (vm *VM) StartOutput(ctx context.Context) (StartOutput, error)

StartOutput reads the kept output of the start command. A guest from an older template does not know the request and answers with an error.

func (*VM) StartProcess added in v0.9.0

func (vm *VM) StartProcess(ctx context.Context, req ProcessRequest) (string, error)

StartProcess starts a detached process and returns its id once it runs.

func (*VM) StartService added in v0.10.0

func (vm *VM) StartService(ctx context.Context, req ServiceRequest, deadlineSeconds int) error

StartService runs one service and returns once it is ready.

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) TrustCA added in v0.12.0

func (vm *VM) TrustCA(ctx context.Context, certPEM string) error

TrustCA adds a certificate to the trust store of the root, while a template builds.

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