Overview
TheSmolVM class provides a user-friendly interface for creating and managing microVMs. It supports automatic configuration, context manager usage, and SSH-based command execution.
Constructor
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 for VM state storage.
Override the default socket directory.
Runtime backend override. Options:
"firecracker", "qemu", or "auto".Guest memory in MiB for auto-config mode (when both
config and vm_id are omitted). Default is 512 MiB.Root filesystem size in MiB for auto-config mode. Default is 512 MiB. Minimum is 64 MiB.
SSH user for command execution via the
run() method.SSH private key path. If omitted, SmolVM first tries default SSH auth, then falls back to
~/.smolvm/keys/id_ed25519.Raises
ValueError: If bothconfigandvm_idare provided, or ifmem_size_mib/disk_size_mibare set outside auto-config mode.
Class Methods
from_id
VM identifier to reconnect to.
Override the default data directory.
Override the default socket directory.
Runtime backend override.
SSH user for command execution.
SSH private key path.
A 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 to complete.
Returns
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.
Returns
self for method chaining.delete
Command Execution Methods
run
Shell command to execute on the guest.
Maximum seconds to wait for the command to complete.
Command execution mode:
"login"(default): run via guest login shell"raw": execute command directly with no shell wrapping
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
Maximum seconds to wait.
Returns
self for method chaining.Raises
OperationTimeoutError: If SSH is not available within the timeout.SmolVMError: If the VM is not running.
ssh_commands
SSH user override. Defaults to the instance’s configured ssh_user.
SSH key path override.
Public hostname override for remote access.
Dictionary mapping command names to ready-to-run SSH command strings.
Environment Variable Methods
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. If False, replace all variables.
Sorted list of variable names present after the update.
unset_env_vars
Variable names to remove.
Mapping of removed keys to their previous values.
list_env_vars
Dictionary of environment variable names to values.
Port Forwarding Methods
expose_local
127.0.0.1:<host_port> on the host to <guest_ip>:<guest_port> inside the VM.
Guest TCP port to expose (1-65535).
Host localhost port. If omitted, an available port is automatically chosen.
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
Host localhost port (1-65535).
Guest TCP port (1-65535).
Returns
self for method chaining.Properties
vm_id
info
refresh() to update from the state store.
status
CREATED, RUNNING, STOPPED, ERROR.
data_dir
Utility Methods
get_ip
The guest VM’s IP address.
Raises
SmolVMError: If the VM has no network configuration.
refresh
Returns
self for method chaining.can_run_commands
init=/init.
True if SSH command execution is supported, False otherwise.
close
Context Manager
SmolVM implements the context manager protocol for automatic lifecycle management:__enter__):
- Auto-starts VMs created by this facade instance
__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()