Random Musings

O for a muse of fire, that would ascend the brightest heaven of invention!


What version am I running anyway?

Thursday, 25 Feb 2021 Tags: freebsdversions

You’d think knowing what version of FreeBSD you are running is easy, but once you have jails, upgrades, boot environments, and just getting older, it’s hard to keep track.

Guntbert emailed me about a previous post, side-loading FreeBSD and asked why some of the version numbers didn’t match his intution. I couldn’t answer all of his questions, but luckily, FreeBSD is all open source, so it was easy enough to find out — and I learned a few things along the way. I did not know that freebsd-version(1) is a shell script until today, and the what(1) command was also new.

The results from uname and freebsd-version show the new kernel running but there was no reboot yet. Am I overlooking something obvious?

— Guntbert Reiter

Yes, but well spotted!

freebsd-version(1) is a shell script that gets some of its info from where it thinks the current kernel is. But from inside a jail, it’s completely wrong about that /boot/loader.conf derived info, but it is right about the actual running kernel, as it gets that via sysctl(8) from kernel data structures.

In line#52, it uses loader.conf to determine if we’re using an alternate kernel, which we’re not, finds the default in /boot/kernel/kernel which is our next-boot kernel, not the currently running one, and runs a command I did not know about, called what(1) to extract the version information from that:

# what -qs /boot/kernel/kernel
FreeBSD 13.0-BETA4 #0 releng/13.0-n244592-e32bc253629: Fri Feb 26 06:17:34 UTC 2021

Further down, in line#77. the currently running kernel info is extracted by the expected sysctl(8) invocation, of kern.osrelease.

The current userland version, interestingly, is just compiled in: see line#32 for that, and on my desktop, in the jail, that’s:

USERLAND_VERSION="13.0-BETA4"

Wrapping that all up:

#### main OS running 14.0-CURRENT
# uname -a
... 14.0-CURRENT FreeBSD 14.0-CURRENT main-n245063-d52cbc0be18c GENERIC  amd64

# freebsd-version -kru
14.0-CURRENT
14.0-CURRENT
14.0-CURRENT

# sysctl -a kern |egrep 'kern\.(osrel|ident|build)'
kern.osrelease: 14.0-CURRENT
kern.osreldate: 1400005
kern.build_id: 879b8aabcd91163a1a59a0c392bc4354c29dba11
kern.ident: GENERIC

And within the 13.x boot environment chroot:

#### enter the 13.x chroot before rebooting
# chroot /mnt /bin/sh
# uname -a
... 14.0-CURRENT FreeBSD 14.0-CURRENT main-n245063-d52cbc0be18c GENERIC  amd64

# freebsd-version -kru
13.0-BETA3
14.0-CURRENT
13.0-BETA3

# sysctl -a kern |egrep 'kern\.(osrel|ident|build)'
kern.osrelease: 14.0-CURRENT
kern.osreldate: 1400005
kern.build_id: 879b8aabcd91163a1a59a0c392bc4354c29dba11
kern.ident: GENERIC

pkg(8)

As a bonus, when we are using pkg(8), it’s also able to install to the filesystem, packages from different FreeBSD releases, versions, and architectures:

# pkg -o ABI=FreeBSD:13:$(uname -p) \
    --chroot ... \
    --config ... \
    install -r FreeBSD <tasty packages>

Which evaluates to amd64 as expected on my intel x86_64 architecture box, and to aarch64 on my arm64 behemoth.