The Host Agent is a host-side daemon process that manages the lifecycle of a Lima instance. It orchestrates VM drivers, establishes communication with the guest agent, handles port forwarding, manages filesystem mounts, and monitors instance status. The host agent runs for the entire lifetime of a VM instance and serves as the primary control plane for host-to-guest interactions.
For information about guest-side operations, see Guest Agent System. For VM driver details, see VM Driver System. For configuration management, see Configuration System.
The host agent serves as the central coordinator between:
Key responsibilities:
Sources: pkg/hostagent/hostagent.go1-86
The HostAgent struct contains:
| Field Category | Key Fields | Purpose |
|---|---|---|
| Configuration | instConfig, instDir, instName | Instance configuration and paths |
| Network | sshLocalPort, udpDNSLocalPort, tcpDNSLocalPort | Port allocations for communication |
| Port Forwarding | portForwarder, grpcPortForwarder | Dual-mode port forwarding (SSH and gRPC) |
| Guest Communication | client, vSockPort, virtioPort | Guest agent connection details |
| Driver | driver | VM driver abstraction (QEMU/VZ/WSL2) |
| Lifecycle | signalCh, onClose | Shutdown coordination |
| Event Reporting | eventEnc, currentStatus | Status emission to CLI |
Sources: pkg/hostagent/hostagent.go51-85
The New() function performs the following initialization steps:
The SSH port determination logic:
freeport.TCP()Sources: pkg/hostagent/hostagent.go119-258 pkg/hostagent/hostagent.go293-309
The main execution flow in Run():
driver.Start(ctx) which returns an error channelstartHostAgentRoutines() in a goroutineclose() to execute registered cleanup functions in LIFO orderdriver.Stop(ctx) to gracefully stop the VMThe routine startup process (startHostAgentRoutines):
Sources: pkg/hostagent/hostagent.go343-477 pkg/hostagent/hostagent.go492-616
The host agent establishes guest agent connections using a priority order:
driver.GuestAgentConn(ctx) if the driver supports it~/.lima/INSTANCE/ga.sock which is forwarded to /run/lima-guestagent.sock in the guestConnection establishment:
getOrCreateClient() pkg/hostagent/hostagent.go773-782 creates or reuses the guest agent clientcreateConnection() pkg/hostagent/hostagent.go784-792 provides the connection callbackguestagentclient.NewGuestAgentClient()Event processing flow:
watchGuestAgentEvents() runs continuously in a goroutineprocessGuestAgentEvents() when connectedprocessGuestAgentEvents() retrieves guest info and streams eventsonEvent() callback which dispatches to port forwardersThe host agent chooses between SSH-based and gRPC-based port forwarding via the LIMA_SSH_PORT_FORWARDER environment variable (defaults to false for gRPC-based).
Sources: pkg/hostagent/hostagent.go640-722 pkg/hostagent/hostagent.go773-840
The host agent interacts with VM drivers through the driver.Driver interface. Key driver operations used:
| Operation | Usage in Host Agent | Description |
|---|---|---|
Start(ctx) | Called in Run() to start the VM | Returns error channel for async failures |
Stop(ctx) | Called during shutdown | Gracefully stops the VM |
Info() | Called during initialization | Returns driver capabilities (VSOCK, VirtIO, features) |
GuestAgentConn(ctx) | Called when creating guest connection | Provides native connection if available |
SSHAddress(ctx) | Called after start (WSL2) | Retrieves dynamic SSH address |
ForwardGuestAgent() | Used in event watching | Determines if SSH forwarding is needed |
Driver-specific features extracted from Info():
VsockPort: Port number for VSOCK communicationVirtioPort: Port identifier for VirtIO communicationNoCloudInit: Whether driver bypasses cloud-initRosettaEnabled, RosettaBinFmt: Rosetta support (VZ on macOS)DynamicSSHAddress: Whether SSH address is determined post-start (WSL2)CanRunGUI: Whether driver supports GUI modeSkipSocketForwarding: Whether to skip SSH socket forwarding setupThe driver is created during initialization via driverutil.CreateConfiguredDriver() which:
vmType configurationSources: pkg/hostagent/hostagent.go157-162 pkg/hostagent/hostagent.go378-390 pkg/hostagent/hostagent.go436-445 pkg/hostagent/hostagent.go784-792
The host agent emits JSON-encoded status events to stdout, which are consumed by the Lima CLI:
Key event emission points:
| When | Status Fields | Purpose |
|---|---|---|
| VM starting | SSHLocalPort set, others false | Indicate boot in progress |
| Host agent routines started | Running=true, errors if any degraded | Main operation started |
| During cloud-init | CloudInitProgress updated | Show provisioning progress |
| Error occurs | Degraded=true, Errors populated | Report non-fatal issues |
| Shutdown initiated | Exiting=true | Signal termination |
The emitEvent() function pkg/hostagent/hostagent.go311-325:
currentStatus with the new statuseventEncMu mutex for thread safetyCloud-init progress monitoring pkg/hostagent/hostagent.go940-1051:
WithCloudInitProgress() optionSources: pkg/hostagent/hostagent.go311-336 pkg/hostagent/hostagent.go940-1051
The host agent implements a LIFO (Last-In-First-Out) cleanup mechanism:
Cleanup functions are registered via cleanUp() pkg/hostagent/hostagent.go618-624:
onClose sliceonCloseMu mutexCommon cleanup operations registered:
grpcPortForwarder.Close() pkg/hostagent/hostagent.go695deleteOnStop=true pkg/hostagent/hostagent.go603-614The cleanup process:
errors.Join()Sources: pkg/hostagent/hostagent.go465-477 pkg/hostagent/hostagent.go618-638
This overview covers the host agent's role and structure. For detailed information:
New() function, driver configuration, cloud-init generation, and SSH setupSources: pkg/hostagent/hostagent.go1-1461 pkg/limayaml/defaults.go1-1005
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.