Overview
TheSmolVM class provides a user-friendly interface for creating and managing microVMs. It wraps the lower-level SmolVMManager and provides an instance-style API with context manager support.
Key Features:
- Auto-configuration mode for SSH-ready VMs
- Context manager support with automatic lifecycle management
- Lazy SSH connection with automatic fallback
- Environment variable injection and management
- Localhost port forwarding with dual transport (nftables/SSH tunnel)
Constructor
Parameters
VM configuration. Mutually exclusive with
vm_id. If omitted (and vm_id is omitted), SmolVM auto-creates a default SSH-capable VM configuration.ID of an existing VM to reconnect to. Mutually exclusive with
config.Override the default data directory.
Override the default socket directory.
Runtime backend override:
"firecracker", "qemu", or "auto".Guest memory in MiB for auto-config mode (only when both
config and vm_id are omitted). Defaults to 512.Root filesystem size in MiB for auto-config mode (only when both
config and vm_id are omitted). Defaults to 512. Must be >= 64.SSH user for
run() method.Optional SSH private key path. If omitted, SmolVM first tries default SSH auth, then falls back to
~/.smolvm/keys/id_ed25519 when needed.Raises
ValueError: If bothconfigandvm_idare provided, or ifmem_size_mib/disk_size_mibare set when not in auto-config mode.
Example
Class Methods
from_id
VM identifier.
Override the default data directory.
Override the default socket directory.
Runtime backend override.
SSH user for
run() method.Optional SSH private key path.
SmolVM instance bound to the existing VM.
Raises: VMNotFoundError if no VM with this ID exists.
Lifecycle Methods
start
env_vars, they are injected into the guest via SSH after boot completes.
Maximum seconds to wait for boot.
self for method chaining.
Raises:
SmolVMError: Ifenv_varsis set but the image does not support SSH (missinginit=/initin boot args).
stop
Seconds to wait for graceful shutdown.
self for method chaining.
delete
Command Execution
run
Shell command to execute.
Maximum seconds to wait for the command.
Command execution mode:
"login"(default): run via guest login shell"raw": execute command directly with no shell wrapping
CommandResult with exit_code, stdout, and stderr.
Raises:
SmolVMError: If the VM is not running or has no network.CommandExecutionUnavailableError: If SSH is not available.
Example
wait_for_ssh
Maximum seconds to wait.
self for method chaining.
Raises:
OperationTimeoutError: If SSH is not available in time.SmolVMError: If the VM is not running.
ssh_commands
"private_ip", "localhost_port", and optionally "public".
Environment Variables
set_env_vars
/etc/profile.d/smolvm_env.sh and affect new SSH sessions/login shells.
Key/value pairs to set.
If True, merge with existing variables.
unset_env_vars
Variable names to remove.
list_env_vars
Port Forwarding
expose_local
127.0.0.1:<host_port> on the host to <guest_ip>:<guest_port> inside the VM.
Guest TCP port to expose.
Host localhost port. If omitted, an available port is chosen.
Example
unexpose_local
Host localhost port (1-65535).
Guest TCP port (1-65535).
self for method chaining.
Properties
vm_id
info
refresh() to update).
status
data_dir
Utility Methods
get_ip
SmolVMError if the VM has no network configuration.
refresh
self for method chaining.
can_run_commands
init=/init.
Returns: True if the VM supports run(), False otherwise.
close
Context Manager
TheSmolVM class implements the context manager protocol:
__enter__: Auto-starts VMs created by this facade instance (not reconnected VMs)__exit__: Best-effort stop and auto-delete for VMs created by this instance