How can I automatically install the latest kernel?

I’ve been working on my post-install script and I want to include a functionality that automatically selects and installs the latest kernel. However, when I run the script, it displays an error message along with a list of available options instead of installing the latest kernel. Can anyone provide guidance on how I can modify my script to achieve this? Here’s the relevant code snippet:

install_latest_kernel() {
  echo "Do you want to install the latest kernel? (y/n)"
  read -r kernel_confirmation
  if [[ $kernel_confirmation =~ ^[Yy]$ ]]; then
    sudo mhwd-kernel -i linux-latest
  fi
}

I appreciate any assistance you can offer!

I guess you would have to parse the result of mhwd-kernel -l and select latest version from there (exclude the -rt lines first, sanitize to remove * and extra spaces, and select line with highest number). Your script can not work because linux-latest does not exist in Manjaro

1 Like

The error message you’re encountering is because mhwd-kernel does not recognize linux-latest as a valid kernel option. Instead, you need to determine the latest kernel version from the list of available kernels and then pass that version to the mhwd-kernel -i command.

Here’s a modified version of your install_latest_kernel() function that should achieve your desired functionality:

install_latest_kernel() {
  echo "Do you want to install the latest kernel? (y/n)"
  read -r kernel_confirmation
  if [[ $kernel_confirmation =~ ^[Yy]$ ]]; then
    # Get the list of available kernels
    available_kernels=$(mhwd-kernel -l | grep -oP 'linux\d+')

    # Find the latest kernel version
    latest_kernel=""
    for kernel in $available_kernels; do
      if [[ $kernel > $latest_kernel ]]; then
        latest_kernel=$kernel
      fi
    done

    # Install the latest kernel
    if [[ -n $latest_kernel ]]; then
      echo "Installing the latest kernel: $latest_kernel"
      sudo mhwd-kernel -i $latest_kernel
    else
      echo "Error: Could not find the latest kernel."
    fi
  fi
}

This modified function first retrieves the list of available kernels using mhwd-kernel -l and filters the output to get the kernel versions using grep. It then iterates through the available kernel versions to find the latest one. Finally, it installs the latest kernel using sudo mhwd-kernel -i $latest_kernel.

Citations:
[1] https://ostechnix.com/linux-kernel-utilities-scripts-compile-update-latest-linux-kernel-debian-derivatives/
[2] How to Boot into a Specific Kernel Version :: DigitalOcean Documentation
[3] https://unix.stackexchange.com/questions/556304/how-to-get-the-kernel-version-of-the-next-boot-from-a-script
[4] https://askubuntu.com/questions/119080/how-to-update-kernel-to-the-latest-mainline-version-without-any-distro-upgrade
[5] How to Complete a Kernel Update on Linux
[6] How to change default kernel in Ubuntu 22.04 | 20.04 LTS - Linux Shout

1 Like

There is at least one error in that script - this filter just strips the -rt part of the names of the realtime kernels so you end with a list like
linux64
linux61
linux64

But that should be the right way to do it. Just fix it not using AI :slight_smile:
I am not that good with grep sed awk etc. so i cannot help with the correct filtering.

1 Like

It works in bash but, as @Teo pointed out, the rt kernels will be included in available_kernels, without the -rt part.

This will include only normal kernels. The $ means end of line.

available_kernels=$(mhwd-kernel -l | grep -oP 'linux\d+$')

In zsh all the kernels will be lumped together in latest_kernel which will cause an error with mhwd-kernel.

2 Likes

why not get only the last ? last item in this list is the latest

last_version=$(mhwd-kernel -l | grep -Po '\d+$' | tail -1)

Note:
mhwd-kernel is not useful here (can change display tomorrow), it’s only a script bash

we can only use this line:
https://gitlab.manjaro.org/applications/mhwd/-/blob/master/scripts/mhwd-kernel#L57

2 Likes

So something like this.

install_latest_kernel() {
  echo "Do you want to install the latest kernel? (y/n)"
  read -r kernel_confirmation
  if [[ $kernel_confirmation =~ ^[Yy]$ ]]; then
    # Get the latest kernel
    latest_kernel=$(pacman -Ssq "^linux[0-9][0-9]?([0-9])$" | tail -1)

    # Install the latest kernel
    if [[ -n $latest_kernel ]]; then
      echo "Installing the latest kernel: $latest_kernel"
      sudo mhwd-kernel -i $latest_kernel
    else
      echo "Error: Could not find the latest kernel."
    fi
  fi
}

Which will also work in zsh.

2 Likes

why want zsh ? this script is not to incluse in .zshrc. and if we use zsh or fish, we add bash shebang in this script file.

Here, we can check version (is good to install RC ??? or minus version is < 2)

isrc=$(LAN=C pacman -Si linux64 | grep '^Ver' | grep -v "rc")
#OR
[[ $(LAN=C pacman -Si linux64 | grep '^Ver') =~ "rc" ]] && { 
   echo "want a RC version ?"; 
}

install_latest_kernel() {
  echo "Do you want to install the latest kernel? (Y/n)"
  read -r kernel_confirmation
  if [[ $kernel_confirmation =~ ^[Nn]$ ]]; then
    return
  fi

  # Get the latest kernel
  latest_kernel=$(mhwd-kernel -l | grep -Po 'linux\d+$' | tail -1)

  # Install the latest kernel
  if [[ -n $latest_kernel ]]; then
    echo "Installing the latest kernel: $latest_kernel"
    sudo mhwd-kernel -i $latest_kernel
  else
    echo "Error: Could not find the latest kernel."
  fi
}

It’s just a note.

It’s a function so you usually would put it in a .bashrc, or .zshrc or similar.

It would be my preference.

I have yet to understand why someone can blindly copy paste scripts from AI without critical thinking and without testing them. Even better, there is always some other guy, that marks it as a solution, again without checking if it works…
Dark times ahead, if that is the new generation script kiddies, future developers…

That said, let me fix it for you…because that filter leaves you with a number, and the kernels in manjaro are called linux99 and not just 99

install_latest_kernel() {
  echo "Do you want to install the latest kernel? (Y/n)"
  read -r kernel_confirmation
  if [[ $kernel_confirmation =~ ^[Nn]$ ]]; then
    return
  fi

  # Get the latest kernel
  latest_kernel=$(mhwd-kernel -l | grep -Po 'linux\d+$' | tail -1)

  # Install the latest kernel
  if [[ -n $latest_kernel ]]; then
    echo "Installing the latest kernel: $latest_kernel"
    sudo mhwd-kernel -i $latest_kernel
  else
    echo "Error: Could not find the latest kernel."
  fi
}

p.s. i did not check it all, just the grep line… maybe there are other bugs too.

3 Likes

It ~works … but doesnt have any syntax for comparing the latest versus installed … its just "if the variable latest exists then try to install latest’ with no care of whether its already installed

install_latest_kernel() {
  echo "Do you want to install the latest kernel? (Y/n)"
  read -r kernel_confirmation
  if [[ $kernel_confirmation =~ ^[Nn]$ ]]; then
    return
  fi

  # Get the latest kernel
  installed_kernel=$(pacman -Qqs linux[0-9] | grep -Po 'linux\d+$' | tail -1)
  latest_kernel=$(mhwd-kernel -l | grep -Po 'linux\d+$' | tail -1)

  # Install the latest kernel
  if [[ $latest_kernel > $installed_kernel ]]; then
    echo "Installing the latest kernel: $latest_kernel"
    sudo mhwd-kernel -i $latest_kernel
  else
    echo "Error: Could not find the latest kernel."
  fi
}

If you are set on this … maybe you want it to run periodically… or add zenity windows.

But … it might be worth mentioning that msm-notifier basically does this already.

3 Likes

not here, in first post, it’s a function in a script post-install

Ah.
Guess I missed that …
Well in that case … maybe it should do the check before even prompting the user?
(I also added a little bit more verbosity, etc)
ex:

install_latest_kernel() {

  # Get Current and Latest Kernel
  current_kernel=$(pacman -Qqs linux[0-9] | grep -Po 'linux\d+$' | tail -1)
  latest_kernel=$(mhwd-kernel -l | grep -Po 'linux\d+$' | tail -1)

  # If Latest is newer than Current continue
  if [[ $latest_kernel > $current_kernel ]]; then

    # Ask nicely
    echo "Current kernel : $current_kernel"
    echo "Latest kernel  : $latest_kernel"
    echo "Do you want to install the latest kernel? (Y/n)"
    read -r kernel_confirmation
    if [[ $kernel_confirmation =~ ^[Nn]$ ]]; then
      echo "User exited." && return
    fi

    # Do the installing
    echo "Installing kernel $latest_kernel"
    if sudo mhwd-kernel -i $latest_kernel; then 
      echo "Success"
    else
      echo "Error installing $latest_kernel"
    fi

  else
    echo "$current_kernel is the newest kernel."
  fi
}

image

Edit … fixed a typo :slight_smile:

4 Likes
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+$' | tail -1)

  if [[ $latest_kernel > $current_kernel ]]; then
    if ! sudo mhwd-kernel -i $latest_kernel; then
      echo "Error installing $latest_kernel"
    fi
  fi
}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.