...to get kernel source from kernel.org and build a vanilla kernel.
The latest kernel source can be found at www.kernel.org.
To save bandwidth (both yours and kernel.org's) I recommend downloading the base source and the latest patch. This way for future upgrades, you just need to download the patch, which is generally much smaller than the complete source. As an example, let's say we want to build version 3.8.5. We would download the following two files:
Next we extract the source and apply the patch.
tar -xf linux-3.8.tar.xz
xz -dc ../patch-3.8.5.xz | patch -p1
Then give the directory its rightful name.
mv linux-3.8 linux-3.8.5
Configuring the Linux kernel is probably the most difficult task you'll encounter. However, with a few tricks, it isn't really all that bad.
It helps to have an existing kernel config as a starting point. The best config to start with is one that is either for the same kernel version you are building, or one whose version is as close as possible to the version you are building. Look in /boot for files starting with "config". Also check the online repositories for your distro to see if there is a kernel with a closer version number. A while back, using Debian, I was able to find a 2.6.30 kernel in "backports" that was close to the 2.6.31 that I wanted to build, so I went with it.
Copy the config that you find to the filename ".config" in your kernel source main directory. Let's suppose we found a config-3.5.0-26-generic in /boot.
cp /boot/config-3.5.0-26-generic .config
If you don't do this, the configuration tool will use the config for the currently running kernel, which might be just fine.
"menuconfig" is the preferred kernel configuration tool. It is easy to use, and has minimal dependencies. Run it like this:
You can now wander through the numerous kernel configuration settings and see if anything grabs your attention. For many of the settings, the help (press "?") will explain what the typical setting is.
To make a smaller kernel that will build a bit faster, make sure debug info is turned off. You can find it in "Kernel hacking > Compile the kernel with debug info". Turning this off will reduce the size of the modules by a factor of 10.
When you are done looking through the various settings, you're ready to build. First, we update the .config file to the current version.
Since making a .config is a lot of work, be sure to backup your config as it stands.
cp -p .config ~/config.backup
Now clean up any leftovers from previous builds.
And build the kernel and the modules.
Before you install a kernel, make sure you aren't running the kernel you are about to install. I've not tried it, but I'm betting it won't work. Reboot with one of your distro's standard kernels before proceeding.
If you are building the same version of the kernel again, I've found that it is a good idea to clear out the old modules. You can safely skip this the first time you build a specific kernel version. For this example, we'll clear out the modules for 3.1.9:
sudo rm -rf 3.1.9
"cd" with a hyphen will take us back to where we were previously. That will be our linux-3.1.9 directory.
Install the new modules to /lib/modules.
sudo make modules_install
If you are building the same version of the kernel again, go into the /boot directory and either remove or rename the old initial ramdisk. This step is required or else you will end up with a bad initial ramdisk.
sudo rm initrd.img-3.1.9
Install the new kernel to /boot, create the initial ramdisk, and update the bootloader.
sudo make install
Now take a look at the /boot directory to make sure everything was installed correctly. You should see the following files at a minimum:
If the initrd file is missing, you'll need to make the initial ramdisk manually. See "Updating the Initial RAM Disk" below.
Now would be a good time to backup your machine in case something bad happens with the new kernel.
Reboot and try your new kernel.
To make sure you are running your new kernel, verify the version and build date with the uname command:
If the reboot fails, you can select a known working kernel through your bootloader's boot menu. If the menu isn't visible when you boot, hold down the shift key while booting.
Linux Kernel in a Nutshell - Much more detail on building kernels. This appears to be missing at the moment due to the hacking of kernel.org. Hopefully it will return. It might be a bit dated, but since it is missing, I'm not sure.
The following are portions that I've pulled out of the main text. At one time I was not seeing the kernel makefiles create the initial ramdisk and update the bootloader. Turns out I was running make install BEFORE make modules_install instead of the other way around. As a result, I had to learn about making initial ramdisks and updating bootloaders. This info might be helpful in the future, so I don't want to lose it.
Go into the /boot directory and either remove or rename the old initial ramdisk. This step is required or else you will end up with a bad initial ramdisk.
sudo rm initrd.img-3.1.9
Build a new initial ramdisk. In a Debian-based distro (e.g. Ubuntu), the update-initramfs script will do the job for you:
sudo update-initramfs -c -k 3.1.9
If update-initramfs is not available in your distro, you can use mkinitramfs directly:
sudo mkinitramfs -o initrd.img-3.1.9 3.1.9
If you had to do this step manually because your distro didn't properly create the initial ramdisk, you should update your bootloader so that it can find the initrd.img file. Continue to the next section.
To boot a new kernel, we need to update our bootloader to point to the new kernel and initial ramdisk. There are a number of different bootloaders, so the instructions vary depending on which one you are using.
Version 1.x of GRUB is configured through menu.lst. Look for /boot/grub/menu.lst and edit it. Copy the lines for one of the other kernels and modify it to point to your new one.
If you have built a new kernel (not replaced an old one), run update-grub to get it into the grub menu. grub2 defaults to the newest kernel that you've installed. Press and hold SHIFT while booting to get the grub menu and select a different kernel.
For lilo, take a look at /etc/lilo.conf, and make any changes that are needed. Then you must run the "lilo" command before rebooting, even if you make no changes to the lilo.conf. This is because lilo has to figure out where your new kernel is on the hard drive.
Copyright (C) 2009-2012, Ted Felix
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. See http://www.gnu.org/licenses/fdl.html for the full text of this license.