2. Quick Steps - Kernel Compile

This section is written by Al Dev (alavoor[AT]yahoo.com) (The latest version of this document is at "http://www.milkywaygalaxy.freeservers.com" . You may want to check there for changes). Mirror sites are at - angelfire , geocities . These sites have lot of linux goodies and tips.

Kernel re-compile is required in order to make the kernel very lean and which will result in FASTER operating system . It is also required to support any new devices.

2.1. Precautionary Preparations

Before you build kernel, it is a good idea to do a backup of the system. If you had not backed up your system recently then you can do it now. You can use commercial backup tools like BRS Backup-Recovery-Software (also in this page you can find open-source/freeware backup tools listed under 'Backup and Restore Utility'). Backup is just a suggestion and it is not mandatory to do backup before building the Linux kernel.

2.2. Minor Upgrading of Kernel

If you had already built the kernel and you want to upgrade to next patch release, then you can simply copy the existing config file and reuse it. (For example you have built kernel 2.4.19 and want to upgrade to 2.4.20).

For minor upgrades : This step may save you time, if you want to reuse the old settings. Whenever you install the kernel, generally you put the config file in /boot. So, you can use the existing version of config file:


	bash# mv /usr/src/linux/.config  /usr/src/linux/.config.save
	bash# cp /boot/config-2.4.18-19.8.0  /usr/src/linux/.config
        
Or another method is - you can copy the .config file from your old linux kernel source tree to new kernel tree.

	bash# ls -l /usr/src/lin*  # You can see that /usr/src/linux is a soft link
	bash# cd /usr/src/linux
	bash# cp ../linux-old-tree/.config .  # Example cp ../linux-2.4.19/.config .
        

or one other method is - you can use "make oldconfig" which default all questions based on the contents of your existing ./.config file.

NOTE: If you do not have lot of disk space in /usr/src then you can unpack the kernel source package on any partition where you have free disk space (like /home). Because kernel compile needs lot of disk space for object files like *.o. For this reason the /usr/src/linux MUST be a soft link pointing to your source directory.

After this, look in the next section to do make and install.

2.3. For the Impatient

  1. Unpack the sources

  2. Optional - Copy config file : You can copy the config file from your old linux kernel source tree to new kernel tree (may save time, if you want to reuse the old settings).

  3. make clean; make mrproper

  4. make xconfig

  5. make dep

  6. Give a unique name to your new Kernel - Edit /usr/src/linux/Makefile and change EXTRAVERSION

  7. nohup make bzImage

  8. 'make modules' and 'make modules_install'

  9. And you can go to lunch or go to bed (have nice Linux dreams in sleep) and when you come back the system is ready! And see the log with 'less nohup.out'.

  10. make install [num ] But NOT recommended - use cp /usr/src/linux/arch/i386/boot/bzImage /boot/bzImage.myker

  11. Configure GRUB or LILO.

  12. Reboot and check new kernel is booting

  13. Create emergency boot disk - bzdisk or mkbootdisk

  14. Optional - make rpm [num ] To build rpm packages

  15. Optional - make clean (If you want to free up disk space)

See details of above steps in the following sections....

2.4. Building New Kernel - Explanation of Steps

Details of the steps mentioned in the previous section:

Note: Below 'bash[num ]' denotes the bash prompt, you should type the commands that appear after the 'bash[num ]' prompt. Below are commands tested on Redhat Linux Kernel 2.4.7-10, but it should work for other distributions with very minor changes. It should also work for older kernel versions like 2.2, 2.0 and 1.3. It should also work for future or newer versions of kernel (with little changes - let me know).

  • Note: You can have many kernel images on your system. By following the steps below you do not overwrite or damage your existing kernel. These steps are very safe and your current kernel will be intact and will not be touched.

  1. Unpack the sources: Login in as 'root' throughout all these steps. Mount Redhat linux cdrom and install the linux kernel source rpm

    
	bash$ su - root
    	bash# cd /mnt/cdrom/RedHat/RPMS
    	bash# rpm -i kernel-headers*.rpm
    	bash# rpm -i kernel-source*.rpm
    	bash# rpm -i dev86*.rpm
    	bash# rpm -i bin86*.rpm
                  
    (The bin86*.rpm and 'as86' is required only for OLDER Linux systems like Redhat 5.x. Get Intel assembler 'as86' command from dev86*.rpm on cdrom or from bin86-mandrake , bin86-kondara ). Also make sure that /usr/src/linux is soft link pointing to proper unpacked source.
    
	bash# cd /usr/src
    	bash# ls -l    # You should see that /usr/src/linux is soft link pointing to source
    	lrwxrwxrwx    1 root     root           19 Jan 26 11:01 linux -> linux-2.4.18-19.8.0
    	drwxr-xr-x   17 root     root         4096 Jan 25 21:08 linux-2.4.18-14
    	drwxr-xr-x   17 root     root         4096 Mar 26 12:50 linux-2.4.18-19.8.0
    	drwxr-xr-x    7 root     root         4096 Jan 14 16:32 redhat
                  
    If it is not a soft link then do rename /usr/src/linux to /usr/src/linux-2.4.yy and create a soft link.

    NOTE: If you do not have lot of disk space in /usr/src then you can unpack the kernel source package on any partition where you have free disk space (like /home). Because kernel compile needs lot of disk space for object files like *.o. For this reason the /usr/src/linux MUST be a soft link pointing to your source directory.

  2. Optional - Copy config file : This step may save you time, if you want to reuse the old settings. Whenever you install the kernel, generally you put the config file in /boot. So, you can use the existing version of config file:

    
	bash# mv /usr/src/linux/.config  /usr/src/linux/.config.save
    	bash# cp /boot/config-2.4.18-19.8.0  /usr/src/linux/.config
                  
    Or another method is - you can copy the .config file from your old linux kernel source tree to new kernel tree
    
	bash# ls -l /usr/src/lin*  # You can see that /usr/src/linux is a soft link
    	bash# cd /usr/src/linux
    	bash# cp ../linux-old-tree/.config .  # Example cp ../linux-2.4.19/.config .
                  
    or one other method is - you can use "make oldconfig" which default all questions based on the contents of your existing ./.config file.

  3. Clean : Before doing mrproper below, you may want to backup the .config file.

    
	bash# cd /usr/src/linux
    	bash# cp .config .config.save
    	bash# make clean
    	bash# make mrproper  # Must do this if want to start clean slate or if you face lot of problems
                  

  4. Configure:

    • Start X-windows with 'startx'. If you are not able to start X-window then see next step below.

      
	bash# man startx
      	bash# startx
      	bash# cd /usr/src/linux
      	bash# make xconfig
                          

    • If you are not able to start X-window above then try -

      
		bash# export TERM=xterm
      		bash# make menuconfig
      	If you find scrambled display, then use different terminal emulators like vt100,
      	vt102, vt220 or ansi. The display will be scrambled and will have garbage
      	characters in cases where you use telnet to login to remote linux. In such
      	cases you should use the terminal emulators like vt100, vt220.
      	For example:
      		bash# export TERM=vt220
      		bash# export TERM=ansi
      	At a lower level of VT, use:
      		bash# export TERM=vt100
      		bash# make menuconfig
      	If the menuconfig command fails then try -
      		bash# make config
                          

    The "make xconfig" or "make menuconfig" brings up a user friendly GUI interface. And "make config" brings up command-line console mode interface. You can load the configuration file from /usr/src/linux/.config (dot config file. Note the dot before config). Click on button "Load Configuration from File". Within 'make xconfig' you must do these (to avoid problems) -

    • VERY IMPORTANT !!! : Select proper CPU type - Pentium 3, AMD K6, Cyrix, Pentium 4, Intel 386, DEC Alpha, PowerPC otherwise kernel compile will fail and even if it compiles, it will not boot!!

    • Select SMP support - whether single CPU or multiple CPUs

    • Filesystems - Select Windows95 Vfat, MSDOS, NTFS as part of kernel and not as loadable modules. (My personal preference, but you are free to pick your own option).

    • Enable the Loadable kernel modules support! With this option you can load/unload the device drivers dynamically on running linux system on the fly. See the Modules chapter at Section 3 .

    Save and Exit "make xconfig". All the options which you selected is now saved into configuration file at /usr/src/linux/.config (dot config file).

  5. Dep : And now, do -

    
	bash# make dep
                  

  6. Give a unique name to your new Kernel: You can give a name to your kernel, so that it is unique and does not interfere with others.

    
	bash# cd /usr/src/linux
    	bash# vi Makefile
                  
    Here look for EXTRAVERSION = -19.8.0_Blah_Blah_Blah and change to something like EXTRAVERSION = -19.8.0MyKernel.26Jan2003

  7. Do make: Read the following file (to gain some knowledge about kernel building. Tip: Use the color editor gvim for better readability.

    
	bash# gvim -R   /usr/src/linux/arch/i386/config.in
    	bash# man less
    	bash# less   /usr/src/linux/arch/i386/config.in
    	Type 'h' for help and to navigate press i, j, k, l, h or arrow, page up/down keys.
                  
    Now, give the make command -
    
		bash# cd /usr/src/linux
    		bash# man nohup
    		bash# nohup make bzImage &
    		bash# man tail
    		bash# tail -f nohup.out     (.... to monitor the progress)
    	This will put the kernel in /usr/src/linux/arch/i386/boot/bzImage
                    

  8. LOADABLE MODULES: Now, while the 'make' is cranking along in the previous step "Do make", you should bring up another new xterm shell window and follow these steps: This step is required ONLY if you had enabled Loadable module support in step "Configure Step" above. Loadable module are located in /lib/modules. You MUST do this step if you enabled or disabled any modules, otherwise you will get 'unresolved symbols' errors during or after kernel boot.

    
	# Bring up a new Xterm shell window and ...
    	bash# cd /usr/src/linux
    	# Redirect outputs such that you do not overwrite the nohup.out which is still running...
    	bash# nohup make modules 1> modules.out 2> modules.err  &
    	bash# make modules_install   # Do this, only after the above make command is successful
                  
    This will copy the modules to /lib/modules directory. See the Modules chapter at Section 3 .

  9. Now go to Lunch or Bed : Since both the make windows are cranking along, and now, you can go to lunch (chitchat, have nap) or go to bed (have nice Linux dreams in sleep) and when you wake up and come back the system is ready! You can check with command 'less nohup.out' to see the log of output.

    
	bash# cd /usr/src/linux
    	bash# less nohup.out
    	bash# less modules.err
    	bash# less modules.out
    	If no errors then do:
    	bash# make modules_install
                  

  10. bzImage: After bzImage is successful, copy the kernel image to /boot directory. You must copy the new kernel image to /boot directory, otherwise the new kernel MAY NOT boot. You must also copy the config file to /boot area to reflect the kernel image, for documentation purpose.

    
	bash# cp /usr/src/linux/arch/i386/boot/bzImage     /boot/bzImage.myker.26mar2001
    	# You MUST copy the config file to reflect the corresponding kernel image,
    	# for documentation purpose.
    	bash# cp /usr/src/linux/.config /boot/config-<your_kernelversion_date>
    	# Example: cp /usr/src/linux/.config /boot/config-2.4.18-19.8.0-26mar2001
                  
    NOTE : If you are planning to use the initrd in LILO or GRUB then you may want to build initrd and place it in /boot/initrd*.img. See the Appendix A at Section 13 .

  11. Configure GRUB or LILO : There are two options for boot loading under Redhat Linux - GRUB and LILO. Configure GRUB: GRUB is recent and much better tool than LILO and it is my first preference to use GRUB. LILO is an older technology. GRUB differs from bootloaders such as LILO in that "it can lie to MS Windows and make MS Windows believe that it's installed on the first partition even if it's not!!" . So you can keep your current Linux system where it is and install Windows on the side. See the Section 15 file. Configure LILO: LILO is older tool and see the Section 14 to configure LILO.

  12. Reboot the machine and at lilo press tab key and type 'myker' If it boots then you did a good job! Otherwise at lilo select your old kernel, boot and re-try all over again. Your old kernel is still INTACT and SAFE at say /boot/vmlinuz-2.0.34-0.6

  13. If your new kernel 'myker' boots and works properly, you can create the boot disk. Insert a blank floppy into floppy drive and -

    
	bash# cd /usr/src/linux
    	bash# make bzdisk
    	See also mkbootdisk -
    	bash# rpm -i mkbootdisk*.rpm
    	bash# man mkbootdisk
                  

  14. Build RPMs Optional - You can also build RPM packages of kernel, in case you want to install the new image on several machines.

    
	make rpm   # To build rpm packages
                    

  15. Clean: Optional - make clean (If you want to free up disk space)

2.5. Troubleshooting

Having any problems? See the Section 17 .

2.6. Post Kernel Building

See the Section 16 .