Building the Linux Kernel

Steps for building the Linux kernel and installing it.

Prep:  Install developer tools using yum

  sudo yum group install "Development Tools"
  sudo yum install kernel-devel
  sudo yum install qt-devel xz-libs openssl-devel elfutils-libelf-devel
  sudo yum install valgrind gitk

Step 1. Locate/Download the source code for the linux kernel.
          tar xJvf linux-<version>.tar.xz

Step 2.  Identify the hardware on your system.  Note the type of CPU you have, amount of memory, the number of SATA drives, SCSI drives (if any). Look under the entry for PCI bus for network card information, sound card information and any other specialized PCI cards that the system might have installed. Alternately you can get the same information by looking at the files /proc/cpuinfo, /proc/meminfo and /proc/pci. You can also use the command /sbin/lspci.

Step 3.  Clean the kernel source to remove any stale files.  Use the following command in the kernel source directory.

                   make mrproper

Step 4.  Modify kernel source if you need to or want to. :-)

Step 5.  Configure the kernel (the main thing to do...). Make sure that you are in the kernel source directory.  Use the following command to bring up a graphical kernel configurator.

           make xconfig
You may need to install some extra software at this point. The error messages should give you enough of a pointer as to what packages to install. Walk through the options, changing the ones you want to and leaving others unchanged. Reasonable defaults are given for most options. For each option there is a help button that describes the option in more detail and advises you of what the safest choice on that option would be. Once you are done choose the Save and Exit option. Some common options that you want to set:
      
Note that make xconfig saves the config file in the hidden file named .config in the main kernel source directory. If you recompile a kernel that you have already compiled and installed, the new kernel will replace the old kernel with the same release number. To create a custom version for the same release,  edit the .config file in the top-level of the kernel source folder and change the following line:

CONFIG_LOCALVERSION=""

to something like the following (before building the kernel):

CONFIG_LOCALVERSION="-My-Custom-Version"

Step 6.  Compiling the kernel. Use make command to build the kernel. This will take a while depending upon the speed of
               your computer. If you have more than one CPU, use the  -j option (e.g. make -j 4)  to allow make
               to use multiple CPUs for faster compilation.

             make

Step 7. Install your modules. This installs them under /lib/modules directory. If you are recompiling the stock Fedora
             kernel, then the new kernel has the suffix prep added to it.

                 sudo make modules_install

Step 8. Make sure you are still in the top level of kernel source directory and then install the kernel using the following command.

                 sudo make install

Step 9. Check the bootloader configuration file for the new kernel.  The command make install  automatically modifies the GRUB bootloader to add entries for the new kernel. Check that it did not set your test kernel as your default kernel using a text editor (such as vim).    
           
                   sudo vim /boot/grub2/grub.cfg
               

Here is what the relevant sections of the grub.cfg file looked like on my system before we installed the new kernel.

### BEGIN /etc/grub.d/00_header ###
.
.
.
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/10_linux ###
menuentry 'Fedora (4.6.6-300.fc24.x86_64) 24 (Twenty Four)' --class fedora --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-4.5.5-300.fc24.x86_64-advanced-abd618de-9731-4941-9d24-99197c0e0dec' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  e8048c96-12dc-493f-bb9e-05d64efc91ab
    else
      search --no-floppy --fs-uuid --set=root e8048c96-12dc-493f-bb9e-05d64efc91ab
    fi
    linux16 /vmlinuz-4.6.6-300.fc24.x86_64 root=/dev/mapper/fedora-root ro rd.lvm.lv=fedora/root rd.lvm.lv=fedora/swap rhgb quiet LANG=en_US.UTF-8
    initrd16 /initramfs-4.6.6-300.fc24.x86_64.img
}

.
.
.
### END /etc/grub.d/10_linux ###
.
.
.

Here is what relevant sections of the grub.cfg file looked like after we installed the new kernel. Note that for developmental purposes, we recommend taking out the rhgb and quiet options (in bold red) so that we get the full diagnostic messages when the new kernel boots. You can also change the title of the new kernel to anything you want. Also note that we set the  CUSTOM_LOCALVERSION flag in the .config file before building a second version of the 4.7.2 kernel (with a modified fork.c) and that's why there are two new entries in the grub.cfg file.


#### BEGIN /etc/grub.d/00_header ###
.
.
.
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/10_linux ###
menuentry 'Fedora (4.7.2-My-Custom-Version) 24 (Twenty Four)' --class fedora --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-4.5.5-300.fc24.x86_64-advanced-abd618de-9731-4941-9d24-99197c0e0dec' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  e8048c96-12dc-493f-bb9e-05d64efc91ab
    else
      search --no-floppy --fs-uuid --set=root e8048c96-12dc-493f-bb9e-05d64efc91ab
    fi
    linux16 /vmlinuz-4.7.2-My-Custom-Version root=/dev/mapper/fedora-root ro rd.lvm.lv=fedora/root rd.lvm.lv=fedora/swap rhgb quiet LANG=en_US.UTF-8
    initrd16 /initramfs-4.7.2-My-Custom-Version.img
}
menuentry 'Fedora (4.7.2) 24 (Twenty Four)' --class fedora --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-4.5.5-300.fc24.x86_64-advanced-abd618de-9731-4941-9d24-99197c0e0dec' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  e8048c96-12dc-493f-bb9e-05d64efc91ab
    else
      search --no-floppy --fs-uuid --set=root e8048c96-12dc-493f-bb9e-05d64efc91ab
    fi
    linux16 /vmlinuz-4.7.2 root=/dev/mapper/fedora-root ro rd.lvm.lv=fedora/root rd.lvm.lv=fedora/swap rhgb quiet LANG=en_US.UTF-8
    initrd16 /initramfs-4.7.2.img
}
menuentry 'Fedora (4.6.6-300.fc24.x86_64) 24 (Twenty Four)' --class fedora --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-4.5.5-300.fc24.x86_64-advanced-abd618de-9731-4941-9d24-99197c0e0dec' {
    load_video
    set gfxpayload=keep
    insmod gzio
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos1'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  e8048c96-12dc-493f-bb9e-05d64efc91ab
    else
      search --no-floppy --fs-uuid --set=root e8048c96-12dc-493f-bb9e-05d64efc91ab
    fi
    linux16 /vmlinuz-4.6.6-300.fc24.x86_64 root=/dev/mapper/fedora-root ro rd.lvm.lv=fedora/root rd.lvm.lv=fedora/swap rhgb quiet LANG=en_US.UTF-8
    initrd16 /initramfs-4.6.6-300.fc24.x86_64.img
}

.
.
.
### END /etc/grub.d/10_linux ###

.
.
.

Step 10.  Reboot your machine.

Step 11
.  Choose the new kernel. When the GRUB screen comes up,  boot using the new kernel (it should be default) <drum roll>  and watch the system boot up<more drum roll>.  Enjoy the ride!  Note that it is a good idea to save the .config file in the  /boot folder (named appropriately) if you need it later to recompile the kernel.
dancing penguins


Step 12
.  Oops! If for some reason the new kernel doesn't work, then reboot the system and choose an earlier installed kernel that works and start over.