hypervisor

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package hypervisor defines the VM specification and the operations a virtual machine monitor must support. Implementations live in subpackages.

Index

Constants

View Source
const MemoryResizeKernelArgs = "memory_hotplug.online_policy=auto-movable"

MemoryResizeKernelArgs is the kernel argument that lets a guest give back the memory a resize gave it. It brings hotplugged memory online where the kernel keeps only pages it can move, up to about three times its own, as the kernel recommends for virtio-mem. Memory brought online elsewhere can hold pages that pin it, and a shrink then never finishes.

View Source
const MemoryResizeStep = 2 << 20

MemoryResizeStep is the unit a guest's memory is resized in: the block virtio-mem plugs and unplugs, the same on both hypervisors.

Variables

View Source
var ErrRestoringMemory = errors.New("the guest's memory is still being restored")

ErrRestoringMemory is what SnapshotVM returns while the VMM is still restoring the memory of a VM restored on demand. That takes seconds, after which the snapshot can be tried again.

Functions

func CheckMemoryResize added in v0.4.0

func CheckMemoryResize(bytes, bootBytes, hotplugBytes int64) error

CheckMemoryResize returns an error unless a guest that booted with bootBytes of memory, and hotplugBytes set aside, can be resized to bytes: errors.ErrUnsupported if it booted with none set aside, and errdefs.ErrInvalidArgument for a size it cannot have.

func DeprecatedVersions added in v0.4.0

func DeprecatedVersions(starters []Starter) []string

DeprecatedVersions returns the deprecated versions among starters, which list one hypervisor's versions with the default first. By Dicer's deprecation policy, every version but the default is deprecated, and is kept only for what still uses it.

func DialVsock

func DialVsock(ctx context.Context, socketPath string, port uint32) (net.Conn, error)

DialVsock connects to a guest port through the VMM's hybrid vsock Unix socket, using the "CONNECT <port>" handshake.

func IsDeprecated added in v0.4.0

func IsDeprecated(starters []Starter, version string) bool

IsDeprecated reports whether version is a deprecated version among starters, listed as DeprecatedVersions takes them. An empty version means the default, which is never deprecated.

func LogPath

func LogPath(socketPath string) string

LogPath returns the file the VMM serving its API on socketPath writes its own output to. It sits beside the socket, so it goes wherever the socket goes.

func StartProcess

func StartProcess(ctx context.Context, socketPath, binaryPath string, args ...string) (*process.Process, error)

StartProcess launches a VMM detached from the daemon and waits for it to serve on socketPath. The VMM runs in the directory holding socketPath, so that relative paths given to it resolve there. ctx bounds only the wait; on failure the process is killed and the error includes its log.

Types

type BootConfig

type BootConfig struct {
	KernelPath string
	KernelArgs string
	InitrdPath string
}

BootConfig is what the guest boots: a kernel, its command line, and an initrd.

type CPUAffinity

type CPUAffinity struct {
	VCPU     int
	HostCPUs []int
}

CPUAffinity pins one vCPU to a set of host CPUs.

type CPUConfig

type CPUConfig struct {
	Count    int
	MaxCount int
	Topology *CPUTopology
	Affinity []CPUAffinity
}

CPUConfig describes the guest's virtual CPUs.

type CPUTopology

type CPUTopology struct {
	ThreadsPerCore int
	CoresPerDie    int
	DiesPerPackage int
	Packages       int
}

CPUTopology describes how vCPUs are presented to the guest as threads, cores, dies and packages.

type Capabilities

type Capabilities struct {
	// SupportsSnapshot reports whether SnapshotVM and Starter.RestoreVM work.
	SupportsSnapshot bool

	// SupportsHotplugMemory reports whether ResizeVMMemory works.
	SupportsHotplugMemory bool

	// SupportsHotplugCPU reports whether ResizeVMCPU works.
	SupportsHotplugCPU bool

	// SupportsCPUAffinity reports whether vCPUs can be pinned to host CPUs.
	SupportsCPUAffinity bool

	// SupportsPause reports whether PauseVM and ResumeVM work.
	SupportsPause bool

	// SupportsVsock reports whether the guest can have a vsock device.
	SupportsVsock bool

	// SupportsGPUPassthrough reports whether host PCI devices, GPUs among
	// them, can be passed through to the guest.
	SupportsGPUPassthrough bool

	// SupportsDiskRateLimit reports whether a disk's reads and writes can
	// be rate limited.
	SupportsDiskRateLimit bool
}

Capabilities reports which optional features a hypervisor supports. Callers check them before using a feature.

type ConsoleConfig

type ConsoleConfig struct {
	// Path is the file the console's output is appended to.
	Path string
}

ConsoleConfig describes where the guest's serial console is written.

type DiskConfig

type DiskConfig struct {
	Path     string
	ReadOnly bool

	// RateLimitBytesPerSecond and RateLimitIOPS limit the bytes and the
	// operations per second the disk is read and written at, together. Zero
	// is unlimited.
	RateLimitBytesPerSecond int64
	RateLimitIOPS           int64
}

DiskConfig attaches a disk image to the guest.

type FilesystemConfig added in v0.4.0

type FilesystemConfig struct {
	Tag    string
	Socket string
}

FilesystemConfig shares a host directory with the guest over virtio-fs: a vhost-user device served by virtiofsd on Socket, which the guest mounts by Tag.

type GPUConfig

type GPUConfig struct {
	// Profile is the vGPU profile's name, such as nvidia-35.
	Profile string

	// MediatedDeviceUUID is the mediated device's UUID.
	MediatedDeviceUUID string
}

GPUConfig describes a mediated GPU device assigned to the guest.

type Hypervisor

type Hypervisor interface {
	// DestroyVM immediately and forcefully destroys the guest without sending
	// any ACPI signal. Use ShutdownVM for a graceful guest shutdown instead.
	DestroyVM(ctx context.Context) error

	// ShutdownVM sends an ACPI power-off signal to the guest OS, allowing it
	// to shut down gracefully. The VMM process remains running afterwards.
	ShutdownVM(ctx context.Context) error

	// Shutdown stops the VMM process itself.
	Shutdown(ctx context.Context) error

	// VMInfo returns the VM's state and memory.
	VMInfo(ctx context.Context) (*VMInfo, error)

	// PauseVM halts the vCPUs; ResumeVM continues them.
	PauseVM(ctx context.Context) error
	ResumeVM(ctx context.Context) error

	// SnapshotVM writes the VM's state, its memory included, into the
	// directory destPath. The VM must be paused. Writing a large guest's
	// memory can take minutes: ctx should have a deadline to suit. It fails
	// with ErrRestoringMemory if the VM was restored moments ago and its
	// memory is still being restored.
	SnapshotVM(ctx context.Context, destPath string) error

	// ResizeVMMemory sets the guest's memory, from the memory it booted with
	// up to that and MemoryConfig.HotplugBytes, in steps of
	// MemoryResizeStep; another size is refused with
	// errdefs.ErrInvalidArgument, before anything changes. Where the
	// hypervisor can tell, it returns once the guest has taken or given up
	// the memory, or ctx is done.
	ResizeVMMemory(ctx context.Context, bytes int64) error

	// ResizeVMCPU sets the guest's vCPUs, up to CPUConfig.MaxCount.
	ResizeVMCPU(ctx context.Context, count int) error
}

Hypervisor controls a single running virtual machine. Each request is bounded by ctx's deadline or, if it has none, by a timeout of the implementation's, long enough for anything but SnapshotVM.

type MemoryConfig

type MemoryConfig struct {
	SizeBytes int64

	// HotplugBytes is how much more memory ResizeVMMemory can give the
	// guest. Zero sets none aside.
	HotplugBytes int64
}

MemoryConfig describes the guest's RAM, including any set aside for hotplug.

type NetworkInterfaceConfig

type NetworkInterfaceConfig struct {
	TAPDevice string
	MAC       string
	IP        string
	Netmask   string
	MTU       int
}

NetworkInterfaceConfig attaches the guest to a host TAP device.

type PCIDeviceConfig

type PCIDeviceConfig struct {
	Path string
}

PCIDeviceConfig passes a host PCI device through to the guest.

type RestoreSpec added in v0.4.0

type RestoreSpec struct {
	// Console is where the guest's serial console is written.
	Console ConsoleConfig

	// TAPDevice is the host's end of the guest's network interface, in place
	// of the one it was snapshotted with, which another VM may have.
	TAPDevice string
}

RestoreSpec is what a VM restored from a snapshot is given afresh, rather than taking it from the snapshot.

type Starter

type Starter interface {
	// Version returns the hypervisor binary version string (e.g. "v53.0.0").
	Version() string

	// DefaultKernelArgs returns the kernel arguments this hypervisor needs
	// the guest booted with, such as its console device and what a panic
	// does. They are used when the instance sets no kernel arguments of its
	// own.
	DefaultKernelArgs() string

	// PowerOffEndsVM reports whether a guest powering off ends the VMM. If
	// not, the guest resets instead, which DefaultKernelArgs makes end it.
	PowerOffEndsVM() bool

	// StartVM launches a VMM and boots the virtual machine with the given
	// configuration. The returned process is the VMM; the caller owns it from
	// then on.
	StartVM(ctx context.Context, socketPath string, spec VMSpec) (vmm *process.Process, hypervisor Hypervisor, err error)

	// RestoreVM launches a VMM and restores the VM from the snapshot
	// SnapshotVM wrote into the directory snapshotPath, leaving it paused,
	// with what spec gives it in place of what the snapshot holds. As with
	// SnapshotVM, ctx should have a deadline that suits the guest's memory.
	//
	// It may return before all of the guest's memory is restored. The VMM
	// keeps the snapshot's memory file open and restores from it while the
	// guest runs, so the file may be deleted, but must not be changed.
	RestoreVM(
		ctx context.Context, socketPath string, snapshotPath string, spec RestoreSpec,
	) (vmm *process.Process, hypervisor Hypervisor, err error)

	// Connect returns a Hypervisor for a VMM already serving its API on
	// socketPath, such as one started before the daemon restarted.
	Connect(socketPath string) (Hypervisor, error)

	// Capabilities reports which of the optional operations the hypervisor
	// supports on this host.
	Capabilities() Capabilities
}

Starter launches and connects to VMMs of one hypervisor version. The VMM process must have socketPath among its arguments, so process.Attach can identify it after a daemon restart, and runs in the directory holding socketPath: a relative path in a VMSpec resolves there, both when the VM boots and when a snapshot of it is restored, maybe by another VMM in another directory. The caller clears stale sockets from the directory.

type Type added in v0.4.0

type Type string

Type is the virtual machine monitor an instance runs on.

const (
	// TypeCloudHypervisor is Cloud Hypervisor, the default.
	TypeCloudHypervisor Type = "cloud-hypervisor"

	// TypeFirecracker is Firecracker.
	TypeFirecracker Type = "firecracker"

	// DefaultType is what an instance that names none runs on.
	DefaultType = TypeCloudHypervisor
)

func Types added in v0.4.0

func Types() []Type

Types returns every hypervisor an instance may run on, the default first.

func (Type) Valid added in v0.4.0

func (t Type) Valid() bool

Valid reports whether t names a hypervisor Dicer can start instances with.

type VMInfo added in v0.4.0

type VMInfo struct {
	State       VMState `json:"state"`
	MemoryBytes *int64  `json:"memory_bytes,omitempty"`
}

VMInfo is a running guest's observed state, as reported by the VMM.

type VMSpec added in v0.4.0

type VMSpec struct {
	Boot   BootConfig
	CPU    CPUConfig
	Memory MemoryConfig
	Disks  []DiskConfig
	// Filesystems are host directories shared over virtio-fs.
	Filesystems       []FilesystemConfig
	PCIDevices        []PCIDeviceConfig
	NetworkInterfaces []NetworkInterfaceConfig
	Console           ConsoleConfig
	Vsock             *VsockConfig
	GPU               *GPUConfig
}

VMSpec is the full specification of a guest, handed to a Starter to boot.

type VMState added in v0.4.0

type VMState string

VMState is the VMM's view of whether a guest is running.

const (
	VMStateStopped VMState = "Stopped"
	VMStateRunning VMState = "Running"
	VMStatePaused  VMState = "Paused"
)

The states a VMM reports for a guest.

type VsockConfig

type VsockConfig struct {
	CID uint32

	// SocketPath is the Unix socket the VMM proxies the device through.
	SocketPath string
}

VsockConfig describes the vsock device used to reach the in-guest agent.

Directories

Path Synopsis
Package cloudhypervisor provides primitives to interact with the openapi HTTP API.
Package cloudhypervisor provides primitives to interact with the openapi HTTP API.
Package firecracker implements the hypervisor interfaces with Firecracker.
Package firecracker implements the hypervisor interfaces with Firecracker.

Jump to

Keyboard shortcuts

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