# Mac Runtime Reference

> What a barn mac machine owns, how it is networked, and the JSON results, failure reasons, and files that scripts rely on.

---

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

---

`barn mac` runs named macOS virtual machines on an Apple Silicon Mac, alongside the Linux lab but separate from it. This page defines what scripts and agents depend on: what each machine owns, how it is networked, what the commands return, how they fail, and where state lives.
{#commands}

Per-command syntax and options are in the [macOS Command Reference](../commands/mac/); the walkthrough is [macOS Virtual Machines](../../start/macos/).
{#flags-by-command}

```text
barn [--json|--yaml] [-v|--verbose] mac <command> [flags] [name...]
```

## Host and runner {#runner}

Barn drives Apple's Virtualization framework through `Barn.app`, the native macOS app bundle that ships with the Apple Silicon release and the Homebrew formula ([install](../../start/macos/#install)). The CLI and the app must come from the same build: before any VM operation the CLI checks that the app's runner speaks protocol `2`, and `barn mac doctor` also verifies the runner's local code signature.

- Run `barn mac` as the logged-in desktop user. Commands that talk to the runner reject `root` with `mac_root`, and a host that is not Apple Silicon on macOS 27 or later with `mac_host_unsupported`. Disk space and the two-VM limit are under [requirements](../../start/macos/#requirements).
- On Linux and Intel Macs, `barn --help` hides `mac`. Commands that only read local state, such as `barn mac image ls`, work without the runner.
- `barn mac` with no subcommand runs `barn mac ls`.
- The CLI looks for the runner relative to its own resolved binary path, covering the app bundle (`Barn.app/Contents/MacOS/barn-mac-runner`), the pkg installer, and Homebrew `libexec` layouts. `BARN_MAC_RUNNER` overrides the search with an absolute, relative, or `~/` path.

## Machines {#machines}

A machine is a named macOS VM with its own copy-on-write disk cloned from a shared base image, its own private network and fixed address, an administrator account with passwordless sudo, and its own SSH keys. Machines never join the Linux lab or read its inventory, and Linux `destroy` and `purge` leave them alone.

Names are 1–32 characters: lowercase letters, digits, and internal hyphens, starting with a letter (`mac1`, `dev`, `build-2`). `mac1` is the default name.

| Setting | New machine default | Change it with |
|---|---|---|
| CPUs | `4` | `configure --cpu`, while stopped |
| Memory | `8 GiB` | `configure --memory`, while stopped |
| Shared folders | none | `configure --share` / `--unshare`, while stopped |
| Subnet | first free `/24` | `configure --subnet`, while stopped |
| Clipboard sync | `on` | `configure --clipboard`, anytime; applies at the next start |
| Disk capacity | the prepared base's capacity, or `100 GiB` for the first base | fixed |
| Administrator account | your host macOS username, or `barn` if that is not a valid account name | fixed |
| macOS build | the default base | `recreate --update` (erases the disk) |
{.wide .expand}

Disk capacity and the administrator account are fixed for the machine's lifetime and survive `recreate`; create a new machine to change them. Value ranges and syntax are in [`barn mac up`](../commands/mac/#barn-mac-up) and [`barn mac configure`](../commands/mac/#barn-mac-configure).
{#flag-values}

- **Administrator account**: passwordless sudo, Ed25519 SSH key authentication, automatic desktop login, and Remote Login enabled. SSH password authentication is off. A random local password, needed at the lock screen and in GUI administrator prompts, is saved in the machine's `password` file (mode `0600`) and printed by `barn mac password`.
- **Hostnames**: the guest ComputerName is `<name>` and LocalHostName is `barn-<name>` (reachable as `barn-<name>.local`).
- **Shared folders**: one VirtioFS device mounts every share at `/Volumes/My Shared Files/<name>` in the guest. Each host source must resolve to an existing directory; symlinks are followed. Share names may contain spaces and Unicode and must be valid UTF-8 of 1–255 bytes, other than `.` or `..`, without `/` or ASCII control characters. `configure --share` with an existing name replaces that share's path and mode.
- **Clipboard sync**: plain text up to 1 MiB is synced over the machine's SSH connection whenever its desktop window gains or loses focus. Pasteboard items marked concealed are skipped.
- **Host limit**: Apple runs at most two macOS VMs at a time per Mac, counted across all apps and including an in-progress base installation ([requirements](../../start/macos/#requirements)). Starting a third fails with `mac_vm_limit` and names a machine to stop.

## Networks {#networks}

Each machine gets its own private `/24`, so its address stays the same across restarts and does not collide with another machine or the Linux lab. The machine's runner creates the network with Apple's [shared-mode `vmnet` API](https://developer.apple.com/documentation/vmnet/vmnet_network_configuration_create(_:_:)) when the machine starts and tears it down when it stops, without root or a background daemon.

| Item | Value |
|---|---|
| Subnet | First free `/24` from `10.10.20.0/24` through `10.10.59.0/24` that overlaps neither a host route nor another machine, or an explicit `--subnet` |
| Gateway | `.1`, on the Mac host |
| Guest IP | `.10`, a `vmnet` DHCP reservation for the machine's MAC address |
| Reachability | Host-to-guest routing and outbound NAT; no LAN bridging and no inbound port forwards |
{.wide .expand}

If a route added later, such as a VPN, overlaps a machine's saved subnet, `start` refuses with `mac_subnet_in_use`. Run `barn mac configure NAME --subnet auto` to move it to a free subnet. SSH trust survives the move because Barn pins the guest host key to `barn-mac-<instance_id>`, not to the IP address.

macOS Local Network privacy can block a third-party terminal or IDE from reaching the guest subnet, usually as `No route to host`. Allow that app under **System Settings → Privacy & Security → Local Network**. `barn mac ssh` and `exec` are unaffected: they run `/usr/bin/ssh`, and Barn's own SSH transport goes through `/usr/bin/nc`, both Apple system binaries.

## SSH and exec {#ssh-and-exec}

`barn mac ssh` and `barn mac exec` connect with the machine's own key and pinned host key and return the remote command's exit status. They differ in how they hand the command to the guest:

- `ssh` joins the arguments after `--` into one command string for the guest shell, as plain `ssh` does. Without a command it opens an interactive shell.
- `exec` passes the program and each argument separately, so spaces, quotes, and empty arguments arrive intact. It requires a command. Wrap pipelines, redirection, and compound commands in `sh -c '...'`. For compatibility `exec` also accepts a single command string as shell shorthand; new scripts should pass separate arguments or an explicit `sh -c`.

```bash
barn mac ssh dev
barn mac ssh dev -- 'sw_vers; id'
barn mac exec dev -- sw_vers
barn mac exec dev -- printf '<%s>\n' 'a b' ''
barn mac exec dev -- sh -c 'sw_vers; id'
barn --json mac exec dev -- sh -c 'echo out; exit 3'
```

Argument rules for both commands:

- A remote command always follows `--`, and its first argument must not be empty. Interactive `barn mac ssh dev` needs no separator.
- Before `--`, pass Barn flags (`--json`, `-v`, `--help`) and at most one existing machine name; omit the name to select the default machine. Everything after `--` belongs to the guest command.
- An unknown machine name or invalid arguments exit `2` (`usage`) before any connection is attempted, with `barn mac ls` as the next step.
- `--json` needs a remote command; an interactive shell has no JSON form.

## JSON output {#json-output}

Every `barn mac` command except interactive `ssh` supports `--json` and `--yaml`. The result goes to stdout; progress goes to stderr.

### `ls` {#ls}

In this sample, `barn mac image update` has made 27.2.1 the default base (`base`), while `mac1` still runs the 27.0 base it was cloned from (`machines[0].image`).

```json
{
  "schema_version": 2,
  "root": "/Users/alice/.barn/mac",
  "prepared": true,
  "base": {"version": "27.2.1", "build": "26B131", "base_id": "26B131-c8269f7cdc99451b71db4104"},
  "machines": [
    {
      "name": "mac1",
      "kind": "macos",
      "state": "running",
      "ready": true,
      "ssh": "ready",
      "address": "10.10.20.10",
      "address_stable": true,
      "ssh_host": "10.10.20.10",
      "ssh_port": 22,
      "user": "alice",
      "image": {"version": "27.0", "build": "26A428", "base_id": "26A428-e16af589f4b705ca397f5b21"},
      "cpus": 4,
      "memory_bytes": 8589934592,
      "disk": {"capacity_bytes": 107374182400, "allocated_bytes": 1202647040},
      "network": {"subnet": "10.10.20.0/24", "gateway": "10.10.20.1", "address": "10.10.20.10"},
      "shares": [{"name": "src", "host": "/Users/alice/src", "guest": "/Volumes/My Shared Files/src", "readonly": false}],
      "clipboard": true,
      "pid": 2545,
      "instance_id": "b1482ffc-2da7-45d9-ace5-c5f3193a9172",
      "warnings": []
    }
  ],
  "running": 1,
  "limit": 2
}
```

| Field | Values |
|---|---|
| `base` | The default base for new machines |
| `machines[].image` | The base this machine was cloned from |
| `state` | `prepared` (created, first boot not completed), `starting`, `running`, `stopping`, `stopped`, `unknown` |
| `ready` | `true` when this check verified guest SSH and passwordless sudo |
| `ssh` | `ready`, `pending` (first boot in progress), `unavailable`, `offline`, `unchecked` |
| `observed` | Present when the running guest reports a macOS build different from its base |
| `window_visible` | Present while the desktop window is open |
| `error`, `warnings` | The last failure recorded for the machine, and SSH issues found during this check |
| `running`, `limit` | Machines running in this `BARN_HOME`, and Apple's per-host limit; the limit itself is enforced across the whole Mac |
{.wide .expand}

### Lifecycle results {#lifecycle-results}

Single-machine commands (`up`, `restart`, `open`, `recreate`, `configure`) return one result object. Batch commands (`start`, `stop`, `destroy`) return `{"machines": [...]}`, even for one name. When a batch partly fails, the result lists both the machines that succeeded and the failures, and the command exits `5`.

```json
{"name": "mac1", "action": "created", "state": "running", "ready": true,
 "address": "10.10.20.10", "user": "alice", "version": "27.2.1", "build": "26B131"}
```

| Field | Values |
|---|---|
| `action` | `created`, `started`, `running` (already running), `restarted`, `recreated`, `opened`, `configured`, `stopped`, `powered_off`, `already_stopped`, `destroyed`, `absent` |
| `forced` | `true` when the VM was powered off rather than shut down: the two-minute shutdown in `stop` or `restart` timed out, or `stop --force` was used (`action: powered_off`) |
| `window` | `true` when the desktop window was opened |
| `warnings` | Non-fatal notices, such as a clipboard change that takes effect at the next start |
{.wide .expand}

Other commands:

- **`exec --json`**: the same schema as Linux `barn exec --json` (`node`, `exit_code`, `stdout`, `stderr`).
- **`password --json`**: `name` and `password`, or `{"name": "mac1", "copied": true}` with `--copy`.
- **`ssh-config --json`**: `fragment` when printing; installation status with `--install` or `--remove`.
- **`logs --json`**: `name` and `logs`.
- **`doctor --json`**: `ok` and `checks[]` (`name`, `ok`, `detail`, optional `next`); exits `3` if any check fails.

## Failures {#failures}

A failed command reports a stable `reason` and, when there is one, a `next` command to run. Exit codes follow the [CLI exit code table](../cli/#exit-codes); `ssh` and `exec` pass the remote command's exit status through.

| Reason | Exit | Meaning and next step |
|---|---:|---|
| `mac_host_unsupported` | `3` | Host is not Apple Silicon or is running macOS older than 27 |
| `mac_runner_missing` | `3` | `Barn.app` / native runner is not installed alongside the `barn` CLI |
| `mac_runner_protocol` | `3` | Installed `Barn.app` uses a different runner protocol version; reinstall matching CLI and app builds |
| `mac_root` | `2` | Invoked as `root`; run as the logged-in desktop user without `sudo` |
| `mac_download_consent` | `2` | Non-interactive macOS download requires `--yes` (or supply a local file with `--ipsw`) |
| `mac_machine_absent` | `4` | Named VM does not exist; create it with `barn mac up NAME` |
| `mac_not_initialized` | `4` | VM has not finished its first boot; complete initialization with `barn mac up NAME` |
| `mac_not_running` | `4` | `ssh` or `exec` called on a stopped VM; run `barn mac start NAME` |
| `mac_running` | `4` | Requested configuration change requires the VM to be stopped; run `barn mac stop NAME` |
| `mac_configuration_conflict` | `4` | `up` flags differ from the existing VM; run the suggested `configure` or `recreate` command |
| `ssh_config_linked` | `4` | `~/.ssh/config` is a symlink or hard link; add the printed `Include` line manually |
| `mac_vm_limit` | `6` | Two macOS VMs are already running on this Mac; stop the reported VM first |
| `mac_subnet_in_use` | `6` | A host route overlaps the VM's subnet; run `barn mac configure NAME --subnet auto` |
| `disk_full` | `6` | Insufficient free disk space to download or install the macOS base image |
| `mac_machine_damaged` | `7` | An initialized VM slot is missing its disk or identity files (the slot directory is preserved for inspection) |
| `mac_readiness_interrupted` | `130` | `barn mac stop` interrupted a first-boot SSH readiness wait |
{.wide .expand}

## Files {#files}

```filetree {title="$BARN_HOME/mac"}
config.json # Installation identity, default base image, and ssh_config_off flag
images/
  ipsw/
    <build>.ipsw # Cached Apple restore image (.partial while downloading)
    <build>.ipsw.json # Restore image metadata and digest
  base/
    <id>/ # Read-only, unbooted macOS base image bundle
slots/
  <name>/
    state.json # Machine record (schema 2)
    disk.asif # Copy-on-write APFS disk layer cloned from the base image
    machine-id.bin # Apple Virtualization machine identifier
    auxiliary-storage.bin # NVRAM / boot auxiliary storage
    id_ed25519 # Per-VM SSH private key
    id_ed25519.pub # Per-VM SSH public key
    known_hosts # Pinned guest host key under barn-mac-<instance>
    password # Generated guest login password (mode 0600)
    runner.log # Per-VM runtime log read by barn mac logs
runtime/ # Global state lock and per-machine operation locks
```

Outside `$BARN_HOME/mac`:

- **Control sockets**: `/tmp/barn-mac-<uid>-<hash>/`.
- **OpenSSH fragment**: `barn mac ssh-config` manages `~/.ssh/barn-mac_config` and a `# barn-mac:include` block in `~/.ssh/config`, independent of the Linux `# barn:include` block. `barn mac ssh-config --remove` sets `ssh_config_off` in `config.json`, so lifecycle commands stop re-adding the `Include` block until you run `barn mac ssh-config --install`.
- **Desktop preferences**: window positions live in `~/Library/Preferences/com.pgsty.barn.plist` (`0.9.0` used `~/Library/Preferences/io.pgsty.barn.mac-runner.plist`).
- **Inside the guest**: Barn writes `~/.ssh/authorized_keys`, `/private/etc/sudoers.d/80-barn`, and `/etc/ssh/sshd_config.d/000-barn.conf`, sets `ComputerName` and `LocalHostName`, and disables idle sleep with `pmset`.

### Recovery state {#recovery-state}

An interrupted `recreate` leaves a recovery journal, and `barn mac image prune` keeps every base that journal still references until recovery cleanup finishes. While a journal is malformed, `barn mac image ls`, the space estimate in `barn mac doctor`, and pruning fail until it is repaired, and every image stays in place. If one machine's state cannot be read, `barn mac ls` fails as a whole, while commands that name the other machines keep working.
