How to programmatically determine the highest version kernel RPM installed?

What I want to script is something along the lines of:

if [ uname -r is not == highest version of kernel RPM installed ]
then
  echo "You need to reboot to use the latest kernel"
fi

The problem is, if the output of rpm -q kernel is something like:

kernel-2.6.32-358.10.2.el6.x86_64
kernel-2.6.32-358.6.1.el6.x86_64

… how do I determine which is higher? I know a simple string sort is not reliable (it will be backwards with this example). Is there a shortcut with rpm or do I have to parse out everything and compare it myself?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

TL;DR

The 3rd attempt actually works! I’m leaving the first 2 attempts so that others that may come across this Q&A in the future will hopefully gain some insight into how non-trivial a problem it is to parse RPM version information and determine the lineage of which came first, second, etc.

Attempt #1 (OP said didn’t work)

This command will sort the output and give you them in version order:

$ rpm -q kernel --queryformat "%{VERSION} %{RELEASE}n"|sort -n
2.6.18 238.12.1.el5
2.6.18 238.19.1.el5
2.6.18 274.12.1.el5
2.6.18 308.8.2.el5

WHY IT DIDN’T WORK: A naive person would think that you can use some variant of the sort command to perform this task, but there is enough variability and inconsistency in the formatting of the actual version information for a given RPM that it just isn’t up to the task.

Attempt #2 (OP said didn’t work)

$ rpm -q --last kernel | head -n 1 | cut -d' ' -f1
kernel-2.6.35.14-106.fc14

WHY IT DIDN’T WORK: I had high hopes that this approach would yield the results the OP was looking for, but the issue with this one as @Joel pointed out in the comments, is that the --last switch is merely returning the results sorted by the date the RPMs were installed.

Attempt #3

This one will definitely do the job. I found a suite of tools called RPM Development Tools. There are 2 tools in this suite that will give you the capability to determine whether one version of a RPM is newer or older than another.

If the RPM isn’t already installed you can do so as follows:

yum install rpmdevtools

The first tool that’s useful is called rpmdev-vercmp. This tool can compare 2 names of RPMs and tell you which one is newer. For example:

$ rpmdev-vercmp kernel-2.6.35.14-100.fc14.x86_64 kernel-2.6.35.14-103.fc14.x86_64
0:kernel-2.6.35.14-103.fc14.x86_64 is newer

After finding this I was all set to put together a shell script but then realized, man I’m lazy, so I poked a few minutes more and found another tool in the suite called rpmdev-sort.

This tool is pay-dirt. You can use it as follows:

$ rpm -q kernel | rpmdev-sort 
kernel-2.6.35.14-100.fc14.x86_64
kernel-2.6.35.14-103.fc14.x86_64
kernel-2.6.35.14-106.fc14.x86_64

There are a lot of tools in RPM Development Tools that might be worth a look for others so I’m listing them here for future reference.

$ rpm -q --queryformat '[%{NAME} %{FILEMODES:perms} %{FILENAMES}n]' rpmdevtools 
    | grep -E "^.* -..x..x..x " 
    | awk '{print $3}'          
    | sed 's#/usr/bin/##'       
    | paste - - -               
    | column -t

annotate-output   checkbashisms    licensecheck
manpage-alert     rpmargs          rpmdev-bumpspec
rpmdev-checksig   rpmdev-cksum     rpmdev-diff
rpmdev-extract    rpmdev-md5       rpmdev-newinit
rpmdev-newspec    rpmdev-packager  rpmdev-rmdevelrpms
rpmdev-setuptree  rpmdev-sha1      rpmdev-sha224
rpmdev-sha256     rpmdev-sha384    rpmdev-sha512
rpmdev-sort       rpmdev-sum       rpmdev-vercmp
rpmdev-wipetree   rpmelfsym        rpmfile
rpminfo           rpmls            rpmpeek
rpmsodiff         rpmsoname        spectool

An alternative to #3

An alternative that the OP mentioned in the comments is to use sort -V. That’s a capital -V. I’d never heard of this switch either. From the sort man page:

-V, --version-sort
       natural sort of (version) numbers within text

As it turns out sort does provide a facility to sort version numbers so you could also perform the sorting like so:

$ rpm -q kernel | sort -V
kernel-2.6.35.14-100.fc14.x86_64
kernel-2.6.35.14-103.fc14.x86_64
kernel-2.6.35.14-106.fc14.x86_64

Method 2

You really need to use the RPM library to get a good result. The version comparison algorithm is… decidedly complex. It’s non-trivial to reimplement in shell, but if you can use Python to do the actual comparison, it becomes relatively straightforward. See https://stackoverflow.com/questions/3206319/how-do-i-compare-rpm-versions-in-python for an exmaple of how to do so.

Method 3

rpm -q kernel --queryformat="%{buildtime}t%{name}-%{version}-%{release}.%{arch}n" | sort -nr | head -1 | cut -f2

I think sorting by buildtime is less likely to have a corner case where it fails, unlike installtime. However, –last is neater.

Method 4

Found this thread looking for the same thing but determine the latest kernel version in Ubuntu (16.04 LTS in my case) if it is not yet the current one.

Thanks to @slm this works beautifully for me:

ls /lib/modules | sort -V | tail --lines=1

4.15.0-161-generic

uname -r

4.15.0-159-generic

at the time of this writing.

Method 5

--last won’t tell you the highest version number but it will order by date of installation. So you can see the most recently installed version:

[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f684999982b68e9b85">[email protected]</a>_apps ~]# rpm -qa kernel-xen --last
kernel-xen-2.6.18-348.1.1.el5                 Tue 29 Jan 2013 02:18:52 PM EST
kernel-xen-2.6.18-308.11.1.el5                Fri 20 Jul 2012 04:00:26 PM EDT
kernel-xen-2.6.18-308.8.2.el5                 Wed 20 Jun 2012 03:32:47 PM EDT

Most of the time (unless they did a manual kernel install) the two should be the same.

To get it 100% right 100% of the time, you’re going to have to get the two kernel versions down to 2.6.* format then just break it apart starting after the 2.6 (RHEL won’t rebase that drastically within a single version, RHEL5 will always be a 2.6 kernel) and just iterate over each line of rpm output (possibly sorted by --last for performance) and compare each position against the analogous position in the kernel version you got from uname -r if any of the numbers are bigger in the rpm db’s string than the uname string, immediately exit with that message.

To help you out there there’s a similar question asked here. But that function assumes purely dotted decimal notation, so you may compare number position before the hyphen (since there’s only one) then use that person’s bash function to test if the patch version is higher in uname than in the rpm db string.

Method 6

[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="74061b1b00341d045945434659474559474259454740">[email protected]</a> ~]# whatis grub2-rpm-sort
grub2-rpm-sort (8)   - Sort input according to RPM version compare.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a4855554e7a534a170b0d0817090b17090c170b090e">[email protected]</a> ~]# rpm -qf `which grub2-rpm-sort`
grub2-tools


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x