Change the kernel for Linux
This post records the procedure to change the kernel for Linux. The main processes are downloading source, configuring, building, installing and booting.
1. Install a default kernel
1.1 Download the source code
You can find the required version of Linux kernel from kernel/git/stable/linux.git - Linux kernel stable tree. And we could clone it, we can specify the version:
1 | git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git --depth 1 --branch v6.1.4 |
1.2 Configuration
There are different ways to do this configuration:
1 | make config # The most basic traditional configuration interface |
No matter which kind of methods you choose to do the configure, they are just used to generate the .config file for the following building. I choose the .make menuconfig, it will look like:
There will have three choices for each function list in the above menu:
built-in:
excluded:
module:
Users can customize the kernel on demand. But now we do not change anything, we’ll install a default kernel. Just exiting the configure interface.
1.3 Build the kernel and modules
- build the kernel
1 | make -j 4 # Allocate more resources to speed up this process |
This process is really slow, it may costs a pretty long time. So you could use the nohup or related instructions to avoid the interruption of process.
You may meet some errors related to missing of dependency, and you must solve them at first.
Besides, you may meet the problem like:
They could be solved through Compiling the kernel 5.11.11 - Ask Ubuntu:
1 | scripts/config --disable SYSTEM_TRUSTED_KEYS |
- build the modules
1 | make modules_install |
- install the new kernel
1 | make install |
The installed kernel image could be find in ./arch/x86/boot/.
1.4 Boot into new kernel
The new kernel has been installed, but there are still some processes to boot into this new kernel. The bootloader should be seted up before rebooting.
The file /boot/grub/grub.cfg will decider the order of OS booting, but it’s just a automatically generated file. Just changing the settings file /etc/default/grub, and using the instruction to regenerate the grub.cfg file.
The changed settings in /etc/default/grub, GRUB_DEFAULT will determine the default kernel:
Then, using the following instruction to regenerate grub.cfg:
1 | sudo update-grub |
1.5 Reboot
1 | sudo reboot |
After rebooting, checking the version of our kernel:
1 | uname -a |
2. The method to deal with crash
It’s not unusual to build a kernel which can not be started normally, especially for the beginner. If you try to install it at your physical machine directly, it may cause crash. So it’s recommended to try your customised kernel in the virtual machine Oracle VM VirtualBox or QEMU.
And for the headless Virtual Box, you can set it as Using the headless Virtual Box | HDY blog (hudongyue.com).