Documentation
¶
Overview ¶
Package vm provides a controller for managing the lifecycle of a Utility VM (UVM).
A Utility VM is a lightweight virtual machine used to host Linux (LCOW) or Windows (WCOW) containers. This package abstracts the VM lifecycle — creation, startup, stats collection, and termination — behind the Controller interface, with Manager as the primary implementation.
Lifecycle ¶
A VM follows the state machine below.
┌─────────────────┐
│ StateNotCreated │
└────────┬────────┘
│ CreateVM ok
▼
┌─────────────────┐ StartVM fails /
│ StateCreated │──────── TerminateVM fails ──────┐
└──┬─────┬────────┘ │
│ │ StartVM ok ▼
│ ▼ ┌───────────────┐
│ ┌─────────────────┐ TerminateVM │ StateInvalid │
│ │ StateRunning │───── fails ──────►│ │
│ └────────┬────────┘ └───────┬───────┘
│ │ VM exits / │ TerminateVM ok
TerminateVM ok │ TerminateVM ok │
│ ▼ ▼
│ ┌─────────────────────────────────────────────────┐
└─►│ StateTerminated │
└─────────────────────────────────────────────────┘
State descriptions:
- StateNotCreated: initial state after NewController is called.
- StateCreated: after [Controller.CreateVM] succeeds; the VM exists but has not started.
- StateRunning: after [Controller.StartVM] succeeds; the guest OS is up and the Guest Compute Service (GCS) connection is established.
- StateTerminated: terminal state reached after the VM exits naturally or [Controller.TerminateVM] completes successfully.
- StateInvalid: error state entered when [Controller.StartVM] fails after the underlying HCS VM has already started, or when [Controller.TerminateVM] fails during uvm.Close (from either StateCreated or StateRunning). A VM in this state can only be cleaned up by calling [Controller.TerminateVM].
Platform Variants ¶
Certain behaviors differ between LCOW and WCOW guests and are implemented in platform-specific source files selected via build tags (default for lcow shim and "wcow" tag for wcow shim).
Usage ¶
ctrl := vm.NewController()
if err := ctrl.CreateVM(ctx, &vm.CreateOptions{
ID: "my-uvm",
HCSDocument: doc,
}); err != nil {
// handle error
}
if err := ctrl.StartVM(ctx, &vm.StartOptions{
GCSServiceID: serviceGUID,
}); err != nil {
// handle error
}
// ... use ctrl.Guest() for guest interactions ...
if err := ctrl.TerminateVM(ctx); err != nil {
// handle error
}
_ = ctrl.Wait(ctx)
Index ¶
- type Controller
- type CreateOptions
- type ExitStatus
- type Manager
- func (c *Manager) CreateVM(ctx context.Context, opts *CreateOptions) error
- func (c *Manager) DumpStacks(ctx context.Context) (string, error)
- func (c *Manager) ExecIntoHost(ctx context.Context, request *shimdiag.ExecProcessRequest) (int, error)
- func (c *Manager) ExitStatus() (*ExitStatus, error)
- func (c *Manager) Guest() *guestmanager.Guest
- func (c *Manager) StartTime() (startTime time.Time)
- func (c *Manager) StartVM(ctx context.Context, opts *StartOptions) (err error)
- func (c *Manager) State() State
- func (c *Manager) Stats(ctx context.Context) (*stats.VirtualMachineStatistics, error)
- func (c *Manager) TerminateVM(ctx context.Context) (err error)
- func (c *Manager) Wait(ctx context.Context) error
- type StartOptions
- type State
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Controller ¶
type Controller interface {
// Guest returns the guest manager instance for this VM.
Guest() *guestmanager.Guest
// State returns the current VM state.
State() State
// CreateVM creates and initializes a new VM with the specified options.
// This prepares the VM but does not start it.
CreateVM(ctx context.Context, opts *CreateOptions) error
// StartVM starts the created VM with the specified options.
// This establishes the guest connection, sets up necessary listeners for
// guest-host communication, and transitions the VM to StateRunning.
StartVM(context.Context, *StartOptions) error
// ExecIntoHost executes a command in the running UVM.
ExecIntoHost(ctx context.Context, request *shimdiag.ExecProcessRequest) (int, error)
// DumpStacks dumps the GCS stacks associated with the VM.
DumpStacks(ctx context.Context) (string, error)
// Wait blocks until the VM exits or the context is cancelled.
// It also waits for log output processing to complete.
Wait(ctx context.Context) error
Stats(ctx context.Context) (*stats.VirtualMachineStatistics, error)
TerminateVM(context.Context) error
// StartTime returns the timestamp when the VM was started.
// Returns zero value of time.time, if the VM is not in StateRunning or StateTerminated.
StartTime() time.Time
// ExitStatus returns information about the stopped VM, including when it
// stopped and any exit error. Returns an error if the VM is not in StateTerminated.
ExitStatus() (*ExitStatus, error)
}
type CreateOptions ¶
type CreateOptions struct {
// ID specifies the unique identifier for the VM.
ID string
// HCSDocument specifies the HCS schema document used to create the VM.
HCSDocument *hcsschema.ComputeSystem
}
CreateOptions contains the configuration needed to create a new VM.
type ExitStatus ¶
type ExitStatus struct {
// StoppedTime is the timestamp when the VM stopped.
StoppedTime time.Time
// Err is the error that caused the VM to stop, if any.
// This will be nil if the VM exited cleanly.
Err error
}
ExitStatus contains information about a stopped VM's final state.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager is the VM controller implementation that manages the lifecycle of a Utility VM and its associated resources.
func NewController ¶
func NewController() *Manager
NewController creates a new Manager instance in the StateNotCreated state.
func (*Manager) CreateVM ¶
func (c *Manager) CreateVM(ctx context.Context, opts *CreateOptions) error
CreateVM creates the VM using the HCS document and initializes device state.
func (*Manager) DumpStacks ¶
DumpStacks dumps the GCS stacks associated with the VM
func (*Manager) ExecIntoHost ¶
func (c *Manager) ExecIntoHost(ctx context.Context, request *shimdiag.ExecProcessRequest) (int, error)
ExecIntoHost executes a command in the running UVM.
func (*Manager) ExitStatus ¶
func (c *Manager) ExitStatus() (*ExitStatus, error)
ExitStatus returns the final status of the VM once it has reached StateTerminated, including the time it stopped and any exit error. Returns an error if the VM has not yet stopped.
func (*Manager) Guest ¶
func (c *Manager) Guest() *guestmanager.Guest
Guest returns the guest manager instance for this VM. The guest manager provides access to guest-host communication.
func (*Manager) StartTime ¶
StartTime returns the timestamp when the VM was started. Returns zero value of time.Time if the VM has not yet reached StateRunning or StateTerminated.
func (*Manager) StartVM ¶
func (c *Manager) StartVM(ctx context.Context, opts *StartOptions) (err error)
StartVM starts the VM that was previously created via CreateVM. It starts the underlying HCS VM, establishes the GCS connection, and transitions the VM to StateRunning. On any failure the VM is transitioned to StateInvalid.
func (*Manager) Stats ¶
Stats returns runtime statistics for the VM including processor runtime and memory usage. The VM must be in StateRunning.
func (*Manager) TerminateVM ¶
TerminateVM forcefully terminates a running VM, closes the guest connection, and releases HCS resources.
The context is used for all operations, including waits, so timeouts/cancellations may prevent proper UVM cleanup.
type StartOptions ¶
type StartOptions struct {
// GCSServiceID specifies the GUID for the GCS vsock service.
GCSServiceID guid.GUID
// ConfigOptions specifies additional configuration options for the guest config.
ConfigOptions []guestmanager.ConfigOption
// ConfidentialOptions specifies security policy and confidential computing
// options for the VM. This is optional and only used for confidential VMs.
ConfidentialOptions *guestresource.ConfidentialOptions
}
StartOptions contains the configuration needed to start a VM and establish the Guest Compute Service (GCS) connection.
type State ¶
type State int32
State represents the current state of the VM lifecycle.
The normal progression is:
StateNotCreated → StateCreated → StateRunning → StateTerminated
If an unrecoverable error occurs during [Controller.StartVM] or [Controller.TerminateVM], the VM transitions to StateInvalid instead. A VM in StateInvalid can only be cleaned up via [Controller.TerminateVM].
Full state-transition table:
Current State │ Trigger │ Next State ─────────────────┼────────────────────────────────────┼───────────────── StateNotCreated │ CreateVM succeeds │ StateCreated StateCreated │ StartVM succeeds │ StateRunning StateCreated │ TerminateVM succeeds │ StateTerminated StateCreated │ StartVM fails │ StateInvalid StateCreated │ TerminateVM fails │ StateInvalid StateRunning │ VM exits or TerminateVM succeeds │ StateTerminated StateRunning │ TerminateVM fails (uvm.Close) │ StateInvalid StateInvalid │ TerminateVM called │ StateTerminated StateTerminated │ (terminal — no further transitions)│ —
const ( // StateNotCreated indicates the VM has not been created yet. // This is the initial state when a Controller is first instantiated via [NewController]. // Valid transitions: StateNotCreated → StateCreated (via [Controller.CreateVM]) StateNotCreated State = iota // StateCreated indicates the VM has been created but not yet started. // Valid transitions: // - StateCreated → StateRunning (via [Controller.StartVM], on success) // - StateCreated → StateTerminated (via [Controller.TerminateVM], on success) // - StateCreated → StateInvalid (via [Controller.StartVM], on failure) StateCreated // StateRunning indicates the VM has been started and is running. // The guest OS is up and the Guest Compute Service (GCS) connection is established. // Valid transitions: // - StateRunning → StateTerminated (VM exits naturally or [Controller.TerminateVM] succeeds) // - StateRunning → StateInvalid ([Controller.TerminateVM] fails during uvm.Close) StateRunning // StateTerminated indicates the VM has exited or been successfully terminated. // This is a terminal state — once reached, no further state transitions are possible. StateTerminated // StateInvalid indicates that an unrecoverable error has occurred. // The VM transitions to this state when: // - [Controller.StartVM] fails after the underlying HCS VM has already started, or // - [Controller.TerminateVM] fails during uvm.Close (from either [StateCreated] or [StateRunning]). // A VM in this state can only be cleaned up by calling [Controller.TerminateVM]. StateInvalid )