Configuration
17 minute read
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.
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:
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
Commands that need a desired state look for an inventory in this order and use the first one that exists:
- The path given with
-f. barn.yml,barn.yaml,pigsty.yml, thenpigsty.yamlin 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.- The applied specification stored in
BARN_HOME— only forplan,up,reload,recreate, andresize.
| 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 |
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
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.
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. 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_*, andnode_packages, are ignored and never cause VM drift. - Inheritance: Values inherit
all.vars→ nestedchildren.<group>.vars→ host variables, deepest wins. A host-level list such asvm_disksreplaces 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_ipinall.varsnames the control node. If it is omitted or matches no managed node, Barn picks the first managed node in traversal order (each group’shostsbefore itschildren). Setadmin_ipso reordering groups cannot move the control role. - Login account: All nodes share one login user matching
[a-z_][a-z0-9_-]{0,31}, defaultdba. Withdba, an explicitnode_admin_uidmust be88; with a custom user,node_admin_uidonly has to be an integer and the guest assigns the UID. - Skipped hosts:
vm_skip: truemarks 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 asmissinginplanwithout destroying it. - Strict
vm_*namespace: Unknownvm_*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
| 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 |
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 |
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
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
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:
| 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). Commands that use applied state without an inventory keep the recorded resources.
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 |
Each field resolves per node; the first source that sets it wins:
- CLI flag
- Non-empty environment variable
- Host variable
- Deepest child group
- Parent groups
all.vars- Default
- CPU and memory are always bases, multiplied once by the final scale.
-S 3replaces the scale rather than multiplying it; add-S 1to set exact CPU and memory. - CLI and environment values override host values on the selected nodes. Put common defaults in
all.varswhen 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 2uses 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 |
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. The default 128 GiB /data disk is separate.
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.
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 |
-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 |
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
-IorBARN_IMAGE, the suffix sets the deployment architecture, overridingvm_arch. - In
vm_image, the suffix takes precedence overvm_arch: native, the value generated templates write. A concretevm_archthat names the other architecture fails validation, as do nodes whose suffixes disagree. -AandBARN_ARCHmust agree with every suffix still in effect: naming the other architecture, ornative, fails with exit2. A matching value such asel10a -A arm64is accepted.
Image selection details
vm_archbelongs to the whole deployment: set it once inall.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_imageis omitted, existing labs use their saved default for existing and added nodes. A lonevm_versionselects 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 invm_imagerather than reassigning built-in aliases.
vm_version keeps the family and the version prefix in separate fields:
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
- Overrides on
up,plan,reload,recreate,resize, andvalidateapply to that invocation only. To keep them, setall.vars.vm_archand the node or groupvm_image, and remove any supersededvm_version.initand template-generatingsetupwrite both into the new inventory;setuprejects-I/-Awith an existing inventory. - Without an inventory file, lifecycle commands apply overrides on top of the saved specification.
startandrestartalways use applied state. - Existing VMs are never converted. After you edit
vm_archor omit-A,upreports drift; switching the architecture requiresrecreatewith all existing nodes selected.resizecannot change the image or architecture. - A guest whose architecture differs from the host runs under QEMU TCG emulation. On Linux,
barn setupinstalls only the native emulator: install the targetqemu-system-*binary and UEFI firmware first. See Runtime policy.
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.
| 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 |
Data disks are test storage: up can reset a corrupted ext4 or XFS filesystem, persistent disks included. When Barn resets a data disk lists the exact rules.
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 or barn mac configure to mount a directory through VirtioFS.
| 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 |
- 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
.sshdirectory. Do not put PostgreSQL data directories on 9p. - If the guest user cannot write a
readonly: falseshare, Barn mounts it read-only with a warning and leaves host ownership alone; fix host permissions and re-runbarn upto upgrade the mount. - Changing
vm_shareson an existing node requiresbarn recreate. A missing host directory duringuporstartfails only that node;restart,reload, andrecreateverify share paths before stopping any running VM.
Local service names
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:
- Each entry needs an
addressand a non-emptynameslist. Addresses must be canonical IPv4 in the inventory’s/24, within.9–.254(.1–.8are 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.upnever publishes them. hosts installwithout-fpublishes only names and aliases from applied state and replaces the whole Barn block, dropping these service names; repeat the-fcommand 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.
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> |
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.