# Preparation and Utilities

> Prepare the host with barn setup, write and validate inventories, diagnose the host, and print the version or completion scripts.

---

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

---

`barn setup` prepares a host to run Linux VMs: it installs QEMU and UEFI firmware, creates the fixed-IP network the nodes share, and writes an inventory if you have none. This page also covers `init`, `validate`, `doctor`, `version`, and `completion`. Every command accepts the [global options](/v1.5/docs/reference/commands/#global-options); **Available in** shows availability from the 1.1.0 baseline onward ([version baseline](/v1.5/docs/reference/commands/#versions)).

## `barn setup` {#barn-setup}

```text
barn setup [meta|dual|trio|full] [flags]
```

On a terminal, the first `barn up` runs this preparation itself and asks once. Run `barn setup` directly to preview the changes, to prepare a host from a script or CI job, or to get ready before the first `up`. It creates no VMs and downloads no base images; [`barn up`](../lifecycle/#barn-up) does that.

```bash
barn setup --dry-run              # print the plan, change nothing
barn setup                        # print the plan, ask, then apply
barn setup full -I el9 --yes      # generate a four-node EL9 lab without asking
barn setup -f pigsty.yml --yes    # prepare the host for an existing inventory
barn up
```

Aliases: `s`.

### What setup changes {#setup-steps}

Setup skips steps that are already satisfied and prints the whole plan before the first change:

| Step | What it does | Root |
|---|---|---|
| `dependencies` | Installs missing QEMU, `qemu-img`, UEFI firmware, and OpenSSH: `brew install qemu` on macOS (Homebrew required), `apt-get` or `dnf` on Linux | Linux only |
| `capabilities` | Runs the host checks of [`barn doctor`](#barn-doctor) | No |
| `network` | Installs or repairs the [fixed-IP network](../network/): `socket_vmnet` as a root service under `/opt/barn` on macOS, the `barn0` bridge through NetworkManager or systemd-networkd on Linux | Yes |
| `verify` | Re-checks the installed network | No |
| `config` | Writes the generated inventory, or uses the existing one | No |
{.wide .expand}

The network step needs root because it changes host networking; in return, QEMU itself runs as you. `/etc/hosts` is not part of setup: the first [`barn hosts install`](../access/#barn-hosts-install) installs its root-owned helper, which is why setup reports the `hosts-helper` step as `on-demand`.

On macOS, `socket_vmnet` comes from the first of `BARN_VMNET_ARCHIVE`, Barn's cache, Homebrew, or a SHA-256-pinned download from the selected repository or `github.com/lima-vm`; `--mirror` and `--repo` change the download source. Behind a proxy, setup passes `HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`, and `NO_PROXY` (either case) through `sudo` to `apt-get` and `dnf`. On Linux, setup installs only the host-architecture emulator; for foreign-architecture guests, install the other `qemu-system-*` package and its UEFI firmware yourself.

### sudo and confirmation {#setup-privileges}

Run setup as your normal user. It asks for your password at most once, at the first root step, after printing why; Homebrew runs as you. On a terminal, setup shows the plan and asks `Continue with this setup? [Y/n]`, unless nothing needs to change.

- `--yes` skips that question and is required without a terminal, where setup otherwise exits `2`. It cannot answer `sudo`: without a terminal, `sudo` must work without a password, or setup exits `3` with reason `sudo_unavailable` ([troubleshooting](../../../start/troubleshooting/#setup-needs-sudo)).
- `--dry-run` prints the plan and changes nothing; its `next:` line is the command that applies it. It cannot be combined with `--yes`.
- `barn --json setup` reports `ready`, `blocked`, `steps[]`, `dependencies` (with `missing[]` and the exact package-manager `commands[]`), `network`, and `next`/`next_argv`.

### Which inventory it prepares {#setup-inventory}

With no template and no `-f`, setup reuses the first [discovered](../../configuration/#discovery) inventory or writes the `meta` template to `./barn.yml`. A template (`meta`, `dual`, `trio`, or `full`, with 1 to 4 nodes) generates that lab; `-f` prepares an existing file and cannot be combined with a template. `-c` and the specification flags shape generated templates only: setup rejects them for an existing inventory and ignores the matching `BARN_*` variables when it reuses one.

If the default subnet is taken and the inventory is a fresh, unedited template, setup picks a free private `/24` and keeps the original as `barn.yml.before-network-change`. An inventory you wrote or edited keeps its subnet, and a conflict stops setup.

### Options {#setup-options}

| Option | Meaning / default | Available in |
|---|---|---|
| `-A, --arch string` | override deployment vm_arch: native, amd64, or arm64; environment overrides require 1.2.0 | 1.1.0 |
| `-c, --cidr string` | generated template network as a canonical RFC1918 IPv4 /24 | 1.1.0 |
| `-C, --cpu string` | Base vCPUs before scale, integer 1–256; effective CPU count is also limited to 256; overrides BARN_CPU | 1.2.0 |
| `-D, --disk string` | Absolute root capacity, at least the base virtual size; bare integer means GiB; overrides BARN_DISK | 1.2.0 |
| `-d, --dry-run` | show the resolved setup plan without changing anything | 1.1.0 |
| `-f, --file string` | Inventory to prepare; mutually exclusive with a template argument | 1.1.0 |
| `-I, --image string` | override vm_image/vm_version for selected nodes; environment overrides require 1.2.0 | 1.1.0 |
| `-M, --mem string` | Base memory before scale, minimum 512 MiB; bare integer means MiB; overrides BARN_MEM | 1.2.0 |
| `--mirror` | use the China official repository at repo.pigsty.cc; overridden by `--repo` | 1.1.0 |
| `-m, --mode string` | macOS fixed-IP network backend: host or shared (default "host") | 1.1.0 |
| `-r, --repo string` | image repository URL or local directory, also used for socket_vmnet; overrides `--mirror` and $BARN_REPO | 1.1.0 |
| `-S, --scale string` | Integer 1–256 multiplying base CPU and memory only; disk does not scale; overrides BARN_SCALE | 1.2.0 |
| `-y, --yes` | accept the one-time setup plan (required without a terminal) | 1.1.0 |
{.wide .expand}

## `barn init` {#barn-init}

```text
barn init [meta|dual|trio|full] [flags]
```

Write an editable, Pigsty-compatible inventory without touching the host. The default is the one-node `meta` template in `./barn.yml`, with the control node at `.10`; `dual`, `trio`, and `full` add `.11`, `.12`, and `.13`. Existing files are kept unless you pass `--force`, and `-o -` prints to stdout.

In 1.5.0, all four templates write `vm_image: d13` and `vm_arch: native` by default. On Barn 1.4.0, pass `-I d13` when generating a template to select Debian 13.

Generated nodes get 1 vCPU, 2 GiB of memory, a 64 GiB root disk, and a 128 GiB `/data` disk, written out as all six specification fields including `vm_scale` ([specification overrides](../../configuration/#specification-overrides)). Barn 1.1.0 templates used 2 vCPUs and 4 GiB ([upgrade notes](../../../start/installation/#upgrade-barn)).

| Option | Meaning / default | Available in |
|---|---|---|
| `-A, --arch string` | override deployment vm_arch: native, amd64, or arm64; environment overrides require 1.2.0 | 1.1.0 |
| `-c, --cidr string` | rebase the generated template to a canonical RFC1918 IPv4 /24 | 1.1.0 |
| `-C, --cpu string` | Base vCPUs before scale, integer 1–256; effective CPU count is also limited to 256; overrides BARN_CPU | 1.2.0 |
| `-D, --disk string` | Absolute root capacity, at least the base virtual size; bare integer means GiB; overrides BARN_DISK | 1.2.0 |
| `--force` | Overwrite the destination inventory; default preserves existing files | 1.1.0 |
| `-I, --image string` | override vm_image/vm_version for selected nodes; environment overrides require 1.2.0 | 1.1.0 |
| `-M, --mem string` | Base memory before scale, minimum 512 MiB; bare integer means MiB; overrides BARN_MEM | 1.2.0 |
| `-o, --output string` | write to this path instead of ./barn.yml; '-' writes to stdout | 1.1.0 |
| `-S, --scale string` | Integer 1–256 multiplying base CPU and memory only; disk does not scale; overrides BARN_SCALE | 1.2.0 |
{.wide .expand}

```bash
barn init -I d13
barn init full -I d13 -A amd64 -o lab.yml
barn init dual -I d13 -c 10.20.30.0/24 -o -
```

## `barn validate` {#barn-validate}

```text
barn validate [flags]
```

Check an inventory before `up`. `validate` checks every field Barn reads and prints the resolved nodes, or the full resolved specification with `--json`. It requires an inventory file, either `-f` or the first of `barn.yml`, `barn.yaml`, `pigsty.yml`, and `pigsty.yaml` in the current directory. In 1.5.0, a readable deployment supplies the same image default as `plan`. It takes no node names, and overrides apply to this check only. To compare an inventory with the running deployment, use [`barn plan`](../lifecycle/#barn-plan).

Aliases: `v`.

| Option | Meaning / default | Available in |
|---|---|---|
| `-A, --arch string` | override deployment vm_arch: native, amd64, or arm64; environment overrides require 1.2.0 | 1.1.0 |
| `-C, --cpu string` | Base vCPUs before scale, integer 1–256; effective CPU count is also limited to 256; overrides BARN_CPU | 1.2.0 |
| `-D, --disk string` | Absolute root capacity, at least the base virtual size; bare integer means GiB; overrides BARN_DISK | 1.2.0 |
| `-f, --file string` | Inventory to validate; otherwise use local discovery, with no applied-state fallback | 1.1.0 |
| `-I, --image string` | override vm_image/vm_version for selected nodes; environment overrides require 1.2.0 | 1.1.0 |
| `-M, --mem string` | Base memory before scale, minimum 512 MiB; bare integer means MiB; overrides BARN_MEM | 1.2.0 |
| `-r, --repo string` | image repository whose catalog checks the images; overrides $BARN_REPO | 1.1.0 |
| `-S, --scale string` | Integer 1–256 multiplying base CPU and memory only; disk does not scale; overrides BARN_SCALE | 1.2.0 |
{.wide .expand}

```bash
barn validate -f lab.yml
barn --json validate -f lab.yml -I d13 -A arm64
```

## `barn doctor` {#barn-doctor}

```text
barn doctor [flags]
```

Check whether this host can run Linux VMs: QEMU, native acceleration, firmware, OpenSSH, a short accelerated boot, and host networking. A missing compute capability exits `3` and prints a `fix:` line for each problem. A missing network is only a warning, because `setup` installs it. For network detail, run [`barn network status`](../network/#barn-network-status); for macOS guests, run `barn mac doctor`.

Aliases: `dt`. No options of its own.

```bash
barn doctor
barn --json doctor
barn doctor --verbose
```

## `barn version` {#barn-version}

```text
barn version [flags]
```

Print the version, source commit, build time, OS, and architecture to include in a bug report; `barn --version` prints the same line. `--json` returns `name`, `version`, `commit`, `built`, `os`, and `arch`. Source builds without a release stamp report `dev`.

Aliases: `ver`. No options of its own.

```bash
barn version
barn --json version
```

## `barn completion` {#barn-completion}

```text
barn completion [bash|zsh|fish|powershell] [flags]
```

Print one shell's completion script to stdout, then install it as shown below. It completes commands, flags, templates, image aliases, and, on a best-effort basis, node names. Regenerate it after upgrading Barn. Use text mode: with `--json` or `BARN_OUTPUT=json`, the script arrives wrapped in a `{shell, script}` object.

No options of its own.

```bash
source <(barn completion bash)
barn completion zsh > "${fpath[1]}/_barn"
barn completion fish > ~/.config/fish/completions/barn.fish
barn completion powershell
```

## Implementation {#implementation}

See `cmd/barn/command_setup.go` for the CLI and `internal/setup/` for host preparation. `cmd/barn/command_misc.go` and `cmd/barn/command_root.go` define the diagnostic and root commands.
