---
name: barn
description: "Barn VMs: use Barn (github.com/pgsty/barn, docs at barn.pgsty.com) to run local virtual machines on macOS or Linux: fixed-IP multi-node Linux labs described by one Ansible/Pigsty-compatible inventory (QEMU), and native macOS VMs on Apple Silicon. Use when preparing Linux or macOS VMs for development, testing, CI, or reproducing a bug; bringing up Pigsty sandbox nodes; replacing Vagrant with Barn; testing across distributions or architectures; running commands, moving files, or reaching services in guests; and troubleshooting, changing, or cleaning up VMs. Not for developing Barn itself (follow its repository's AGENTS.md)."
---

# Barn VMs

> Public edition of the Barn skill. URL: <https://barn.pgsty.com/skill.md> · 中文：<https://barn.pgsty.com/skill.zh.md>. Save it as `SKILL.md` in a skill directory named `barn/`, or hand the URL to any agent. This snapshot matches Barn 1.4.0 (image catalog `2026112601`), dated 2026-11-27.

Barn (Bootstrap And Run Nodes) is a CLI that creates virtual machines on the local computer. It has two independent workflows:

- **Linux labs**: on a macOS (HVF) or Linux (KVM) host, QEMU boots fixed-IP nodes from one Ansible YAML inventory, with SSH, passwordless sudo, and a test data disk. The same inventory works with Pigsty or `ansible-playbook -i`.
- **macOS VMs**: `barn mac` runs named macOS 27 guests on Apple Silicon through Apple Virtualization, with a desktop window, SSH, shared folders, and clipboard sync.

Barn prepares the machines. What runs on them, such as Pigsty, a database, or the user's application, belongs to the tools above it.

## How to use this file

The Barn documentation and the local `barn <command> --help` are authoritative for commands, options, and behavior. This file gives you a mental model, a documentation index, scenario references, and a few hard boundaries. For the details, read the page it points to and apply your own judgment to the user's project.

Requests are often a sentence or two: "spin up an Ubuntu box to try this", "give Pigsty a four-node sandbox", "replace this project's Vagrant setup with Barn", "start a clean macOS VM for builds". Fill the gaps with sensible defaults and say what you chose: the default Ubuntu 24.04 image, the host's architecture, and Barn's default sizes. Ask only when the answer changes the outcome, for example whether you would touch the user's existing lab, delete anything, change the host, or download tens of GiB.

## Mental model

- **One Linux deployment per user.** State lives in `$BARN_HOME` (default `~/.barn`). Changing directories does not create another lab; `barn up` in any directory acts on the same deployment.
- **The inventory is desired state.** It is static Ansible YAML: IPs are host keys, `vm_*` variables describe hardware, and variables Barn does not read (such as Pigsty's `pg_*`) are left for the layer above. `-f` wins; otherwise Barn looks for `barn.yml`, `barn.yaml`, `pigsty.yml`, and `pigsty.yaml` in the working directory, in that order. `plan` shows the difference and `up` converges; CPU and memory change with `resize`, while image, disk, and share changes need `recreate`.
- **Deletion is always explicit.** Removing a host from YAML does not delete its VM; `plan` marks it `missing`. Only `destroy` and `purge` delete.
- **One /24 private network.** `.1` is the host, `.2`–`.8` are reserved, and nodes start at `.9`. Node names use lowercase letters, digits, and hyphens only.
- **Defaults.** Image `u24` (Ubuntu 24.04); per node 1 vCPU, 2 GiB of memory, a 64 GiB root disk, and a 128 GiB test disk at `/data`; login user `dba`. All disks are thin-provisioned.
- **macOS is a separate world.** `barn mac` manages named macOS VMs with state under `~/.barn/mac` and never reads the inventory. Linux `status`, `destroy`, and `purge` do not touch Mac VMs, and Mac commands do not touch the Linux lab.

## Sources of truth

- Documentation: <https://barn.pgsty.com/docs/> (Chinese under `/zh/docs/`). Append `index.md` to any page URL to read its Markdown; <https://barn.pgsty.com/llms.txt> (Chinese: `/zh/llms.txt`) lists every page.
- Installed version: `barn version` and `barn <command> --help`. The documentation describes the current stable release and the local binary may be older; trust the local help for options, and suggest an upgrade when a feature is missing.
- Actual state: `barn status`, `barn plan`, `barn mac ls`. Add `--json` for structured results; the CLI contracts define the exit codes.
- Source and releases: <https://github.com/pgsty/barn>; release notes at <https://barn.pgsty.com/blog/release/>.
- The target project's own `AGENTS.md`, `CLAUDE.md`, `README`, and `Makefile` are binding for that project.

Documentation map (paths relative to `https://barn.pgsty.com/docs/`):

| Page | Covers |
|---|---|
| `start/installation/` | Installation, host requirements, upgrades |
| `start/tutorial/` | Linux quick start, from one node to several |
| `start/macos/` | macOS VMs: create, desktop, SSH, shared folders, cleanup |
| `start/operations/` | Daily operations: inspect, start and stop, change, concurrency, destroy |
| `start/storage/` | Data disks and retention, file transfer, reaching guest services, shared folders |
| `start/images/` | Choosing images, the China mirror, self-hosted repositories, importing qcow2 |
| `start/automation/` | Unattended runs, `--json` and exit codes, `provision` scripts, pairing with Pigsty |
| `start/troubleshooting/` | Runbooks for host, network, image, drift, interruption, and SSH problems |
| `start/uninstall/` | Layered cleanup and uninstall |
| `reference/configuration/` | Inventory fields, defaults, resource scaling, data disks, shares, names and addresses, drift |
| `reference/commands/` | Every command's options, defaults, and examples |
| `reference/cli/` | Structured output, exit codes, interrupted-run recovery, environment variables |
| `reference/images/` | Image aliases, catalog signatures, cache and pruning |
| `reference/mac/` | `barn mac` rules, JSON results, failure reasons, file layout |
| `about/status/` | Platforms and limits |
| `about/security/` | Privileges, credentials, and trust boundaries |

Design decisions and acceptance records live at <https://barn.pgsty.com/blog/design/>. Look there to learn why a behavior works the way it does or whether a capability exists, and keep released behavior, unreleased work, and proposals apart.

## Before you act

- **Look first.** Run `barn version` and `barn status`, plus `barn mac ls` for macOS work. An existing Linux lab is probably in use; running `up` with a different inventory changes that lab. Explain the effect, then decide whether to reuse it, adjust it, or destroy it once the user agrees.
- **Your shell usually has no terminal.** A first `up` does not prompt; it only reports what is missing. Host preparation needs `barn setup --yes`, which installs packages, creates the private network, and calls sudo. `destroy` and `recreate` refuse to run without `--yes`, and `purge` never asks at all. Get the user's agreement before changing the host, deleting VMs, or downloading tens of GiB; when sudo needs a password, hand the command to the user to run in their own terminal.
- **Explain slow steps.** The first Linux image download and the first macOS base (about 24 GiB to download plus about 20 minutes to install) take time. Tell the user what you are waiting for, run it in the background, and do not retry in a loop. In mainland China, add `--mirror` to the first `barn up` to use the China image repository.
- **Keep files where they belong.** Inventories and scripts go in the directory or project the user named; VM disks and image caches stay in `~/.barn` and never go into a repository.

## Scenarios

Find the closest scenario, read the pages it points to, then adapt to what the user actually needs. The commands below are starting points, not a script.

### One Ubuntu 24.04 machine

The most common request: a clean Linux machine for trying commands, running tests, or reproducing a problem.

```bash
barn init                  # write a one-node Ubuntu 24.04 barn.yml here
barn setup --dry-run       # on first use, see what the host needs
barn up                    # create the VM and wait for SSH
barn exec meta -- uname -a
```

`barn ssh` logs in as `dba`, and `up` installs SSH aliases so plain `ssh meta` works. See `start/tutorial/`.

### A multi-node fixed-IP lab

Cluster software such as etcd, Patroni, Kafka, or Kubernetes needs several machines that can reach each other. `barn init dual|trio|full` writes a 2-, 3-, or 4-node template; `-I` selects the image and `-c` the subnet. Nodes share one /24, and the control node (`admin_ip`) already has SSH access to its peers. Before booting a large topology, multiply the node count by the per-node size and check the host's resources. See `start/tutorial/#scale` and `reference/configuration/`.

### A Pigsty sandbox

The `pigsty.yml` that Pigsty's `./configure -c <template>` writes is an inventory Barn reads directly; templates live under `conf/` in the Pigsty repository (`meta`, `dual`, `trio`, `full`, `simu`, and more). In the Pigsty directory:

```bash
./configure -c full
barn plan -f pigsty.yml
barn up -f pigsty.yml
```

Once the machines are ready, log in to the control node and install Pigsty following its documentation: [installation](https://pigsty.io/docs/deploy/install/) and [sandbox](https://pigsty.io/docs/deploy/sandbox/). As of this snapshot, Pigsty `Makefile` targets such as `make full` still call Vagrant, so check whether the repository already has a Barn entry point.

A few common traps: pass `-f` explicitly so a `barn.yml` in the same directory does not take precedence; Barn's default sizes are smaller than the old Vagrant templates, so raise `vm_cpu`, `vm_mem`, or `vm_scale` for heavy workloads; L2 VIPs in `.2`–`.8` collide with Barn's reserved range; and a `nodename` containing dots is invalid, so use a short name and put the domain in `vm_alias`. See `start/automation/#pair-the-lab-with-pigsty` and the design article [Replacing Vagrant with Barn in Pigsty](https://barn.pgsty.com/blog/design/pigsty-barn-migration/).

### Replacing Vagrant

Read what the project's `Vagrantfile` and surrounding scripts actually rely on before mapping them, rather than translating line by line:

| Vagrant | Barn |
|---|---|
| Multi-machine definitions, `private_network` IPs, CPU and memory | Host IPs in the inventory, `vm_cpu`, `vm_mem`, `vm_scale` |
| box | `vm_image` (alias, `:channel`, or `@version`); `barn image import` for your own images |
| `up`, `halt`, `reload`, `destroy` | `up`, `stop`, `reload`, `destroy` |
| `ssh`, `ssh-config` | `ssh`, `exec`, `ssh-config` |
| Shell or Ansible provisioner | `barn provision --script`, or `ansible-playbook -i` with the same inventory from the control node or the host |
| Synced folder | `vm_shares` (9p), or `scp` and `rsync` |
| Forwarded port | The fixed IP directly, or `ssh -L` |

Barn has no counterpart for suspend and resume, snapshots, Windows hosts, forced power-off, or several Linux environments running side by side. Tell the user plainly when the project depends on one of these, and offer an alternative or keep the old path instead of pretending the two are equivalent. The design article [What Barn Can Learn from Vagrant](https://barn.pgsty.com/blog/design/vagrant-workflow-boundary/) lists existing capabilities and gaps.

### Distribution and architecture matrices

Available families: Ubuntu 22.04/24.04/26.04 (`u22`/`u24`/`u26`), Debian 12/13 (`d12`/`d13`), Rocky Linux 8/9/10 (`el8`/`el9`/`el10`), and CentOS 7 (`el7`, amd64 Linux hosts only). `barn image list` shows the exact versions in the local catalog.

Nodes in one deployment can each run a different distribution by setting their own `vm_image`. The guest architecture, however, is shared by the whole deployment, so an amd64 and arm64 matrix runs as successive rounds. Foreign-architecture guests run under QEMU TCG emulation: they work, slowly, and suit compatibility testing. `-I` and `-A` override the image and architecture for one invocation without editing the inventory. When results must be reproducible, pin an exact version (`u24@<version>`). Short aliases select each family's newest release: `ubuntu` and `u` mean `u26`, not the default `u24`. See `start/images/` and `reference/images/`.

### Scripts and CI

For unattended runs, review the plan with `validate`, `plan`, and `setup --dry-run`, then run `setup --yes` and `up --json`. `up` exits 0 once management SSH is ready, even when a data disk, the private network, or a share reported a limitation, so check each node's `warnings` and `repairs` in the JSON. To run a script on several guests, use `barn provision --script` and make the script safe to re-run. See `start/automation/` and `reference/cli/#exit-codes`.

### macOS VMs

Requires Apple Silicon, macOS 27 or later, a logged-in desktop session, and the `Barn.app` that ships with Homebrew and the Apple Silicon release archive; no sudo.

```bash
barn mac doctor
barn mac up dev --cpu 8 --memory 16G --share ~/src
barn mac exec dev -- sw_vers
barn mac open dev          # when the user needs the desktop
```

The first run downloads Apple's restore image (about 24 GiB) and installs a base, so reserve about 65 GiB of disk. Without a terminal the download needs `--yes`, so ask the user first. Later machines clone the base in seconds. Apple allows at most two running macOS VMs per Mac, including those started by other tools. See `start/macos/` and `reference/mac/`.

### Moving files and reaching services

Once `up` has installed the SSH aliases, `ssh`, `scp`, `rsync`, and IDE remote development reach nodes by name. The host can connect directly to services that listen on a node's fixed IP; forward services bound to `127.0.0.1` with `ssh -L`. For browsers, `psql`, and other applications to resolve node names, use `barn hosts install`, which edits `/etc/hosts` and needs sudo. See `start/storage/`.

## Change, troubleshoot, and clean up

- After editing the inventory, run `plan` first: `create` means `up`; `resize` means `resize <node>`, which keeps disks and identity; `recreate` means `recreate <node>`, which replaces the root disk and non-persistent data disks; `missing` means restore the entry or run `destroy <node>` explicitly.
- `barn up` is safe to repeat: healthy nodes keep running and unfinished steps retry. When something fails, check `barn doctor` and `barn logs <node>`, then follow `start/troubleshooting/`.
- Data disks are disposable test storage. `recreate` and `destroy` delete non-persistent data disks, and when a filesystem check finds corruption, `up` resets the disk, persistent ones included. Keep important data on the host or in version control.
- `stop` and `start` keep disks. `destroy` deletes VMs but keeps persistent data disks, image caches, and the private network. `purge` deletes the whole Linux deployment, persistent data disks included, without asking. `barn mac destroy` deletes the named macOS VM. All of this is the user's data: ask before deleting.

## Report back

State which nodes you created (names, IPs, image versions, sizes), how to connect, any warnings `up` reported, what changed on the host (packages, the private network, `~/.ssh/config`, `/etc/hosts`), and what is still running, with how to stop or delete it. Say plainly what you did not do or did not verify.
