Nested FreeBSD Jail Lab with Bastille and ZFS
This builds a shared lab inside one Bastille VNET jail. Students administer ZFS-backed child jails without receiving access to the physical host.
The example uses FreeBSD 15.1:
| Node | Address |
|---|---|
Host on vm-public |
192.168.1.10/24 |
| Host lab gateway | 10.0.0.1/24 |
Bastille jail lab |
10.0.0.2/24 |
| Nested jail network | 10.10.0.0/24 |
| Nested gateway | 10.10.0.1 |
Example jail student1 |
10.10.0.11 |
Students share root in lab and are therefore mutually trusted.
Because vm-public is bridged to the physical LAN, this design contains the jail hierarchy but not layer-2 access: root in lab can readdress vnet0 onto 192.168.1.0/24. Use a dedicated bridge or VLAN instead when the LAN must be isolated from students.
Host: prepare Bastille
vm-public already exists and is managed by vm-bhyve. Do not recreate it or change its members. Add a secondary address:
sysrc ifconfig_vm_public_alias0="inet 10.0.0.1/24"
ifconfig vm-public inet 10.0.0.1/24 alias
sysrc gateway_enable=YES
sysctl net.inet.ip.forwarding=1
Add NAT to the host's existing /etc/pf.conf, before its filtering rules:
nat on vm-public inet from 10.0.0.0/24 to any -> 192.168.1.10
pass in on vm-public inet from 10.0.0.0/24
Reload PF only after validating the complete ruleset:
pfctl -nf /etc/pf.conf && pfctl -f /etc/pf.conf
Create the outer jail on the existing bridge:
bastille bootstrap 15.1-RELEASE
bastille create -B -g 10.0.0.1 -n 1.1.1.1 \
lab 15.1-RELEASE 10.0.0.2/24 vm-public
bastille stop lab
The VNET ruleset must expose PF, ZFS, and BPF devices. Bastille normally uses ruleset 13 for VNET jails; verify /etc/devfs.rules contains:
[bastille_vnet=13]
add include $devfsrules_hide_all
add include $devfsrules_unhide_basic
add include $devfsrules_unhide_login
add include $devfsrules_jail
add include $devfsrules_jail_vnet
add path 'bpf*' unhide
service devfs restart
bastille config lab set devfs_ruleset 13
bastille config lab set securelevel 0
bastille config lab set children.max 64
bastille config lab set enforce_statfs 1
bastille config lab set allow.mount
bastille config lab set allow.mount.zfs
bastille config lab set allow.mount.devfs
bastille config lab set allow.raw_sockets
Create and delegate a quota-limited dataset. It is separate from the jail's root dataset because a jail root cannot itself be delegated.
zfs create -o quota=100G -o mountpoint=none zroot/lab-data
bastille zfs lab jail zroot/lab-data /jails
bastille start lab
bastille console lab
Lab: configure routing
Run the remaining commands inside lab.
sysrc gateway_enable=YES
sysctl net.inet.ip.forwarding=1
ifconfig bridge0 create
ifconfig bridge0 inet 10.10.0.1/24 up
sysrc cloned_interfaces="bridge0"
sysrc ifconfig_bridge0="inet 10.10.0.1/24 up"
Use PF inside lab to NAT child traffic through Bastille's vnet0. Put this in /etc/pf.conf:
set skip on lo
nat on vnet0 inet from 10.10.0.0/24 to any -> (vnet0)
pass all
sysrc pf_enable=YES
pfctl -nf /etc/pf.conf
service pf start
Lab: create a ZFS template
zfs create -o mountpoint=/jails/template zroot/lab-data/template
fetch -o /tmp/base.txz \
https://download.freebsd.org/releases/amd64/15.1-RELEASE/base.txz
tar -xpf /tmp/base.txz -C /jails/template
cp /etc/resolv.conf /jails/template/etc/resolv.conf
zfs snapshot zroot/lab-data/template@clean
Create a child root with a clone:
zfs clone -o mountpoint=/jails/student1 \
zroot/lab-data/template@clean zroot/lab-data/student1
Add the child to /etc/jail.conf inside lab:
student1 {
path = "/jails/student1";
host.hostname = "student1.lab";
exec.clean;
mount.devfs;
devfs_ruleset = 0;
vnet;
vnet.interface = "epair1b";
exec.prestart = "/sbin/ifconfig epair1 create up";
exec.prestart += "/sbin/ifconfig bridge0 addm epair1a up";
exec.start = "/sbin/ifconfig epair1b inet 10.10.0.11/24 up";
exec.start += "/sbin/route add default 10.10.0.1";
exec.start += "/bin/sh /etc/rc";
exec.stop = "/bin/sh /etc/rc.shutdown";
exec.poststop = "/sbin/ifconfig bridge0 deletem epair1a";
exec.poststop += "/sbin/ifconfig epair1a destroy";
}
Ruleset 0 is intentional: nested jails may mount devfs inherited from lab, but cannot apply the host's devfs rulesets.
Start and test the child:
sysrc jail_enable=YES
service jail start student1
jls
jexec student1 ifconfig
jexec student1 ping -c 2 10.10.0.1
jexec student1 fetch -qo /dev/null https://download.freebsd.org/
zfs list -r zroot/lab-data
Cleanup
Inside lab:
service jail stop student1
zfs destroy zroot/lab-data/student1
The delegated tree belongs to lab. ZFS permits students to create, clone, snapshot, and destroy datasets there, but it does not permit a child of an already-jailed dataset to be delegated to another jail. Thus student1 can use a ZFS-backed root, but cannot administer that ZFS subtree itself.
See the FreeBSD jail handbook, Bastille VNET documentation, and zfs-jail(8).