How unixshells/clone works
Purpose and Entry Points
Clone is a Linux/KVM virtual-machine monitor optimized for high-density, isolated workloads. It cold-boots templates, snapshots them, then forks copy-on-write VM instances using Shadow Clone memory mappings. The primary CLI and single-binary entry point is src/main.rs; VM runtime orchestration begins in src/vmm/mod.rs. Supporting in-guest binaries are crates/guest-agent/src/main.rs and crates/clone-init/src/main.rs.
VM Boot and Hardware Model
The VMM constructs guest memory, KVM state, vCPU threads, MMIO devices, serial I/O, and lifecycle state in src/vmm/mod.rs, src/vmm/vcpu.rs, and src/vmm/serial.rs. Boot preparation loads kernel/initrd images, builds guest page tables and ACPI metadata, and optionally verifies measured boot hashes through src/boot/mod.rs, src/boot/acpi.rs, and src/boot/measured.rs.
Snapshot, Shadow Fork, and Migration
Template snapshots capture memory and CPU/register state; forked VMs map snapshot memory privately so pages diverge only on write. Template identity and fork-specific guest state are handled by src/boot/template.rs and src/boot/identity.rs, with lifecycle coordination in src/vmm/mod.rs. Pre-copy live migration transfers memory and final CPU state over TCP in src/migration/mod.rs, separating source and receiver command paths exposed by the CLI/control layer.
Memory Efficiency
Guest memory management combines sparse/overcommitted allocation, cross-VM KSM eligibility, split regions around x86 PCI MMIO space, and virtio-balloon reclamation. These mechanisms reside in src/memory/mod.rs, src/memory/overcommit.rs, and src/memory/balloon.rs; guest-facing balloon transport is src/virtio/balloon.rs. This is the central boundary between guest-visible RAM and host physical-memory consumption.
Devices, Storage, and Host Integration
Virtio transports provide block, networking, filesystem sharing, vsock, and balloon devices through src/virtio/mod.rs, src/virtio/mmio.rs, and src/virtio/queue.rs. Backends include raw/qcow2 disks in src/storage/mod.rs and src/storage/qcow2.rs, TAP/bridge/NAT host networking in src/net/mod.rs, and inline virtio-fs in src/virtio/fs.rs. PCI ECAM and VFIO passthrough are implemented in src/pci/mod.rs and src/pci/vfio.rs, requiring host devices bound to vfio-pci.
Control Plane and Guest Communication
The CLI supports direct per-VM operation and a multi-VM daemon. Unix-socket protocol, daemon orchestration, synchronization server, metrics, and optional jailer are grouped in src/control/protocol.rs, src/control/daemon.rs, and src/control/jailer.rs. Host-to-guest exec, heartbeat, shutdown, and post-fork recovery cross userspace vsock via src/vmm/agent_listener.rs, src/virtio/vsock.rs, and crates/guest-agent/src/main.rs.
Root Filesystems and Runtime Boundaries
src/rootfs.rs creates boot plumbing for disk-rootfs mode, including the minimal init and agent; src/rootfs_create.rs builds/imports Alpine, Ubuntu, Debian, and Docker-derived images. The VMM runs on Linux with /dev/kvm; networking needs /dev/net/tun, while KVM, Linux memory APIs, namespaces/capabilities, seccomp BPF, and VFIO are material host-kernel dependencies. The project intentionally has no libvirt, QEMU, or external virtiofs daemon dependency.
Main entry points: vmm/mod.rs VMM lifecycle · virtio/mod.rs Virtio transport · memory/mod.rs Guest memory · main.rs Clone CLI
Components 18
Entry & Control
- Clone CLI · Single-binary VM and daemon command entry point.
src/main.rs - Control plane · Unix-socket commands, multi-VM orchestration, metrics, and jailer.
src/control/daemon.rs
VMM Runtime
- VMM lifecycle · Coordinates boot, templates, forks, devices, and VM lifecycle.
src/vmm/mod.rs - vCPU threads
src/vmm/vcpu.rs - Boot preparation · Loads images and creates page tables and ACPI metadata.
src/boot/mod.rs - Measured boot
src/boot/measured.rs - Templates & identity · Captures template identity and fork-specific guest state.
src/boot/template.rs - Guest memory · Sparse allocation, overcommit, KSM eligibility, and PCI-MMIO splits.
src/memory/mod.rs - Memory reclamation
src/memory/balloon.rs - Live migration · Transfers memory followed by final CPU state.
src/migration/mod.rs - Virtio transport · Provides block, network, filesystem, vsock, and balloon devices.
src/virtio/mod.rs - Agent listener · Relays exec, heartbeat, shutdown, and recovery commands.
src/vmm/agent_listener.rs
Guest Runtime
- Guest agent
crates/guest-agent/src/main.rs - Rootfs builder · Builds or imports Linux root filesystem images.
src/rootfs_create.rs
Host Services
- Disk backends
src/storage/mod.rs - Host networking
src/net/mod.rs - PCI & VFIO · ECAM model and vfio-pci device passthrough.
src/pci/mod.rs - Linux host kernel · Supplies KVM, memory APIs, TUN, namespaces, seccomp, and VFIO.
Connections 23
- Clone CLI → Control plane: daemon commands
- Clone CLI → VMM lifecycle: direct VM commands
- Control plane → VMM lifecycle: manages VMs
- VMM lifecycle → vCPU threads: starts
- VMM lifecycle → Boot preparation: prepares boot
- Boot preparation → Measured boot: optionally verifies
- VMM lifecycle → Templates & identity: snapshots and forks
- Templates & identity → Guest memory: private COW mappings
- VMM lifecycle → Guest memory: creates guest RAM
- Guest memory → Memory reclamation: reclaims pages
- VMM lifecycle → Live migration: source/receiver lifecycle
- VMM lifecycle → Virtio transport: attaches devices
- Virtio transport → Disk backends: block I/O
- Virtio transport → Host networking: network I/O
- Virtio transport → Memory reclamation: balloon transport
- VMM lifecycle → PCI & VFIO: PCI and passthrough
- Agent listener → Guest agent: userspace vsock
- VMM lifecycle → Agent listener: guest control
- Rootfs builder → Guest agent: installs agent
- VMM lifecycle → Linux host kernel: /dev/kvm and host APIs
- Guest memory → Linux host kernel: host memory APIs
- Host networking → Linux host kernel: /dev/net/tun
- PCI & VFIO → Linux host kernel: VFIO