2

I need to find out which kernel will be used for the next boot, regardless of current running kernel, and this is done in a shell script.

There could be multiple kernels installed, GRUB is configured to boot one, and this could change by kernel upgrade/downgrades. So the script needs to find out what will be the kernel for the next boot.

One approach is to parse the default kernel from GRUB configurations. The default GRUB menu entry can be detected, for example:

grep GRUB_DEFAULT /etc/default/grub | cut -d '=' -f 2

Then get the menu entries from GRUB config, like:

grep -A 15 '^menuentry' /boot/grub/grub.cfg  | grep -o -P 'vmlinuz-\S+'

and find the right menu entry by matching the index with the configured default one.

For my specific case there is no need to cover for GRUB saved entries, but would be nice if that was also covered.

Is there any better/cleaner way to get this information?

4
  • Presumably the guarantee lasts only until the script finishes, as grub could be updated any time?
    – Jeff Schaller
    Commented Dec 9, 2019 at 12:28
  • Yes of course. It needs to detect the kernel version to be booted, at the time the script started running. The next run may have a different output (if GRUB config was updated while the script ran).
    – farzad
    Commented Dec 9, 2019 at 12:46
  • Note that just parsing the config file might fail if grub was set up to boot the last kernel (GRUB_SAVE_DEFAULT=0, GRUB_DEFAULT=saved) Commented Dec 9, 2019 at 13:07
  • Thanks for the note @EduardoTrápani , as mentioned in the question covering saved defaults is not intended in my own case, but if a solution covers that as well, it could be helpful for someone else reading this question later, if any. :)
    – farzad
    Commented Dec 9, 2019 at 13:13

0

You must log in to answer this question.

Browse other questions tagged .