# Self-hosted image repositories: one command to build, usable unsigned

> Let users host their own Barn images the way they host yum/apt repositories: infer the catalog from file names, declare the repository in the inventory, overlay it on the official catalog, and make signing optional.

---

LLMS index: [llms.txt](/v1.5/llms.txt)

---

Self-hosted image repositories are one of Barn's core workflows: a user puts customized `qcow2` images into a static directory, and teammates or other hosts reference them directly. This design reduces building a repository to one command, reduces using one to a single inventory field, and lets any repository the user explicitly selects work without a signature, like `gpgcheck=0` in yum or `[trusted=yes]` in apt.

**Status (2026-11-27)**: implemented in commit `57a46ed` on branch `claude/user-repo-design-review-1ae7c1`, passing `make check` and the end-to-end acceptance below; not yet merged into `main` and not released. [Current state before the change](#current-state) is based on Barn `1.3.0` and the `main` of that time (`39befbe`); [future work](#future-work) lists what this change leaves out.

**Barn 1.5.0, December 3, 2026.** Barn builds catalogs from filenames, reads local repositories immediately and refreshes the selected repository with `barn update --repo`. Custom HTTPS repositories may be unsigned. Plain HTTP requires an official ML-DSA-87 signature. Inventory `vm_repo`, catalog overlays and `repo init` belong to the experimental branch described below. [1.5 repository guide](/v1.5/docs/reference/repository-signing/#user-repository)

## Goals {#goals}

- **One command to build**: put `qcow2` files into `images/` and run `barn repo build`, without hand-writing `repo.yaml` or bumping `revision`.
- **One setting to use**: write the repository address once in `barn.yml`; lifecycle commands run against that inventory, and `barn image` and `barn update` run in its directory, use it automatically and fetch its catalog on first use.
- **Unsigned by default**: repositories the user explicitly selects are trusted by default, whether they are local directories, HTTPS, or plain HTTP; use HTTPS when tampering is a concern.
- **Coexist with official images**: one deployment can use a self-built `lab-pg` and the official `d13`.
- **Official chain unchanged**: both official repositories keep mandatory Minisign signatures, rollback protection, and the [release gate](/v1.5/blog/design/image-update-evidence/).

## Current state before the change {#current-state}

Barn already supported self-hosted repositories, and local-directory and HTTPS repositories could be unsigned; the workflow is in the [image guide](/v1.5/docs/start/images/#build-a-static-repository). The user hand-wrote a `repo.yaml` with `schema`, `revision`, `defaults`, and nested `variants`, placed images under canonical file names, ran `barn repo build`, activated the catalog with `barn update --repo <root>`, and from then on passed `--repo` or set `BARN_REPO` on every command; `barn.yml` had no field for it.

We reproduced the following behavior with a `barn` built from `39befbe`, a temporary `BARN_HOME`, and an empty 64 MiB `qcow2`:

| Scenario | Behavior before the change | Impact |
|---|---|---|
| Add a version without changing `revision` | `repo build` fails with `bump revision first` | Every change requires editing `repo.yaml` by hand |
| Rebuild from `revision: 1` on a new server | The client fails with `below accepted high-water mark 2` | Each host must recover with `barn image sync <catalog.json> --allow-downgrade` |
| Sign with the user's own Minisign key | Exit code `7`, `unknown key ID` | Only the official public keys built into the binary are trusted, so adding a signature makes the repository unusable |
| Plain HTTP repository, unsigned | Rejected with `manifest download failed status/size policy: 404` | The missing file is `catalog.json.minisig`, which the error does not say |
| First `--repo` use without `barn update` | Shows the built-in official catalog; `pull mypg` reports `unknown image`, and `pull u24` looks for official file names in the self-hosted repository and finds nothing | The catalog shown does not match the selected repository |
| After activating a self-hosted repository | Every official image disappears | The self-hosted catalog replaces the official one, so a deployment cannot mix them |
| Every command | Must pass `--repo` or set `BARN_REPO` | Forgetting it once falls back to the official catalog |
{.wide .expand}

The signature and rollback rules were designed for the official repositories. For a catalog the user hosts, the `revision` high-water mark blocks no attack: without a trusted signature, an attacker can forge any larger `revision`. The rule's only remaining effect is that a repository becomes unusable after moving to a new server.

## Trust follows whoever chose the source {#trust-model}

Barn sorts repositories by whether their catalog verifies with a built-in official key:

| Repository | Catalog signature | `revision` rollback protection | Image source |
|---|---|---|---|
| Either official repository, or a catalog at a user address that verifies with an official key (an internal mirror) | Required | On | Serves as the official source, as before |
| Any other user repository | Not required | Off; new bytes under the same `revision` and a lower `revision` are both accepted | Overlaid on the official catalog |
{.wide .expand}

Both kinds keep the same integrity checks: after download Barn verifies byte size, SHA-256, and `qemu-img` structure, then makes the file read-only. These checks catch corruption and incomplete downloads; without a signature they do not stop tampering, a risk the user accepts by choosing the repository, matching yum/apt with GPG checks disabled.

"Explicitly selected" means a repository chosen through `--repo`, `BARN_REPO`, or the `vm_repo` field below. Nothing on the official default path is relaxed; the official host names `repo.pigsty.io/barn` and `repo.pigsty.cc/barn` stay in the signed trust domain whether they are written with HTTP or HTTPS.

A signature is no longer a burden for a user repository:

- **No signature**: when `catalog.json.minisig` returns 404 or 410, or a local directory has no such file, the catalog is unsigned. An empty signature file or any other download error is still an error.
- **A signature from an unknown key**: when the catalog holds only self-built images, `barn update` prints a warning and uses it as unsigned. When it holds official image names, it claims to be the official catalog, as an internal mirror that has not caught up with a key rotation does, and Barn reports the `unknown key ID`.
- **Verifies with an official key**: the address is an internal mirror and its signed history keeps rollback protection; when the mirror starts serving an unsigned catalog, Barn refuses to activate it unless `barn image sync --allow-downgrade` explicitly accepts it.

## Building: `barn repo build` infers from file names {#build}

Without `repo.yaml`, the `images/` directory itself is the only source of truth, much like `createrepo` scanning RPMs:

```filetree {title="/srv/barn"}
barn/
├── catalog.json              # generated by barn repo build
└── images/
    ├── lab-pg-1-amd64.qcow2
    ├── lab-pg-1-arm64.qcow2
    └── lab-pg-2-arm64.qcow2
```

```bash
barn repo build /srv/barn
```

A `qcow2` file records no image name, version, or architecture, so the file name carries them and every other field takes a default:

| Field | Inference rule |
|---|---|
| Image name, version, architecture | Split `<name>-<version>-<arch>.qcow2` from the end: the last segment is the architecture (`amd64` or `arm64`), the second-to-last is the version (no `-`), and the rest is the image name. For example, `my-pg-17.2-arm64.qcow2` yields `my-pg`, `17.2`, and `arm64` |
| `stable` channel | Points to the image's highest version, compared as dot-separated integers (`17.10` above `17.2`), falling back to string order when a version is not numeric |
| Boot mode | `uefi` |
| Support status | `supported`. The repository owner is the publisher; `unknown` would print a risk warning on every `up` |
| Default image | The first image by name; ignored when overlaid on the official catalog |
| `revision` | Unchanged content keeps the existing value and the same `catalog.json` bytes; changed content takes `max(previous + 1, current UTC time as YYYYMMDDHHMMSS)` |
| Aliases, `source_user`, upstream URL | Not inferred; left empty |
{.wide .expand}

Files that do not follow the naming rule are left out; `build` and `scan` list them as `untracked` with the expected file name format. When an image name belongs to the official catalog (for example `d13-1-arm64.qcow2`), `build` refuses it and suggests renaming it to `my-d13-1-arm64.qcow2`, because clients would not overlay such a catalog. `build` still runs `qemu-img check` on every included image, so the host that builds the repository needs `qemu-img`.

Any static server can host the repository: a local directory, NFS, nginx, object storage, or a temporary `python3 -m http.server`.

### Explicit mode stays as it is {#explicit-mode}

When `repo.yaml` exists, it is the only source of truth and behaves as before: undeclared files are not included. The official pipeline depends on this, because candidate images are uploaded to `images/` before acceptance and enter the catalog afterwards. The one change is that `revision` may be omitted, in which case it is generated as in the table above; an explicit `revision` still has to be bumped by hand when content changes, and the official `repo.yaml` keeps an explicit value.

When you need aliases, BIOS boot, extra channels, or `deprecated` status, run `barn repo init /srv/barn`. It writes the current inference result to `repo.yaml`, refusing to overwrite an existing file; the generated file omits `revision` and starts with a short explanation. The catalog built from that `repo.yaml` is byte-identical to the inferred one. BIOS-only images require explicit mode.

## Using: `vm_repo` and first-use activation {#use}

```yaml {title="barn.yml"}
all:
  vars:
    vm_repo: http://10.0.0.5/barn
    vm_image: d13
  children:
    nodes:
      hosts:
        10.10.10.10: { nodename: meta }
        10.10.10.11: { nodename: node-1, vm_image: lab-pg }
```

- **Scope**: `vm_repo` is a deployment-level field that, like `vm_hosts`, may only appear in `all.vars`. It accepts an HTTP(S) URL or a local directory, with a relative path resolved from the inventory's directory. It only decides where images come from and is not part of the resolved deployment, so changing it never marks a node for recreation.
- **Precedence**: the user repository is the first of `--repo` > `BARN_REPO` > `vm_repo`, and `--mirror` only chooses the official source (global or China). Both can apply at once: `--mirror` with `vm_repo` takes official images from the China source and self-built images from `vm_repo`. A user repository that names an official address is a choice of official source instead, and `--mirror` still outranks an official address from `BARN_REPO` or `vm_repo`, keeping the earlier `--repo` > `--mirror` > `BARN_REPO` order.
- **Which commands read it**: lifecycle commands read `vm_repo` from the inventory they use. `barn update`, the `barn image` commands, and shell completion read only `all.vars` of the inventory in the working directory, without validating its hosts, so a host error in the inventory does not affect them.
- **First-use activation**: when no catalog for the repository exists locally, the first command that needs the image catalog fetches and activates it once, as if `barn update` had run. Barn never refreshes it automatically after that: only `barn update` fetches a new catalog, so everyday commands still work offline. A failed fetch fails the command, as yum does with an unreachable repository. `barn validate` and shell completion never access the network; for a user repository not yet fetched, `validate` only warns that images were not checked.

## Overlaying the official catalog {#overlay}

A self-hosted catalog is overlaid on the official catalog, and the command uses the merged result as one catalog:

- **Source**: each overlaid image remembers the user repository that serves it, and `barn image pull` and `up` download from there; official images still download from the official source, keeping the automatic fallback between the global and China sources.
- **No shared names**: user image names and aliases must not use official names; otherwise the whole overlay fails and lists the conflicts. Official names are the names and aliases the active or built-in official catalog resolves, the built-in shorthands that inventory parsing rewrites (such as `centos79` or `ubuntu`), and the reserved cache directory `manifests`. The cache path `images/<alias>/<alias>-<release>-<arch>.qcow2` carries no repository information, so allowing the same name would make two sources compete for one cache file; the rule also guarantees that an unsigned repository cannot change what `d13` means. A customized Debian 13 image can be named `my-d13`.
- **Default image**: the self-hosted catalog's `defaults` are ignored; when `vm_image` is omitted, the official default image still applies.
- **Listing and pruning**: with a user repository, `barn image list` adds a `user:` line with the user catalog's revision and address, and a `REPO` column (`official`, `user`, `local`); in JSON output, overlaid entries carry a `repository` field. `barn image prune` protects every image in both catalogs.

The first version supports one user repository. Whether an internal mirror and self-built images can be used together is listed under [future work](#future-work).

## Refresh and offline use {#refresh}

With a user repository, `barn update` refreshes the user repository first and the official catalog second, and reports one row for each:

```text
ITEM     STATE   MESSAGE
catalog  failed  manifest signature uses unknown key ID 4686B39A40F9B562: unknown catalog signing key
user     active  updated image catalog from revision 20261127041419 to 20261127041445
```

- **User repository**: a failed refresh of it ends the command with a runtime error.
- **Official source**: when the official source is unreachable or fails verification, the active official catalog stays usable, so the command ends as `partial` (exit code `5`) and keeps the user repository refresh. In JSON, the official part carries an `error` field and the user part sits in the `user` field.
- **`barn image update`**: after such a partial refresh it keeps updating images against both catalogs and also ends as `partial`; `--dry-run` writes no local state.
- **First fetch of a user catalog**: `update` reports `fetched image catalog revision N`.

`barn image reset` applies only to repositories with a built-in catalog to fall back to. For an unsigned user repository it refuses with a usage error that suggests `barn update --repo <repo>`, which always recovers because such a repository has no rollback record.

## Security trade-offs {#trade-offs}

- **Unsigned HTTP repositories**: anyone on the network path can replace both the catalog and the images. A malicious base image can read and write the host directories mounted into the guest through `vm_shares`, and can impersonate any node on the lab network. Use unsigned repositories only on the local host or a trusted internal network, and HTTPS across the public internet.
- **First-use activation**: this departs from the earlier rule that opening the image catalog never touches the network. Network access is limited to the one time no local catalog exists for the repository, in exchange for "set `vm_repo` once and it works"; the official catalog is still never refreshed implicitly.
- **No rollback protection for unsigned repositories**: this costs no security, because `revision` can already be forged without a signature.
- **No shared names**: an unsigned repository can add images but cannot change what an official image means; the cost is that image names must avoid official ones.

## Implementation and verification {#verification}

| Part | Implementation |
|---|---|
| Repository selection and the unsigned rule | `internal/image/repository.go` (`SelectRepositories`, `RepositoryAllowsUnsigned`) |
| Missing signatures, unknown keys, no rollback memory for unsigned repositories, refused resets | `internal/image/manifest_store.go` |
| Overlay and name rules | `internal/image/overlay.go`, `internal/image/catalog_session.go` |
| Two-catalog refresh and `partial` | `internal/image/service.go`, `internal/image/update.go` |
| Per-entry download source | `internal/image/store.go` |
| File-name inference, generated `revision`, `repo init` | `internal/image/repo.go`, `cmd/barn/command_repo.go` |
| `vm_repo` parsing and discovery | `internal/config/inventory.go`, `internal/config/discover.go` |
| Command wiring and output | `cmd/barn/main.go`, `cmd/barn/images.go`, `internal/private/manager.go` |
{.wide .expand}

New tests cover repository selection precedence, unsigned plain HTTP, the missing-signature error, unknown-key signatures being ignored or refused, an officially signed user repository acting as a mirror, the overlay and four kinds of name conflict, fetching a user catalog only once, `partial` when the official source is unreachable, per-entry downloads, file-name inference and generated revisions, `repo init` reproducing the inferred catalog byte for byte, and the scope, relative paths, and discovery of `vm_repo`. Tests that relied on replacement semantics now use mirror catalogs signed with test keys and keep covering the official path. Package-level `TestMain` functions clear any `BARN_REPO` inherited from the developer's shell so the tests stay offline.

On 2026-11-27, on macOS arm64, we ran an end-to-end acceptance with a `barn` built from that commit, a temporary `BARN_HOME`, and empty 64 MiB `qcow2` files:

| Scenario | Result |
|---|---|
| `repo build` without `repo.yaml`, then `verify` and a repeated `build` | Infers `lab-pg` with a generated timestamp `revision`; `verify` passes; the repeated build keeps the revision and bytes |
| `image list --repo` against an unsigned plain HTTP repository served by `python3 -m http.server` | Fetches the catalog on first use and lists `lab-pg` beside the official `u24`, with `REPO` showing `user` and `official` |
| `image pull lab-pg` and `image info u24` | `lab-pg` downloads from the user repository and verifies; `u24` resolves from the official catalog as usual |
| `vm_repo` in `barn.yml`, then `image list`, `validate`, and `plan` without `--repo` | All three use the repository; `plan` shows `u24@20261017.0.1/arm64, lab-pg@1/arm64` |
| `repo build` after adding a version, then `barn update` in the lab directory | The user row shows `updated`; the online official catalog was still signed with a retired key at the time, so the official row shows `failed` and the command ends as `partial` |
| Delete `catalog.json` and rebuild from a `repo.yaml` with `revision: 1` | The client accepts going back from a larger revision to `1` |
| `barn image reset` | Usage error suggesting `barn update --repo <repo>` |
| `u24-1-arm64.qcow2` in an inferred repository | `build` refuses and suggests `my-u24-1-arm64.qcow2` |
| `BARN_REPO=http://m0/barn` (an internal mirror signed with the old key) | Reports `unknown key ID 4686B39A40F9B562`, as before |
{.wide .expand}

## Compatibility {#compatibility}

- Repositories with a `repo.yaml` and an explicit `revision`, including the official ones, build the same output.
- Internal mirrors with official signatures keep their mirror behavior; `--mirror` does not override a user repository already activated as a mirror.
- Existing unsigned user repositories only get more permissive: they no longer hit high-water mark errors.
- **Breaking change**: with overlay instead of replacement, an unsigned user repository that uses official names (for example, a copy of the official catalog hosted without its signature) is refused and must rename those images or also publish the official signature. The release notes must state this.
- `barn image reset` no longer accepts unsigned user repositories.
- `barn update --repo <user>` now also refreshes the official catalog and ends as `partial`, not success, when the official source is unreachable.
- Older Barn releases validate `vm_*` fields strictly and reject `vm_repo` as an unknown field.
- `barn setup --repo`, which downloads `socket_vmnet`, is unchanged.

## Future work {#future-work}

This change leaves out:

- **User keys**: `vm_repo_key`, `--repo-key`, and `BARN_REPO_KEY`, requiring a user repository signature that verifies with that key and restoring rollback protection. Until then, teams that need tamper protection use HTTPS.
- **`barn repo add <root> <file> --name <name>`**: copy under the canonical name, then `build`. Today `cp` plus `barn repo build` is already one step.
- **Several user repositories**: a list in `vm_repo` would allow an internal official mirror together with a self-built repository; keeping the no-shared-names rule avoids defining a priority order.
- **`barn repo mirror`**: sync an internal mirror of the official repository in one step.
- **Exporting base images**: export an existing VM's root disk as a base image, completing the "customize a machine, publish it as a base image" path.
- **BIOS boot in inference mode**: whether a file-name suffix such as `-bios` should declare it, so BIOS images do not require explicit mode.
- **Catalog expiry**: an automatic refresh like yum's `metadata_expire`. This design leans against it to keep offline behavior deterministic.

For the repository format, see [repo.yaml and catalog.json](/v1.5/blog/design/repo-yaml-catalog-json/) and the [1.5 image reference](/v1.5/docs/reference/images/#static-repository-format).
