0

I recently applied a kernel patch (mix of Debian 10 and Ubuntu 18.04) using Ansible. I am trying to figure out a way to check if each machine being updated has been, or needs, a reboot. I've read that test -f /var/run/reboot-required is the official indicator however I've noticed this is not always true. Sometimes the file is there after a kernel upgrade, sometimes not. I'm not using LivePatch so any time there is a kernel update, I need to schedule reboots of all these systems.

In ansible, it was straightforward enough to test for the existence of the file above but since that's not consistent, I came up with the bash code below to test the running kernel version against the newest kernel image laying under /boot. This isn't exactly something you can plug right into ansible without a lot of hammering.

Is there a better way to determine if a reboot is needed?

if [[ $(uname -r) != .*"$(ls -1 /boot/vmlinuz* | sort -V | tail -1)"*. ]]; then
   echo "Needs reboot. Running kernel $(uname -r) is older than $(ls -1 /boot/vmlinuz* | sort -u | tail -1)"
fi
7
  • 3
    The "reboot required" flag files are actually /var/run/reboot*. There can be more than one Commented Feb 8, 2022 at 13:19
  • Strange. find /var/run | grep reboot comes up empty. The running kernel on one system is 4.15.0-166-generic and the latest is /boot/vmlinuz-4.15.0-167-generic. I've never known a kernel patch that didn't require a reboot so something is amiss with documentation or the system (/var/ is only 18% capacity) Commented Feb 8, 2022 at 14:39
  • @waltinator Provides the correct path towards the ideal solution. Complicated ansible tasks to compare running versus installed kernel versions may bring you to the answer you are looking for, but the system already has built-in and standardized mechanisms to check for this condition. Relying on those makes for better portability of your code. Commented Feb 11, 2022 at 4:19
  • @0xSheepdog I see their comment. Do you not understand find /var/run | grep reboot? Commented Feb 11, 2022 at 9:57
  • 1
    This question has been asked on askubuntu.com: askubuntu.com/questions/164/… Commented Sep 17, 2024 at 19:55

2 Answers 2

1

Here is what my solution, in case it's of use to anyone else.

---
- hosts: all

  vars:
    kernel_version: "{{ ansible_kernel }}"

  tasks:

    # sort -V specifically sorts software versions :)
    - name: find newest kernel under /boot
      shell   : ls -1 /boot/vmlinuz* | sort -V | tail -1
      register: latest_kernel

    # running kernel does not match latest kernel - can't do <> comparison since text is involved
    - name: test kernel versions different
      when: kernel_version not in latest_kernel.stdout
      debug:
        msg: "WARNING: needs reboot - running kernel {{ kernel_version }} was not found in variable {{ latest_kernel.stdout }}"
1
  • What programming language is this? Commented Aug 22 at 18:33
-1

My solution is the OP's code with some additional filtering since /boot/vmlinuz.old is always present and to remove /boot/vmlinuz- so the conditional matching is correct:

if [[ $(uname --kernel-release) != "$(ls -1 /boot/vmlinuz-* | sort --version-sort | tail -1 | sed 's/\/boot\/vmlinuz-//g')" ]]; then
  echo "Needs reboot: Running kernel $(uname --kernel-release) is older than $(ls -1 /boot/vmlinuz-* | sort --unique | tail -1 | sed 's/\/boot\/vmlinuz-//g')"
fi

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.