Files
zosstorage/docs/API.md

13 KiB

zosstorage Public API Skeletons

This document defines the initial public API surface for all modules. It lists traits, structs, enums, constants, and functions with responsibilities and behavioral contracts. Implementations are intentionally deferred until approval. Function bodies will be added later and guarded by todo placeholders.

Conventions

  • All modules return a shared Result alias and Error enum.
  • No interactive prompts; APIs are deterministic and suitable for initramfs use.
  • External tooling calls are mediated via utility wrappers.

Module index

Common errors and result

  • enum Error
    • Top-level error type covering parse/validation errors, device discovery errors, partitioning failures, filesystem mkfs errors, mount errors, report write errors, and external tool invocation failures with stderr capture.
  • type Result = std::result::Result<T, Error>
    • Shared result alias across modules.

Crate root

  • src/lib.rs
    • Exposes crate version constants, the prelude, and re-exports common types for consumers of the library (tests/integration). No heavy logic.

Entrypoint

  • fn main()
    • Initializes logging based on CLI defaults, parses CLI flags and kernel cmdline, loads and validates configuration, and invokes the orchestrator run sequence. Avoids stdout; logs via tracing only.

Orchestrator

  • struct Context
    • Aggregates resolved configuration, logging options, and environment flags suited for initramfs execution.
  • fn run(ctx: &Context) -> Result<()>
    • High-level one-shot flow:
      • Idempotency detection
      • Device discovery
      • Partition planning and application
      • Filesystem planning and creation
      • Mount planning and application
      • Report generation and write
    • Aborts the entire run on any validation or execution failure. Returns Ok on successful no-op if already provisioned.

CLI

  • struct Cli
    • Mirrors kernel cmdline semantics with flags:
      • --config PATH
      • --log-level LEVEL
      • --log-to-file
      • --fstab
      • --show
      • --report PATH
      • --apply
      • --force (present, returns unimplemented error)
  • fn from_args() -> Cli
    • Parses argv without side effects; suitable for initramfs.

Logging

Configuration types

  • struct Config
    • The validated configuration used by the orchestrator, containing logging, device selection rules, topology, partitioning, filesystem options, mount scheme, and report path.
  • enum Topology
    • Values: btrfs_single, bcachefs_single, dual_independent, bcachefs-2copy, ssd_hdd_bcachefs, btrfs_raid1 (opt-in).
  • struct DeviceSelection
    • Include and exclude regex patterns, minimum size, removable policy.
  • struct Partitioning
    • Alignment, emptiness requirement, bios_boot, esp, data, cache GPT names and sizes where applicable.
  • struct BtrfsOptions
    • Compression string and raid profile (none or raid1).
  • struct BcachefsOptions
    • Cache mode (promote or writeback), compression, checksum.
  • struct VfatOptions
    • Reserved for ESP mkfs options; includes label ZOSBOOT.
  • struct FsOptions
    • Aggregates BtrfsOptions, BcachefsOptions, VfatOptions and shared defaults such as ZOSDATA label.
  • enum MountSchemeKind
    • Values: per_uuid, custom (future).
  • struct MountScheme
    • Base directory (/var/cache), scheme kind, fstab enabled flag.
  • struct ReportOptions
    • Output path (/run/zosstorage/state.json).

Configuration IO

  • fn load_and_merge(cli: &Cli) -> Result
    • Loads built-in defaults, optionally merges on-disk config, overlays CLI flags, and finally overlays kernel cmdline via zosstorage.config=. Validates the YAML against types and constraints.
  • fn validate(cfg: &Config) -> Result<()>
    • Ensures structural and semantic validity (e.g., disk selection rules not empty, sizes non-zero, supported topology combinations).

Device discovery

Partitioning

Filesystems

Mounting

Reporting

Idempotency

Utilities

Behavioral notes and contracts

  • Safety and idempotency:
    • Partitioning strictly aborts on any pre-existing partition or filesystem signature when require_empty_disks is true.
    • Filesystem creation never proceeds if partitioning validation fails.
    • The orchestrator treats any partial progress as failure; state report is only written on success.
  • Labels and GPT names:
    • ESP partitions are labeled ZOSBOOT (filesystem) and named zosboot (GPT).
    • Data filesystems use label ZOSDATA regardless of backend kind.
    • Cache partitions in bcachefs topology use GPT name zoscache.
  • Topology-specific behavior:
    • btrfs_single: one data filesystem (btrfs) on the sole disk.
    • bcachefs_single: one data filesystem (bcachefs) on the sole disk.
    • dual_independent: independent btrfs filesystems on each eligible disk (one or more).
    • bcachefs-2copy: multi-device bcachefs across two or more data partitions with --replicas=2 (data and metadata).
    • ssd_hdd_bcachefs: bcachefs spanning SSD (cache/promote) and HDD (backing), labeled ZOSDATA.
    • btrfs_raid1: only when explicitly requested; otherwise default to independent btrfs.
  • UEFI vs BIOS: when running under UEFI (/sys/firmware/efi present), the BIOS boot partition is suppressed.

Module dependency overview

flowchart LR
  cli --> config
  config --> orchestrator
  logging --> orchestrator
  orchestrator --> idempotency
  orchestrator --> device
  device --> partition
  partition --> fs
  fs --> mount
  mount --> report
  orchestrator --> report
  util --> partition
  util --> fs
  util --> mount
  util --> idempotency

Status

  • This API surface is ready for code-mode skeleton implementation with todo placeholders in function bodies and comprehensive doc comments.