Kernel Compilation under Linux
By Vegas
mailto:vegas@advknowledge.net?subject=Kernel Compilation under Linux
06/11/2002



1. Introduction
2. Installation requirements
   2.1 Download the new kernel
   2.2 Verify your download
   2.3 Decompress the kernel
3. How to patch a kernel
4. Configuration and Installation
   4.1 make commands: description and usage
   4.2 Build dependencies
   4.3 Build the kernel
   4.4 Build the modules
   4.5 Make an initrd image
   4.6 Backup and install the kernel
   4.7 Install the modules
5. Configure Bootloaders
   5.1 LILO
   5.2 GRUB
6. When it works
7. Conclusion


1. Introduction

I'm sometimes surprised to see people calling themselves linux geeks and don't even know how to install a new kernel on their OS. I decided to write this paper to show you how simple it is. Be very cautious when messing around with the kernel. Make or have a working bootable recovery floppy disk or CD-ROM. Don't forget to backup all of your files. I don't take any responsability of what you will do with this, it worked for me and will normally work for you but take care. This example uses a 2.4.19 kernel which is actually the lastest stable version on a x86 architecture. To know what kernel version you are actually running, type the command "uname -r". Download the lastest version at http://www.kernel.org/. You need about 200 MB of free space to install the new kernel.


2. Installation requirements

   2.1 Download the new kernel

$ su
$ cd /usr/src
$ wget http://www.fr.kernel.org/pub/linux/kernel/v2.4/linux-2.4.19.tar.gz


Use your country code instead of "fr" in the kernel download url as www.kernel.org is quite busy. You can look for all the mirrors at http://kenel.org/mirrors. I've chosen http but you can try to download it through ftp (ftp://ftp.fr.kernel.org/pub/linuxkernel/v2.4/linux-2.4.19.tar.gz).

   2.2 Verify your download

You have to verify if the download is correct by downloading the cryptographic signature file, here linux-2.4.19.tar.gz.sign. This file will permit us to verify if the kernel you downloaded isn't corrupted and/or authentic. You need gpg to do this. If it's the first time you verify your kernel using this, you will first need to get the "Linux Kernel Archives Verification Key" by doing this:

$ gpg -- keyserver wwwkeys.pgp.net --recv-keys 0x517D0F0E

You can also get the key at http://www.kernel.org/signature.html. Now that you have the PGP key on your gpg keyring and the .sign, you can verify the file:

$ gpg --verify linux-2.4.19.tar.gz.sign linux-2.4.19.tar.gz

gpg will tell you if the test succeed or not. If it has, you can untar your kernel.

   2.3 Decompress the kernel

$ tar zxvf linux-2.4.19.tar.gz
$ cd linux-2.4.19


In case you downloaded a bz2 file:

$ tar jxvf linux-2.4.19.tar.bz2


3. How to patch a kernel

You may also have downloaded a patch file (http://www.kernel.org/pub/linux/kernel/v2.4/patch-2.4.19.gz). First make sure that this patch is compatible with your old kernel:

$ gzip -d patch-2.4.19.gz | patch -p1 --dry-run

If the output is full of file foo lines then its ok. --dry-run just test your kernel to see if it's compatible. Now if you are ready to patch it:

$ gzip -d patch-2.4.19.gz | patch -p1

You won't have to do all the following steps if you use a patch.


4. Configuration and Installation

   4.1 make commands: description and usage

- "make xconfig" starts a GUI interface and can only be used if you're running a X Window
- "make menuconfig" starts a console mode interface. It will ask you if you want to add in the new options from the kernel by selecting Y (yes), N (no), or M (module).
- "make oldconfig" generates the .config from your current working kernel.
- "make config" will make the configuration from the .config that you will have edited and configured by hand (if you want headache, go for it).

I suggest the sweety GUI interface as you may imagine. All this commands create or use the .config file at /usr/src/linux-2.4.19/.config. If a .config has already been copied in that directory (see below), "make xconfig" will read it.

$ make xconfig

Configure your kernel by selecting all the features you want to compile into it like networking, sound, SCSI... Save and exit. All the selected options are saved in /usr/src/linux-2.4.19/.config before being compiled. If you want to use your old kernel configuration, execute "make oldconfig" (it will generate the .config file of your old kernel) and copy .config in the new kernel directory:

$ make oldconfig
$ cp -p /usr/src/linux-2.4.x/.config /usr/src/linux-2.4.19/.config


I suggest you to backup the .config generated or modified by "make xconfig", "make menuconfig" or by yourself somewhere for future kernel upgrades.

$ cp -p /usr/src/linux-2.4.19/.config /root/config-2.4.19.txt

The -p is used to keep permissions on the copied file. "make mrproper" deletes all your kernel sources including your old .config file. This command isn't needed if you NEVER compiled a kernel before or if you want to keep your old configuration. If you recompile your kernel because of some errors you had in the first compilation don't forget "make mrproper".

$ make mrproper
$ make xconfig


   4.2 Build dependencies

$ make dep
$ make clean


   4.3 Build the kernel

If everything goes right, you can now compile your kernel. The operation will take about 5 minutes (depends your processor)

$ make bzImage

This will put the new kernel in /usr/src/linux-2.4.19/arch/i386/boot/bzImage. Instead of "make bzImage" you could also use "make bzdisk". It compiles the kernel and copies it on a floppy disk. This command is used to test a new kernel without rewriting lilo.conf or grub.conf. A "cp /usr/src/linux-2.4.19/arch/i386/boot/bzImage /dev/fd0" after a "make bzImage" would do the same. You could also execute "make zImage" if you don't want your kernel to be compressed. It was the command used to compile older kernels. The image would then be named "vmlinux". This make isn't used anymore as the produced kernel is generally too big to be started by lilo or grub.

   4.4 Build the modules

Modules are parts of the kernel that are loaded on the fly, as they are needed. Modules are generally stored in ".o" files.

$ make modules

   4.5 Make an initrd image

If you have a SCSI adapter and you made your SCSI driver modular, build a new initrd image. If you don't need it skip this part. Also, if you build your kernel with ext3 support as a module, you must create an initrd image. Use /sbin/mkinitrd to build an initrd image for your machine. To use this command, the loopback block device must be available.

$ mkinitrd /boot/initrd-2.4.19.img 2.4.19

initrd-2.4.19.img is the name of the image that you are creating. You can give the name you want to that file but you will have to specify the same name on lilo.conf or grub.conf (see below). 2.4.19 is the name of the kernel found from /lib/modules that you need to use to build the initrd image.

   4.6 backup and install the kernel

Now that you compiled your new kernel you have to install it on your system. Kernels are generally loaded in /boot so we have to copy our new kernel there. I suggest you to backup your old System.map. Also make sure you have a bootdisk (mkbootdisk --device /dev/fd0 2.4.x) of your old kernel. You could also do a "make install", it will automatically copy the kernel and System.map in /boot. Don't forget to backup System.map before doing it.

$ cd /boot
$ mv System.map System.map-old


Now copy your new kernel in /boot (or do a "make install"):

$ cp /usr/src/linux-2.4.19/System.map /boot/System.map
$ cp /usr/src/linux-2.4.19/arch/i386/boot/bzImage /boot/vmlinuz-2.4.19


   4.7 Install the modules

$ make modules_install

This will copy all the modules in /lib/modules/2.4.19/kernel/drivers even if you didn't build any.


5. Configure bootloaders

The bootloader permits you to boot on the OS you want and on the kernel you want. It makes the link between the BIOS and the kernel. I'll talk about the two most well known bootloaders on linux: LILO and GRUB.

   5.1 LILO

First backup your working lilo.conf:

$ cp -p /etc/lilo.conf /root/lilo.txt

Now edit /etc/lilo.conf to make it bootable on the new kernel. Add this in the file:

image=/boot/vmlinuz-2.4.19
label=Linux-2.4.19
initrd=/boot/initrd-2.4.19.img
read-only
root=/dev/hda2


Do not change the "default=" line (above in lilo.conf) for the moment and do not delete lines corrosponding to current working kernel. The line "initrd=/boot/initrd-2.4.19.img" will be used only if you have made an initrd image (step 9). Now tell LILO to read lilo.conf:

$ lilo -v

Make sure the kernel have been loaded by looking at the output "Writing boot sector". Also tell LILO to treat "linux-2.4.19" as default boot kernel just for one time only with -R:

$ lilo -R linux-2.4.19

   5.2 GRUB

backup the config file of GRUB:

$ cp /boot/grub/grub.conf /root/grub.txt

Edit grub.conf and make this changes:

title Linux 2.4.19
root (hd0,1)
kernel /boot/vmlinuz-2.4.19 ro root=/dev/hda2 hdd=ide-scsi
initrd /boot/initrd-2.4.19.img


(hd0,1) is the partition where GRUB is installed. root=/dev/hda2 is where the kernel image is located. Here again the line "initrd /boot/initrd-2.4.19.img" will be used only if you have made an initrd image (step 9). If your /boot has its own partition, remove /boot in the kernel path:

kernel /vmlinuz-2.4.19 ro root=/dev/hda2 hdd=ide-scsi

Higher in the configuration file, you can see a line "default=0". It means the first entry of grub.conf will be the default entry. If you want to set your new kernel as default, you have to put the above lines before your old kernel entry or if you wrote it at the end of the file, set "default=1".

6. When it works

Reboot to your new kernel and pray. Ensure that your hardware is detected properly during the boot. If it does not boot, reboot again and try to find why it didn't work. Don't be scared to recompile it again, you can have to do it more than a time. Now if everything goes ok put the new kernel as default:

$ rm /usr/src/linux-2.4
$ ln -s /usr/src/linux-2.4.19 /usr/src/linux-2.4


or (it depends the version you have):

$ rm /usr/src/linux
$ ln -s /usr/src/linux-2.4.19 /usr/src/linux


To see which one you have to choose, simply do this:

$ ls -ld /usr/src/linux*
lrwxrwxrwx 1 root root 12 Oct 30 18:55 /usr/src/linux -> linux-2.4.17
drwxr-xr-x 14 root root 4096 Oct 30 17:26 /usr/src/linux-2.4.17
drwxr-xr-x 14 root root 4096 Oct 30 15:16 /usr/src/linux-2.4.17-openmosix-r2
drwxr-xr-x 14 root root 4096 Nov 1 20:11 /usr/src/linux-2.4.19
drwxr-xr-x 15 root root 4096 Nov 1 18:01 /usr/src/linux-2.4.19-openmosix-r7


It will show you lines with linux in their name. Have a look at the symlink, you will find it by looking at the rights of the files: lrwxrwxrwx. The "l" let you know it is a symlink so look at the name of this file and remove it to place the new symlink. Here you will choose the second solution as the symlink is called linux (/usr/src/linux). You can also change your "default=" in lilo.conf. Now make a bootdisk with the new kernel installed (mkbootdisk $(uname -r) or mkbootdisk 2.4.19). If after many efforts it didn't decide to work, apply backuped files in their original path. If everything goes good, just enjoy :)

Find more infos about kernel compiling in /usr/src/linux/README (general view on kernel compilation), /usr/src/linux/Documentation/Configure.help (explain in detail every elements of make config) or /usr/src/linux/Documentation/modules.txt for modules explanation.

7. Conclusion

I hope this tutorial will help you on some stuff. I wrote it for everyone and every levels on linux. Compiling the kernel is frightening when you don't know how it works but as you may have seen, it's not that hard. Enjoy your new kernel and look on kernel.org for changes and new stuff added in the new version. If you have any question or simply want to have a talk, join irc.netstat.org #aknet. See you there ;P