# Configuration

> The inventory file that defines a Linux lab — its shape, the variables Barn reads, their defaults, and how flags and environment variables override them.

---

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

---

A Linux lab is one YAML file that lists each node under its fixed IPv4 address. This page defines the file's shape, every variable Barn reads with its default, and how command-line flags and environment variables override them.
{#a-complete-inventory}

```yaml {title="barn.yml"}
all:
  vars:                                  # applies to every host below
    admin_ip: 10.10.10.10                # the control node
    vm_image: d13                        # Debian 13
  children:                              # ordinary Ansible groups; they may nest
    lab:
      vars:
        vm_mem: 4GiB                     # group default, overrides all.vars
      hosts:
        10.10.10.10: { nodename: meta }  # key = the node's address on the lab /24
        10.10.10.11: { nodename: node-1 }
        10.10.10.12: { nodename: node-2, vm_cpu: 2 }  # host value wins over groups
```

This is an ordinary Ansible inventory. Barn reads the `vm_*` variables and a few Pigsty fields and ignores everything else, so the same file can also drive Pigsty or Ansible: add `vm_*` variables to a Pigsty `pigsty.yml`, and Barn creates exactly the nodes its playbooks target.

Check the file and preview its effect before starting anything:

```bash
barn validate -f barn.yml
barn --json validate -f barn.yml
barn plan -f barn.yml
```

`validate` checks YAML syntax, field rules, and image references against the local catalog without starting VMs or downloading images. Its JSON output includes `valid`, `source`, `spec_hash`, and `resolved`; if the catalog cannot be read, it warns and leaves image selectors unresolved. To check against another repository's catalog, pass `--repo`, for example `--repo https://repo.pigsty.cc/barn` for the China mirror (`validate` has no `--mirror`). `up` later verifies what `validate` cannot: `local-*` image bytes, host capabilities, shared directories, networking, and guest SSH.

To start from a template, `barn init [meta|dual|trio|full]` writes a 1-, 2-, 3-, or 4-node inventory: `meta` at `.10`, then `node-1`, `node-2`, and `node-3` at `.11`–`.13`. Templates define VMs only; Pigsty service roles stay in your playbook. `--cidr` moves the template to another `/24` while keeping the host octets, and `barn init full -o -` prints it instead of writing a file.

## Discovery {#discovery}

Commands that need a desired state look for an inventory in this order and use the first one that exists:

1. The path given with `-f`.
2. `barn.yml`, `barn.yaml`, `pigsty.yml`, then `pigsty.yaml` in the current directory. All four use the same format. A parse or validation error in the first match stops the command; Barn does not try the next name.
3. The applied specification stored in `BARN_HOME` — only for `plan`, `up`, `reload`, `recreate`, and `resize`.

| Commands | Desired state comes from |
|---|---|
| `plan`, `up`, `reload`, `recreate`, `resize` | `-f`, then discovery, then the applied specification |
| `validate` | `-f`, then discovery; an error if neither exists, even when a deployment exists |
| `setup [template]` | `-f`, then discovery, otherwise a generated `meta`; a template argument and `-f` are mutually exclusive |
| `init [template]` | Writes a new inventory without reading any state; `--force` overwrites an existing file |
| `hosts install` | Uses applied node names by default. An explicit `-f` reads that inventory and its `vm_hosts` mappings |
| `start`, `stop`, `restart`, `status`, `destroy`, `purge` | Operate on the applied deployment in `BARN_HOME` |
{.wide .expand}

The inventory must resolve to a regular file of at most 4 MiB; symlinks are followed, and relative share paths resolve beside the real file.

### One Linux deployment per `BARN_HOME` {#one-deployment-per-barn-home}

Barn keeps exactly one Linux deployment per `BARN_HOME` (default `~/.barn`). The inventory is the desired state; `BARN_HOME` holds what is actually applied. Because a lab is defined by its fixed addresses rather than by the directory you work in, renaming or moving the inventory does not create a second deployment, and commands that use applied state (`status`, `ssh`, `stop`, `start`, `destroy`, …) work from any directory. Set `BARN_HOME` to an absolute path if you set it at all, so every working directory reaches the same deployment. The design rationale is in [Design](../../about/design/#one-linux-lab-per-user).

## What Barn reads {#what-barn-reads}

An inventory needs a top-level `all:` group. Barn walks its `hosts` and nested `children`, and reads only node IPv4 keys, `nodename`, `admin_ip`, `pg_cluster`, `pg_seq`, `node_admin_username`, `node_admin_uid`, and the [`vm_*` variables](#vm-variables). It parses static YAML: Ansible inventory plugins, Jinja expressions, `group_vars`, and `host_vars` are not evaluated.

- **Other fields**: Variables outside that list, such as `pg_role`, `pg_version`, `repo_*`, and `node_packages`, are ignored and never cause VM drift.
- **Inheritance**: Values inherit `all.vars` → nested `children.<group>.vars` → host variables, deepest wins. A host-level list such as `vm_disks` replaces the inherited list entirely. If two sibling groups at the same depth give one node different values, set the variable on the host.
- **Repeated hosts**: An IPv4 address listed in several groups is one node; its inherited variables merge.
- **Control node**: `admin_ip` in `all.vars` names the control node. If it is omitted or matches no managed node, Barn picks the first managed node in traversal order (each group's `hosts` before its `children`). Set `admin_ip` so reordering groups cannot move the control role.
- **Login account**: All nodes share one login user matching `[a-z_][a-z0-9_-]{0,31}`, default `dba`. With `dba`, an explicit `node_admin_uid` must be `88`; with a custom user, `node_admin_uid` only has to be an integer and the guest assigns the UID.
- **Skipped hosts**: `vm_skip: true` marks a physical or external host. It may use a hostname or IPv6 key and does not count toward the managed `/24`; at least one managed node must remain. Skipping an existing node shows it as `missing` in `plan` without destroying it.
- **Strict `vm_*` namespace**: Unknown `vm_*` keys (with a spelling suggestion), wrong types, Jinja templates, duplicate keys, and multi-document YAML fail with the line number and the rule violated. YAML anchors and `<<` merge keys work: explicit keys win, then the first mapping in a merge sequence.

## VM variables {#vm-variables}

| Variable | Default | Meaning |
|---|---|---|
| `vm_skip` | `false` | Skip virtualization for this physical or external host |
| `vm_image` | `d13` (new lab in 1.5.0) | Image family, `:channel` reference, or `image@version` selector; see [Image and architecture](#image-and-architecture) |
| `vm_version` | unset | Newest numeric version matching this prefix, such as `9` or `9.7` |
| `vm_arch` | `native` | Deployment-wide guest architecture; follows the host unless set to `amd64` or `arm64` |
| `vm_cpu` | `1` | Base vCPU count; the effective count after scaling must be `1`–`256` |
| `vm_mem` | `2048` | Base memory: bare integer in MiB, or a size string such as `8GiB` (minimum `512MiB`) |
| `vm_scale` | `1` | Positive integer multiplying CPU and memory only |
| `vm_hosts` | `[]` | Extra host `/etc/hosts` mappings, declared only in `all.vars` |
| `vm_disk` | `64` | Root disk capacity: bare integer in GiB, or a size string such as `64GiB` |
| `vm_disks` | `[{path: /data}]` | Extra data disks; the default is one 128 GiB non-persistent disk at `/data` |
| `vm_alias` | `[]` | Extra names for guest `/etc/hosts`, SSH config, and optionally host `/etc/hosts` |
| `vm_shares` | `[]` | QEMU 9p host-directory shares on Linux and macOS hosts |
{.wide .expand}

An empty host mapping (`10.10.10.10: {}`) defines a complete VM with these defaults.

Bare integers mean MiB for `vm_mem` and GiB for disks. Size strings take a positive integer and a case-insensitive unit: `8G`, `512m`, `4gib`, and `64GB` all work. `K/M/G/T` and `KiB/MiB/GiB/TiB` are binary; `KB/MB/GB/TB` are decimal. Fractions (`1.5GiB`) and unitless strings (`"8192"`) are invalid. `up` checks that the root disk is at least the base image's virtual size.

## Names and addresses {#names-and-addresses}

Each node's name is the first of: explicit `nodename`, `<pg_cluster>-<pg_seq>`, or `node-<last-octet>`. Names must be unique, 1–63 characters of lowercase letters, digits, and hyphens, and must not start or end with `-`. `vm_alias` adds lowercase DNS names (dots allowed) that must differ from every node name and other alias. Keep an explicit `nodename` on every host: otherwise renaming `pg_cluster` turns an existing node into a `missing` old node plus a new one.

Every node gets two NICs: one for internet access through QEMU's built-in NAT, and one on a fixed `/24` so nodes reach each other at the addresses the inventory names. All managed nodes must share one RFC1918 `/24`: `.1` is the host gateway, `.2`–`.8` are reserved, and `.9`–`.254` are available to nodes. Inside the guest, `ip -br addr` shows the inventory address on the fixed-IP NIC.

## Resource scaling {#resource-scaling}

`vm_scale` multiplies CPU and memory so you can size a whole lab up or down with one number. Barn resolves `vm_cpu`, `vm_mem`, and `vm_scale` independently through inheritance, then multiplies once:

```text
CPU    = resolved(vm_cpu, default=1) × resolved(vm_scale, default=1)
Memory = resolved(vm_mem, default=2048 MiB) × resolved(vm_scale, default=1)
```

| Settings | Effective CPU | Effective memory |
|---|---:|---:|
| None | 1 | 2 GiB |
| `vm_scale: 3` | 3 | 6 GiB |
| `vm_cpu: 2`, `vm_scale: 3` | 6 | 6 GiB |
| `vm_mem: 4GiB`, `vm_scale: 3` | 3 | 12 GiB |
| `vm_cpu: 2`, `vm_mem: 4GiB`, `vm_scale: 3` | 6 | 12 GiB |

Scales do not compound: a host's `vm_scale: 1` replaces a group's `vm_scale: 3`. Scale is an integer from 1 to 256, subject to the 256-vCPU limit and memory overflow checks; quoted numbers, fractions, booleans, null, zero, and negative values fail. Base memory must still be at least 512 MiB. Root disks, data disks, and shares do not scale. The keys are exactly `vm_cpu` and `vm_mem` (`vm_memory` is not an alias).

Preview CPU or memory changes with `barn plan -f pigsty.yml` and apply them with `barn resize -f pigsty.yml`. These defaults changed in 1.2.0; inventories that omit them read differently after an upgrade from 1.1.0 (see [Upgrade Barn](../../start/installation/#upgrade-barn)). Commands that use applied state without an inventory keep the recorded resources.

## Specification overrides {#specification-overrides}

Six fields can also be set per command, so scripts and one-off experiments can change a lab without editing the file (1.2.0+; 1.1.0 has only `--image` and `--arch`, without environment variables).

| Inventory field | CLI | Environment variable | Default |
|---|---|---|---|
| `vm_image` | `--image/-I` | `BARN_IMAGE` | `d13` (new lab in 1.5.0) |
| `vm_arch` | `--arch/-A` | `BARN_ARCH` | `native` |
| `vm_cpu` | `--cpu/-C` | `BARN_CPU` | 1 |
| `vm_mem` | `--mem/-M` | `BARN_MEM` | 2048 MiB |
| `vm_scale` | `--scale/-S` | `BARN_SCALE` | 1 |
| `vm_disk` | `--disk/-D` | `BARN_DISK` | 64 GiB |
{.wide .expand}

```bash
barn init full -C 2 -M 4GiB -S 2 -D 128GiB
barn plan -f barn.yml -S 3
BARN_SCALE=2 barn validate -f barn.yml -S 1
```

Each field resolves per node; the first source that sets it wins:

1. CLI flag
2. Non-empty environment variable
3. Host variable
4. Deepest child group
5. Parent groups
6. `all.vars`
7. Default

- CPU and memory are always bases, multiplied once by the final scale. `-S 3` replaces the scale rather than multiplying it; add `-S 1` to set exact CPU and memory.
- CLI and environment values override host values on the selected nodes. Put common defaults in `all.vars` when host exceptions should stay in effect.
- The inventory itself must still be valid: syntax, field types, and inheritance conflicts are checked as usual.
- Whitespace-only environment values count as unset, and an invalid environment value shadowed by a flag is ignored: `BARN_CPU=bad barn plan -C 2` uses 2. Suggested follow-up commands repeat effective environment overrides as explicit flags.

| Command | Overrides | Effect |
|---|---|---|
| `init`, template-generating `setup` | All six | Written into the new inventory, defaults included; CPU and memory as bases plus scale |
| `up` that generates an inventory interactively | All six | Saved in the generated inventory |
| `validate`, `plan`, `up`, `reload`, `recreate` | All six | Change this run's desired specification only; the YAML is not rewritten, so the next run against the file reports drift |
| `resize` | All six | Applies CPU and memory changes and grows the root disk; `-I`/`-A` are only checked for consistency |
| `setup` reusing an inventory | None | Flags are rejected and environment variables ignored |
| `start`, `stop`, `restart`, `status`, `destroy`, `purge` | None | Applied state is used; environment variables are ignored |
{.wide .expand}

Image commands and `barn mac` also ignore the six variables; `image info` and `image pull` take their own `-A`. With no inventory, only applied state, `--scale` or `BARN_SCALE` requires `-f`; `-C` and `-M` then set absolute values with an implicit scale of 1.

`--disk` sets the absolute root capacity, which must be at least the base image's virtual size: `plan` checks catalog metadata and creation checks the actual image again. It applies on creation, on explicit `recreate`, and to in-place growth with `resize -D` (`BARN_DISK` with `resize` only grows). Growth supports a last plain ext4/XFS root partition and never shrinks; see [root disk growth](../cli/#root-disk-growth). The default 128 GiB `/data` disk is separate.

## Image and architecture {#image-and-architecture}

`vm_image` chooses the guest OS per node; `vm_arch` chooses one CPU architecture for the whole deployment. New labs in 1.5.0 default to Debian 13 (`d13`) on the host architecture. On Barn 1.4.0, `barn init -I d13` writes the same image to a new inventory.

```bash
barn plan -I d13                 # Debian 13 using the environment, inventory, or host architecture
barn up -I d13 -A amd64          # amd64 Debian 13 for this invocation
barn plan -A native              # override the inventory architecture with the host's
barn init full -I d13 -A arm64   # save both choices in a new inventory
```

### Precedence {#image-and-architecture-precedence}

| Order | Image (per node) | Architecture (whole deployment) |
|---:|---|---|
| 1 | `--image/-I`, on the selected nodes | `--arch/-A` |
| 2 | `BARN_IMAGE` | `BARN_ARCH` |
| 3 | `vm_image` and `vm_version`: host, then groups, then `all.vars` | `vm_arch` |
| 4 | Saved deployment default, or the embedded default for a new lab | `native`: the host architecture |
{.wide .expand}

`-I` and `BARN_IMAGE` replace both `vm_image` and `vm_version`. A bare family name such as `d13` leaves the architecture alone; `-A native` overrides the environment and inventory and follows the host again.

| Architecture | Aliases | Meaning |
|---|---|---|
| `native` | — | Follow the current host; the default |
| `amd64` | `x86_64`, `x64` | x86-64 guest |
| `arm64` | `aarch64`, `a64` | ARM64 guest |
{.wide .expand}

### Architecture-suffixed aliases {#architecture-suffixed-aliases}

An image alias ending in `a` (`arm64`) or `x`/`i` (`amd64`), such as `el10a`, `d13x`, `d12i`, `ea`, `ua`, or `da`, carries its own architecture. For inventory commands:

- From `-I` or `BARN_IMAGE`, the suffix sets the deployment architecture, overriding `vm_arch`.
- In `vm_image`, the suffix takes precedence over `vm_arch: native`, the value generated templates write. A concrete `vm_arch` that names the other architecture fails validation, as do nodes whose suffixes disagree.
- `-A` and `BARN_ARCH` must agree with every suffix still in effect: naming the other architecture, or `native`, fails with exit `2`. A matching value such as `el10a -A arm64` is accepted.

### Image selection details {#image-selection-details}

- `vm_arch` belongs to the whole deployment: set it once in `all.vars`. Every managed node must resolve to the same architecture.
- The catalog must offer the requested architecture. A missing variant is an error; Barn never substitutes another architecture.
- When `vm_image` is omitted, existing labs use their saved default for existing and added nodes. A lone `vm_version` selects a version of that family. A new lab uses the binary's embedded default even when a custom repository declares another default.
- Built-in aliases are normalized while parsing the inventory: `ubuntu` → `u26`, `rocky` → `el10`, `debian` → `d13`. With a custom repository, write canonical family names in `vm_image` rather than reassigning built-in aliases.

`vm_version` keeps the family and the version prefix in separate fields:

```yaml {title="barn.yml"}
vm_image: el9
vm_version: "9.9"
```

An exact catalog version wins; otherwise Barn matches dot-separated numeric prefixes and picks the highest (`9.9` selects the latest `9.9.*` build, `9` the latest `9.x` release, and `9.10` sorts after `9.9`). Use `vm_version` only with a bare family name in `vm_image`, without `:channel` or `@version`.

### Keep or change a choice {#keep-or-change-image-and-architecture}

- Overrides on `up`, `plan`, `reload`, `recreate`, `resize`, and `validate` apply to that invocation only. To keep them, set `all.vars.vm_arch` and the node or group `vm_image`, and remove any superseded `vm_version`. `init` and template-generating `setup` write both into the new inventory; `setup` rejects `-I`/`-A` with an existing inventory.
- Without an inventory file, lifecycle commands apply overrides on top of the saved specification. `start` and `restart` always use applied state.
- Existing VMs are never converted. After you edit `vm_arch` or omit `-A`, `up` reports drift; switching the architecture requires `recreate` with all existing nodes selected. `resize` cannot change the image or architecture.
- A guest whose architecture differs from the host runs under QEMU TCG emulation. On Linux, `barn setup` installs only the native emulator: install the target `qemu-system-*` binary and UEFI firmware first. See [Runtime policy](../images/#runtime-policy).

## Data disks {#data-disks}

Each node gets one 128 GiB `/data` disk by default, separate from the root disk, so test data has its own device and, with `persistent: true`, can outlive `destroy` and `recreate`. `vm_disks` replaces that list; `vm_disks: []` creates a node with no data disks.

```yaml {title="barn.yml"}
vm_disks:
  - path: /data
    size: 128
    fs: auto
    persistent: false
```

| Field | Default | Meaning |
|---|---|---|
| `path` | required | Absolute mount path, normalized; spaces, Unicode, and shell punctuation are allowed. Simple paths keep their existing disk names; other paths get stable digest-based names. Identities must be unique per node; system directories and overlapping mounts are rejected |
| `size` | `128` | Capacity: GiB integer or size string |
| `fs` | `auto` | `auto`, `xfs`, or `ext4`. A blank `auto` disk gets XFS when the guest has `mkfs.xfs`, otherwise ext4; explicit `xfs` or `ext4` requires that tool and never falls back. Healthy existing filesystems are mounted as-is |
| `persistent` | `false` | When `true`, ordinary `destroy` and `recreate` keep the disk image on the host, and the recreated node reattaches it while its identity and definition stay compatible |
{.wide .expand}

Data disks are test storage: `up` can reset a corrupted ext4 or XFS filesystem, persistent disks included. [When Barn resets a data disk](../../start/storage/#when-barn-resets-a-data-disk) lists the exact rules.

## Shares {#shares}

`vm_shares` mounts host directories into Linux guests through QEMU Virtio-9p, for source code and build artifacts you edit on the host. It works on Linux and macOS hosts. For a native macOS guest, use the `--share` option of [`barn mac up`](../commands/mac/#barn-mac-up) or [`barn mac configure`](../commands/mac/#barn-mac-configure) to mount a directory through VirtioFS.

```yaml {title="barn.yml"}
vm_shares:
  - host: ./src
    guest: /src
    readonly: true
```

| Field | Default | Meaning |
|---|---|---|
| `host` | required | Absolute path, `~/`, a path relative to the inventory, or a directory symlink. Must exist, be accessible to the invoking user (ownership not required), and not overlap `BARN_HOME` |
| `guest` | required | Absolute mount point, normalized; spaces, Unicode, and shell punctuation are accepted, control characters are not |
| `readonly` | `true` | Set `false` for a writable share |
{.wide .expand}

- Host QEMU and the guest kernel must support 9p; backend device limits apply.
- Shares use the invoking user's real filesystem permissions. Host sources may overlap and may have several readers or writers; coordinating concurrent writes is up to you.
- Guest mount points must not overlap each other, data-disk mounts, system directories, or the login user's `.ssh` directory. Do not put PostgreSQL data directories on 9p.
- If the guest user cannot write a `readonly: false` share, Barn mounts it read-only with a warning and leaves host ownership alone; fix host permissions and re-run `barn up` to upgrade the mount.
- Changing `vm_shares` on an existing node requires `barn recreate`. A missing host directory during `up` or `start` fails only that node; `restart`, `reload`, and `recreate` verify share paths before stopping any running VM.

## Local service names {#local-hosts}

`all.vars.vm_hosts` (1.2.0+) publishes extra names, such as Pigsty service domains, in the host's `/etc/hosts` alongside node names and `vm_alias`:

```yaml {title="pigsty.yml"}
all:
  vars:
    vm_hosts:
      - {address: 10.10.10.10, names: [i.pigsty, m.pigsty]}
      - {address: 10.10.10.100, names: [pg-dns-service]}
  hosts:
    10.10.10.10: {nodename: meta}
```

```bash
barn hosts install -f pigsty.yml --json  # preview, including before VM creation
barn hosts install -f pigsty.yml --yes  # publish; sudo may ask for a password
barn hosts uninstall --yes
```

- Each entry needs an `address` and a non-empty `names` list. Addresses must be canonical IPv4 in the inventory's `/24`, within `.9`–`.254` (`.1`–`.8` are reserved for the host and DHCP).
- Identical name/address pairs deduplicate. Names compare case-insensitively; one name with two addresses fails validation, as does a conflicting name already present outside the Barn-owned block.
- The mappings touch only the host's `/etc/hosts`: they do not change the VM spec hash, configure guest DNS, set up a VIP, or cause drift. `up` never publishes them.
- `hosts install` without `-f` publishes only names and aliases from applied state and replaces the whole Barn block, dropping these service names; repeat the `-f` command to keep them.
- An unchanged block needs no sudo. Wildcards, SRV records, and live primary discovery need a real DNS service.

Pigsty's development `bin/sandbox-dns` extracts its portal, node, PostgreSQL, VIP, and MinIO names for this interface; see the [migration audit](/v1.5/blog/design/pigsty-barn-migration/).

## Drift {#drift}

Barn hashes each node's resolved definition and compares it with applied state, so editing the inventory never changes a VM until you run the command that applies that kind of change:

| Change | Apply with |
|---|---|
| New node | `barn up` |
| CPU or memory, including `vm_scale` | `barn resize <node>`; keeps identity and disks |
| Larger `vm_disk` | `barn resize <node>`, or `barn resize -D <size> <node>` without editing; grow-only |
| Image, data disks, shares, or other VM definition | `barn recreate <node>` |
| `vm_arch`, login user, or the `/24` | `barn recreate` of the whole deployment |
| Node removed or set to `vm_skip: true` | Shown as `missing` in `plan`; remove it with `barn destroy <node>` |
{.wide .expand}

`barn up` also starts stopped nodes and retries unfinished guest provisioning on running nodes without restarting QEMU. After `barn update`, a node whose definition is unchanged keeps its recorded base image. Creating or explicitly recreating a node resolves its selector against the active catalog.

Node selectors limit a command to the named nodes: `plan node-1`, `up node-1`, `reload node-1`, `recreate node-1`, and `resize node-1` keep the applied definitions of unselected peers, even if the inventory edits or omits them. The whole inventory must still parse, and conflicts with applied addresses, aliases, control-node roles, or deployment-wide settings are still rejected; images for uncreated, unselected nodes are not resolved.
