EWK Projects

HOWTO Build the Linux kernel

There is a lot of good information floating around on how to get started building the Linux kernel, but some of it is still incomplete or out of date. Between Linux Kernel Newbies and Linux Kernel in a Nutshell I've put together these steps that have worked consistently.

First, if you haven't done this already, make sure your system has all the necessary development tools. On Fedora:

% yum install gcc make git ctags ncurses-devel

Grab the mainline kernel source if you don't already have it.

% git clone git://git.kernel.org/pub/scm/ \
linux/kernel/git/torvalds/linux.git linux_src

Then 'cd' into the source directory and pull down the updates.

% cd linux_src
% git pull
% git checkout v3.14-rc8

The last step ensures that you build from a commit that is tagged for release. You don't necessarily want to build from the tip of the commit tree, which is where git pull will leave you. (I've learned that lesson the hard way.) git tag will list all the tags in the repository.

Create a directory for the build target. On most systems /tmp is mounted directly into RAM, which we can exploit to make the build go faster:

% mkdir /tmp/rc

Next you'll need a configuration file for the build. You could walk through the menu configuration process, but that way lies madness. If you're running your distro's stock kernel, just borrow your distro's config:

% cp /boot/config-`uname -r`* /tmp/rc/.config

To learn how to build a .config just for your system, read through Linux Kernel in a Nutshell. For our purposes, the configuration from your stock kernel works fine.

Depending on the state of your source tree, you may have to do some quick cleanup first. (We're still in linux-src).

% make mrproper

Now we're ready to kick things off:

% time make -j8 O=/tmp/rc

You'll have some questions to answer. Hitting 'return' to accept all the defaults should be fine. Then kick back. Enjoy your favorite brewed beverage. Catch up on an episode of Doctor Who. This is going to take a while.

While we wait, there are a couple of things to notice in that last line. The time command isn't needed at all- I just like to see how long the build process actually takes, with the hope that one day I'll improve on it.

The -j8 flag tells make how many processes it can spawn for the build. The number is a multiple of the available cores on your machine. I give make 8 for my 4 core system. You might want to dial this number back if it's melting down your PC, but you shouldn't really expect to be using it "normally" while the compile runs.

The O= parameter tells makewhere to place the compiled output.

The build will finish. Don't be surprised if it takes close to an hour, especially if you're using your distro's kernel config. When the process completes without errors, the fresh kernel is ready to be installed. (If you do get errors, try to figure out what they mean and start the process again.)

% cd /tmp/rc
% sudo make modules_install install
% sudo grub2-mkconfig --output=/boot/grub2/grub.cfg

If you have a newer computer that boots from UEFI instead of BIOS you'll need to modify the third line slightly:

% sudo grub2-mkconfig \
--output=/boot/efi/EFI/fedora/grub.cfg

That last step regenerates the boot menu for you. Much better than the bad old days when I had to configure grub by hand.

Reboot, and your homemade kernel should be on the top of the boot menu. If it doesn't finish bootstrapping the system, you can reboot into an old kernel and try building again.

The whole process takes some practice. And a lot of patience.

% uname -a