image

package
v0.51.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const SchemaVersion = 1

Variables

This section is empty.

Functions

func BuildManifest

func BuildManifest(cfg BuildConfig) string

BuildManifest constructs a Nanos manifest that includes the main program and any additional package files. Guest paths with directory separators (e.g. "lib/x86_64-linux-gnu/libc.so.6" from ops sysroot packages) are serialized as nested nodes — the Nanos manifest parser treats '/' as an unknown discriminator and rejects flat slash-separated keys. cfg.Entrypoint, if non-empty, is emitted as arguments:(0:/program 1:/<entrypoint> ...) so that the runtime interpreter (e.g. node, python) receives its own path as argv[0] and the script path as argv[1] on startup. cfg.Args, if non-empty, is appended after the entrypoint (or starting at argv[1] if cfg.Entrypoint is empty) — used by lang="raw" builds to pass arguments such as ["-jar", "/app.jar"] to the resolved program. The Nanos tuple parser reads '(' as a tuple of name:value pairs — not a bare value list — so arguments must use integer-string keys rather than e.g. ("/<entrypoint>"). cfg.Env entries are emitted as environment:(KEY:val ...) sorted by key. Static network config is not baked into the manifest: the daemon injects the assigned TAP IP at run time (QEMU fw_cfg or Firecracker boot args).

func DigestSHA256

func DigestSHA256(data []byte) string

DigestSHA256 returns the sha256 digest string for data in "sha256:<hex>" form.

func Marshal

func Marshal(m Manifest) ([]byte, error)

Marshal serializes m to JSON.

Types

type BuildConfig

type BuildConfig struct {
	// Name is the image name (e.g. "hello").
	Name string
	// Tag is the image tag (default "latest" if empty).
	Tag string
	// BinaryPath is the path to the static ELF binary to package.
	BinaryPath string
	// ProgramPath, when non-empty, is the in-image guest path at which the
	// program is placed and from which it is executed (e.g.
	// "usr/local/postgresql/bin/postgres"). The Nanos manifest's program field,
	// argv[0], and /proc/self/exe all resolve to this path, so binaries that
	// locate their installation prefix relative to their own executable (e.g.
	// postgres) and binaries with $ORIGIN-relative RPATHs resolve correctly.
	// Empty places the program flat at /program (the default for compiled and
	// interpreted-runtime builds, where the layout is irrelevant).
	ProgramPath string
	// MkfsRun invokes mkfs to produce the disk image.
	// Use internal/tools.ResolveMkfs to obtain a platform-appropriate func.
	MkfsRun MkfsFunc
	// Memory is the default VM memory string (e.g. "256M").
	Memory string
	// CPUs is the default number of virtual CPUs.
	CPUs int
	// PkgFiles is a list of package files to include in the image.
	// Each entry carries both the host path (on the build machine) and the
	// guest path (inside the Nanos image). For jerboa packages, GuestPath is
	// typically filepath.Base(HostPath). For ops packages, GuestPath
	// preserves the sysroot/ hierarchy (e.g. "lib/x86_64-linux-gnu/libc.so").
	PkgFiles []pkg.File
	// Entrypoint is the script or file to pass as the first argument to the
	// runtime binary (e.g. "hi.js" for Node.js). Empty for compiled languages.
	// Emitted with a leading "/" (image-root-relative).
	Entrypoint string
	// Args holds additional argv elements appended after Entrypoint (if set).
	// Used by lang="raw" builds to pass arguments to the resolved program
	// (e.g. ["-jar", "/app.jar"]). Each element is emitted as-is — use
	// absolute in-image paths for file arguments.
	Args []string
	// Env holds runtime environment variables to bake into the image manifest.
	// Sourced from ops package.manifest Env fields and language driver output.
	Env map[string]string
	// Port is the service port declared for the image. It is metadata only:
	// network config is injected at run time (fw_cfg / boot args), not baked
	// into the manifest.
	Port int
	// Ports holds default host:guest port-publish specs (from [run] ports).
	// Stored in the image manifest and applied at run time when the VM joins a
	// network and no -p flag is given.
	Ports []string
	// DiskSize is the minimum image file size passed to mkfs (e.g. "512M", "1G").
	// When non-empty, emitted as imagesize in the Nanos manifest so mkfs pads
	// the image to at least that size, leaving free space for runtime writes.
	DiskSize string
	// Output is where mkfs subprocess output is written. Nil defaults to os.Stderr.
	Output io.Writer
}

BuildConfig holds the parameters for building a unikernel image.

type Builder

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

Builder produces unikernel images from ELF binaries and stores them.

func NewBuilder

func NewBuilder(store *Store) *Builder

NewBuilder returns a Builder that stores images in store.

func (*Builder) Build

func (b *Builder) Build(ctx context.Context, cfg BuildConfig) (Manifest, error)

Build packages binaryPath into a disk image and registers it in the store.

type Config

type Config struct {
	// Memory is the default QEMU memory string (e.g. "256M").
	Memory string `json:"memory"`
	// CPUs is the default number of virtual CPUs.
	CPUs int `json:"cpus"`
	// Env is the list of environment variables passed to the application.
	Env []string `json:"env,omitempty"`
	// Ports holds default host:guest port-publish specs (from [run] ports in
	// unikernel.toml). They are applied at run time when the VM joins a network
	// and no -p flag is given, so an image can declare the ports it serves.
	Ports []string `json:"ports,omitempty"`
}

Config holds default VM launch parameters for an image.

type Manifest

type Manifest struct {
	// SchemaVersion must equal SchemaVersion (1).
	SchemaVersion int `json:"schemaVersion"`
	// Name is the image name (e.g. "hello").
	Name string `json:"name"`
	// Tag is the image tag (e.g. "latest").
	Tag string `json:"tag"`
	// Created is the build timestamp.
	Created time.Time `json:"created"`
	// Config holds default VM parameters.
	Config Config `json:"config"`
	// DiskDigest is the sha256 digest of the raw disk image ("sha256:<hex>").
	DiskDigest string `json:"diskDigest"`
	// DiskSize is the byte size of the raw disk image.
	DiskSize int64 `json:"diskSize"`
}

Manifest describes a unikernel disk image.

func Parse

func Parse(data []byte) (Manifest, error)

Parse decodes and validates manifest JSON from data.

func (Manifest) Ref

func (m Manifest) Ref() string

Ref returns the canonical name:tag reference string.

type MkfsFunc

type MkfsFunc func(ctx context.Context, imgPath, binaryPath string, manifest string) *exec.Cmd

MkfsFunc creates an exec.Cmd that runs mkfs to package binaryPath into imgPath. manifest is the Nanos manifest string to pass on stdin. Defined here so callers can satisfy it without importing internal/tools.

type Store

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

Store is a content-addressable local image store.

Layout on disk:

<root>/
  <sha256>/
    manifest.json
    disk.img
  refs.json          (name:tag → sha256)

func NewStore

func NewStore(root string) (*Store, error)

NewStore opens (or creates) a Store rooted at root.

func (*Store) DiskPath

func (s *Store) DiskPath(ref string) (string, error)

DiskPath returns the absolute path to the disk image for ref.

func (*Store) Get

func (s *Store) Get(ref string) (Manifest, string, error)

Get returns the manifest and disk image path for ref (name:tag or sha256:<hex>).

func (*Store) List

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

List returns all unique manifests in the store.

func (*Store) Put

func (s *Store) Put(name, tag string, m Manifest, diskPath string) error

Put stores the disk image at diskPath and its manifest under name:tag.

func (*Store) Remove

func (s *Store) Remove(ref string) error

Remove removes the ref from the index. Deletes the image dir if no refs remain.

Jump to

Keyboard shortcuts

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