# Building BSDnas in a Nested Bastille Jail

This guide describes the FreeBSD jail used to build BSDnas. The outer host runs Bastille, while the `bsdnas` jail can create child jails and manage ZFS datasets for isolated build work.

## Host prerequisites

The host should provide FreeBSD, ZFS, Bastille, and a network bridge. This setup uses the existing `vm-public` bridge and the `192.168.3.0/24` network.

```sh
doas bastille bootstrap 15.1-RELEASE
doas sysrc -f /usr/local/etc/bastille/bastille.conf \
  bastille_network_loopback="" \
  bastille_network_shared="vm-public"
```

Choose an unused address for the build jail. The example below uses `192.168.3.64`.

```sh
doas bastille create bsdnas 15.1-RELEASE 192.168.3.64
```

## Enable nested jails and ZFS

Give the jail a bounded child-jail capacity and a dedicated jailed ZFS subtree. Do not expose Bastille’s entire storage tree to the build environment.

```sh
doas zfs create \
  -o mountpoint=/data \
  -o jailed=on \
  zroot/bastille/jails/bsdnas/data

doas bastille config bsdnas set children.max 64
doas bastille config bsdnas add allow.mount
doas bastille config bsdnas add allow.mount.devfs
doas bastille config bsdnas add allow.mount.zfs
doas bastille config bsdnas add allow.mount.procfs
doas bastille config bsdnas set enforce_statfs 1
doas bastille config bsdnas set \
  zfs.dataset zroot/bastille/jails/bsdnas/data
doas bastille restart bsdnas
```

The resulting dataset is mounted at `/data` inside the jail. Child datasets can be created below it without granting access to unrelated host datasets.

## Install build tools

Copy `rtk` from the host to reduce repetitive build output, then install Bastille inside the jail for nested jail operations.

```sh
doas bastille cp bsdnas \
  /usr/local/bin/rtk /usr/local/bin/rtk
doas bastille cmd bsdnas pkg -N
doas bastille cmd bsdnas \
  env ASSUME_ALWAYS_YES=yes pkg install bastille
```

Run noisy commands through `rtk`, and retain a full log when diagnosing failures:

```sh
doas bastille cmd bsdnas sh -c \
  'build-command 2>&1 | tee /data/bsdnas-build.log | rtk log'
```

## BSDnas source and jail compatibility findings

The official build instructions use the public Git repository `https://github.com/bsdnas/core-build.git`, not the unrelated local `zvault151` project. The source was cloned inside the jail at `/data/src/core-build` from the upstream `bsdnas` branch, and a private local branch named `bsdnas-jail` was created for jail-specific changes.

The documented build sequence is:

```sh
doas bastille cmd bsdnas sh -c '
  cd /data/src/core-build
  make bootstrap-pkgs
  make checkout
  make release
'
```

The full release is not yet started. The source inspection found several reasons the current jail setup is not sufficient by itself:

- The project requires a build host matching the target `stable/15` kernel. The jail currently runs the host’s FreeBSD `15.1-RELEASE-p3` kernel, not a `stable/15` build kernel.
- The build creates poudriere jails and uses nested `chroot`, devfs, fdescfs, nullfs, and ZFS mounts.
- `build/tools/install-jail.py` creates a ZFS dataset for the build jail; `build/tools/build-ports.py` snapshots ZFS and starts poudriere jails.
- Customization runs `zpool` inside a target chroot and may load `openzfs.ko`. This is the documented kernel-mismatch panic risk and must be tested before a release build.
- The build host needs substantial resources: 32 GB RAM, 16 GB real swap, and about 320 GB free disk. A full build takes hours.

The next safe step is a preflight run of the host and sandbox checks, followed by a small build target that exercises ZFS/chroot/mount behavior. Do not start `make release` until the kernel-version and resource requirements are resolved. Keep output in `/data/bsdnas-build.log` and summarize it with `rtk log`.

## Poudriere ZFS layout and jail permissions

The BSDnas build scripts use `ZPOOL` and `ZROOTFS` to construct two datasets:

```text
${ZPOOL}${ZROOTFS}/data       build data and ports working storage
${ZPOOL}${ZROOTFS}/jail       poudriere jail filesystem
${ZPOOL}${ZROOTFS}/jail@clean clean snapshot restored between builds
```

For this jail, use `ZPOOL=zroot` and `ZROOTFS=/bastille/jails/bsdnas/data/build`. The host-side parent dataset is jailed and mounted at `/data/build`:

```text
zroot/bastille/jails/bsdnas/data        /data       jailed=on
zroot/bastille/jails/bsdnas/data/build  /data/build jailed=on
```

The build creates and destroys `data`, `jail`, and `jail@clean` below `data/build`; these were tested from inside `bsdnas`. The jail now permits `allow.mount.zfs`, `allow.mount.nullfs`, `allow.mount.fdescfs`, `allow.mount.devfs`, and `allow.mount.procfs`, with `children.max=64`.

Poudriere’s nested-jail path also requests `allow.mount.tmpfs`, `allow.raw_sockets`, `allow.socket_af`, `allow.sysvipc`, and `allow.chflags`; all are now enabled. `allow.socket_af` permits socket address families other than IPv4, IPv6, local UNIX, and route sockets. `allow.sysvipc` enables System V IPC; newer FreeBSD configurations may prefer the separate `sysvmsg`, `sysvsem`, and `sysvshm` parameters. These permissions reduce isolation and should remain limited to this disposable build jail. The separate requirement for a host kernel matching `stable/15` still applies.

The permissions were verified by mounting and unmounting tmpfs, setting a file flag with `chflags`, and creating, snapshotting, and destroying a delegated ZFS child dataset from inside `bsdnas`.

Example build environment:

```sh
doas bastille cmd bsdnas sh -c '
  cd /data/src/core-build
  export ZPOOL=zroot
  export ZROOTFS=/bastille/jails/bsdnas/data/build
  make release 2>&1 | tee /data/bsdnas-release.log | rtk log
'
```

This storage setup addresses the poudriere dataset and mount requirements, but it does not solve the separate `stable/15` kernel compatibility requirement.

## Verify the environment

```sh
doas bastille list
doas bastille cmd bsdnas rtk --version
doas bastille cmd bsdnas bastille --version
doas bastille cmd bsdnas sh -c \
  'zfs create -o mountpoint=/data/test \
   zroot/bastille/jails/bsdnas/data/test && \
   zfs destroy zroot/bastille/jails/bsdnas/data/test'
```

Keep source code in the repository workspace, generated artifacts under `/data`, and host-specific credentials and logs out of version control.
