Fixing dreaded "Userland and kernel are not in sync" error

It happened to me several times to encounter the error when jail userland version differs from the kernel version. I thought I was correctly upgrading my jails, yet I was missing something. After the last time, I decided to dig a bit further and finally got a working solution.

This error occurs in several forms. For example, you can see the following in /var/log/daily.log:

Checking userland and kernel versions:
Userland and kernel are not in sync
Userland version: 1202000
Kernel version: 1300139

Or, you can see the following when running pkg:

Newer FreeBSD version for package foo:
To ignore this error set IGNORE_OSVERSION=yes
- package: 1300139
- running kernel: 1202000
Allow missmatch now?[Y/n]:

Turns out that when one upgrades the host machine, freebsd-update -r RELEASE install (invoked several times) would correctly bring system up to date. Then pkg bootstrap -f will update all the packages so they are built with the latest ABI. Simply invoking freebsd-update -b /path/to/jail -r RELEASE will however not do these steps correctly, mainly because it doesn’t know from which version it needs to upgrade from. I found this info in ezjail instructions, which I was skipping, since I don’t use ezjail.

In essence, the right thing to do is:

pkg bootstrap -f
pkg update
pkg upgrade