Create, start, stop, pause, and reconnect to SmolVM sandboxes
Every SmolVM sandbox follows a simple lifecycle: you create it, start it, run your work, and tear it down. Most of the time the context manager (with SmolVM() as vm) handles all of this for you. This guide covers the full lifecycle for cases where you need more control.
def start(self, boot_timeout: float = 30.0) -> SmolVM: """Start the VM. Args: boot_timeout: Maximum seconds to wait for boot. Returns: self for method chaining. """
If a sandbox is already running, calling start() is a no-op and returns immediately.
On the Firecracker backend, you can pause a running VM and resume it later. This suspends execution without stopping the VM process, so the guest resumes exactly where it left off.
The recommended way to manage the lifecycle is with Python’s with statement:
from smolvm import SmolVMwith SmolVM() as vm: # VM automatically starts on context entry print(f"VM running: {vm.vm_id}") result = vm.run("hostname") print(result.output) # VM automatically stops and deletes on context exit
from smolvm import SmolVM# Create a VM without context managervm = SmolVM()vm.start()vm_id = vm.vm_idvm.close() # Release resources but don't delete# Later, reconnect to the same VMvm2 = SmolVM.from_id(vm_id)print(f"Reconnected to {vm2.vm_id}")print(f"Status: {vm2.status}") # VMState.RUNNING# Continue using the VMresult = vm2.run("uptime")print(result.output)
from smolvm import SmolVM# In a different Python session or scriptvm = SmolVM.from_id("persistent-agent-vm")print(f"Reconnected to {vm.vm_id}")print(f"Status: {vm.status}")result = vm.run("uptime")print(result.output)
3
Clean up when done
vm = SmolVM.from_id("persistent-agent-vm")vm.stop()vm.delete()print("VM permanently deleted")
You can explicitly wait for SSH to become available:
vm = SmolVM()vm.start()# Wait up to 60 seconds for SSHvm.wait_for_ssh(timeout=60.0)print("SSH is ready")# Now run commandsresult = vm.run("whoami")print(result.output)
You typically don’t need to call wait_for_ssh() explicitly. The run() method automatically waits for SSH on first use.
You can save the full state of a running VM and restore it later using snapshots. This is useful for checkpointing before risky operations or reusing a configured environment.