There was an error in a previous script that I posted here but I couldn’t edit it. Here’s a revised version of the function that installs the latest kernel. To properly install the latest kernel in a post-install script, you can use the following function:
install_latest_kernel() {
current_kernel=$(pacman -Qqs linux[0-9] | grep -Po 'linux\d+$' | tail -1)
latest_kernel=$(mhwd-kernel -l | grep -Po 'linux\d+$' | sort -V | tail -1)
if [[ $latest_kernel > $current_kernel ]]; then
echo "Current kernel: $current_kernel"
echo "Latest kernel: $latest_kernel"
echo "Installing the latest kernel..."
if sudo mhwd-kernel -i $latest_kernel; then
echo "Successfully installed $latest_kernel"
else
echo "Error installing $latest_kernel"
fi
else
echo "$current_kernel is already the latest kernel"
fi
}
This function automatically installs the latest kernel if a newer version is available, without prompting for user confirmation. It performs the following steps:
Determines the current kernel version.
Finds the latest available kernel using mhwd-kernel -l, grep, sort, and tail.
Compares the latest kernel with the current one.
If a newer kernel is available, it attempts to install it using sudo mhwd-kernel -i.
Provides feedback on the installation process, whether successful or not.
The sort -V command is added to ensure proper version sorting of kernel numbers.
I still have to figure out how to remove the previous one and use the new one.
This has a typo (missing ', which seems to have been replaced by a `).
It also would not be very effective at presenting the current kernel after fixing.
In my case it prints linux66 when I am running linux612
You might consider using something like uname to actually print current running kernel, use mhwd-kernel, or revise your approach to something like the following should print the latest installed;
Not sure about the accuracy of the current_kernel line of your script. I’ve been running Linux 612 since it was release candidate several weeks ago, but that line of the script says I am running the LTS kernel that I also have installed on my system:
Regarding automatically removing the currently running kernel, I don’t think that can be done via mhwd (to prevent users from accidentally removing their kernel when they have no other kernels installed). Maybe you could have the script create a systemd job to run at next boot and remove the old kernel?
mhwd-kernel also has the rmc option (secondary option for -i) that would remove the current kernel … though I dont know how it actually does that or if it is advisable.
I imagine that kernel-modules-hook would also need to be installed to keep the uninstalled but currently active kernel running until the next reboot. Otherwise, as the script automatically installs the new kernel without user confirmation, if a user isn’t aware that the script has removed their running kernel during an update, they may encounter issues if they continue to use their system without rebooting.