5

I have a scenario where I need to recompile my display drivers after kernel updates. I'm trying to automate it with Bash scripts during boot time. So I need a way to know that my kernel changed, and reinstall my drivers for the new running kernel.

I was thinking of when first installing my drivers output the kernel version to a file and in my script always checking if this file content is different than what is now installed.

Is this the right way? I would appreciate any suggestion on how to know if the kernel changed since I last installed my drivers.

  • I know there is DKMS and that is exactly what is for but it's not always working so I want to do this in a different way.
Toby Speight
  • 8,460
  • 3
  • 26
  • 50
Asaf Magen
  • 487
  • 1
  • 9
  • 21
  • What distribution are you running? On my PC I know that kernel has changed, because I've just compiled it. – MatthewRock Dec 15 '15 at 10:51
  • 2
    How is DKMS failing for you? That really is the safest approach for this kind of problem... – Stephen Kitt Dec 15 '15 at 10:51
  • i'm managing a lot of distributions ( SLES RHEL UBUNTU DEBIAN) so on each OS DKMS might not work out of the box. the changes happen when cron task is running full OS updates and reboot the os. – Asaf Magen Dec 15 '15 at 10:54

4 Answers4

0

you can simply run uname -mrs to see if the version has been changed, but you can also dpkg --list | grep linux-image to check the list of all installed kernels.

Dhia
  • 148
  • 7
0

automate it with bash scripts during boot time

automate...yes, but not during boot time? After a kernel update you have to reboot. And before, shutdown. So you should know when it happens. Or am I missing something. Have that script ready to make that switch (compilation) with one command, but not while a new kernel is booting for the first time.

Just like modules have to be taken care of (by make direct, by the distro from where you get also a new initrd ), if you have something as kernel version specific as your drivers, this belongs on a checklist and not in a bootscript. Otherwise too much risk of n.a.i.w. (= not always it works :-)

uname -r was mentioned...but to store that in a file and check it?

The place to put information and to do the kernelversion - driver control could be a boot script. Over the months it could look like

# 4.1.0 dispdr=firstone
# 4.2.0 dispdr=second_driver
# 4.4.0  
dispdr=third 
# next version probably 4.6.0

This is transparent and reversible. I know nothing about these drivers, obviously.

0

As for general question: "how to do something in case of kernel changes" I'm agree with you and I will also do something like:

program=my_system_version_checker
state_file=/var/lib/${program}/version

if [ -r "$state_file" ]; then
  last_version=$( cat "$state_file" )
else
  last_version="unknown"
fi

current_version=$(uname -r) # or may be uname -a or anything else you'd like
if [ $? -ne 0 ]; then
  exit 1 # Handle case when we can't get current_version for some reason
fi
if [ "$last_version" != "${current_version}"]; then
  make LAST_VERSION="$last_version" CURRENT_VERSION=${current_version} -C /opt/${program} update
fi

[ -d $(dirname $state_file) ] || mkdir -p $(dirname $state_file)
echo "$current_version" > "$state_file"

and put it on startup scripts to run on every boot (with systemd unit file or upstart or anything else depend on your system and startup manager).

But as for rebuilding kernel modules on such way I don't think it a good idea as you could break something with new drivers and you will know about it on next boot or you could add rmmod/insmod logic in your script which also may be not safe to run. So I would better use some hooks similar to DPkg::Post-Invoke for apt to run my script after kernel updates.

Fedor Dikarev
  • 1,761
  • 8
  • 13
0

I am comparing the files in /boot with uname -r here. It's has been working fine for me for several months:

#!/bin/bash

cd /boot || exit 1
shopt -s nullglob ; for file in config-* ; do kernels+=( "${file#config-}" ) ; done
newest="$(printf '%s\n' "${kernels[@]}" | sort -V -t - -k 1,2 | tail -n1)"
current="$(uname -r)"
[[ $current != $newest ]] && echo "Reboot needed for new kernel"

I've tested it on Debian and RHEL-based distros.

paradroid
  • 1,159
  • 1
  • 13
  • 28