Monday 22 August 2016

Compile and install new kernel on CentOS

Objective: Compile and install new kernel 

OS: CentOS 6.7

Current kernel version  : 2.6.32
Upgraded kernel version : 3.14.77

Download the latest kernel version from https://www.kernel.org/ to /tmp.

1. Make sure you install "Development Tools" using yum which includes gcc packages which are required for compiling kernel. 

[root@centos ~]# yum groupinstall "Development Tools"
Loaded plugins: fastestmirror, security
Setting up Group Process
Loading mirror speeds from cached hostfile
Package 1:make-3.81-20.el6.x86_64 already installed and latest version
.
<snip>
.

2. Extract the newer kernel to /usr/src directory and create an soft link for 'linux' directory.  That would be the directory which is required for you to compile your newer kernel.

[root@centos ~]# tar -Jxvf linux-4.7.2.tar.xz -C /usr/src/
[root@centos src]# ln -s linux-4.7.2/ linux
[root@centos src]# ls
debug  kernels  linux  linux-4.7.2
[root@centos src]# cd linux
[root@centos linux]# ls
arch   certs    CREDITS  Documentation  firmware  include  ipc     Kconfig  lib          Makefile  net     REPORTING-BUGS  scripts   sound  usr
block  COPYING  crypto   drivers        fs        init     Kbuild  kernel   MAINTAINERS  mm        README  samples         security  tools  virt
[root@centos linux]#


3. Since I have only newer kernel and not much to be cleaned for older unnecessary modules, I would choose 'mid-range' i...e mrproper. You can choose depending on your compilations. for more info type 'make help' 

Cleaning targets:
  clean           - Remove most generated files but keep the config and
                    enough build support to build external modules
  mrproper        - Remove all generated files + config + various backup files
  distclean       - mrproper + remove editor backup and patch files

[root@centos linux]# make mrproper
[root@centos linux]#

4. Despite installing "Development Tools", you additional are required to install 'ncurses-devel' for menuconfig (custom configuration of the new kernel i..e which modules needs to be compiled or loadable by kernel) and after saving you it would create a .config file. 

[root@centos linux]#make menuconfig

The one chosen with * modules will be compiled and others which are unchecked they wouldn't be compiled. Hence un-necessary modules which are not required for your system need not be checked here. so you can only choose what your system wishes to do.



5. Compile your actual kernel, which will take around 15-20 mins depending on your number of CPU.

[root@centos linux]# make bzImage
  HOSTCC  scripts/kconfig/conf.o
  HOSTLD  scripts/kconfig/conf
scripts/kconfig/conf  --silentoldconfig Kconfig
.
.
<snip>
.
.
  BUILD   arch/x86/boot/bzImage
Setup is 15164 bytes (padded to 15360 bytes).
System is 4374 kB
CRC 96b7ee84
Kernel: arch/x86/boot/bzImage is ready  (#1)
[root@centos linux]#


6. compile your modules would considerably takes more time than kernel ( approx 60-90 mins ) depending on your nummber of CPU

root@centos linux]# make modules
make[1]: Nothing to be done for `all'.
make[1]: Nothing to be done for `relocs'.
  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CALL    scripts/checksyscalls.sh
 .
 .
 <snip>
 .
 .
  H16TOFW firmware/edgeport/down2.fw
  IHEX    firmware/edgeport/down3.bin
  IHEX2FW firmware/whiteheat_loader.fw
  IHEX2FW firmware/whiteheat.fw
  IHEX2FW firmware/keyspan_pda/keyspan_pda.fw
  IHEX2FW firmware/keyspan_pda/xircom_pgs.fw
[root@centos linux]#

7. copies all the modules to a new directory .. /lib/modules/3.14.77/

[root@centos linux]# make modules_install
  INSTALL arch/x86/crypto/aes-x86_64.ko
  INSTALL arch/x86/crypto/aesni-intel.ko
  INSTALL arch/x86/crypto/crc32c-intel.ko
  INSTALL arch/x86/crypto/crct10dif-pclmul.ko
  .
  .
  <snip>
  .
  .
  INSTALL /lib/firmware/keyspan_pda/keyspan_pda.fw
  INSTALL /lib/firmware/keyspan_pda/xircom_pgs.fw
  DEPMOD  3.14.77
[root@centos linux]#

8. Move kernel to the right location and name it correctly , update initramfs, grub so boot up with new kernel.

[root@centos linux]# make install
sh /usr/src/linux-3.14.77/arch/x86/boot/install.sh 3.14.77 arch/x86/boot/bzImage \
                System.map "/boot"
ERROR: modinfo: could not find module parport
ERROR: modinfo: could not find module snd_page_alloc
[root@centos linux]#

On summary, below are the commands used for building newer kernel.
make mrproper
make menuconfig
make bzImage
make modules
make modules_install
make install

9 Reboot system and boot into new kernel from the grub menu.

[root@centos linux]# reboot


[root@centos ~]# uptime
 10:39:41 up 0 min,  2 users,  load average: 0.00, 0.00, 0.00
[root@centos ~]#

[root@centos ~]# uname -r
3.14.77
[root@centos ~]#

Thanks for re-sharing.