computer-host/internal/firecracker/paths.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

84 lines
2.8 KiB
Go

package firecracker
import (
"fmt"
"path"
"path/filepath"
"strconv"
"strings"
)
const (
defaultChrootRootDirName = "root"
defaultLogDirName = "logs"
defaultSerialLogName = "serial.log"
defaultFirecrackerSocketDir = "run"
defaultFirecrackerLogName = "firecracker.log"
defaultFirecrackerSocketName = "firecracker.socket"
defaultFirecrackerSocketPath = "/run/firecracker.socket"
)
type machinePaths struct {
BaseDir string
ChrootRootDir string
JailerBaseDir string
LogDir string
FirecrackerLogPath string
JailedFirecrackerLogPath string
SerialLogPath string
JailedSerialLogPath string
PIDFilePath string
SocketPath string
}
func buildMachinePaths(rootDir string, id MachineID, firecrackerBinaryPath string) (machinePaths, error) {
rootDir = strings.TrimSpace(rootDir)
if rootDir == "" {
return machinePaths{}, fmt.Errorf("root dir is required")
}
if strings.TrimSpace(string(id)) == "" {
return machinePaths{}, fmt.Errorf("machine id is required")
}
binName := filepath.Base(strings.TrimSpace(firecrackerBinaryPath))
if binName == "." || binName == string(filepath.Separator) || binName == "" {
return machinePaths{}, fmt.Errorf("firecracker binary path is required")
}
baseDir := filepath.Join(rootDir, "machines", string(id))
jailerBaseDir := filepath.Join(baseDir, "jailer")
chrootRootDir := filepath.Join(jailerBaseDir, binName, string(id), defaultChrootRootDirName)
logDir := filepath.Join(chrootRootDir, defaultLogDirName)
return machinePaths{
BaseDir: baseDir,
ChrootRootDir: chrootRootDir,
JailerBaseDir: jailerBaseDir,
LogDir: logDir,
FirecrackerLogPath: filepath.Join(logDir, defaultFirecrackerLogName),
JailedFirecrackerLogPath: path.Join("/", defaultLogDirName, defaultFirecrackerLogName),
SerialLogPath: filepath.Join(logDir, defaultSerialLogName),
JailedSerialLogPath: path.Join("/", defaultLogDirName, defaultSerialLogName),
PIDFilePath: filepath.Join(chrootRootDir, binName+".pid"),
SocketPath: filepath.Join(chrootRootDir, defaultFirecrackerSocketDir, defaultFirecrackerSocketName),
}, nil
}
func procSocketPath(pid int) string {
return filepath.Join("/proc", strconv.Itoa(pid), "root", defaultFirecrackerSocketDir, defaultFirecrackerSocketName)
}
type snapshotPaths struct {
BaseDir string
MemFilePath string
StateFilePath string
}
func buildSnapshotPaths(rootDir string, id string) snapshotPaths {
baseDir := filepath.Join(rootDir, "snapshots", id)
return snapshotPaths{
BaseDir: baseDir,
MemFilePath: filepath.Join(baseDir, "memory.bin"),
StateFilePath: filepath.Join(baseDir, "vmstate.bin"),
}
}