Documentation
¶
Overview ¶
Package runtime owns the microVM processes. Firecracker is the only implementation, and this is the only package that names it.
Index ¶
- Constants
- func CgroupDir(id string) string
- func IsExecError(err error, code string) bool
- func KVMReady() bool
- func KillProcess(pid int) error
- func ManagerCgroupDir() string
- func RemoveCgroup(id string) error
- func RestoreUffdSocket(root, id string) string
- func SandboxDir(root, id string) string
- func SandboxMemoryLimit(memoryMiB int) int64
- type AdoptSpec
- type Config
- type ExecError
- type ExecRequest
- type ExecResult
- type Firecracker
- func (f *Firecracker) Adopt(ctx context.Context, spec AdoptSpec) (*VM, error)
- func (f *Firecracker) Pause(ctx context.Context, vm *VM) error
- func (f *Firecracker) Restore(ctx context.Context, spec RestoreSpec) (*VM, error)
- func (f *Firecracker) Resume(ctx context.Context, vm *VM) error
- func (f *Firecracker) Snapshot(ctx context.Context, vm *VM, dir string) (SnapshotFiles, error)
- func (f *Firecracker) SnapshotPaused(ctx context.Context, vm *VM, dir string) (SnapshotFiles, error)
- func (f *Firecracker) Start(ctx context.Context, spec Spec) (*VM, error)
- func (f *Firecracker) Stop(ctx context.Context, vm *VM) error
- func (f *Firecracker) StopPaused(ctx context.Context, vm *VM) error
- type ProcessOutput
- type ProcessRequest
- type RestoreSpec
- type Runtime
- type SnapshotFiles
- type Spec
- type StartResult
- type Terminal
- type VM
- func (vm *VM) Await(ctx context.Context, port int) (StartResult, error)
- func (vm *VM) CheckUser(ctx context.Context, user string) error
- func (vm *VM) ConsoleTail() string
- func (vm *VM) Exec(ctx context.Context, req ExecRequest) (ExecResult, error)
- func (vm *VM) ExecStream(ctx context.Context, req ExecRequest, ...) (ExecResult, error)
- func (vm *VM) LogTail() string
- func (vm *VM) OpenFile(ctx context.Context, path string) (io.ReadCloser, error)
- func (vm *VM) ReadProcess(ctx context.Context, id string, stdoutOffset, stderrOffset int64) (ProcessOutput, error)
- func (vm *VM) ResumeHooks(ctx context.Context, entropy []byte, unixNanos int64, hostname string, ...) error
- func (vm *VM) RunningProcesses(ctx context.Context) (int, error)
- func (vm *VM) SignalProcess(ctx context.Context, id, signal string) error
- func (vm *VM) Start(ctx context.Context, cmd []string, port, deadlineSeconds int, ...) error
- func (vm *VM) StartProcess(ctx context.Context, req ProcessRequest) (string, error)
- func (vm *VM) Terminal(ctx context.Context, size guestproto.Winsize, cwd string, ...) (*Terminal, error)
- func (vm *VM) WriteFile(ctx context.Context, path string, r io.Reader) error
Constants ¶
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.
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.
const ConfigRoot = "/var/lib/kiln"
ConfigRoot is the default Kiln root.
Variables ¶
This section is empty.
Functions ¶
func IsExecError ¶
IsExecError reports whether err is a stable agent error with the given code.
func KillProcess ¶
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 ¶
RemoveCgroup removes the cgroup of one microVM, including the child cgroup jailer creates for the process. It is idempotent.
func RestoreUffdSocket ¶
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 ¶
SandboxDir returns the host directory holding one sandbox's files.
func SandboxMemoryLimit ¶
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 ¶
AdoptSpec names a microVM whose process survived a daemon restart. The jail directory and both sockets are still in place.
type ExecRequest ¶
ExecRequest is one command to run inside a microVM.
type ExecResult ¶
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 ¶
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) 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
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
}
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 ¶
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 ¶
Close ends the session. The agent kills the shell when the connection goes.
func (*Terminal) Read ¶
Read returns the next of the shell's output. It returns io.EOF when the shell exits.
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 ¶
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 ¶
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) OpenFile ¶
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
RunningProcesses counts the detached processes that run now.
func (*VM) SignalProcess ¶ added in v0.9.0
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) StartProcess ¶ added in v0.9.0
StartProcess starts a detached process and returns its id once it runs.