Files
firecracker-orchestrator/main.go
Honza Novak 04067f7e6b feat: add structured logging, --dev flag, and snapshot network_overrides
- Add --dev flag to main that enables logrus caller info (file:line) for
  easier debugging without changing production log output
- Wire firecracker SDK logger (WithLogger) and FIFO log file to both the
  golden VM and each clone machine so Firecracker's own logs are surfaced
- Log the exact shell commands being run (cp --reflink, ip tuntap, ip link,
  firecracker binary) at Info level before each syscall/exec, making it
  straightforward to reproduce steps manually
- Extract snapshot.go with loadSnapshotWithNetworkOverride: a direct PUT
  /snapshot/load call over the Unix socket that includes network_overrides,
  remapping the stored tap to the per-clone tap name (Firecracker v1.15+
  feature not yet exposed by SDK v1.0.0)
- Use firecracker.WithSnapshot + a Handlers.FcInit.Swap to replace the SDK's
  LoadSnapshotHandler with the above when Bridge != "none"

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 09:07:35 +00:00

120 lines
2.7 KiB
Go

// fc-orchestrator — Poor man's Firecracker snapshot orchestrator in Go.
//
// Creates a golden VM, snapshots it, then spawns N clones that share the
// base memory file via Firecracker's MAP_PRIVATE (kernel-level COW).
// Rootfs gets a filesystem-level COW copy (reflink where supported).
//
// Usage:
//
// go build -o fc-orch .
// sudo ./fc-orch init
// sudo ./fc-orch golden
// sudo ./fc-orch spawn 10
// sudo ./fc-orch status
// sudo ./fc-orch kill
// sudo ./fc-orch cleanup
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
log "github.com/sirupsen/logrus"
"github.com/kacerr/fc-orchestrator/orchestrator"
)
func main() {
// strip --dev flag before subcommand routing
dev := false
filtered := os.Args[:1]
for _, a := range os.Args[1:] {
if a == "--dev" {
dev = true
} else {
filtered = append(filtered, a)
}
}
os.Args = filtered
if dev {
log.SetReportCaller(true)
log.SetFormatter(&log.TextFormatter{
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
return "", fmt.Sprintf("%s:%d", filepath.Base(f.File), f.Line)
},
})
}
// figure out if we are running as root
if os.Geteuid() == 0 {
fmt.Println("Running with root/sudo privileges!")
} else {
fmt.Println("Running as a normal user.")
}
if len(os.Args) < 2 {
usage()
os.Exit(1)
}
orch := orchestrator.New(orchestrator.DefaultConfig())
switch os.Args[1] {
case "init":
fatal(orch.Init())
case "golden":
fatal(orch.Golden())
case "spawn":
n := 1
if len(os.Args) > 2 {
fmt.Sscanf(os.Args[2], "%d", &n)
}
fatal(orch.Spawn(n))
case "status":
orch.Status()
case "kill":
fatal(orch.Kill())
case "cleanup":
fatal(orch.Cleanup())
default:
usage()
os.Exit(1)
}
}
func usage() {
fmt.Fprintf(os.Stderr, `Usage: %s [--dev] <command> [args]
Flags:
--dev log format with source file:line (e.g. file="orchestrator.go:123")
Commands:
init Download kernel + create Alpine rootfs
golden Boot golden VM → pause → snapshot
spawn [N] Restore N clones from golden snapshot (default: 1)
status Show running clones
kill Kill all running VMs
cleanup Kill VMs + remove all state
Environment:
FC_BIN firecracker binary path (default: firecracker)
FC_BASE_DIR working directory (default: /tmp/fc-orch)
FC_KERNEL vmlinux path
FC_KERNEL_URL vmlinux download URL (default: pinned Firecracker CI build)
FC_ROOTFS rootfs.ext4 path
FC_VCPUS vCPUs per VM (default: 1)
FC_MEM_MIB MiB per VM (default: 128)
FC_BRIDGE bridge name or "none" (default: fcbr0)
`, os.Args[0])
}
func fatal(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "fatal: %v\n", err)
os.Exit(1)
}
}