This comprehensive guide covers all aspects of virtual machine management in the MicroVM Sandbox.
# Create a minimal Linux VM
curl -X POST http://localhost:8000/api/v1/vms \
-H "Content-Type: application/json" \
-d '{
"name": "web-server",
"os_type": "linux",
"vcpus": 2,
"memory_mb": 512,
"template": "linux-default"
}'# Create a VM with custom networking and storage
curl -X POST http://localhost:8000/api/v1/vms \
-H "Content-Type: application/json" \
-d '{
"name": "database-server",
"os_type": "linux",
"vcpus": 4,
"memory_mb": 2048,
"template": "linux-default",
"network_config": {
"bridge": "chbr0",
"ip": "192.168.200.50"
},
"storage_config": {
"disk_size_gb": 20,
"disk_type": "ssd"
},
"cpu_limit_percent": 80,
"memory_limit_percent": 90
}'VM templates provide predefined configurations for common use cases:
# config/vm-templates/web-server.yaml
web_server:
vcpus: 2
memory_mb: 1024
kernel: "images/linux/vmlinux.bin"
rootfs: "images/linux/web-server-rootfs.ext4"
boot_args: "console=ttyS0 reboot=k panic=1 root=/dev/vda rw"
network_config:
bridge: "chbr0"
guest_agent:
enabled: true
port: 8080
packages:
- nginx
- nodejs
- npmUse the template:
curl -X POST http://localhost:8000/api/v1/vms \
-H "Content-Type: application/json" \
-d '{
"name": "my-web-server",
"template": "web-server"
}'# Start a VM
curl -X POST http://localhost:8000/api/v1/vms/web-server/start
# Check boot progress
curl http://localhost:8000/api/v1/vms/web-server | jq '.status'# Graceful shutdown (gives VM 30 seconds to shut down)
curl -X POST http://localhost:8000/api/v1/vms/web-server/stop \
-H "Content-Type: application/json" \
-d '{
"force": false,
"timeout_seconds": 30
}'# Force immediate shutdown
curl -X POST http://localhost:8000/api/v1/vms/web-server/stop \
-H "Content-Type: application/json" \
-d '{"force": true}'# Restart a VM
curl -X POST http://localhost:8000/api/v1/vms/web-server/restart# Get detailed VM information
curl http://localhost:8000/api/v1/vms/web-serverResponse includes:
- Current status (created, running, stopped)
- Resource allocation and usage
- Network configuration
- Uptime and performance metrics
- Guest agent status
# List all VMs
curl http://localhost:8000/api/v1/vms
# Filter by status
curl "http://localhost:8000/api/v1/vms?status=running"
# Filter by OS type
curl "http://localhost:8000/api/v1/vms?os_type=linux"
# Pagination
curl "http://localhost:8000/api/v1/vms?limit=10&offset=20"# Execute a simple command
curl -X POST http://localhost:8000/api/v1/vms/web-server/execute \
-H "Content-Type: application/json" \
-d '{"command": "uname -a"}'# Execute with custom environment
curl -X POST http://localhost:8000/api/v1/vms/web-server/execute \
-H "Content-Type: application/json" \
-d '{
"command": "echo $MY_VAR",
"environment": {
"MY_VAR": "Hello from host",
"PATH": "/usr/local/bin:/usr/bin:/bin"
}
}'# Execute with custom timeout
curl -X POST http://localhost:8000/api/v1/vms/web-server/execute \
-H "Content-Type: application/json" \
-d '{
"command": "sleep 60 && echo done",
"timeout_seconds": 120
}'# Execute in specific directory
curl -X POST http://localhost:8000/api/v1/vms/web-server/execute \
-H "Content-Type: application/json" \
-d '{
"command": "ls -la",
"working_directory": "/var/log"
}'For interactive workflows, use the WebSocket API:
const ws = new WebSocket('ws://localhost:8000/api/v1/vms/web-server/shell');
ws.onopen = function() {
ws.send(JSON.stringify({
type: 'command',
data: 'bash'
}));
};
ws.onmessage = function(event) {
const message = JSON.parse(event.data);
console.log('Output:', message.data);
};
// Send commands
ws.send(JSON.stringify({
type: 'input',
data: 'ls -la\n'
}));# Upload a configuration file
curl -X POST http://localhost:8000/api/v1/vms/web-server/files/upload \
-F "file=@nginx.conf" \
-F "path=/etc/nginx/nginx.conf" \
-F "permissions=644"# Upload multiple files
for file in config/*.conf; do
curl -X POST http://localhost:8000/api/v1/vms/web-server/files/upload \
-F "file=@$file" \
-F "path=/etc/app/$(basename $file)"
done# Upload with progress bar
curl -X POST http://localhost:8000/api/v1/vms/web-server/files/upload \
-F "file=@large-dataset.tar.gz" \
-F "path=/tmp/dataset.tar.gz" \
--progress-bar -o upload-progress.txt# Download a log file
curl "http://localhost:8000/api/v1/vms/web-server/files/download?path=/var/log/nginx/access.log" \
-o access.log# List files in a directory
curl "http://localhost:8000/api/v1/vms/web-server/files?path=/var/log&recursive=true"# Create and download archive of directory
curl -X POST http://localhost:8000/api/v1/vms/web-server/execute \
-H "Content-Type: application/json" \
-d '{"command": "tar -czf /tmp/logs.tar.gz /var/log"}'
curl "http://localhost:8000/api/v1/vms/web-server/files/download?path=/tmp/logs.tar.gz" \
-o logs.tar.gz# Upload and extract archive
curl -X POST http://localhost:8000/api/v1/vms/web-server/files/upload \
-F "file=@application.tar.gz" \
-F "path=/tmp/app.tar.gz"
curl -X POST http://localhost:8000/api/v1/vms/web-server/execute \
-H "Content-Type: application/json" \
-d '{
"command": "cd /opt && tar -xzf /tmp/app.tar.gz",
"working_directory": "/opt"
}'# Get current resource usage
curl http://localhost:8000/api/v1/vms/web-server/metrics# Get metrics over time period
curl "http://localhost:8000/api/v1/vms/web-server/metrics/history?start=2025-10-01T00:00:00Z&end=2025-10-01T23:59:59Z"# Scale up VM resources
curl -X PUT http://localhost:8000/api/v1/vms/web-server/resources \
-H "Content-Type: application/json" \
-d '{
"vcpus": 4,
"memory_mb": 2048,
"cpu_limit_percent": 80,
"memory_limit_percent": 90
}'# Set resource limits
curl -X PUT http://localhost:8000/api/v1/vms/web-server/limits \
-H "Content-Type: application/json" \
-d '{
"cpu_quota_percent": 75,
"memory_limit_mb": 1024,
"disk_limit_gb": 10,
"network_bandwidth_mbps": 100
}'# Configure auto-scaling rules
curl -X POST http://localhost:8000/api/v1/vms/web-server/autoscale \
-H "Content-Type: application/json" \
-d '{
"enabled": true,
"cpu_threshold": 80,
"memory_threshold": 85,
"scale_up_action": {
"vcpus_delta": 1,
"memory_mb_delta": 512
},
"scale_down_action": {
"vcpus_delta": -1,
"memory_mb_delta": -512
},
"cooldown_seconds": 300
}'# Create a snapshot
curl -X POST http://localhost:8000/api/v1/vms/web-server/snapshots \
-H "Content-Type: application/json" \
-d '{
"name": "before-update",
"description": "Clean state before applying security updates"
}'# Include VM memory state in snapshot
curl -X POST http://localhost:8000/api/v1/vms/web-server/snapshots \
-H "Content-Type: application/json" \
-d '{
"name": "running-state",
"description": "Snapshot with running state",
"include_memory": true
}'# List all snapshots for a VM
curl http://localhost:8000/api/v1/vms/web-server/snapshots# Get detailed snapshot info
curl http://localhost:8000/api/v1/vms/web-server/snapshots/before-update# Stop VM first
curl -X POST http://localhost:8000/api/v1/vms/web-server/stop
# Restore from snapshot
curl -X POST http://localhost:8000/api/v1/vms/web-server/snapshots/before-update/restore
# Start VM
curl -X POST http://localhost:8000/api/v1/vms/web-server/start# Create new VM from snapshot
curl -X POST http://localhost:8000/api/v1/vms/web-server/snapshots/before-update/clone \
-H "Content-Type: application/json" \
-d '{
"new_vm_name": "web-server-clone",
"vcpus": 2,
"memory_mb": 1024
}'# Get VM network info
curl http://localhost:8000/api/v1/vms/web-server/network# Change VM IP address
curl -X PUT http://localhost:8000/api/v1/vms/web-server/network \
-H "Content-Type: application/json" \
-d '{
"ip_address": "192.168.200.100",
"netmask": "255.255.255.0",
"gateway": "192.168.200.1"
}'# Forward host port 8080 to VM port 80
curl -X POST http://localhost:8000/api/v1/vms/web-server/network/port-forward \
-H "Content-Type: application/json" \
-d '{
"host_port": 8080,
"guest_port": 80,
"protocol": "tcp",
"description": "Web server access"
}'# List active port forwards
curl http://localhost:8000/api/v1/vms/web-server/network/port-forwards# Remove port forward
curl -X DELETE http://localhost:8000/api/v1/vms/web-server/network/port-forward/8080# Create private network for VM group
curl -X POST http://localhost:8000/api/v1/networks \
-H "Content-Type: application/json" \
-d '{
"name": "private-net",
"subnet": "10.0.1.0/24",
"isolated": true,
"dhcp_enabled": true
}'# Move VM to private network
curl -X PUT http://localhost:8000/api/v1/vms/web-server/network \
-H "Content-Type: application/json" \
-d '{
"network": "private-net",
"ip_address": "10.0.1.10"
}'# Create Windows VM with UEFI boot
curl -X POST http://localhost:8000/api/v1/vms \
-H "Content-Type: application/json" \
-d '{
"name": "windows-server",
"os_type": "windows",
"vcpus": 4,
"memory_mb": 4096,
"template": "windows-server-2022",
"boot_type": "uefi"
}'# Execute PowerShell command
curl -X POST http://localhost:8000/api/v1/vms/windows-server/execute \
-H "Content-Type: application/json" \
-d '{
"command": "Get-Process | Where-Object CPU -gt 10",
"shell": "powershell"
}'# Install Windows feature
curl -X POST http://localhost:8000/api/v1/vms/windows-server/execute \
-H "Content-Type: application/json" \
-d '{
"command": "Install-WindowsFeature -Name IIS-WebServerRole -IncludeManagementTools",
"shell": "powershell"
}'# cloud-init.yaml
#cloud-config
users:
- name: admin
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys:
- ssh-rsa AAAAB3...
packages:
- nginx
- docker.io
- git
runcmd:
- systemctl enable nginx
- systemctl start nginx
- usermod -aG docker admin# Create VM with cloud-init
curl -X POST http://localhost:8000/api/v1/vms \
-H "Content-Type: application/json" \
-d '{
"name": "auto-provisioned",
"os_type": "linux",
"vcpus": 2,
"memory_mb": 1024,
"template": "linux-default",
"cloud_init": "'$(cat cloud-init.yaml | base64 -w 0)'"
}'#!/bin/bash
# create-vm-cluster.sh
VMS=("web-1" "web-2" "web-3" "db-1")
for vm in "${VMS[@]}"; do
curl -X POST http://localhost:8000/api/v1/vms \
-H "Content-Type: application/json" \
-d "{
\"name\": \"$vm\",
\"os_type\": \"linux\",
\"vcpus\": 2,
\"memory_mb\": 1024,
\"template\": \"web-server\"
}"
curl -X POST http://localhost:8000/api/v1/vms/$vm/start
done#!/bin/bash
# parallel-updates.sh
VMS=("web-1" "web-2" "web-3")
# Update all VMs in parallel
for vm in "${VMS[@]}"; do
{
curl -X POST http://localhost:8000/api/v1/vms/$vm/execute \
-H "Content-Type: application/json" \
-d '{"command": "apt update && apt upgrade -y"}'
} &
done
wait # Wait for all background jobs to complete
echo "All VMs updated"# Enable detailed monitoring
curl -X PUT http://localhost:8000/api/v1/vms/web-server/monitoring \
-H "Content-Type: application/json" \
-d '{
"enabled": true,
"interval_seconds": 10,
"metrics": [
"cpu_usage",
"memory_usage",
"disk_io",
"network_io",
"process_count"
]
}'# Configure alerts
curl -X POST http://localhost:8000/api/v1/vms/web-server/alerts \
-H "Content-Type: application/json" \
-d '{
"name": "high-cpu-usage",
"condition": "cpu_usage_percent > 80",
"duration_seconds": 300,
"action": "email",
"recipients": ["admin@example.com"]
}'- Right-sizing VMs: Start with minimal resources and scale up based on monitoring
- Memory allocation: Leave 20% buffer for host system
- CPU allocation: Don't over-allocate vCPUs beyond physical cores
- Storage: Use SSD storage for database VMs
- Network isolation: Use separate networks for different VM groups
- Regular snapshots: Create snapshots before major changes
- Access control: Use authentication and RBAC
- Updates: Keep VM images and host system updated
- Batch operations: Use parallel operations for multiple VMs
- Resource monitoring: Set up alerts for resource exhaustion
- Snapshot management: Clean up old snapshots regularly
- Network optimization: Use appropriate network configurations
- Snapshot strategy: Regular automated snapshots
- External backups: Export important snapshots
- Recovery testing: Regularly test snapshot restoration
- Documentation: Document recovery procedures
# Check VM configuration
curl http://localhost:8000/api/v1/vms/web-server
# Check host resources
curl http://localhost:8000/api/v1/system/metrics
# Check logs
curl http://localhost:8000/api/v1/vms/web-server/logs# Check guest agent status
curl http://localhost:8000/api/v1/vms/web-server/agent/status
# Restart guest agent
curl -X POST http://localhost:8000/api/v1/vms/web-server/execute \
-H "Content-Type: application/json" \
-d '{"command": "systemctl restart guest-agent"}'# Test network connectivity
curl -X POST http://localhost:8000/api/v1/vms/web-server/execute \
-H "Content-Type: application/json" \
-d '{"command": "ping -c 4 8.8.8.8"}'
# Check network configuration
curl http://localhost:8000/api/v1/vms/web-server/network# Get comprehensive debug info
curl http://localhost:8000/api/v1/vms/web-server/debug
# Export VM configuration
curl http://localhost:8000/api/v1/vms/web-server/export > vm-config.json