pkg-be: automatic boot environment before pkg upgrade (plugin, feedback wanted)

From: mozgan <mozgan_at_gmail.com>
Date: Tue, 10 Nov 2026 13:41:32 UTC
Hi,

freebsd-update creates a boot environment before it installs updates;
pkg does not (PR 292983,
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=292983).
With pkgbase replacing freebsd-update in 16,
I think pkg should close this gap, so I wrote a small pkg plugin:

  https://github.com/mozgan/pkg-be   (BSD-2-Clause, v0.1.0)

What it does:
- Hooks PKG_PLUGIN_HOOK_PRE_UPGRADE (optionally PRE_INSTALL,
  PRE_DEINSTALL, PRE_AUTOREMOVE) and creates a BE with libbe(3),
  named pkg-be-YYYYmmdd-HHMMSS.
- Records the operation and the planned changes as ZFS user properties
  (pkg-be:op, pkg-be:packages, ...).
- Retention by count and age; never touches other BEs or the active and
  next-boot BE.
- Adds "pkg be list|show|diff|rollback|prune".
- Skips dry runs, "pkg -r", "pkg -c", jails and non-ZFS roots.

Tested on 15.1 amd64 (ZFS): unit and integration tests, a pkgbase
package reinstall, and a rollback with a real reboot. The port
(sysutils/pkg-be) passes portlint and poudriere testport on 15.1 and
14.5 amd64.

Questions:

1. Should this stay a plugin (I would submit the port), or would you
   prefer it built into pkg, e.g. as a pkg.conf option? I am happy to
   rework it either way.

2. pkg_plugin_hook_exec() ignores the callback's return value, so a
   plugin cannot stop an upgrade when the BE cannot be created. Would a
   patch that lets a failing pre-hook abort the job be acceptable,
   perhaps behind an option?

3. libpkg has no public getter for the root directory. To skip
   "pkg -r DIR" (where a BE of the running system would be useless),
   the plugin reads pkg's own argv via kern.proc.args. Would a small
   getter such as pkg_get_rootdir() be welcome?

Known limits: plugins are disabled in pkg-static, and with -r pkg loads
plugins and pkg.conf from DIR.

Any feedback is welcome.

Thanks,
Mehmet Ozgan