store

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package store manages VM state directories under ~/.fleetbox/clusters/<cluster>/<member>/.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClusterName added in v0.3.1

func ClusterName(name string) string

ClusterName derives a member's cluster name by stripping a single trailing "-<digits>" group, e.g. "web-3" → "web", "dev" → "dev". It is the exported, documented entry point to the membership rule (see clusterName for the exact cases) so callers outside the package — the CLI's down/rm target resolution — reuse the one canonical rule instead of re-deriving the digits-suffix logic.

Types

type Fixture

type Fixture struct {
	HostPath  string `json:"host_path"`
	GuestPath string `json:"guest_path"`
	Label     string `json:"label"`
}

Fixture is a persisted read-only host directory packed into the guest at boot. HostPath is absolute; Label is the stable ext4 volume label assigned at first creation (FBFIX<i>, i = position in the fixture list). The label is the single source of truth shared between the image's volume label and the guest's cloud-init LABEL= mount line, so it is computed once and persisted here rather than re-derived in two places (ADR-0015).

type Lock

type Lock struct {
	// contains filtered or unexported fields
}

Lock represents an advisory file lock on a VM.

func (*Lock) Unlock

func (l *Lock) Unlock() error

Unlock releases the lock.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store manages VM storage.

func New

func New() (*Store, error)

New creates a Store at the default location (<home>/.fleetbox). When the process is root via sudo it resolves the INVOKING user's home, not /root, so state never splits between /root/.fleetbox and ~/.fleetbox across an auto-elevated `up` and a non-root `ls`/`ssh` (ADR-0023). This is the single place the base-dir rule lives, so every process — CLI, orchestrator, holder — agrees.

func NewAt

func NewAt(baseDir string) (*Store, error)

NewAt creates a Store at the given base directory.

func (*Store) BaseDir

func (s *Store) BaseDir() string

BaseDir returns the base storage directory.

func (*Store) BinDir

func (s *Store) BinDir() string

BinDir returns the cache directory for downloaded executables and firmware (~/.fleetbox/bin). The Linux backend caches the checksum-pinned cloud-hypervisor binary and its firmware here; it is created on first download rather than by New, so macOS installs never grow an empty bin directory.

func (*Store) ControlSocketPath

func (s *Store) ControlSocketPath(primary string) string

ControlSocketPath returns the path to a bound helper's holder-wide control socket. It is distinct from the per-member SocketPath: the library client holds one long-lived connection to it for the helper's lifetime, and the helper treats that connection's EOF as "parent gone — tear everything down" (ADR-0017, R4). Like SocketPath it lives in run/ under a name hash to stay under the sun_path limit.

func (*Store) Create

func (s *Store) Create(vm *VM) error

Create creates a new VM directory and writes its config.

func (*Store) Delete

func (s *Store) Delete(name string) error

Delete removes a VM's member directory and all its contents, then drops the parent cluster directory if it is now empty.

func (*Store) DiskPath

func (s *Store) DiskPath(name string) string

DiskPath returns the path to the VM's disk image.

func (*Store) EFIPath

func (s *Store) EFIPath(name string) string

EFIPath returns the path to the VM's EFI variable store.

func (*Store) EnsureDir

func (s *Store) EnsureDir(name string) error

EnsureDir creates the member directory (and its cluster parent) for a VM. It is idempotent and is the single place member directories are created, so the holder (which serves a member's socket/pidfile before the VM boots) and Create both go through it.

func (*Store) Exists

func (s *Store) Exists(name string) bool

Exists returns true if a VM with the given name exists (has config.json).

func (*Store) FixturePath

func (s *Store) FixturePath(name string, i int) string

FixturePath returns the path to the i-th fixture's ext4 image inside the VM's member directory (fixture-<i>.img). It is per-member, not cluster-level, so the existing Delete → RemoveAll(memberDir) wipes it for free with no extra teardown (ADR-0015).

func (*Store) ImagesDir

func (s *Store) ImagesDir() string

ImagesDir returns the images cache directory.

func (*Store) List

func (s *Store) List() ([]string, error)

List returns the names of all stored VM members, walking the two-level clusters/<cluster>/<member>/ tree and returning the flat list of member names (the unit every caller works in). Results are cluster-sorted then member-sorted (os.ReadDir sorts each level), not creation order.

func (*Store) Load

func (s *Store) Load(name string) (*VM, error)

Load reads a VM config from disk.

func (*Store) NetworkStateDir

func (s *Store) NetworkStateDir() string

NetworkStateDir returns the directory holding the Linux backend's per-network write-ahead records and ip_forward marker (~/.fleetbox/networks). It lets a crashed cluster's bridges/taps/iptables rules be reclaimed on the next up or via prune (ADR-0013); it is created on first network create, so macOS installs never grow it.

func (*Store) PidfilePath

func (s *Store) PidfilePath(name string) string

PidfilePath returns the path to the VM's holder pidfile, inside its member directory. Every member served by one holder writes the same PID (os.Getpid) into its own pidfile.

func (*Store) SSHKeyPath

func (s *Store) SSHKeyPath() string

SSHKeyPath returns the path to the SSH private key.

func (*Store) Save

func (s *Store) Save(vm *VM) error

Save writes the VM config to disk.

func (*Store) SeedPath

func (s *Store) SeedPath(name string) string

SeedPath returns the path to the VM's seed ISO.

func (*Store) SerialLogPath

func (s *Store) SerialLogPath(name string) string

SerialLogPath returns the path to the VM's serial log.

func (*Store) SocketPath

func (s *Store) SocketPath(name string) string

SocketPath returns the path to a member's holder control socket. It lives in ~/.fleetbox/run/ under a hash of the name, NOT in the member directory: a unix socket path must fit the 104-byte sun_path limit, and the nested member dir (clusters/<cluster>/<member>/) plus a long name and a long home dir blows past it. The hash keeps the path short and bounded regardless of name length (amends ADR-0014, which had placed it in the member dir).

func (*Store) TryLock

func (s *Store) TryLock(name string) (*Lock, error)

TryLock attempts to acquire an exclusive lock on the VM. Returns nil if locked successfully, error otherwise.

func (*Store) VMDir

func (s *Store) VMDir(name string) string

VMDir returns the member directory for a VM, nested under its cluster: <baseDir>/clusters/<cluster>/<member>/. The cluster segment is derived from the member name (see clusterName), so a solo VM "dev" lives at clusters/dev/dev/ and a cluster member "web-2" at clusters/web/web-2/.

type VM

type VM struct {
	Name      string    `json:"name"`
	MAC       string    `json:"mac"`
	CPUs      int       `json:"cpus"`
	MemoryMB  int       `json:"memory_mb"`
	DiskMB    int       `json:"disk_mb"`
	Image     string    `json:"image"`
	CreatedAt time.Time `json:"created_at"`
	Fixtures  []Fixture `json:"fixtures,omitempty"`
	// IP is the static IPv4 address assigned at first create on backends that
	// allocate from a known subnet (Linux/cloud-hypervisor). It is the persisted
	// source of truth so a rebooted VM keeps its address and a re-joining cluster
	// member does not collide. Empty on macOS, where addresses come from DHCP.
	IP string `json:"ip,omitempty"`
}

VM represents a stored VM configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL