Compile Linux kernel on CentOS

Build preparations

To perform a successful kernel build, the following packages need to be installed:

  • yum groupinstall “Development Tools”
  • yum install ncurses-devel
  • yum install qt3-devel (This is only necessary if you wish to use “make xconfig” instead of “make gconfig” or “make menuconfig”.) In the following example, we use “make menuconfig”.
  • yum install hmaccalc zlib-devel binutils-devel elfutils-libelf-devel

Download kernel source

Download kernel source from The Linux kernel archives.

[root@host1 ~]# mkdir ~/rpmbuild/
[root@host1 ~]# cd ~/rpmbuild/
[root@host1 rpmbuild]# pwd
/root/rpmbuild

[root@host1 rpmbuild]# wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.18.12.tar.xz
[root@host1 rpmbuild]# tar xvf linux-5.18.12.tar.xz
[root@host1 rpmbuild]# cd linux-5.18.12

Configure the new kernel

Note: If you do not intend to modify the distributed kernel configuration file, you may omit this section.

Copy the config file from the current running kernel to the new kernel source directory:

[root@host1 linux-5.18.12]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)

[root@host1 linux-5.18.12]# uname -r
5.18.10-1.el7.elrepo.x86_64

[root@host1 linux-5.18.12]# cp /boot/config-5.18.10-1.el7.elrepo.x86_64 .config

Modify the kernel config with the “make menuconfig” command:

[root@host1 linux-5.18.12]# make menuconfig
  HOSTCC  scripts/basic/fixdep
  UPD     scripts/kconfig/mconf-cfg
  HOSTCC  scripts/kconfig/mconf.o
  HOSTCC  scripts/kconfig/lxdialog/checklist.o
  HOSTCC  scripts/kconfig/lxdialog/inputbox.o
  HOSTCC  scripts/kconfig/lxdialog/menubox.o
  HOSTCC  scripts/kconfig/lxdialog/textbox.o
  HOSTCC  scripts/kconfig/lxdialog/util.o
  HOSTCC  scripts/kconfig/lxdialog/yesno.o
  HOSTCC  scripts/kconfig/confdata.o
  HOSTCC  scripts/kconfig/expr.o
  LEX     scripts/kconfig/lexer.lex.c
  YACC    scripts/kconfig/parser.tab.[ch]
  HOSTCC  scripts/kconfig/lexer.lex.o
  HOSTCC  scripts/kconfig/menu.o
  HOSTCC  scripts/kconfig/parser.tab.o
  HOSTCC  scripts/kconfig/preprocess.o
  HOSTCC  scripts/kconfig/symbol.o
  HOSTCC  scripts/kconfig/util.o
  HOSTLD  scripts/kconfig/mconf


*** End of the configuration.
*** Execute 'make' to start the build or try 'make help'.

The following window should be seen. You can enable or disable certain kernel features as needed. Here we just leave everything the defaults.

Image

Once it’s configured, go to “< Save >” and press “< Ok >” to save it.

Image

Now we can open the file “.config” to verify the content based on the previous modification.

[root@host1 linux-5.18.12]# head .config
#
# Automatically generated file; DO NOT EDIT.
# Linux/x86 5.18.12 Kernel Configuration
#
CONFIG_CC_VERSION_TEXT="gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)"
CONFIG_CC_IS_GCC=y
CONFIG_GCC_VERSION=70301
CONFIG_CLANG_VERSION=0
CONFIG_AS_IS_GNU=y
CONFIG_AS_VERSION=22800

Install multiple gcc versions

Depending on the gcc version included in the system, you may encounter errors when to run “make menuconfig” which requires a higher version of gcc.

[root@host1 linux-5.18.12]# make menuconfig
<...>
***
*** Compiler is too old.
***   Your GCC version:    4.8.5
***   Minimum GCC version: 5.1.0
***
scripts/Kconfig.include:44: Sorry, this compiler is not supported.

Software Collections, also known as SCL is a community project that allows you to build, install, and use multiple versions of software on the same system, without affecting system default packages. By enabling Software Collections, you gain access to the newer versions of programming languages and services which are not available in the core repositories. The SCL repositories provide a package named Developer Toolset, which includes newer versions of the GNU Compiler Collection, and other development and debugging tools.

To check the already installed gcc version:

[root@host1 linux-5.18.12]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 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.

To install the newer version of gcc:

[root@host1 linux-5.18.12]# sudo yum install centos-release-scl
[root@host1 linux-5.18.12]# sudo yum install devtoolset-7

To access gcc version 7, a new shell instance using the Software Collection scl tool needs to be launched.

[root@host1 linux-5.18.12]# scl enable devtoolset-7 bash
149 packages can be updated.
0 updates are security updates.

[root@host1 linux-5.18.12]# gcc --version
gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)
Copyright (C) 2017 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.

[root@host1 linux-5.18.12]# make menuconfig

For more detail, please refer to Developer Toolset 7 or a different version.

Compile the kernel

Before start compiling the new kernel, make sure more than 20GB of free space on the filesystem is available.

[root@host1 linux-5.18.12]# df -h | egrep "Filesystem|root"
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/vgroot-lvroot  1.5T   21G  1.4T   2% /

Now, we can compile the kernel. We run the compilation process in the background since it takes long time.

[root@host1 linux-5.18.12]# nohup make rpm-pkg &
[root@host1 linux-5.18.12]# tail -f nohup.out
[root@host1 linux-5.18.12]# more nohup.out
  SYNC    include/config/auto.conf.cmd
  HOSTCC  scripts/kconfig/conf.o
  HOSTLD  scripts/kconfig/conf
  UPD     include/config/kernel.release
make clean
sh ./scripts/package/mkspec >./kernel.spec
  TAR     kernel-5.18.12.tar.gz
rpmbuild  --target x86_64 -ta kernel-5.18.12.tar.gz \
--define='_smp_mflags %{nil}'
Building target platforms: x86_64
Building for target x86_64
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.nOAadM

Note that the kernel.spec file is created automatically by “sh ./scripts/package/mkspec >./kernel.spec”.

Upon completion, the following output shows the location of the generated rpms.

[root@host1 linux-5.18.12]# tail -15 nohup.out
Obsoletes: kernel-headers
Processing files: kernel-devel-5.18.12-1.x86_64
Provides: kernel-devel = 5.18.12-1 kernel-devel(x86-64) = 5.18.12-1
Requires(rpmlib): rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 rpmlib(CompressedFileNames) <= 3.0.4-1
Checking for unpackaged file(s): /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/kernel-5.18.12-1.x86_64
Wrote: /root/rpmbuild/SRPMS/kernel-5.18.12-1.src.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/kernel-5.18.12-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/kernel-headers-5.18.12-1.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/kernel-devel-5.18.12-1.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.DiAd0f
+ umask 022
+ cd /root/rpmbuild/BUILD
+ cd kernel-5.18.12
+ rm -rf /root/rpmbuild/BUILDROOT/kernel-5.18.12-1.x86_64
+ exit 0

To check the generated kernel rpms:

[root@host1 rpmbuild]# pwd
/root/rpmbuild

[root@host1 rpmbuild]# ls RPMS/x86_64/
kernel-5.18.12-1.x86_64.rpm  kernel-devel-5.18.12-1.x86_64.rpm  kernel-headers-5.18.12-1.x86_64.rpm
[root@host1 rpmbuild]# ls SRPMS/
kernel-5.18.12-1.src.rpm

Install the new kernel

Now you can run the following command to install the rpm packages:

[root@host1 rpmbuild]# rpm -iUv ~/rpmbuild/RPMS/x86_64/*.rpm

Once the installation is complete, run the following command to reboot the system:

[root@host1 rpmbuild]# reboot

Once the system starts, run the following command to check the kernel version:

[root@host1 ~]# uname -r

Reference