computer-host/internal/config/config.go
Hari b5c97aef07 host api alignment (#7)
* feat: add Firecracker API client methods for VM pause/resume and snapshots

Add PatchVm, GetVm, PutSnapshotCreate, and PutSnapshotLoad methods to the
API client, along with supporting types (VmState, SnapshotCreateParams,
SnapshotLoadParams, MemBackend).

* feat: add snapshot data layer - contract types, model, store, config

Add SnapshotID and snapshot contract types, SnapshotRecord model,
store interface CRUD methods with file store implementation,
snapshot paths helper, SnapshotsDir config, and directory creation.

* feat: add runtime methods for VM pause, resume, snapshot, and restore

Implement Pause, Resume, CreateSnapshot, and RestoreBoot on the
firecracker Runtime. RestoreBoot launches a jailer, stages snapshot
files into the chroot, loads the snapshot, and resumes the VM.

* feat: add daemon snapshot create, restore, and reconciliation logic

Implement CreateSnapshot (pause, snapshot, COW-copy disk, resume),
RestoreSnapshot (COW-copy disk, RestoreBoot, wait for guest),
GetSnapshot, ListSnapshots, DeleteSnapshotByID, and crash recovery
reconciliation for snapshot and restore operations.

* feat: add HTTP endpoints for snapshot create, get, list, delete, restore

Wire 5 snapshot routes: POST /machines/{id}/snapshots (create),
GET /machines/{id}/snapshots (list), GET /snapshots/{id} (get),
DELETE /snapshots/{id} (delete), POST /snapshots/{id}/restore (restore).

* fix: cross-device rename, restore network, and snapshot cleanup

- Replace os.Rename with copy+remove for moving snapshot files out of
  /proc/<pid>/root/ (cross-device link error on Linux)
- Reconfigure network interface after snapshot load so the restored VM
  uses its own tap device instead of the source VM's
- Clean partial snapshot dirs immediately on failure instead of only
  via reconcile
- Reject snapshot requests while a machine operation is already pending

* fix: test and modify snapshot runtime

* feat: snapshot lifecycle update, align runtime issues between host image
and daemon
2026-04-08 22:21:46 -04:00

97 lines
3.1 KiB
Go

package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/getcompanion-ai/computer-host/internal/firecracker"
)
const defaultSocketName = "firecracker-host.sock"
// Config contains the host-local daemon settings.
type Config struct {
RootDir string
StatePath string
OperationsPath string
ArtifactsDir string
MachineDisksDir string
SnapshotsDir string
RuntimeDir string
SocketPath string
EgressInterface string
FirecrackerBinaryPath string
JailerBinaryPath string
}
// Load loads and validates the firecracker-host daemon configuration from the environment.
func Load() (Config, error) {
rootDir := filepath.Clean(strings.TrimSpace(os.Getenv("FIRECRACKER_HOST_ROOT_DIR")))
cfg := Config{
RootDir: rootDir,
StatePath: filepath.Join(rootDir, "state", "state.json"),
OperationsPath: filepath.Join(rootDir, "state", "ops.json"),
ArtifactsDir: filepath.Join(rootDir, "artifacts"),
MachineDisksDir: filepath.Join(rootDir, "machine-disks"),
SnapshotsDir: filepath.Join(rootDir, "snapshots"),
RuntimeDir: filepath.Join(rootDir, "runtime"),
SocketPath: filepath.Join(rootDir, defaultSocketName),
EgressInterface: strings.TrimSpace(os.Getenv("FIRECRACKER_HOST_EGRESS_INTERFACE")),
FirecrackerBinaryPath: strings.TrimSpace(os.Getenv("FIRECRACKER_BINARY_PATH")),
JailerBinaryPath: strings.TrimSpace(os.Getenv("JAILER_BINARY_PATH")),
}
if err := cfg.Validate(); err != nil {
return Config{}, err
}
return cfg, nil
}
// Validate reports whether the host configuration is usable.
func (c Config) Validate() error {
if c.RootDir == "" {
return fmt.Errorf("FIRECRACKER_HOST_ROOT_DIR is required")
}
if c.FirecrackerBinaryPath == "" {
return fmt.Errorf("FIRECRACKER_BINARY_PATH is required")
}
if c.JailerBinaryPath == "" {
return fmt.Errorf("JAILER_BINARY_PATH is required")
}
if strings.TrimSpace(c.StatePath) == "" {
return fmt.Errorf("state path is required")
}
if strings.TrimSpace(c.OperationsPath) == "" {
return fmt.Errorf("operations path is required")
}
if strings.TrimSpace(c.ArtifactsDir) == "" {
return fmt.Errorf("artifacts dir is required")
}
if strings.TrimSpace(c.MachineDisksDir) == "" {
return fmt.Errorf("machine disks dir is required")
}
if strings.TrimSpace(c.SnapshotsDir) == "" {
return fmt.Errorf("snapshots dir is required")
}
if strings.TrimSpace(c.RuntimeDir) == "" {
return fmt.Errorf("runtime dir is required")
}
if strings.TrimSpace(c.SocketPath) == "" {
return fmt.Errorf("socket path is required")
}
if strings.TrimSpace(c.EgressInterface) == "" {
return fmt.Errorf("FIRECRACKER_HOST_EGRESS_INTERFACE is required")
}
return nil
}
// FirecrackerRuntimeConfig converts the daemon config into the Firecracker runtime config.
func (c Config) FirecrackerRuntimeConfig() firecracker.RuntimeConfig {
return firecracker.RuntimeConfig{
RootDir: c.RuntimeDir,
EgressInterface: c.EgressInterface,
FirecrackerBinaryPath: c.FirecrackerBinaryPath,
JailerBinaryPath: c.JailerBinaryPath,
}
}