Kernel Compilation From Source For New Users Guide
From ArchWiki
Introduction
The Arch Linux Distribution and Kernel Source Linux-2.6.27.5 were used and tested for this guide. During the kernel configuration process using 'make menuconfig', the Kernel Local Version (revision) which was appended to the kernel release is "ARCHMOD". You can obviously use a different description. The procedures documented here should be more than adequate for all other Linux distributions with minor, if any, differences.
Version 2.6.x - Step by Step
1) Switch from the User prompt ($) to Root (#)
$ su Password: xxxxxx #
Note: The /usr/src directory is owned by Root. This is why Root is used.
2) Back up current kernel
Back up latest (current) kernel ".config" file and save to a /home/user directory
Example:
# cp /usr/src/linux-`uname -r`/.config ~/config.save
Note: It is recommended to back up the .config file as to simplify the kernel configuration process later on using 'make menuconfig'. Many of the Linux kernel defaults already installed on your Linux distribution are properly configured, however, other specifications often require further adjustment for maximum optimization and performance.
AND
Delete any symlinks in /usr/src and /boot directories (assuming these exist):
# cd /usr/src # rm linux # rm linux-2.6 # cd /boot # rm System.map
3) Preparing /usr/src
Goto /usr directory and check if /usr/src exists
# cd /usr # ls | grep src
If it does not exist, create /usr/src
# mkdir src
and proceed to step 4;
If it does exist and is empty, proceed to step 4;
(Optional) If it does exist and contains previous kernel data, you may erase it all if you choose to do so. This is not recommended as you may want your previous kernel as an emergency backup for your system and your GRUB menu since it is functional. It is safer to leave the previous kernel and directory intact. However, if you prefer to erase it, you can do it by going into /usr/src
# cd src
and removing it all
# rm -rf *
(Warning: Be very careful with "rm -rf *" - extremely dangerous if not used properly) and proceed to step 4;
4) Save latest Linux kernel source
Goto the linux kernel website and save latest Linux kernel source "linux-2.6.28.tar.bz2" to the /home/user directory and copy kernel source to /usr/src by clicking on the "F" on that web site;
# cp linux-2.6.28.tar.bz2 /usr/src
5) Unpacking and entering the tarball
Go to /usr/src, unpack the kernel source, enter the source directory
# cd /usr/src # tar -xvjf linux-2.6.28.tar.bz2 # cd /usr/src/linux-2.6.28
6) Make (or edit) two symlinks to the kernel
# ln -s /usr/src/linux-2.6.28 /usr/src/linux # ln -s /usr/src/linux-2.6.28 /usr/src/linux-2.6
Note: Why do we create this two symlinks? The Linux kernel sources may sometimes (more often not) be picked up from a “linux” or "linux-2.6" directory when they are packaged into a “tar” archive file; when you unpack them, therefore, they may sometimes (more often not) expect to go back into a “linux” or "linux-2.6" directory. If you only have a single “linux” directory, and you unpack them into /usr/src without doing the steps above, they will overwrite some of your existing source files, and leave you with a real mess. You’ll have old and new files all mixed together. In a situation like that, the only thing to do is remove the entire “linux” directory and start over. Using a symlink to point to the current version — and keeping each version in its own directory — makes it much easier to keep everything under control. These soft symlinks are created more as a safety measure, to be conservative - just in case.
7) Reset the source directory
Clean out all previous configurations and reset the source directory to a pristine state. The main reason for doing this is that some files do not automatically get rebuilt, which can lead to failed builds, or at worst, a buggy kernel;
# make mrproper
8) Copy backup .config
Copy previously saved backup .config file (config.save) to /usr/src/linux-2.6.28;
# cp /home/USERNAME/config.save /usr/src/linux-2.6.28/.config
9) Configure current kernel source using backup .config file
Note: This is the most crucial step in customizing the kernel to reflect your computer's precise specifications. By setting the configurations in 'menuconfig' properly, your kernel and computer will function most efficiently. Please see "Appendix A" below this guide for common items that are adjusted for a typical kernel.
# make menuconfig
When done with configuration, save .config file.
10) Clean up
Clean up miscellaneous object files. This step is required for all versions of the kernel;
# make clean
Note: The usual preceding command (# make dep) step is only required for the 2.4.x kernel tree in case you are wondering why it is missing in this Linux kernel guide.
11) Kernel Build
We are now (finally) ready to start the actual kernel build;
# make bzImage
Note: This may take a while, be patient;
12) Create modules
After you have created the kernel, now you need to create all the loadable modules;
# make modules
Note: This will take much longer than step 11, be patient;
Once the modules are built they can be installed;
# make modules_install
13) Copy the new kernel to your /boot directory
# cp -v arch/x86/boot/bzImage /boot/vmlinuz-2.6.28-ARCHMOD
14) Build a ramdisk if necessary (Recommended)
# mkinitcpio -k 2.6.28-ARCHMOD -g /boot/kernel26-ARCHMOD.img
Note: In Linux distributions other than Arch Linux, the above command might be:
# mkinitrd -k vmlinux-REVISION -i initrd-REVISION
OR
# mkinitrd /boot/initrd-2.6.0.img 2.6.0
Note: What's an initial RAM disk and why is it necessary? The initial RAM disk (initrd option in the GRUB menu, or, the file "kernel26-ARCHMOD.img") is an initial root file system that is mounted prior to when the real root file system is available. The initrd is bound to the kernel and loaded as part of the kernel boot procedure. The kernel then mounts this initrd as part of the two-stage boot process to load the modules to make the real file systems available and get at the real root file system. The initrd contains a minimal set of directories and executables to achieve this, such as the insmod tool to install kernel modules into the kernel. In the case of desktop or server Linux systems, the initrd is a transient file system. Its lifetime is short, only serving as a bridge to the real root file system. In embedded systems with no mutable storage, the initrd is the permanent root file system.
15) System.map
Copy System.map to /boot and create symlink
# cp System.map /boot/System.map-2.6.28 # ln -s /boot/System.map-2.6.28 /boot/System.map
Note: What is "System.map"? The System.map file is not required for booting Linux. It is a type of "phone directory" list of functions in a particular build of a kernel. The System.map contains a list of kernel symbols (i.e function names, variable names etc) and their corresponding addresses. This "symbol-name to address mapping" is used by...
- Some processes like klogd, ksymoops etc
- By OOPS handler when information has to be dumped to the screen during a kernel crash (i.e info like in which function it has crashed).
Hence, it can said that the kernel itself doesn't need this file and thus the absence of System.map in the /boot directory doesn't affect kernel booting.
After completing all steps above, you should have the following 3 files and 1 soft symlink in your /boot directory along with any other previously existing files:
vmlinuz-2.6.28-ARCHMOD (File - Kernel) kernel26-ARCHMOD.img (File - Ramdisk) System.map-2.6.28 (File - System Map) System.map (Soft Symlink)
16) Configure Bootloader
Edit /boot/grub/menu.lst accordingly so that the partitions and kernel match and you are done!
Note: The GRUB configuration must be done in order to boot into your new Ramdisk and Kernel.
17) Testing
Reboot your system and test that the kernel loaded correctly.
Appendix A
These are some examples of Several Typical Adjustments Made to the Kernel in 'menuconfig'
-
- General Setup --->
- (-ARCHMOD) Local version - append to kernel release
-
- [*] Enable loadable module support --->
- [*] Forced module loading
- [*] Module unloading
- [*] Forced module unloading
-
- -*- Enable the block layer --->
- [*] Support for Large Block Devices
- [*] Support for Large Single Files
-
- Processor type and features --->
- [ ] Symmetric multi-processing support
- Subarchitecture Type (PC-compatible) --->
- Processor family (Athlon/Duron/K7) --->
- Preemption Model (Preemptible Kernel (Low-Latency Desktop)) --->
- [*] Local APIC support on uniprocessors
- [*] IO-APIC support on uniprocessors
- High Memory Support (off) --->
- Memory model (Flat Memory) --->
- [ ] Math emulation
- Timer frequency (300 HZ) --->
-
- Power management options --->
- [*] ACPI (Advanced Configuration and Power Interface) Support --->