Skip to main content

Overview

The SmolVM class provides a user-friendly interface for creating and managing microVMs. It supports automatic configuration, context manager usage, and SSH-based command execution.

Constructor

VMConfig | None
default:"None"
VM configuration. Mutually exclusive with vm_id. If omitted (and vm_id is omitted), SmolVM auto-creates a default SSH-capable VM configuration.
str | None
default:"None"
ID of an existing VM to reconnect to. Mutually exclusive with config.
Path | None
default:"None"
Override the default data directory for VM state storage.
Path | None
default:"None"
Override the default socket directory.
str | None
default:"None"
Runtime backend override. Options: "firecracker", "qemu", or "auto".
int | None
default:"None"
Guest memory in MiB for auto-config mode (when both config and vm_id are omitted). Default is 512 MiB.
int | None
default:"None"
Root filesystem size in MiB for auto-config mode. Default is 512 MiB. Minimum is 64 MiB.
str
default:"root"
SSH user for command execution via the run() method.
str | None
default:"None"
SSH private key path. If omitted, SmolVM first tries default SSH auth, then falls back to ~/.smolvm/keys/id_ed25519.

Raises

  • ValueError: If both config and vm_id are provided, or if mem_size_mib/disk_size_mib are set outside auto-config mode.

Class Methods

from_id

Reconnect to an existing VM by ID.
str
required
VM identifier to reconnect to.
Path | None
default:"None"
Override the default data directory.
Path | None
default:"None"
Override the default socket directory.
str | None
default:"None"
Runtime backend override.
str
default:"root"
SSH user for command execution.
str | None
default:"None"
SSH private key path.
SmolVM
A SmolVM instance bound to the existing VM.

Raises

  • VMNotFoundError: If no VM with this ID exists.

Lifecycle Methods

start

Start the VM. If the VM config contains env_vars, they are injected into the guest via SSH after boot completes.
float
default:"30.0"
Maximum seconds to wait for boot to complete.
SmolVM
Returns self for method chaining.

Raises

  • SmolVMError: If env_vars is set but the image does not support SSH (missing init=/init in boot args).

stop

Stop the VM gracefully.
float
default:"3.0"
Seconds to wait for graceful shutdown.
SmolVM
Returns self for method chaining.

delete

Delete the VM and release all resources.

Command Execution Methods

run

Execute a command on the guest via SSH. Lazily creates an SSH client on first call and reuses it for subsequent invocations.
str
required
Shell command to execute on the guest.
int
default:"30"
Maximum seconds to wait for the command to complete.
Literal['login', 'raw']
default:"login"
Command execution mode:
  • "login" (default): run via guest login shell
  • "raw": execute command directly with no shell wrapping
CommandResult
Result object containing exit code, stdout, and stderr.

Raises

  • SmolVMError: If the VM is not running or has no network.
  • CommandExecutionUnavailableError: If SSH is not available on the guest.

wait_for_ssh

Wait for SSH to become available on the guest.
float
default:"60.0"
Maximum seconds to wait.
SmolVM
Returns self for method chaining.

Raises

  • OperationTimeoutError: If SSH is not available within the timeout.
  • SmolVMError: If the VM is not running.

ssh_commands

Get ready-to-run SSH commands for this VM.
str | None
default:"None"
SSH user override. Defaults to the instance’s configured ssh_user.
str | Path | None
default:"None"
SSH key path override.
str | None
default:"None"
Public hostname override for remote access.
dict[str, str]
Dictionary mapping command names to ready-to-run SSH command strings.

Environment Variable Methods

set_env_vars

Set environment variables on a running VM. Variables are persisted in /etc/profile.d/smolvm_env.sh and affect new SSH sessions/login shells.
dict[str, str]
required
Key/value pairs to set.
bool
default:"True"
If True, merge with existing variables. If False, replace all variables.
list[str]
Sorted list of variable names present after the update.

unset_env_vars

Remove environment variables from a running VM.
list[str]
required
Variable names to remove.
dict[str, str]
Mapping of removed keys to their previous values.

list_env_vars

Return SmolVM-managed environment variables for a running VM.
dict[str, str]
Dictionary of environment variable names to values.

Port Forwarding Methods

expose_local

Expose a guest TCP port on localhost only. Forwards 127.0.0.1:<host_port> on the host to <guest_ip>:<guest_port> inside the VM.
int
required
Guest TCP port to expose (1-65535).
int | None
default:"None"
Host localhost port. If omitted, an available port is automatically chosen.
int
The host localhost port to connect to.

Raises

  • SmolVMError: If the VM is not running or has no network.
  • ValueError: If port numbers are out of valid range (1-65535).

unexpose_local

Remove a previously configured localhost-only port forward.
int
required
Host localhost port (1-65535).
int
required
Guest TCP port (1-65535).
SmolVM
Returns self for method chaining.

Properties

vm_id

The VM identifier.

info

Current VM runtime information (cached). Call refresh() to update from the state store.

status

Current VM lifecycle state (cached). Values: CREATED, RUNNING, STOPPED, ERROR.

data_dir

Directory backing the VM state database and logs.

Utility Methods

get_ip

Return the guest IP address.
str
The guest VM’s IP address.

Raises

  • SmolVMError: If the VM has no network configuration.

refresh

Refresh cached VM info from the state store.
SmolVM
Returns self for method chaining.

can_run_commands

Whether this VM config supports command execution via SSH. Command execution requires SmolVM’s SSH init flow, enabled by booting with init=/init.
bool
True if SSH command execution is supported, False otherwise.

close

Release underlying SDK resources for this facade instance.

Context Manager

SmolVM implements the context manager protocol for automatic lifecycle management:
On context entry (__enter__):
  • Auto-starts VMs created by this facade instance
On context exit (__exit__):
  • Best-effort stop if the VM is running
  • Auto-deletes only VMs created by this facade instance (not reconnected VMs)
  • Releases all resources via close()

Usage Examples

Auto-Configuration Mode

Create an SSH-ready VM with default settings:

Custom Configuration

Create a VM with specific resources:

Manual Lifecycle Management

Reconnecting to Existing VM

Port Forwarding

Environment Variables