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
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 bothconfigandvm_idare provided, or ifmem_size_mib/disk_size_mibare set outside auto-config mode.
Class Methods
from_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
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: Ifenv_varsis set but the image does not support SSH (missinginit=/initin boot args).
stop
float
default:"3.0"
Seconds to wait for graceful shutdown.
SmolVM
Returns
self for method chaining.delete
Command Execution Methods
run
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
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
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
/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
list[str]
required
Variable names to remove.
dict[str, str]
Mapping of removed keys to their previous values.
list_env_vars
dict[str, str]
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.
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
int
required
Host localhost port (1-65535).
int
required
Guest TCP port (1-65535).
SmolVM
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
str
The guest VM’s IP address.
Raises
SmolVMError: If the VM has no network configuration.
refresh
SmolVM
Returns
self for method chaining.can_run_commands
init=/init.
bool
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()