hypervisor

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2026 License: MIT Imports: 5 Imported by: 0

README

Hypervisor Abstraction

Provides a common interface for VM management across different hypervisors.

Purpose

Hypeman originally supported only Cloud Hypervisor. This abstraction layer allows supporting multiple hypervisors (e.g., QEMU) through a unified interface, enabling:

  • Hypervisor choice per instance - Different instances can use different hypervisors
  • Feature parity where possible - Common operations work the same way
  • Graceful degradation - Features unsupported by a hypervisor can be detected and handled

How It Works

The abstraction defines two key interfaces:

  1. Hypervisor - VM lifecycle operations (create, boot, pause, resume, snapshot, restore, shutdown)
  2. ProcessManager - Hypervisor process lifecycle (start binary, get binary path)

Each hypervisor implementation translates the generic configuration and operations to its native format. For example, Cloud Hypervisor uses an HTTP API over a Unix socket, while QEMU would use QMP.

Before using optional features, callers check capabilities:

if hv.Capabilities().SupportsSnapshot {
    hv.Snapshot(ctx, path)
}

Hypervisor Switching

Instances store their hypervisor type in metadata. An instance can switch hypervisors only when stopped (no running VM, no snapshot), since:

  • Disk images are hypervisor-agnostic
  • Snapshots are hypervisor-specific and cannot be restored by a different hypervisor

Documentation

Overview

Package hypervisor provides an abstraction layer for virtual machine managers. This allows the instances package to work with different hypervisors (e.g., Cloud Hypervisor, QEMU) through a common interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterSocketName

func RegisterSocketName(t Type, name string)

RegisterSocketName registers the socket filename for a hypervisor type. Called by each hypervisor implementation's init() function.

func RegisterVsockDialerFactory

func RegisterVsockDialerFactory(t Type, factory VsockDialerFactory)

RegisterVsockDialerFactory registers a VsockDialer factory for a hypervisor type. Called by each hypervisor implementation's init() function.

func SocketNameForType

func SocketNameForType(t Type) string

SocketNameForType returns the socket filename for a hypervisor type. Falls back to type + ".sock" if not registered.

Types

type CPUTopology

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

CPUTopology defines the virtual CPU topology

type Capabilities

type Capabilities struct {
	// SupportsSnapshot indicates if Snapshot/Restore are available
	SupportsSnapshot bool

	// SupportsHotplugMemory indicates if ResizeMemory is available
	SupportsHotplugMemory bool

	// SupportsPause indicates if Pause/Resume are available
	SupportsPause bool

	// SupportsVsock indicates if vsock communication is available
	SupportsVsock bool

	// SupportsGPUPassthrough indicates if PCI device passthrough is available
	SupportsGPUPassthrough bool

	// SupportsDiskIOLimit indicates if disk I/O rate limiting is available
	SupportsDiskIOLimit bool
}

Capabilities indicates which optional features a hypervisor supports. Callers should check these before calling optional methods.

type DiskConfig

type DiskConfig struct {
	Path       string
	Readonly   bool
	IOBps      int64 // Sustained I/O rate limit in bytes/sec (0 = unlimited)
	IOBurstBps int64 // Burst I/O rate in bytes/sec (0 = same as IOBps)
}

DiskConfig represents a disk attached to the VM

type Hypervisor

type Hypervisor interface {
	// DeleteVM sends a graceful shutdown signal to the guest.
	DeleteVM(ctx context.Context) error

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

	// GetVMInfo returns current VM state information.
	GetVMInfo(ctx context.Context) (*VMInfo, error)

	// Pause suspends VM execution.
	// Check Capabilities().SupportsPause before calling.
	Pause(ctx context.Context) error

	// Resume continues VM execution after pause.
	// Check Capabilities().SupportsPause before calling.
	Resume(ctx context.Context) error

	// Snapshot creates a VM snapshot at the given path.
	// Check Capabilities().SupportsSnapshot before calling.
	Snapshot(ctx context.Context, destPath string) error

	// ResizeMemory changes the VM's memory allocation.
	// Check Capabilities().SupportsHotplugMemory before calling.
	ResizeMemory(ctx context.Context, bytes int64) error

	// ResizeMemoryAndWait changes the VM's memory allocation and waits for it to stabilize.
	// This polls until the actual memory size matches the target or stabilizes.
	// Check Capabilities().SupportsHotplugMemory before calling.
	ResizeMemoryAndWait(ctx context.Context, bytes int64, timeout time.Duration) error

	// Capabilities returns what features this hypervisor supports.
	Capabilities() Capabilities
}

Hypervisor defines the interface for VM control operations. A Hypervisor client is returned by VMStarter.StartVM after the VM is running.

type NetworkConfig

type NetworkConfig struct {
	TAPDevice string
	IP        string
	MAC       string
	Netmask   string
}

NetworkConfig represents a network interface attached to the VM

type Type

type Type string

Type identifies the hypervisor implementation

const (
	// TypeCloudHypervisor is the Cloud Hypervisor VMM
	TypeCloudHypervisor Type = "cloud-hypervisor"
	// TypeQEMU is the QEMU VMM
	TypeQEMU Type = "qemu"
)

type VMConfig

type VMConfig struct {
	// Compute resources
	VCPUs        int
	MemoryBytes  int64
	HotplugBytes int64
	Topology     *CPUTopology

	// Storage
	Disks []DiskConfig

	// Network
	Networks []NetworkConfig

	// Console
	SerialLogPath string

	// Vsock
	VsockCID    int64
	VsockSocket string

	// PCI device passthrough (GPU, etc.)
	PCIDevices []string

	// Boot configuration
	KernelPath string
	InitrdPath string
	KernelArgs string
}

VMConfig is the hypervisor-agnostic VM configuration. Each hypervisor implementation translates this to its native format.

type VMInfo

type VMInfo struct {
	State            VMState
	MemoryActualSize *int64 // Current actual memory size in bytes (if available)
}

VMInfo contains current VM state information

type VMStarter

type VMStarter interface {
	// SocketName returns the socket filename for this hypervisor.
	// Uses short names to stay within Unix socket path length limits (SUN_LEN ~108 bytes).
	SocketName() string

	// GetBinaryPath returns the path to the hypervisor binary, extracting if needed.
	GetBinaryPath(p *paths.Paths, version string) (string, error)

	// GetVersion returns the version of the hypervisor binary.
	// For embedded binaries (Cloud Hypervisor), returns the latest supported version.
	// For system binaries (QEMU), queries the installed binary for its version.
	GetVersion(p *paths.Paths) (string, error)

	// StartVM launches the hypervisor process and boots the VM.
	// Returns the process ID and a Hypervisor client for subsequent operations.
	StartVM(ctx context.Context, p *paths.Paths, version string, socketPath string, config VMConfig) (pid int, hv Hypervisor, err error)

	// RestoreVM starts the hypervisor and restores VM state from a snapshot.
	// Each hypervisor implements its own restore flow:
	// - Cloud Hypervisor: starts process, calls Restore API
	// - QEMU: would start with -incoming or -loadvm flags (not yet implemented)
	// Returns the process ID and a Hypervisor client. The VM is in paused state after restore.
	RestoreVM(ctx context.Context, p *paths.Paths, version string, socketPath string, snapshotPath string) (pid int, hv Hypervisor, err error)
}

VMStarter handles the full VM startup sequence. Each hypervisor implements its own startup flow: - Cloud Hypervisor: starts process, configures via HTTP API, boots via HTTP API - QEMU: converts config to command-line args, starts process (VM runs immediately)

type VMState

type VMState string

VMState represents the VM execution state

const (
	// StateCreated means the VM is configured but not running
	StateCreated VMState = "created"
	// StateRunning means the VM is actively executing
	StateRunning VMState = "running"
	// StatePaused means the VM execution is suspended
	StatePaused VMState = "paused"
	// StateShutdown means the VM has stopped but VMM exists
	StateShutdown VMState = "shutdown"
)

type VsockDialer

type VsockDialer interface {
	// DialVsock connects to the guest on the specified port.
	// Returns a net.Conn that can be used for bidirectional communication.
	DialVsock(ctx context.Context, port int) (net.Conn, error)

	// Key returns a unique identifier for this dialer, used for connection pooling.
	Key() string
}

VsockDialer provides vsock connectivity to a guest VM. Each hypervisor implements its own connection method: - Cloud Hypervisor: Unix socket file + text handshake protocol - QEMU: Kernel AF_VSOCK with CID-based addressing

func NewVsockDialer

func NewVsockDialer(hvType Type, vsockSocket string, vsockCID int64) (VsockDialer, error)

NewVsockDialer creates a VsockDialer for the given hypervisor type. Returns an error if the hypervisor type doesn't have a registered factory.

type VsockDialerFactory

type VsockDialerFactory func(vsockSocket string, vsockCID int64) VsockDialer

VsockDialerFactory creates VsockDialer instances for a hypervisor type.

Directories

Path Synopsis
Package cloudhypervisor implements the hypervisor.Hypervisor interface for Cloud Hypervisor VMM.
Package cloudhypervisor implements the hypervisor.Hypervisor interface for Cloud Hypervisor VMM.
Package qemu implements the hypervisor.Hypervisor interface for QEMU.
Package qemu implements the hypervisor.Hypervisor interface for QEMU.

Jump to

Keyboard shortcuts

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