# Building Linux Kernel on CentOS 7 ## Host Configuration - CPU: x86_64, 8 core - Memory: 32GB - Storage: 40GB ```terminal $ uname -r 3.10.0-1127.el7.x86_64 $ cat /etc/redhat-release CentOS Linux release 7.8.2003 (Core) ``` ## Prerequirements ### Check free space on the system It will consume over 17GB of storage area on the system while building the kernel with default configuration. It would be best if you had enough storage area to work. ### Update OS Update operating system and its base components. ```terminal $ sudo yum update -y $ sudo yum upgrade -y ``` OS status after update. ```terminal $ uname -r 3.10.0-1127.el7.x86_64 $ cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core) ``` To keep the session active while the connection between server and terminal is lost. ```terminal $ sudo yum -y install screen ``` Activate [screen](https://linuxize.com/post/how-to-use-linux-screen/) ``` $ screen ``` ### Install essential tools for development. Install required library and compilers. ```terminal $ sudo yum install -y ncurses-devel openssl-devel elfutils-libelf-devel flex bison perl $ sudo yum install -y centos-release-scl $ sudo yum install -y devtoolset-9-gcc ``` Activate installed devtoolset and check the compiler version. ```terminal $ scl enable devtoolset-9 bash $ gcc --version gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2) Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ``` If you want the `devtoolset-9` as a default, please put follwing line on your `.bashrc` ``` source scl_source enable devtoolset-9 ``` ## Retrieve kernel source ```terminal $ curl -LO https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.26.tar.xz ``` ```terminal $ tar -Jxf linux-5.10.26.tar.xz $ cd linux-5.10.26 ``` To create `.config` file to build kernel. Simply run `make menuconfig` and `save` `.config` file as a default. ```terminal $ make menuconfig ``` ## Build linux kernel To speed up the building process, we want to check how many CPUs in this system. ~~cat /proc/cpuinfo | grep 'processor' | wc -l~~ ```terminal $ nproc 8 ``` So, we have 8 CPUs here. Let `make` work eight jobs in parallel. ```terminal $ make -j8 ``` It'll take a significant of time, so let's have a tea break! ## Install modules, kernel, and reboot Install the newly build modules and kernel. You have to submit `make modules-install` at first. Otherwise, the system will complain the module directory doesn't exist. ```terminal $ sudo make modules_install $ sudo make install ``` Then we need to modify boot menu with `grub2-mkconfig` command. ``` $ sudo grub2-mkconfig -o /boot/grub2/grub.cfg ``` ## Reboot your system ``` $ sudo shutdown -r now ``` Now you could select a new kernel on the Grub's boot menu. ## Check kernel version. After selecting a new kernel and start the system, you could see the following lines on the display. ``` CentOS Linux 7 (Core) Kernel 5.10.26 on an x86_64 ``` Enjoy!!