forked from wrenn/wrenn
v0.2.0 (#50)
Co-authored-by: Tasnim Kabir Sadik <tksadik@omukk.dev> Reviewed-on: wrenn/wrenn#50
This commit is contained in:
@ -6,26 +6,28 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.omukk.dev/wrenn/wrenn/pkg/id"
|
||||
)
|
||||
|
||||
// IsMinimal reports whether the given team and template IDs represent the
|
||||
// built-in "minimal" template (both all-zeros).
|
||||
func IsMinimal(teamID, templateID pgtype.UUID) bool {
|
||||
return teamID.Bytes == id.PlatformTeamID.Bytes && templateID.Bytes == id.MinimalTemplateID.Bytes
|
||||
func timeNowNano() int64 { return time.Now().UnixNano() }
|
||||
|
||||
// IsSystemTemplate reports whether the given team and template IDs represent a
|
||||
// built-in system base template (minimal-ubuntu / -alpine / -arch / -fedora):
|
||||
// platform-owned with a template ID in the reserved range. System templates are
|
||||
// protected from deletion.
|
||||
func IsSystemTemplate(teamID, templateID pgtype.UUID) bool {
|
||||
return teamID.Bytes == id.PlatformTeamID.Bytes && id.IsReservedTemplateID(templateID)
|
||||
}
|
||||
|
||||
// TemplateDir returns the on-disk directory for a template.
|
||||
// TemplateDir returns the on-disk directory for a template. Every template —
|
||||
// including the built-in system base templates — lives under the teams tree:
|
||||
//
|
||||
// minimal (zeros, zeros): {wrennDir}/images/minimal
|
||||
// all others: {wrennDir}/images/teams/{base36(teamID)}/{base36(templateID)}
|
||||
// {wrennDir}/images/teams/{base36(teamID)}/{base36(templateID)}
|
||||
func TemplateDir(wrennDir string, teamID, templateID pgtype.UUID) string {
|
||||
if IsMinimal(teamID, templateID) {
|
||||
return filepath.Join(wrennDir, "images", "minimal")
|
||||
}
|
||||
return filepath.Join(wrennDir, "images", "teams",
|
||||
id.UUIDToBase36(teamID.Bytes),
|
||||
id.UUIDToBase36(templateID.Bytes))
|
||||
@ -36,17 +38,64 @@ func TemplateRootfs(wrennDir string, teamID, templateID pgtype.UUID) string {
|
||||
return filepath.Join(TemplateDir(wrennDir, teamID, templateID), "rootfs.ext4")
|
||||
}
|
||||
|
||||
// PauseSnapshotDir returns the directory for a paused sandbox's snapshot files.
|
||||
func PauseSnapshotDir(wrennDir, sandboxID string) string {
|
||||
return filepath.Join(wrennDir, "snapshots", sandboxID)
|
||||
// IsSnapshotTemplate reports whether dir contains a Cloud Hypervisor memory
|
||||
// snapshot (state.json + config.json + memory-ranges) alongside the flattened
|
||||
// rootfs.ext4. Used to distinguish snapshot templates (launch via CH restore)
|
||||
// from base/disk-only templates (launch via fresh boot).
|
||||
//
|
||||
// state.json is CH-authoritative — its presence indicates a complete snapshot.
|
||||
func IsSnapshotTemplate(dir string) bool {
|
||||
for _, name := range []string{"state.json", "config.json", "rootfs.ext4"} {
|
||||
if _, err := os.Stat(filepath.Join(dir, name)); err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// SandboxesDir returns the directory for running sandbox CoW files.
|
||||
// SandboxCowName is the filename for a sandbox's CoW rootfs diff, kept inside
|
||||
// the per-sandbox directory alongside any pause snapshot files.
|
||||
const SandboxCowName = "rootfs.cow"
|
||||
|
||||
// SandboxDir returns the per-sandbox directory under sandboxes/. It holds
|
||||
// the CoW file and, if the sandbox is paused, the snapshot files.
|
||||
//
|
||||
// Layout:
|
||||
//
|
||||
// {wrennDir}/sandboxes/{id}/rootfs.cow CoW file (persistent across pause/resume)
|
||||
// {wrennDir}/sandboxes/{id}/ paused snapshot (config.json, state.json, memory-ranges, wrenn-snapshot.json)
|
||||
// {wrennDir}/sandboxes/{id}.staging-*/ in-flight Pause writes (cleaned up by swapDir or startup GC)
|
||||
// {wrennDir}/sandboxes/{id}.trash-*/ mid-swap previous generation (cleaned up by swapDir or startup GC)
|
||||
func SandboxDir(wrennDir, sandboxID string) string {
|
||||
return filepath.Join(wrennDir, "sandboxes", sandboxID)
|
||||
}
|
||||
|
||||
// SandboxCowPath returns the path to a sandbox's CoW rootfs diff file.
|
||||
func SandboxCowPath(wrennDir, sandboxID string) string {
|
||||
return filepath.Join(SandboxDir(wrennDir, sandboxID), SandboxCowName)
|
||||
}
|
||||
|
||||
// PauseSnapshotDir returns the directory for a paused sandbox's snapshot files.
|
||||
// Same path as SandboxDir — pause snapshot files live alongside the CoW.
|
||||
func PauseSnapshotDir(wrennDir, sandboxID string) string {
|
||||
return SandboxDir(wrennDir, sandboxID)
|
||||
}
|
||||
|
||||
// PauseStagingDir returns a fresh staging directory for an in-flight Pause.
|
||||
// Each call returns a unique path (timestamped) so concurrent retries do not
|
||||
// collide.
|
||||
func PauseStagingDir(wrennDir, sandboxID string) string {
|
||||
return filepath.Join(wrennDir, "sandboxes",
|
||||
fmt.Sprintf("%s.staging-%d", sandboxID, timeNowNano()))
|
||||
}
|
||||
|
||||
// SandboxesDir returns the directory for running sandbox CoW files and paused
|
||||
// snapshot directories.
|
||||
func SandboxesDir(wrennDir string) string {
|
||||
return filepath.Join(wrennDir, "sandboxes")
|
||||
}
|
||||
|
||||
// KernelPath returns the path to the Firecracker kernel.
|
||||
// KernelPath returns the path to the VM kernel.
|
||||
func KernelPath(wrennDir string) string {
|
||||
return filepath.Join(wrennDir, "kernels", "vmlinux")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user