#!/bin/sh
# -*- coding: latin-1 -*-

# Copyright © 2013, 2025, Trond Endrestøl <Trond.Endrestol@ximalas.info>

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

if [ -z "${BOOTPOOL}" ]; then
  echo "${0}: you may set the environment variable BOOTPOOL to the name of a separate boot pool" >/dev/stderr
  echo "${0}: e.g. export BOOTPOOL=bootpool" >/dev/stderr
  #exit 69
else
  if ! zpool list "${BOOTPOOL}" >/dev/null 2>/dev/null; then
    echo "${0}: boot pool ${BOOTPOOL} does not exist" >/dev/stderr
    echo "${0}: try: zpool create -o cachefile=/tmp/zpool.cache -O mountpoint=legacy ${BOOTPOOL} ..." >/dev/stderr
    exit 69
  fi
fi

if [ -z "${ROOTPOOL}" ]; then
  echo "${0}: you must set the environment variable ROOTPOOL to the name of your root pool" >/dev/stderr
  echo "${0}: e.g. export ROOTPOOL=zroot" >/dev/stderr
  exit 69
fi

if ! zpool list "${ROOTPOOL}" >/dev/null 2>/dev/null; then
  echo "${0}: root pool ${ROOTPOOL} does not exist" >/dev/stderr
  echo "${0}: try: zpool create -o cachefile=/tmp/zpool.cache -O mountpoint=legacy ${ROOTPOOL} ..." >/dev/stderr
  exit 69
fi

if [ -z "${DATAPOOL}" ]; then
  echo "${0}: you must set the environment variable DATAPOOL to the name of your data pool" >/dev/stderr
  echo "${0}: e.g. export DATAPOOL=zdata" >/dev/stderr
  echo "${0}: your data pool can be the same as your root pool" >/dev/stderr
  echo "${0}: e.g. export DATAPOOL=\${ROOTPOOL}" >/dev/stderr
  exit 69
fi

if [ "${DATAPOOL}" != "${ROOTPOOL}" ]; then
  if ! zpool list "${DATAPOOL}" >/dev/null 2>/dev/null; then
    echo "${0}: data pool ${DATAPOOL} does not exist" >/dev/stderr
    echo "${0}: try: zpool create -o cachefile=/tmp/zpool.cache -O mountpoint=legacy ${DATAPOOL} ..." >/dev/stderr
    exit 69
  fi
fi

if [ -z "${RELEASEDATE}" ]; then
  echo "${0}: you must set the environment variable RELEASEDATE to the date of the release you are installing" >/dev/stderr
  echo "${0}: e.g. export RELEASEDATE=20130602" >/dev/stderr
  echo "${0}:   or export RELEASEDATE=20130602-133700-stable-14-local" >/dev/stderr
  exit 69
fi

if [ -z "${RELEASEREV}" ]; then
  echo "${0}: you must set the environment variable RELEASEREV to the Git/Subversion revision number of the release you are installing" >/dev/stderr
  echo "${0}: e.g. export RELEASEREV=251259" >/dev/stderr
  echo "${0}:   or export RELEASEREV=282381-7605f59d08f0" >/dev/stderr
  exit 69
fi

if [ -z "${COMPRESSION}" ]; then
  #COMPRESSION=gzip-9
  COMPRESSION=zstd-19

  echo "${0}: you may set the environment variable COMPRESSION to any valid value" >/dev/stderr
  echo "${0}: e.g. export COMPRESSION=gzip-9" >/dev/stderr
  echo "${0}:   or export COMPRESSION=off" >/dev/stderr
  echo "${0}: ${COMPRESSION} is the default in this script" >/dev/stderr
  echo "${0}: Beware of boot loaders lack of support for gzip compressed BEs" >/dev/stderr
  #exit 69
fi

# https://calomel.org/zfs_freebsd_root_install.html
# Set zfs to cache data for longer striped writes
#sysctl vfs.zfs.delay_min_dirty_percent=95
#sysctl vfs.zfs.dirty_data_max=12884901888
#sysctl vfs.zfs.dirty_data_sync=11811160064
#sysctl vfs.zfs.min_auto_ashift=12 # Must be set before pool is created.
#sysctl vfs.zfs.prefetch_disable=1
#sysctl vfs.zfs.top_maxinflight=128
#sysctl vfs.zfs.trim.txg_delay=3
#sysctl vfs.zfs.txg.timeout=60
#sysctl vfs.zfs.vdev.aggregation_limit=524288
#sysctl vfs.zfs.vdev.scrub_max_active=3
# Should we use all or some of these sysctls in production?

# This script assumes the pools were created with -O mountpoint=legacy (that's capital O).

# This ZFS layout tries to separate base, localbase, src, obj, ports, and user data.

# The founding idea is that you create a snapshot of the running BE,
# create a clone attached to that snapshot, upgrade the clone, inherit
# the value for the mountpoint from the parent, point the bootfs pool
# property to the new BE, and reboot.

# Some parts of base are considered common to all boot environments and
# thus exists as separate filesystems.

# WARNING! This layout may be flawed. Use with care.

# Handle a separate bootpool.
if [ -n "${BOOTPOOL}" ]; then
  # In the future I might go atime=off for most datasets.
  # You should set atime=off in particular for SSDs.
  #zfs set atime=off ${BOOTPOOL}

  zfs set compression=off ${BOOTPOOL}
  #zfs set compression=${COMPRESSION} ${BOOTPOOL}
  #zfs set recordsize=1M ${BOOTPOOL}

  # Create a seatbelt for the bootpool.
  # Ideally you should reserve 30 % of the pool and not just 1G.
  zfs create ${BOOTPOOL}/do-not-destroy
  zfs set canmount=off   ${BOOTPOOL}/do-not-destroy
  zfs set reservation=1G ${BOOTPOOL}/do-not-destroy

  # Create a dataset for the kernel and kernel modules.
  # bootpool BEs live in ${BOOTPOOL}/ROOT and follows a YYYYMMDD-rNNNNNN pattern.
  # Update this pattern to match Git versioning of the kernel.
  zfs create ${BOOTPOOL}/BOOT
  zfs set canmount=off ${BOOTPOOL}/BOOT
  zfs create ${BOOTPOOL}/BOOT/${RELEASEDATE}-r${RELEASEREV}

  # This is the bootfs property used by the boot loader.
  zpool set bootfs=${BOOTPOOL}/BOOT/${RELEASEDATE}-r${RELEASEREV} ${BOOTPOOL}
fi

# You should set atime=off in particular for SSDs.
#zfs set atime=off ${ROOTPOOL}

# Create a seatbelt for the rootpool.
# Ideally you should reserve 30 % of the pool and not just 1G.
zfs create ${ROOTPOOL}/do-not-destroy
zfs set canmount=off   ${ROOTPOOL}/do-not-destroy
zfs set reservation=1G ${ROOTPOOL}/do-not-destroy

# Create a seatbelt for the datapool.
# Ideally you should reserve 30 % of the pool and not just 1G.
if [ "${DATAPOOL}" != "${ROOTPOOL}" ]; then
  # You should set atime=off in particular for SSDs.
  #zfs set atime=off ${DATAPOOL}

  zfs create ${DATAPOOL}/do-not-destroy
  zfs set canmount=off   ${DATAPOOL}/do-not-destroy
  zfs set reservation=1G ${DATAPOOL}/do-not-destroy
fi

# rootpool BEs live in ${ROOTPOOL}/ROOT and follows a YYYYMMDD-rNNNNNN pattern.
# Update this pattern to match Git versioning of the kernel.
zfs create ${ROOTPOOL}/ROOT
zfs set canmount=off ${ROOTPOOL}/ROOT
zfs create ${ROOTPOOL}/ROOT/${RELEASEDATE}-r${RELEASEREV}

# If no separate bootpool, then set the bootfs property on the rootpool.
if [ -z "${BOOTPOOL}" ]; then
  zpool set bootfs=${ROOTPOOL}/ROOT/${RELEASEDATE}-r${RELEASEREV} ${ROOTPOOL}
fi

# These datasets exists solely for classification purposes and for providing base mountpoints for their children.
if [ "${DATAPOOL}" != "${ROOTPOOL}" ]; then
  zfs create ${DATAPOOL}/usr
  zfs set canmount=off ${DATAPOOL}/usr

  zfs create ${DATAPOOL}/usr/local
  zfs set canmount=off ${DATAPOOL}/usr/local
fi

# These datasets exists solely for classification purposes and for providing base mountpoints for their children.
if [ "${DATAPOOL}" != "${ROOTPOOL}" ]; then
  zfs create ${DATAPOOL}/var
  zfs set canmount=off ${DATAPOOL}/var

  zfs create ${DATAPOOL}/var/cache
  zfs set canmount=off ${DATAPOOL}/var/cache

  zfs create ${DATAPOOL}/var/db
  zfs set canmount=off ${DATAPOOL}/var/db

  zfs create ${DATAPOOL}/var/spool
  zfs set canmount=off ${DATAPOOL}/var/spool
fi

zfs create ${ROOTPOOL}/media
zfs create ${ROOTPOOL}/nfs
zfs create ${ROOTPOOL}/tmp

# This dataset exists solely for classification purposes and for providing a base mountpoint for its children.
zfs create ${ROOTPOOL}/usr
zfs set canmount=off ${ROOTPOOL}/usr

zfs create ${ROOTPOOL}/usr/compat
zfs create ${ROOTPOOL}/usr/compat/linux
zfs create ${ROOTPOOL}/usr/local # Reconsider this dataset and its children when 13.1-RELEASE is out.
zfs create ${ROOTPOOL}/usr/local/certs
zfs create ${ROOTPOOL}/usr/local/etc
#zfs create ${ROOTPOOL}/usr/local/etc/namedb # Only useful when you run ISC BIND without chroot.
zfs create ${ROOTPOOL}/usr/local/tests
zfs create ${ROOTPOOL}/usr/local/var
zfs create ${ROOTPOOL}/usr/obj
zfs create ${ROOTPOOL}/usr/ports
zfs create ${ROOTPOOL}/usr/ports/.git
zfs create ${ROOTPOOL}/usr/ports/.git/objects
zfs set canmount=off ${ROOTPOOL}/usr/ports/.git # Unhide these two after your working copy is cloned and the existing .git subtree is renamed to .git0. Then move the .git0 subtree into the .git subtree.
zfs set canmount=off ${ROOTPOOL}/usr/ports/.git/objects
zfs create ${ROOTPOOL}/usr/ports/distfiles
#zfs create ${ROOTPOOL}/usr/ports/local # Not particularly useful with ports-mgmt/synth unless you also use ports-mgmt/portshaker to combine ports trees.
zfs create ${ROOTPOOL}/usr/ports/packages
zfs create ${ROOTPOOL}/usr/src
zfs create ${ROOTPOOL}/usr/src/.git
zfs create ${ROOTPOOL}/usr/src/.git/objects
zfs set canmount=off ${ROOTPOOL}/usr/src/.git # Unhide these two after your working copy is cloned and the existing .git subtree is renamed to .git0. Then move the .git0 subtree into the .git subtree.
zfs set canmount=off ${ROOTPOOL}/usr/src/.git/objects

# This dataset exists solely for classification purposes and for providing a base mountpoint for its children.
zfs create ${ROOTPOOL}/var
zfs set canmount=off ${ROOTPOOL}/var

zfs create ${ROOTPOOL}/var/Named # Capitalized first letter to avoid collision with /var/named which make delete-old will attempt to remove.
zfs create ${ROOTPOOL}/var/account
zfs create ${ROOTPOOL}/var/audit
zfs create ${ROOTPOOL}/var/backups

# This dataset exists solely for classification purposes and for providing a base mountpoint for its children.
zfs create ${ROOTPOOL}/var/cache
zfs set canmount=off ${ROOTPOOL}/var/cache

# These datasets might belong on another zpool, maybe a dedicated one.
zfs create ${ROOTPOOL}/var/cache/ccache
zfs create ${ROOTPOOL}/var/cache/ccache/base
zfs create ${ROOTPOOL}/var/cache/ccache/localbase
zfs create ${ROOTPOOL}/var/cache/ccache/poudriere # This dataset might need several children, one for each combination of architecture and branch.
zfs create ${ROOTPOOL}/var/cache/ccache/synth

# These snapshots are handy for easy rollback. They should probably be recreated when each configuration file is in place.
zfs snapshot ${ROOTPOOL}/var/cache/ccache/base@empty
zfs snapshot ${ROOTPOOL}/var/cache/ccache/localbase@empty
zfs snapshot ${ROOTPOOL}/var/cache/ccache/poudriere@empty
zfs snapshot ${ROOTPOOL}/var/cache/ccache/synth@empty

zfs create ${ROOTPOOL}/var/cache/pkg # Reconsider this dataset when 13.1-RELEASE is out.
zfs create ${ROOTPOOL}/var/cache/synth

zfs create ${ROOTPOOL}/var/crash

# This dataset exists solely for classification purposes and for providing a base mountpoint for its children.
zfs create ${ROOTPOOL}/var/db
zfs set canmount=off ${ROOTPOOL}/var/db

zfs create ${ROOTPOOL}/var/db/clamav
zfs create ${ROOTPOOL}/var/db/dcc
zfs create ${ROOTPOOL}/var/db/etcupdate
zfs create ${ROOTPOOL}/var/db/entropy
zfs create ${ROOTPOOL}/var/db/fontconfig
zfs create ${ROOTPOOL}/var/db/hyperv
zfs create ${ROOTPOOL}/var/db/ntp
zfs create ${ROOTPOOL}/var/db/pkg # Reconsider this dataset when 13.1-RELEASE is out.
zfs create ${ROOTPOOL}/var/db/ports
zfs create ${ROOTPOOL}/var/db/portsnap
#zfs create ${ROOTPOOL}/var/db/postfix # Is part of the datapool.
zfs create ${ROOTPOOL}/var/db/tlpkg
zfs create ${ROOTPOOL}/var/empty
zfs create ${ROOTPOOL}/var/lib
zfs create ${ROOTPOOL}/var/log

# Mail files could potentially be stored on a dedicated zpool.
# Modify this script accordingly.
zfs create ${ROOTPOOL}/var/mail

zfs create ${ROOTPOOL}/var/munin
zfs create ${ROOTPOOL}/var/ossec
zfs create ${ROOTPOOL}/var/run
zfs create ${ROOTPOOL}/var/spool

# Mail files could potentially be stored on a dedicated zpool.
# Modify this script accordingly.
zfs create ${ROOTPOOL}/var/spool/clientmqueue
zfs create ${ROOTPOOL}/var/spool/mqueue
zfs create ${ROOTPOOL}/var/spool/postfix

zfs create ${ROOTPOOL}/var/spool/workdirs

# These datasets might belong on another zpool, maybe a dedicated one.
zfs create ${ROOTPOOL}/var/synth
zfs create ${ROOTPOOL}/var/synth/builders # Tell ports-mgmt/synth to use /var/synth/builders/synth-live as "build base directory".
zfs create ${ROOTPOOL}/var/synth/live_packages
zfs create ${ROOTPOOL}/var/synth/systems

zfs create ${ROOTPOOL}/var/tmp
zfs create ${ROOTPOOL}/var/unbound

zfs create ${DATAPOOL}/home

zfs create ${DATAPOOL}/usr/local/mailman
zfs create ${DATAPOOL}/usr/local/moodledata

zfs create ${DATAPOOL}/usr/local/www

zfs create ${DATAPOOL}/var/cache/netdata

# Database files could potentially be stored on a dedicated zpool.
# Modify this script accordingly.
zfs create ${DATAPOOL}/var/db/bacula
zfs create ${DATAPOOL}/var/db/bareos
zfs create ${DATAPOOL}/var/db/boinc
zfs create ${DATAPOOL}/var/db/clickhouse
zfs create ${DATAPOOL}/var/db/darkstat
zfs create ${DATAPOOL}/var/db/grafana7
zfs create ${DATAPOOL}/var/db/influxdb
zfs create ${DATAPOOL}/var/db/mysql
zfs create ${DATAPOOL}/var/db/mysql_secure
zfs create ${DATAPOOL}/var/db/mysql_tmpdir
zfs create ${DATAPOOL}/var/db/netdata
zfs create ${DATAPOOL}/var/db/ntopng
zfs create ${DATAPOOL}/var/db/postfix

# The optimizations for MySQL and PostgreSQL are due to Allan Jude &
# Michael W. Lucas and their book "FreeBSD Mastery: Advanced ZFS".
# Also see their article in the Winter 2016 issue (Vol. 41, No. 4) of USENIX ";login:".
# https://www.usenix.org/publications/login/winter2016
# https://www.usenix.org/publications/login/winter2016/jude
# https://www.usenix.org/system/files/login/articles/login_winter16_09_jude.pdf

zfs create ${DATAPOOL}/var/db/postgres # From PostgreSQL 9.6 onward, see ports/head r421360.
zfs create ${DATAPOOL}/var/db/postgres/data13
zfs create ${DATAPOOL}/var/db/postgres/data13/base
zfs create ${DATAPOOL}/var/db/postgres/data13/pg_wal
zfs create ${DATAPOOL}/var/db/postgres/data14
zfs create ${DATAPOOL}/var/db/postgres/data14/base
zfs create ${DATAPOOL}/var/db/postgres/data14/pg_wal
zfs create ${DATAPOOL}/var/db/postgres/data15
zfs create ${DATAPOOL}/var/db/postgres/data15/base
zfs create ${DATAPOOL}/var/db/postgres/data15/pg_wal
zfs create ${DATAPOOL}/var/db/postgres/data16
zfs create ${DATAPOOL}/var/db/postgres/data16/base
zfs create ${DATAPOOL}/var/db/postgres/data16/pg_wal
zfs create ${DATAPOOL}/var/db/postgres/data17
zfs create ${DATAPOOL}/var/db/postgres/data17/base
zfs create ${DATAPOOL}/var/db/postgres/data17/pg_wal
zfs create ${DATAPOOL}/var/db/postgres/data18
zfs create ${DATAPOOL}/var/db/postgres/data18/base
zfs create ${DATAPOOL}/var/db/postgres/data18/pg_wal
zfs create ${DATAPOOL}/var/db/prometheus # Should we have some form of special handling of this fs ...
zfs create ${DATAPOOL}/var/db/prometheus/data # ... and this fs?
zfs create ${DATAPOOL}/var/db/prometheus/data/wal
zfs create ${DATAPOOL}/var/db/redis
zfs create ${DATAPOOL}/var/db/samba4

zfs create ${DATAPOOL}/var/spool/bareos
zfs create ${DATAPOOL}/var/spool/ftp
zfs create ${DATAPOOL}/var/spool/sympa

zfs create ${DATAPOOL}/var/squid
zfs create ${DATAPOOL}/var/squid/cache
zfs create ${DATAPOOL}/var/squid/ssl_db

# Set various properties.
# Keep the lists in alphabetical order.
# Sort first on property, second on property value, and finally on each dataset's mountpoint.

# You should set atime=off in particular for SSDs.
# Ccache and mail related filesystems must have atime=on.
zfs set atime=on  ${ROOTPOOL}/var/cache/ccache
zfs set atime=on  ${ROOTPOOL}/var/mail
zfs set atime=on  ${ROOTPOOL}/var/spool/clientmqueue
zfs set atime=on  ${ROOTPOOL}/var/spool/mqueue
zfs set atime=off ${ROOTPOOL}/usr/obj
zfs set atime=off ${ROOTPOOL}/var/spool/workdirs
zfs set atime=off ${DATAPOOL}/var/spool/ftp

zfs set compression=off ${ROOTPOOL}
#zfs set compression=${COMPRESSION} ${ROOTPOOL}
#zfs set recordsize=1M ${ROOTPOOL}

if [ "${DATAPOOL}" != "${ROOTPOOL}" ]; then
  zfs set compression=off ${DATAPOOL}
  #zfs set compression=${COMPRESSION} ${DATAPOOL}
  #zfs set recordsize=1M ${DATAPOOL}
fi

zfs set compression=${COMPRESSION} ${ROOTPOOL}/tmp
zfs set compression=${COMPRESSION} ${ROOTPOOL}/usr/local/certs
zfs set compression=${COMPRESSION} ${ROOTPOOL}/usr/local/etc
zfs set compression=${COMPRESSION} ${ROOTPOOL}/usr/local/tests
zfs set compression=${COMPRESSION} ${DATAPOOL}/usr/local/www
zfs set compression=${COMPRESSION} ${ROOTPOOL}/usr/ports
zfs set compression=${COMPRESSION} ${ROOTPOOL}/usr/src
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/Named
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/db/etcupdate
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/db/hyperv
zfs set compression=${COMPRESSION} ${DATAPOOL}/var/db/mysql
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/db/pkg # Reconsider this dataset when 15.0-RELEASE is out.
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/db/ports
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/log
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/mail
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/munin
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/spool/clientmqueue
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/spool/mqueue
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/synth/systems # This dataset might belong on another zpool, maybe a dedicated one.
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/tmp
zfs set compression=${COMPRESSION} ${ROOTPOOL}/var/unbound

zfs set compression=off ${ROOTPOOL}/usr/src/.git/objects
zfs set compression=off ${ROOTPOOL}/usr/ports/.git/objects
zfs set compression=off ${ROOTPOOL}/usr/ports/distfiles
zfs set compression=off ${ROOTPOOL}/usr/ports/packages
zfs set compression=off ${ROOTPOOL}/var/spool/workdirs

#zfs set exec=off ${ROOTPOOL}/usr/local/etc/namedb # Only useful when you run ISC BIND without chroot.
zfs set exec=off ${ROOTPOOL}/usr/ports/distfiles
zfs set exec=off ${ROOTPOOL}/usr/ports/packages
zfs set exec=off ${ROOTPOOL}/usr/src
zfs set exec=off ${ROOTPOOL}/var/Named
zfs set exec=off ${ROOTPOOL}/var/backups
zfs set exec=off ${ROOTPOOL}/var/crash
zfs set exec=off ${ROOTPOOL}/var/db
zfs set exec=off ${ROOTPOOL}/var/empty
zfs set exec=off ${ROOTPOOL}/var/log
zfs set exec=off ${ROOTPOOL}/var/mail
zfs set exec=off ${ROOTPOOL}/var/run
zfs set exec=off ${ROOTPOOL}/var/spool
zfs set exec=off ${ROOTPOOL}/var/unbound

if [ "${DATAPOOL}" != "${ROOTPOOL}" ]; then
  zfs set exec=off ${DATAPOOL}/var/db
  zfs set exec=off ${DATAPOOL}/var/spool
fi

# Do we need exec=on for /var/db/pkg with the old pkg_* tools and the old package format now gone?
# Do we need exec=on for /var/db/ports at all?
zfs set exec=on ${ROOTPOOL}/var/db/pkg # Reconsider this dataset when 13.0-RELEASE is out.
zfs set exec=on ${ROOTPOOL}/var/db/ports
zfs set exec=on ${ROOTPOOL}/var/spool/workdirs

zfs set logbias=throughput ${DATAPOOL}/var/db/postgres/data13/base
zfs set logbias=throughput ${DATAPOOL}/var/db/postgres/data13/pg_wal
zfs set logbias=throughput ${DATAPOOL}/var/db/postgres/data14/base
zfs set logbias=throughput ${DATAPOOL}/var/db/postgres/data14/pg_wal
zfs set logbias=throughput ${DATAPOOL}/var/db/postgres/data15/base
zfs set logbias=throughput ${DATAPOOL}/var/db/postgres/data15/pg_wal
zfs set logbias=throughput ${DATAPOOL}/var/db/postgres/data16/base
zfs set logbias=throughput ${DATAPOOL}/var/db/postgres/data16/pg_wal
zfs set logbias=throughput ${DATAPOOL}/var/db/postgres/data17/base
zfs set logbias=throughput ${DATAPOOL}/var/db/postgres/data17/pg_wal
zfs set logbias=throughput ${DATAPOOL}/var/db/postgres/data18/base
zfs set logbias=throughput ${DATAPOOL}/var/db/postgres/data18/pg_wal

zfs set primarycache=metadata ${DATAPOOL}/var/db/mysql
zfs set primarycache=metadata ${DATAPOOL}/var/db/postgres/data13/base
zfs set primarycache=metadata ${DATAPOOL}/var/db/postgres/data13/pg_wal
zfs set primarycache=metadata ${DATAPOOL}/var/db/postgres/data14/base
zfs set primarycache=metadata ${DATAPOOL}/var/db/postgres/data14/pg_wal
zfs set primarycache=metadata ${DATAPOOL}/var/db/postgres/data15/base
zfs set primarycache=metadata ${DATAPOOL}/var/db/postgres/data15/pg_wal
zfs set primarycache=metadata ${DATAPOOL}/var/db/postgres/data16/base
zfs set primarycache=metadata ${DATAPOOL}/var/db/postgres/data16/pg_wal
zfs set primarycache=metadata ${DATAPOOL}/var/db/postgres/data17/base
zfs set primarycache=metadata ${DATAPOOL}/var/db/postgres/data17/pg_wal
zfs set primarycache=metadata ${DATAPOOL}/var/db/postgres/data18/base
zfs set primarycache=metadata ${DATAPOOL}/var/db/postgres/data18/pg_wal

zfs set quota=10G ${ROOTPOOL}/tmp
zfs set quota=10G ${ROOTPOOL}/var/tmp

#zfs set recordsize=1M  ${DATAPOOL}/home
zfs set recordsize=1M  ${ROOTPOOL}/usr/ports
zfs set recordsize=1M  ${ROOTPOOL}/usr/src
zfs set recordsize=16K ${DATAPOOL}/var/db/mysql # Assumes InnoDB and SET GLOBAL innodb_file_per_table=ON.
zfs set recordsize=8K  ${DATAPOOL}/var/db/postgres/data13/base
zfs set recordsize=8K  ${DATAPOOL}/var/db/postgres/data13/pg_wal
zfs set recordsize=8K  ${DATAPOOL}/var/db/postgres/data14/base
zfs set recordsize=8K  ${DATAPOOL}/var/db/postgres/data14/pg_wal
zfs set recordsize=8K  ${DATAPOOL}/var/db/postgres/data15/base
zfs set recordsize=8K  ${DATAPOOL}/var/db/postgres/data15/pg_wal
zfs set recordsize=8K  ${DATAPOOL}/var/db/postgres/data16/base
zfs set recordsize=8K  ${DATAPOOL}/var/db/postgres/data16/pg_wal
zfs set recordsize=8K  ${DATAPOOL}/var/db/postgres/data17/base
zfs set recordsize=8K  ${DATAPOOL}/var/db/postgres/data17/pg_wal
zfs set recordsize=8K  ${DATAPOOL}/var/db/postgres/data18/base
zfs set recordsize=8K  ${DATAPOOL}/var/db/postgres/data18/pg_wal
zfs set recordsize=32K ${DATAPOOL}/var/db/prometheus/data/wal # https://github.com/prometheus/tsdb/blob/master/docs/format/wal.md, accessed on 2019-02-04.
zfs set recordsize=1M  ${DATAPOOL}/var/db/ntopng
zfs set recordsize=1M  ${ROOTPOOL}/var/log
zfs set recordsize=1M  ${ROOTPOOL}/var/mail
zfs set recordsize=1M  ${DATAPOOL}/var/spool/ftp
zfs set recordsize=1M  ${DATAPOOL}/var/squid/cache

zfs set redundant_metadata=most ${DATAPOOL}/var/db/mysql
zfs set redundant_metadata=most ${DATAPOOL}/var/db/postgres/data13/base
zfs set redundant_metadata=most ${DATAPOOL}/var/db/postgres/data13/pg_wal
zfs set redundant_metadata=most ${DATAPOOL}/var/db/postgres/data14/base
zfs set redundant_metadata=most ${DATAPOOL}/var/db/postgres/data14/pg_wal
zfs set redundant_metadata=most ${DATAPOOL}/var/db/postgres/data15/base
zfs set redundant_metadata=most ${DATAPOOL}/var/db/postgres/data15/pg_wal
zfs set redundant_metadata=most ${DATAPOOL}/var/db/postgres/data16/base
zfs set redundant_metadata=most ${DATAPOOL}/var/db/postgres/data16/pg_wal
zfs set redundant_metadata=most ${DATAPOOL}/var/db/postgres/data17/base
zfs set redundant_metadata=most ${DATAPOOL}/var/db/postgres/data17/pg_wal
zfs set redundant_metadata=most ${DATAPOOL}/var/db/postgres/data18/base
zfs set redundant_metadata=most ${DATAPOOL}/var/db/postgres/data18/pg_wal

zfs set setuid=off ${DATAPOOL}/home
zfs set setuid=off ${ROOTPOOL}/tmp
#zfs set setuid=off ${ROOTPOOL}/usr/local/etc/namedb # Only useful when you run ISC BIND without chroot.
zfs set setuid=off ${ROOTPOOL}/usr/ports
zfs set setuid=off ${ROOTPOOL}/usr/src
zfs set setuid=off ${ROOTPOOL}/var/Named
zfs set setuid=off ${ROOTPOOL}/var/backups
zfs set setuid=off ${ROOTPOOL}/var/crash
zfs set setuid=off ${ROOTPOOL}/var/db
zfs set setuid=off ${ROOTPOOL}/var/empty
zfs set setuid=off ${ROOTPOOL}/var/log
zfs set setuid=off ${ROOTPOOL}/var/mail
zfs set setuid=off ${ROOTPOOL}/var/run
zfs set setuid=off ${ROOTPOOL}/var/spool
zfs set setuid=off ${ROOTPOOL}/var/tmp
zfs set setuid=off ${ROOTPOOL}/var/unbound

if [ "${DATAPOOL}" != "${ROOTPOOL}" ]; then
  zfs set setuid=off ${DATAPOOL}/var/db
  zfs set setuid=off ${DATAPOOL}/var/spool
fi

# EOF
