snapshot

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusDeleting = "DELETING"
	StatusAttached = "ATTACHED"

	LeaseStatusBound = "BOUND"
)
View Source
const FormatVersion uint32 = 1

FormatVersion is the current snapshot format version.

Variables

View Source
var Magic = [4]byte{'M', 'I', 'R', 'N'}

Magic is the file magic for miren snapshot files.

Functions

func Backup

func Backup(dst io.WriteSeeker, src io.Reader, name string, sizeBytes int64, filesystem string) (checksum string, err error)

Backup reads the disk image from src, computes its SHA-256 checksum, and writes a complete snapshot (header + zstd-compressed data) to dst. It uses a TeeReader to hash and compress in a single pass, then seeks back to rewrite the header with the final checksum. It returns the checksum of the uncompressed image data.

func RestoreImage

func RestoreImage(dst *os.File, src io.Reader, meta *Meta) error

RestoreImage reads zstd-compressed data from src, decompresses it, and writes to dst using sparse-aware writes. The src stream should be positioned after the snapshot header (i.e., at the start of compressed data). The SHA-256 checksum of the decompressed data is verified against meta.Checksum.

func WriteHeader

func WriteHeader(w io.Writer, meta *Meta) error

WriteHeader writes the snapshot header (magic + version + length-prefixed JSON metadata) to w.

Types

type BackupTarget

type BackupTarget struct {
	Name       string
	Filesystem string
	ImagePath  string
	IsAttached bool
}

BackupTarget contains resolved and validated information needed to perform a disk backup.

func PrepareBackup

func PrepareBackup(ctx context.Context, resolver DiskResolver, name string, dataPath string) (*BackupTarget, error)

PrepareBackup resolves disk entities and validates the disk is in a state suitable for backup.

type DiskCreator

type DiskCreator interface {
	CreateDiskAndVolume(ctx context.Context, name string, sizeBytes int64, filesystem string, dataPath string) (*RestoreTarget, error)
}

DiskCreator can create disk and volume entities for restore.

type DiskResolver

type DiskResolver interface {
	FindDisk(ctx context.Context, name string) (*DiskState, error)
	FindVolume(ctx context.Context, diskID string) (*VolumeState, error)
	FindLeases(ctx context.Context, diskID string) ([]LeaseState, error)
}

DiskResolver resolves disk-related entities from the entity store.

type DiskState

type DiskState struct {
	ID         string
	Name       string
	Status     string
	Filesystem string
}

DiskState holds the state of a disk entity as returned by a DiskResolver.

type LeaseState

type LeaseState struct {
	ID     string
	Status string
}

LeaseState holds the state of a disk lease entity.

type Meta

type Meta struct {
	Name       string    `json:"name"`
	SizeBytes  int64     `json:"size_bytes"`
	Filesystem string    `json:"filesystem"`
	Timestamp  time.Time `json:"timestamp"`
	Checksum   string    `json:"checksum"` // SHA-256 hex
	Version    uint32    `json:"version"`
}

Meta holds the metadata stored in a snapshot header.

func ReadHeader

func ReadHeader(r io.Reader) (*Meta, error)

ReadHeader reads and validates the snapshot header from r, returning the metadata.

type RestoreOption

type RestoreOption func(*restoreConfig)

RestoreOption configures PrepareRestore behavior.

func WithCreator

func WithCreator(c DiskCreator, sizeBytes int64, filesystem string) RestoreOption

WithCreator enables auto-creation of disk entities if they don't exist.

type RestoreTarget

type RestoreTarget struct {
	Name      string
	ImagePath string
	Created   bool                            // true if the disk was freshly created (no --force needed)
	Finalize  func(ctx context.Context) error // called after image is written to complete entity setup
	Cleanup   func(ctx context.Context) error // called on failure to remove entities created during restore
}

RestoreTarget contains resolved and validated information needed to perform a disk restore.

func PrepareRestore

func PrepareRestore(ctx context.Context, resolver DiskResolver, name string, dataPath string, opts ...RestoreOption) (*RestoreTarget, error)

PrepareRestore resolves disk entities and validates the disk is in a state suitable for restore (not deleting, no bound leases). If the disk doesn't exist and creator is non-nil, it creates the disk and volume entities. If creator is nil and the disk doesn't exist, it returns an error.

type VolumeState

type VolumeState struct {
	VolumeID  string
	ImagePath string
}

VolumeState holds the state of a disk volume entity.

Jump to

Keyboard shortcuts

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