From LQWiki
Contents |
Requirements
Not all Linux distributions come with all of the components needed to build a kernel. You'll need a C-compiler, make, and system header files installed before you can begin to build the kernel. This document assumes you have them. You can test this with:
cc -v make -v ls /usr/src/linux
If you want to update your 2.4.x kernel to a 2.6.x, read THIS and that.
Preparations
It is recommended that you put new kernel sources in a subdirectory of your home directory for example:
mkdir -p /home/$user/src/kernel cd /home/$user/src/kernel
Obtaining the kernel source
The variable $KV is for the kernel version. Replace x.x.x with the kernel version of the kernel you want to compile:
KV=x.x.x
three ways to get the kernel sources:
- From your distribution
- From kernel.org
- From git
From distribution
When installed it usually resides in a /usr/src/linux-x.x.x subdirectory. Link it using:
ln -s /usr/src/linux-x.x.x /home/$user/src/kernel/linux-x.x.x
From kernel.org
- Get the kernel source from kernel.org. Download the kernel sources (linux-x.x.x.tar.gz or linux-x.x.x.tar.bz2).
Extract the sources, using one of:
tar -zxvf linux-$KV.tar.gz tar -jxvf linux-$KV.tar.bz2
From git
- It is also possible to get the latest kernel development version using git. If you clone it in '/home/$user/src/kernel, the git repository will be placed in /home/$user/src/kernel/linux-x.x
Enter the created subdirectory
cd linux-$KV
Patch Kernel
If you want to apply an additional patch to your kernel, you should do that before you configure your kernel, see patching a kernel. If you patched your kernel, don't forget to rename the kernel source directory accordingly.
Building the kernel
It is recommended that you do not build in your kernel source directory.
Preparing to build
Create another directory in your home directory:
mkdir -p /home/$user/build/kernel/$KV/
If this is not the first time you compile the kernel you'll have to clean up your build directory. You may want to backup the previous .config file, however:
cp /home/$user/build/kernel/.config /home/$user/kernel.config make O=/home/$user/build/kernel/$KV/ mrproper
Obtaining a configuration
Do this to restore the previous configuration:
cp /home/$user/kernel.config /home/$user/build/kernel/$KV/.config
Otherwise, if compiling for the first time you may want to start with your distribution's .config You can obtain that from cd or do:
zcat /proc/config.gz > /home/$user/build/kernel/$KV/.config
If you're going to upgrade to a newer kernel, first do:
make O=/home/$user/build/kernel/$KV/ oldconfig
For ncurses GUI in the terminal
make O=/home/$user/build/kernel/$KV/ menuconfig
- Notes
- Most options are about specifying whether you want a feature [*] compiled into the kernel image, [M] compiled as a module, or [ ] not compiled at all. Do not compile your hard drive and file system type as a module - these must be compiled in the kernel [*], e.g. " [*] ReiserFS".
- Here you to specify an enormous number of features. It is advisable to skim through all the sections to get a feel for the different things you can do. The kernel configuration is one LINUX program that offers lots of help--select < Help > on any feature. The raw help file is /usr/src/linux/Documentation/Configure.help can also be worth reading. See also configuring linux kernel.
- When you're done with the config, click exit and save current configuration. Your file is now known as .config .
Extra cleaning for pre 2.6 kernels
This can be skipped for 2.6.x kernels. First ensures that dependancies such as include files are in place, then clean your sources so they compile correctly.
make O=/home/$user/build/kernel/$KV/ dep make O=/home/$user/build/kernel/$KV/ clean
compiling kernel and modules
make the kernel image (compile and creates compressed image of kernel). This may take a while...
make O=/home/$user/build/kernel/$KV/
make the modules
make O=/home/$user/build/kernel/$KV/ modules
installation
for the remainder you need to be root:
su
To install the newly compiled modules to /lib/modules/$KV/:
make O=/home/$user/build/kernel/$KV/ modules_install
Move kernel and related files
Remove the following links:
rm -rf /boot/System.map rm -rf /boot/vmlinuz
Then copy the newly created kernel and system.map to /boot
cp /home/$user/build/kernel/$KV/arch/i386/boot/bzImage /boot/vmlinuz-$KV cp /home/$user/build/kernel/$KV/System.map /boot/System.map-$KV
Make the new links:
ln -s /boot/vmlinuz-$KV /boot/vmlinuz ln -s /boot/System.map-$KV /boot/System.map
Next, if present, remove existing initrd.img file:
rm -rf /boot/initrd-$KV.img
And create the new one:
/sbin/mkinitrd /boot/initrd-$KV.img $KV
Install a boot manager
Use The Steps That Pertain To You
- remember to leave the entry to your old kernel image just in case, so modify what is in the config originally to point to the old image and make a new entry for you new image you just made...
You can determine your root partition with:
ROOTDEV=`mount -l | grep " \/ " | cut -d" " -f1`
GRUB
Note today most distros now use menu.lst, so if you can't find grub or lilo, then you know what to look for.
title New Kernel :D kernel /vmlinuz-$KV ro root=$ROOTDEV initrd /initrd-$KV.img
- Note
- Look at the previous parameters in the grub.conf file and note what "root=" and use what is existing.
Exit and save grub.conf, and if you want (is not mandatory) type
/sbin/grub-install reboot
Lilo
Append the kernel as boot option to your /etc/lilo.config:
cat << EOF >> /etc/lilo.conf image = /boot/vmlinuz-$KV label = vml-$KV root = $ROOTDEV read-only EOF
install this lilo.conf (if listed, resolve errors before rebooting!):
/sbin/lilo
Propietary nvidia module
For some it is required to install proprietary modules. This can be done after booting, see NVIDIA graphics drivers, or directly from the kernel directory after installation of the kernel if you're upgrading to a newer kernel version, by running as root:
/path/to/NVIDIA-Linux-*-pkg1.run -a --kernel-name="`grep -o "2\..\.[0-9]*.*$" .config`" \ --no-x-check --no-runlevel-check --kernel-module-only -s;

This page is available under a