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 CopySparse(ctx context.Context, src, dst string) error
- 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) CopyOverlay(ctx context.Context, vm *VM, dir string) (string, error)
- func (f *Firecracker) Pause(ctx context.Context, vm *VM) error
- func (f *Firecracker) RebaseMemory(ctx context.Context, memory, diff string) 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) SnapshotDiff(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) SnapshotWithDisk(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 ServiceRequest
- type SnapshotFiles
- type Spec
- type StartOutput
- type StartResult
- type Terminal
- type VM
- func (vm *VM) Await(ctx context.Context, port int) (StartResult, error)
- func (vm *VM) AwaitService(ctx context.Context, name string) (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) StartOutput(ctx context.Context) (StartOutput, error)
- func (vm *VM) StartProcess(ctx context.Context, req ProcessRequest) (string, error)
- func (vm *VM) StartService(ctx context.Context, req ServiceRequest, deadlineSeconds int) error
- func (vm *VM) Terminal(ctx context.Context, size guestproto.Winsize, cwd string, ...) (*Terminal, error)
- func (vm *VM) TrustCA(ctx context.Context, certPEM string) 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.
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 CopySparse ¶ added in v0.10.0
CopySparse copies a drive and keeps its holes, so a large empty drive stays small. A filesystem with reflinks shares the blocks.
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) CopyOverlay ¶ added in v0.10.0
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) 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
// 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 ¶
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
MemoryMiB int
// 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) AwaitService ¶ added in v0.10.0
AwaitService blocks until one service ends. The caller runs it in its own goroutine for the life of the sandbox.
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) 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
StartProcess starts a detached process and returns its id once it runs.
func (*VM) StartService ¶ added in v0.10.0
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.