Kernel Compilation

From SlackWiki
Jump to navigation Jump to search

Kernel Compilation guide for the 2.4 series.
Written by Thomas York - straterra@hotmail.com Straterra
Read over the ENTIRE GUIDE at least twice (2x) before trying to compile your first kernel.
Added 2.6 information -- erik

Prequisites

  1. Download the latest kernel from kernel.org.
  2. It is HIGHLY recommended that you create a folder just for your new kernel. It can be in your user directory or anywhere else you want it to be.
  3. You must have the GCC Compiler
  4. You must have NCurses.
  5. You must have access as root.
  6. You must have have a bootloader (grub and lilo, the two most popular, are described below)
  7. You must be familiar with a text file editor (vi, nano, etc)

Getting Ready to Compile

  1. Extract the tar.bz2 file by using tar jxvf linux-VERSION.tar.bz2
  2. Change into the newly created directory by using the cd command.
  3. Run the command make menuconfig. This command will open up a semi-graphical menu where you can pick what you want in your kernel. See Kernel Checklist.
    • NOTE : If you already have a config file, you should instead run the command make oldconfig. Make sure that you have placed the config file in /usr/src/linux-VERSION, and that the config file has been named .config.
    • NOTE : When you navigate through the menus, you use the keys 'M' to make the selection a module and 'Y' to compile it into the kernel. The difference is that if you compile it as a module, you can always unload it later. This is ideal if something causes conflicts. For this reason, I prefer to compile as much as possible as modules. It's your kernel, do what YOU want to do. :) Bear in mind that some options MUST be compiled into the kernel statically (not modular) - for example, support for your root filesystem type must be static.
    • NOTE : If you are unsure what a certain selection does, you can press the '?' while having the confusing item selected. It is recommended you learn what EVERYTHING does your first time around, whether you will compile it in your not. This allows you to not have to press the '?' key every time..you already learned them! :)
  4. When you exit out of menuconfig, it will ask if you want to save. Say YES. This is critical. If you do not answer YES you will have to go back and do it all over again.

Compiling v2.4

  1. make dep - This will organize all module dependencies.
  2. make bzImage - This option may take a while, depending on how much content you decided to compile directly into the kernel, processor speed, ram, etc..
  3. make modules - Again, if you selected for alot of content to be compiled as modules this may take some time.
  4. su - Enter your root password. This allows you to have the access to install the compiled kernel.
  5. make modules_install - This command will install all of your modules.
  6. cp System.map /boot/System.map - To replace the old System.map file.
  7. cp arch/i386/boot/bzImage /boot/bzImage-VERSION - Replacing the variable VERSION with the version of the 2.4 kernel you compiled.

Compiling v2.6

  1. make - This will build the kernel (vmlinux), compressed kernel (bzImage), and modules.
  2. su - Enter your root password.
  3. make modules_install - Installs the modules.
  4. cp System.map /boot/System.map-VERSON - Replace VERSION with the kernel version number.
  5. cp arch/i386/boot/bzImage /boot/bzImage-VERSION - Replace VERSION with the kernel version number.

Lilo Configuration

  1. Edit /etc/lilo.conf. This will open the lilo configuration file for editing.
  2. Input the following lines at the end of the file, again replacing the variable VERSION with the kernel version, the variable y for the hard drive device location and the variable z for the Linux partition.
    image = /boot/bzImage-VERSION
    root = /dev/hdyz
    label = Linux-VERSION
    read-only
  3. Save the file and exit.
  4. Run the command lilo -v. This command (re)installed the lilo boot loader using the modified /etc/lilo.conf file.
  5. Hit Alt-Ctrl-Del, or run the command reboot or init 6. This will switch to runlevel 6 and restart your computer.

Grub Configuration

  1. Edit /boot/grub/grub.conf OR /boot/grub/menu.lst
  2. Replace the variable VERSION with the kernel version
  3. Using grub a device such as /dev/hda1 is not used. Instead a zero indexed system is used (based on all drives grub can see at boot). /dev/hda1 would become (hd0,0), and /dev/hdb3 would become (hd1,2). Note that the root= line is passed to the kernel itself, and is not used by grub - therefore put in the devfs device (/dev/hda1 perhaps)
  4. First the relevant lines
    title Linux-VERSION
    root (hdx,x)
    kernel /boot/bzImage-VERSION root=/dev/hdxx
  5. Now a complete one for reference
    default 0
    timeout 30
    splashimage=(hdx,x)/grub/splash.xpm.gz
    title Linux-VERSION
    root (hdx,x)
    kernel /boot/bzImage-VERSION root=/dev/hdxx
  6. Hit Alt-Ctrl-Del, or run the command reboot or init 6. This will switch to runlevel 6 and restart your computer.

Post-Compilation

  1. When the machine reboots, choose the option 'Linux-2.4.x' or 'Linux-2.6.x', where x is your kernel minor version.
  2. Recompile any software that created a kernel module for your old kernel (nvidia, etc).

NOTES

  1. If the compilation failed at ANY step, you must run the command 'make clean' while in the directory of the extracted kernel source.
  2. If you try to boot up your new kernel, and you get errors, you can boot your old kernel using the old option in lilo.
  3. Make sure to compile in support for your filesystem!
  4. Historically, the kernel source is extracted to /usr/src/ and a linux symlink is made. So you have the following:
    • /usr/src/linux-2.6.16
    • /usr/src/linux -> linux-2.6.16
    This allows you to symlink /boot/System.map and /boot/config right to /usr/src/linux/System.map and /usr/src/linux/.config - this way they are always pointing to the correct file and never have to be replaced. However, neither of these are necessary; a System.map-VERSION file in /boot will work just fine without a symlink to System.map (see http://www.dirac.org/linux/system.map/ for more information).
    Due to stupid users and old distributions replacing (symlinking) /usr/include, the merits of building a kernel in /usr/src are now under fire. It is (and always has been) safe to build a kernel as root in /usr/src, but you should NEVER replace /usr/include.