Create sandboxes, run commands, and check results with SmolVM
This page covers the everyday operations you need to work with SmolVM: creating a sandbox, running commands inside it, reading the output, and handling errors. If you have not installed SmolVM yet, start with the quickstart.
You can create a sandbox with sensible defaults or customize the configuration.
Auto-config (Recommended)
Manual Configuration
from smolvm import SmolVM# Minimal configurationwith SmolVM() as vm: print(f"VM running at {vm.get_ip()}")# Custom memory and disk sizewith SmolVM(mem_size_mib=1024, disk_size_mib=1024) as vm: print(f"VM ID: {vm.vm_id}")
from smolvm import SmolVM, VMConfigfrom smolvm.build import ImageBuilder, SSH_BOOT_ARGS# Build custom imagebuilder = ImageBuilder()kernel, rootfs = builder.build_alpine_ssh()# Create VM with explicit configconfig = VMConfig( vm_id="my-vm", vcpu_count=2, mem_size_mib=1024, kernel_path=kernel, rootfs_path=rootfs, boot_args=SSH_BOOT_ARGS,)with SmolVM(config) as vm: result = vm.run("echo 'Hello from SmolVM'") print(result.output)
with SmolVM() as vm: print(vm.vm_id) # VM identifier print(vm.status) # Current state (VMState.RUNNING) print(vm.get_ip()) # Guest IP address print(vm.info) # Full VMInfo object (cached) print(vm.data_dir) # State directory path # Refresh cached info from state store vm.refresh() print(vm.info.status)
Not all VM images support command execution. You can check before running commands:
with SmolVM() as vm: if vm.can_run_commands(): result = vm.run("whoami") print(result.output) else: print("This VM does not support SSH command execution")
Command execution requires the VM to be booted with init=/init in the boot arguments. All auto-configured VMs and images built with ImageBuilder include this by default.
with SmolVM() as vm: ssh_cmds = vm.ssh_commands() print(ssh_cmds["direct"]) # SSH to guest IP print(ssh_cmds["localhost"]) # SSH via localhost (if forwarded)
SmolVM raises specific exceptions for different failure scenarios:
from smolvm import SmolVMfrom smolvm.exceptions import ( SmolVMError, CommandExecutionUnavailableError, OperationTimeoutError,)try: with SmolVM() as vm: result = vm.run("some-command", timeout=5)except CommandExecutionUnavailableError as e: print(f"SSH not available: {e.reason}") print(f"Remediation: {e.remediation}")except OperationTimeoutError as e: print(f"Operation timed out: {e.message}")except SmolVMError as e: print(f"SmolVM error: {e}")