- Category: Linux
- Published: Wednesday, 02 September 2015 11:39
- Written by Super User
- Hits: 39
How to Compile Linux Kernel from Source to Build Custom Kernel
Linux kernel is the life force of all Linux family of operating systems including Ubuntu, CentOS, and Fedora.
For most part, you don?t need to compile the kernel, as it is installed by default when you install the OS. Also, when there is a critical update done to the kernel, you can use yum, or apt-get to update the kernel on your Linux system.
However you might encounter certain situation, where you may have to compile kernel from source. The following are few situation where you may have to compile Kernel on your Linux system.
- To enable experimental features that are not part of the default kernel.
- To enable support for a new hardware that is not currently supported by the default kernel.
- To debug the kernel
- Or, just to learn how kernel works, you might want to explore the kernel source code, and compile it on your own.
In this tutorial, we?ll explain how to compile Linux kernel from source.
Also, please note that if you just want to compile a driver, you don?t need to compile the kernel. You need only the linux-headers package of the kernel.
1. Download the Latest Stable Kernel
The first step is to download the latest stable kernel from kernel.org.
# cd /usr/src/ # wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.9.3.tar.xz
2. Untar the Kernel Source
The second step is to untar the kernel source file for compilation.
# tar -xvJf linux-3.9.3.tar.xz
3. Configure the Kernel
The kernel contains nearly 3000 configuration options. To make the kernel used by most people on most hardware, the Linux distro like Ubuntu, Fedora, Debian, RedHat, CentOS, etc, will generally include support for most common hardware. You can take any one of configuration from the distro, and on top of that you can add your own configuration, or you can configure the kernel from scratch, or you can use the default config provided by the kernel.
# cd linux-3.9.3 # make menuconfig
The make menuconfig, will launch a text-based user interface with default configuration options as shown in the figure. You should have installed ?libncurses and libncurses-devel? packages for this command to work.
We will use the default config provided by the kernel. So select ?Save? and save the config in the file name ?.config?.
The following is a sample of the ?.config? file:
CONFIG_MMU=y CONFIG_NEED_DMA_MAP_STATE=y CONFIG_NEED_SG_DMA_LENGTH=y CONFIG_GENERIC_ISA_DMA=y CONFIG_GENERIC_BUG=y CONFIG_GENERIC_HWEIGHT=y
4. Compile the Linux Kernel
Compile the main kernel:
Compile the kernel modules:
# make modules
Install the kernel modules:
# make modules_install
At this point, you should see a directory named /lib/modules/3.9.3/ in your system.
5. Install the New Kernel
Install the new kernel on the system:
# make install
The make install command will create the following files in the /boot directory.
- vmlinuz-3.9.3 ? The actual kernel
- System.map-3.9.3 ? The symbols exported by the kernel
- initrd.img-3.9.3 ? initrd image is temporary root file system used during boot process
- config-3.9.3 ? The kernel configuration file
The command ?make install? will also update the grub.cfg by default. So we don?t need to manually edit the grub.cfg file.
6. Boot Linux to the new Kernel
To use the new kernel that you just compiled, reboot the system.
Since, in grub.cfg, the new kernel is added as default boot, the system will boot from the new kernel. Just in case if you have problems with the new kernel, you can select the old kernel from the grub menu during boot and you can use your system as usual.
Once the system is up, use uname command to verify that the new version of Linux kernel is installed.
$ uname -r 3.9.3