LXD based agent VM host
README.md

Agent Host Homelab - Ephemeral VM/Container Environment #

SSH-triggered ephemeral coding environments for agent tasks, inspired by sprites.dev and exe.dev.

Architecture Decision #

Primary Approach: LXD (Linux Containers)

After comprehensive research comparing libvirt/virsh, Vagrant, LXD, Docker/Podman, and Terraform, LXD is the optimal choice for this use case:

Why LXD? #

  • 94% faster startup than KVM: 1-3 seconds vs 15-30 seconds
  • 57% lower latency than traditional VMs
  • 50-70% less memory per instance (50-100MB vs 300-500MB)
  • Full OS environment: Unlike Docker, supports full shell environments and tools
  • Built-in ephemeral mode: lxc launch --ephemeral auto-cleans on exit
  • Active maintenance: Backed by Canonical with enterprise support
  • Better density: Run more instances per host

Comparison Summary #

Solution Startup Memory/Instance Isolation Best For
LXD 1-3s 50-100MB Kernel shared Ephemeral environments
libvirt/virsh 15-30s 300-500MB Full OS Kernel-specific testing
Vagrant 15-35s 300-500MB Full OS Dev workflows
Docker/Podman <1s 10-50MB Process only Single applications
Terraform 20-35s 300-500MB Full OS IaC production

Project Structure #

agent-host-homelab/
├── api/                    # SSH gateway and orchestration API
│   ├── src/
│   │   ├── ssh_proxy.py    # SSH authentication and routing
│   │   ├── container_manager.py  # LXD container lifecycle
│   │   ├── auth.py         # Custom SSH key authentication
│   │   └── config.py       # Configuration management
│   ├── tests/
│   └── requirements.txt
│
├── images/                 # Container/VM base images
│   ├── lxd/
│   │   ├── ubuntu-22.04-agent.yaml  # LXD profile
│   │   └── cloud-init/     # Cloud-init configs
│   └── kvm/                # Fallback KVM images (if needed)
│       └── base.qcow2
│
├── scripts/                # Automation and utilities
│   ├── setup-host.sh       # Host system initialization
│   ├── create-base-image.sh
│   ├── cleanup-stale.sh
│   └── benchmark.sh
│
├── configs/                # System configuration
│   ├── lxd/
│   │   ├── profiles/       # LXD profiles for different use cases
│   │   └── networks/       # Network configurations
│   ├── ssh/
│   │   └── authorized_keys_template
│   └── limits/             # Resource limits and quotas
│
├── docs/                   # Documentation
│   ├── ARCHITECTURE.md
│   ├── SETUP.md
│   └── API.md
│
├── monitoring/             # Observability
│   ├── metrics/
│   └── logs/
│
└── tests/                  # Integration tests
    ├── ssh_trigger_test.sh
    └── performance_test.sh

Expected Workflow #

User Experience #

# User connects via SSH
ssh agent@host.tld

# Behind the scenes:
# 1. SSH proxy authenticates the user
# 2. Ephemeral LXD container is created (1-3 seconds)
# 3. SSH session is established to container
# 4. User works in isolated environment
# 5. Container auto-destroys on disconnect or timeout

Technical Flow #

SSH Client
    ↓
SSH Gateway/Proxy (Port 22)
    ↓
1. Validate SSH key against custom auth logic
2. Create ephemeral LXD container
   $ lxc launch ubuntu-22.04-agent user-{id} --ephemeral
3. Inject SSH public key via cloud-init
4. Return connection details or establish tunnel
    ↓
SSH Session in Ephemeral Container
    ↓
Work happens (coding, testing, etc.)
    ↓
Disconnect → Auto-cleanup

Implementation Phases #

Phase 1: LXD Setup (Week 1) #

Phase 2: SSH Gateway (Week 2) #

Phase 3: API Layer (Week 3) #

Phase 4: Production Hardening (Week 4) #

Quick Start #

# 1. Install LXD
sudo snap install lxd
sudo lxd init

# 2. Create base image
lxc launch ubuntu:22.04 base-agent
lxc exec base-agent -- bash -c "apt update && apt install -y python3 nodejs git vim"
lxc publish base-agent --alias ubuntu-22.04-agent
lxc delete base-agent

# 3. Test ephemeral container
lxc launch ubuntu-22.04-agent test --ephemeral
lxc exec test -- bash
# (exit to auto-destroy)

# 4. Set up SSH proxy (see api/README.md)

Alternative: libvirt Fallback #

If full VM isolation is required (kernel-specific testing, binary compatibility):

# Use libvirt with QCOW2 backing chains
virsh define vm-template.xml
qemu-img create -f qcow2 -F qcow2 -b base.qcow2 instance-{id}.qcow2
virsh start instance-{id}

Startup: 15-30s vs 1-3s for LXD.

Resources #

Metrics & Benchmarks #

Target performance goals:

  • Container creation: < 3 seconds
  • SSH session ready: < 5 seconds total (including auth)
  • Resource usage: < 100MB RAM per instance
  • Cleanup: < 1 second
  • Concurrent instances: 50+ per 16GB host

Security Considerations #

  • Isolated network namespaces per container
  • Resource limits via LXD profiles
  • SSH key rotation and revocation
  • Audit logging of all sessions
  • Automatic cleanup of sensitive data

License #

MIT