User Tools

Site Tools


linux:compiling-the-linux-kernel

Preparing to modify the Linux kernel:

  • Clone your Fedora VM
  • Update your machine:

$ sudo dnf check-update ; sudo dnf update

  • Install the following:

$ sudo dnf install ncurses-devel bison elfutils-libelf-devel

$ sudo dnf install openssl-devel

  • Install the following groups of packages:

``$ sudo dnf groupinstall 'Development Tools'

$ sudo dnf groupinstall 'C Development Tools and Libraries'

  • Download the latest, stable release of the Linux kernel from kernel.org. The kernel is about 100MBs large, and this needs to be done ahead of time. Use wget to download the kernel:

$ wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.18.8.tar.xz

Here are some notes on the basics of compiling a Linux kernel. We're following the text by Soyinka, which means we're downloading the most recent kernel source tree, enabling NTFS support as a module, and then re-compiling. We're recompiling a kernel for a Fedora 23 system that's running in VirtualBox.

Note about code snippets:

  • Lines beginning with a "$" sign signify normal user prompt
  • Lines beginning with a "%" sign signify root prompt
  • Lines beginning with a "#" sign signify comments in Bash
  • Lines w/o any initial sign signify standard output

We can identify the current kernel by using uname.

$ uname -a
4.7.4-100.fc23.x86_64

This is also the latest kernel at kernel.org, but we'll recompile it to see how the process goes and to enable NTFS support.

First, let's download the source code for the latest release (as of this writing), and unpack it in /usr/src:

$ su root
% cd /usr/src
% wget 
https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.18.8.tar.xz
% tar xvJf linux-4.7.4.tar.xz

Now let's begin to configure the source for our particular virtual machine.

As an aside, sometimes we re-compile kernels to eliminate support for hardware we don't have, for enabling hardware that we need, or for patching kernels with security updates. There are more use-cases, but regarding harwdware, we can use lspci and lshw to list PCI devices and other hardware on the system. This is what my virtual system thinks it has:

% lspci
00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter
00:03.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
00:04.0 System peripheral: InnoTek Systemberatung GmbH VirtualBox Guest Service
00:05.0 Multimedia audio controller: Intel Corporation 82801AA AC'97 Audio Controller (rev 01)
00:06.0 USB controller: Apple Inc. KeyLargo/Intrepid USB
00:07.0 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:0d.0 SATA controller: Intel Corporation 82801HM/HEM (ICH8M/ICH8M-E) SATA Controller [AHCI mode] (rev 02)

We need to install lshw. Then:

% dnf install lshw
% lshw | less

(Output too long to show.)

We're going to run make menuconfig, but before we do so, we'll first run make mrproper to clean up the build environment. First, install the development tools, then change into the kernel tree directory, and run mkproper.

# need the tools to build the kernel
% dnf install @development-tools
# alternatively, this is supposed to work: dnf groupinstall "Development Tools"
% cd /usr/src/linux-4.7.4
% make mrproper
# copy the current kernel's config file to $PWD
% cp /boot/config-`uname -r` .config
# alternatively: cp /boot/config-$(uname -r) .config
# run menuconfig
% make menuconfig
# received an error about a missing ncurses header file, so:
% dnf install ncurses-devel.x86_64
# now re-run menuconfig
% make menuconfig

We're following the text by Soyinka, and enabling support for NTFS as a module. I'm skipping her description here, but the changes we've made to .config include:

% grep -i ntfs .config
CONFIG_NTFS_FS=m
CONFIG_NTFS_DEBUG=y
CONFIG_NTFS_RW=y

Next open the Makefile and add -custom to the EXTRAVERSION variable, and then run:

% make kernelversion
4.7.4-custom

After running make, I found I still needed to install some more development files. Here we go:

% dnf -y group install 'C Development Tools and Libraries'

Woops, another error. We also need to install:

% dnf install openssl-devel
% make

And it's compiling. Takes about an hour on the virtual system w/ only 1 CPU. If we want it to go faster, next time assign more CPUs, if we can spare them, in our VB settings.

Now we need to install the modules and then run make again.

% make modules_install

More compiling. (Next time run time command to get a report on how long.)

Next, copy and rename the kernel image to /boot, as well as System.map:

% cp /usr/src/linux-4.7.4/arch/x86_64/boot/bzImage /boot/vmlinux-4.7.4-custom
% cp /usr/src/linux-4.7.4/System.map /boot/System.map-4.7.4-custom

Finally, create the RAM file system image, create list of module dependences, and update the boot loader config. Before rebooting, check /boot/grub2/grub.cfg to see if the boot config was updated, and then reboot.

% new-kernel-pkg --mkinitrd --dracut --depmod --install 4.7.4-custom
% reboot

After reboot and selecting the new image from grub:

$ uname -r
4.7.4-custom

References

Soyinka, Wale. (2015). Linux administration: A beginner's guide. New York: MacGraw Hill Education.

linux/compiling-the-linux-kernel.txt · Last modified: 2019/01/21 11:11 by seanburns