Archive for October 2nd, 2017

Change Linux kernel boot order in Grub2 – Set older kernel as a default to load howto

Monday, October 2nd, 2017

grub-boot-loader-change-boot-order-linux

1. SHORT INTRO

So here is a good question any GNU / Linux Desktop and even sysadmins face at a time

 

How to Change Linux kernel boot order on GNU / Linux OS with Grub2 boot loader?


For those who doesn't know what I'm talking let me shortly explain what Grub2 is? Well its a boot loader, it is installed in the MBR (Master Boot Record) of the computer, that is few bits, the size of Master Boot Record is (512 Bytes), allocated on the beginning of storage (Hard Disk), usually installed on the Primary but sometimes on Extended – also famous as Logical Partitions. The meaning of MBR is to contain a very small program that loads itself into the memory and instructs the CPU how to proceed in Loading an installed Operating System, nomatter the type of OS: Windows has its Boot Loader, DOS has its boot loader, FreeBSD and other BSD OSes have its boot loader etc.

The boot loader allows you to have multiple operating systems configured as a boot option for a PC.
Grub boot loader for example allows booting up mostly all generally in use operating systems (Windows / Linux / BSD / DOS) and so on.

Because of some historical reasons the kind of standard for most GNU / Linux OS distributions is GRUB.
Those younger remember we first used Grub2 and using /boot/grub/grub.conf was a very nifty way to configure the list of bootable operating system for the PC and to be honest I preferred even LILO (LInux LOader – the predecessor standardly used to grub) as a boot loader for Linux, but with time because of its simplicity and lack of enough interactivity and hardship to recover on errors LILO was changed with GRUB and at a certain point GRUB BL on itself substituted with GRUB2.
 

 

2. Change the Linux kernel boot order in GRUB2 to make an older kernel version boot

GRUB2's config /boot/grub/grub.cfg is really complicated to understand and edit and developers of GRUB2 did not recommend editting it, in my experience so far also,  I never had the courage to directly edit it.

grub.cfg is generated usually using grub2-mkconfig command like so:

grub2-mkconfig -o /boot/grub2/grub.cfg


On newer computer running the terrible UEFI (Unified Extensible Firmware Interface) the command is a bit defferant.
 

grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg


In case you wonder what is UEFI – it is a replacement for BIOS (Basic Input Output System) I really hope you don't own computer with it to stay secure as UEFI allows remote diagnostics and repair of computers or in other words has a kinda of spyware in itself, because most likely agencies such as US's NSA can easily penetrate in it having the right default codes from UEFI software programmers / vendors.

Sometimes installing the newest provided distribution kernel is not the best option. Especially on older Laptop Hardware like for example my Old Lenovo Thinkpad r61
just after an upgrade to Debian Strecth 9 from Wheezy 7, the latest provided kernel with Stretch distro – Linux 4.9.0-3-amd64 performs much worser when compared to the older kernel Linux 3.16.0-4-amd64.

So the best thing to do in that case if you are in situation like mine where older kernel is preferrable option for the OS and you'd like to modify GRUB2 BL to load it instead of the default latest distro proided, here is how to achieve it.

After  way to make general changes to GRUB2 BL is to edit /etc/default/grub file.

The file on a Debian Strech 9.1 Linux looks like so:

linux-desktop:~# cat /etc/default/grub
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT="0"
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="text"
GRUB_CMDLINE_LINUX=""

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD …)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640×480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"


http://pc-freak.net/images/grub-boot-loader-change-boot-order-linux-3

So by default the kernel that loads is:

/boot/vmlinuz-4.9.0-3-amd64

and the Advanced Options for Debian GNU / Linux Grub2 Menu gives me options to load – /boot/vmlinuz-4.9.0-3-amd64 Kernel in Recovery mode or Load:

/boot/vmlinuz-3.16.0-4-amd64 in Normal or Recovery mode as

The quesiton is how to change the default boot loader to load the older Linux kernel 3.x.x (vmlinuz-3.16.0-4-amd64) instead of the newer and apparently more bloated for old PCs kernel/boot/vmlinuz-4.9.0-3-amd64

Notice in above /etc/default/grub config variable :

GRUB_DEFAULT="0"

That instructs grub loader to Load the first in line kernel (note that just like in C and other language programming counting starts not from 1 but from 0, so null value means to load 1 listed kernel.

So what all that means on Grub Boot Loader, we have 2 values possible to set for GRUB_DEFAULT (either 0 – the fist kernel in order or value of 1 the second which in that case is Advanced)

 

As you can see from above screenshot the Kernel, I would like to add as a default (Debian GNU / Linux with 3.16.0-4-amd64) instead of the 4.X.X kernel is at 4th position.

grub-boot-loader-change-boot-order-linux-2

So what do you think should we set in /etc/default/grub for GRUB_DEFAULT?

Well lets count, we have the default loaded kernel Debian / GNU Linux that would be 1 (or in terms of config 0), then we have (see above screenshot)

1. Debian / GNU Linux with Linux 4.9.0-3-amd64
2. Debian / GNU Linux with Linux 4.9.0-3-amd64 (SysVinit)
3. Debian / GNU Linux with Linux 4.9.0-3-amd64 (Recovery Mode)
4. Debian GNU / Linux with 3.16.0-4-amd64
5. Debian GNU / Linux with 3.16.0-4-amd64 (SysVinit)
6. Debian GNU / Linux with 3.16.0-4-amd64 (Recovery Mode)

We need to switch back to old kernel at position
– 4. Debian GNU / Linux with 3.16.0-4-amd64

But actually in terms of GRUB_DEFAULT understanding that would be 3 because (don't forget we count starting from 0).

So what we need to configure is Grub Menu 1 again for clarity I includ the grub screenshot (
0 – Debian GNU / Linux,
1. Advanced Options for Debian Gnu / linux):

grub-boot-loader-change-boot-order-linux

So we need 1 (from general menu) and 3 under the 1 submenu the way to tell that to grub is as shown below:

GRUB_DEFAULT="1>3"

 

Finally to make the changes take effect you'll need to execute grub-grub wrapper script, which by itself would run this:
 

#!/bin/sh
set -e
exec grub-mkconfig -o /boot/grub/grub.cfg "$@"

 


You will not see any different result from the regular, however on next boot the correct kernel would boot.
 

root@noah:~# update-grub
Generating grub configuration file …
Found background image: /usr/share/images/desktop-base/desktop-grub.png
Found linux image: /boot/vmlinuz-4.9.0-3-amd64
Found initrd image: /boot/initrd.img-4.9.0-3-amd64
Found linux image: /boot/vmlinuz-3.16.0-4-amd64
Found initrd image: /boot/initrd.img-3.16.0-4-amd64
done

For the lazy ones, that need to change the kernel order not on a Server Machine but home or on a friend's laptop  there is also another way to easy-to-use Modify GRUB loader tool in GUI – Grub Customizer.

Grub Customizer can be installed on Debian Jessie 8, directly with apt unfortunately on Debian Stretch 9 it is not avialable anymore in standard package repositories so on it and other earlier Debian releases the easy way to do it is to follow the usual compilation from source code.

To be able to compile before hand on Debian Linux you need to make sure the following libraries and build tools as well as cmake are installed

 

root@noah:~# apt-get install build-essential cmake libgtkmm-3.0-dev libssl-dev gettext libarchive-dev

 
As of time of writting this post grub-customiezr is at version 5.0.6, this might change in future so just change the version number in below untar and compile cmds:
 

tar zxfv grub-customizer-5.0.6
cd grub-customizer-5.0.6
cmake . && make -j3
make install

change-kernel-boot-order-grub-debian-stretch-linux-9-screenshot

 


Ubuntu Users can enjoy and install Grub Customizer by simply running below 4 cmds:

 

ubuntu:~# sudo add-apt-repository
ubuntu:~# ppa:danielrichter2007/grub-customizer
ubuntu:~# sudo apt-get update
ubuntu:~# sudo apt-get install –yes grub-customizer

There is another way to actually do force the system to use the second old kernel too, though this is not recommended as it might break your package depedencies, but as a last resort if you're in a hurry and you can afford the risk you can do it to BUT BEWARE AND DO IT AT YOUR OWN and don't blame me if it doesn't work afterwards:
 

apt-get remove linux-image-4.9.0-3-amd64

 

 

Share this on