SmolVM provides seamless command execution inside microVMs via SSH. The run() method handles SSH connection management automatically, supporting both login shell and raw execution modes.
Executes commands directly without shell wrapping:
# Direct execution - no shell environment loadingresult = vm.run("hostname", shell="raw")
Raw mode:
Skips profile loading
Faster execution
Useful for simple commands that don’t need environment
Required for non-shell binaries
from smolvm import SmolVMwith SmolVM() as vm: # Set environment variable vm.set_env_vars({"API_KEY": "secret-123"}) # Login shell loads the env var result = vm.run("echo $API_KEY", shell="login") print(result.output) # secret-123
from smolvm.exceptions import CommandExecutionUnavailableErrortry: result = vm.run("whoami")except CommandExecutionUnavailableError as e: print(f"SSH not available: {e.message}") print(f"Remediation: {e.remediation}")
Commands can only run on VMs with SSH support. Auto-configured VMs and images built with ImageBuilder include SSH by default.
from smolvm import SmolVMfrom smolvm.exceptions import ( SmolVMError, CommandExecutionUnavailableError, OperationTimeoutError)with SmolVM() as vm: try: result = vm.run("complex-command", timeout=30) if not result.ok: print(f"Command failed with exit code {result.exit_code}") print(f"stderr: {result.stderr}") # Decide how to handle based on exit code except OperationTimeoutError: print("Command exceeded 30 second timeout") except CommandExecutionUnavailableError as e: print(f"SSH not ready: {e.remediation}") except SmolVMError as e: print(f"VM error: {e}")
For custom boot sequences, explicitly wait for SSH:
vm = SmolVM(config)vm.start()# Wait up to 60 seconds for SSH to be readyvm.wait_for_ssh(timeout=60.0)# Now safe to run commandsresult = vm.run("hostname")
if vm.can_run_commands(): result = vm.run("whoami")else: print("This VM doesn't support command execution") print("Rebuild with SSH_BOOT_ARGS or use SmolVM() auto-config")
When is command execution unavailable?
Command execution requires:
VM booted with init=/init in boot args
SSH server running in the guest
Network connectivity
Auto-configured VMs and ImageBuilder images have SSH by default. Custom images without SSH support cannot execute commands.