5

"uname" will tell me the version of the running kernel. How do I know if this is different from the installed kernel (i.e. the one that will be loaded on the next reboot)?

Ideally I'd like to write a script that can output "reboot needed" or "running and installed kernel match".

TomOnTime
  • 8,131

2 Answers2

3

Use freebsd-version -k! From its manual:

-k Print the version and patch level of the installed kernel. Unlike uname(1), if a new kernel has been installed but the system has not yet rebooted, freebsd-version will print the version and patch level of the new kernel.

You should compare output of freebsd-version -k and uname -r and if differs you should reboot.

Another option is to use strings (idea taken from /bin/freebsd-version):

strings /boot/kernel/kernel | sed -n "s,^@(#)FreeBSD \([^ ]*\).*,\1,p"
uzsolt
  • 286
  • freebsd-version -k shows only release name and patch level. It works for binary updates, but if system updated from source it is not enough. E. g. you can update OS several times, but freebsd-version -k will show the same value 11.0-STABLE – citrin Dec 08 '16 at 19:39
0

Simple, but probably not reliable way: compare boot time (from sysctl kern.boottime) and time when kernel was installed (stat -f '%c' $(sysctl -n kern.bootfile)). Time should be always synced (via ntp) for this method to work.

Better way is to compare version strings from running kernel and installed kernel file.

From running kernel it can be obtained via sysctl -n kern.version | head -1 (this string is also shown in uname -v but in one line instead two lines). Then you can fgrep this string in the kernel file (kern.bootfile) - if it found - running and installed kernel version should match. I don't like using grep here, but it should work good enough. More correct way of extracting version string from kernel file would be some program to extract version variable from kernel ELF data (e. g. using libelf).

citrin
  • 469
  • 2
  • 5