Building a linux Kernel

From Initq

Jump to: navigation, search

Here are the tools you will need to build a linux kernel.

Contents

Introduction

There are over 7 million lines of C and some assembly code in the linux kernel. Most distributions give you a pre-compiled kernel will all types of goodies that you may or may not need. You may still want to compile your own kernel and it is always good practice to compile for many reasons. First of all is the learning factor and second are your needs. You should always take or leave out according to your needs.

PAE Kernel

Physical Address Extension (PAE) is a feature to allow (32-bit) x86 processors to access a physical address space (including random access memory and memory mapped devices) larger than 4 gigabytes. These precompiled kernels were needed in the past when you needed more momeory than 4GIG. This is not needed with newer kernels.


Before you start anything please make sure your system is up to date by running

  • apt-get update; apt-get upgrade

Tools to build the Kernel

You only need three things to compile a kernel: compiler, linker and make utility.

Compiler

You will need gcc C compiler. You may get it from http://gcc.gnu.org.

qais:~# gcc --version
gcc (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Linker

You will also need a linker and assembler which comes bundled in a package called binutils. You may get binutils from http://www.gnu.org/software/binutils. ld is the gnu linker and as is the gnu assembler.

qais:~# ld -v
GNU ld version 2.17 Debian GNU/Linux
 
qais:~# as --version
GNU assembler 2.17 Debian GNU/Linux
Copyright 2005 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License.  This program has absolutely no warranty.
This assembler was configured for a target of `i486-linux-gnu'.

Binutils also include:

   * addr2line - Converts addresses into filenames and line numbers.
   * ar - A utility for creating, modifying and extracting from archives.
   * c++filt - Filter to demangle encoded C++ symbols.
   * dlltool - Creates files for building and using DLLs.
   * gold - A new, faster, ELF only linker, still in beta test.
   * gprof - Displays profiling information.
   * nlmconv - Converts object code into an NLM.
   * nm - Lists symbols from object files.
   * objcopy - Copys and translates object files.
   * objdump - Displays information from object files.
   * ranlib - Generates an index to the contents of an archive.
   * readelf - Displays information from any ELF format object file.
   * size - Lists the section sizes of an object or archive file.
   * strings - Lists printable strings from files.
   * strip - Discards symbols.
   * windmc - A Windows compatible message compiler.
   * windres - A compiler for Windows resource files.

Make

make is a build tool that walks the kernel source tree to determine which files need to be compiled, and then calls the compiler and other build tools. You make download it from http://www.gnu.org/software/make.

qais:~# make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
 
This program built for i486-pc-linux-gnu
 
Use -j flag for multiple processes.

Tools to Use the Kernel

There are small number of programs that might already be installed on your system that are needed to build a kernel.

Util-linux

suite of essential utilities for any Linux system. It contains the following programs: addpart, agetty, blockdev, cal, cfdisk, chfn, chkdupexe, chrt, chsh, col, colcrt, colrm, column, ctrlaltdel, cytune, ddate, delpart, display-services, dmesg, elvtune, fastboot, fasthalt, fdformat, fdisk, flock, fsck.cramfs, fsck.minix, getopt, halt, hexdump, hwclock, initctl, ionice, ipcrm, ipcs, isosize, kill, last, line, logger, login, look, losetup, mcookie, mesg, mkfs, mkfs.bfs, mkfs.cramfs, mkfs.minix, mkswap, more, mount, namei, need, newgrp, partx, pg, pivot_root, provide, ramsize, raw, rdev, readprofile, reboot, rename, renice, reset, rev, rootflags, script, scriptreplay, setsid, setterm, sfdisk, shutdown, simpleinit, swapoff, swapon, taskset, tailf, tunelp, ul, umount, vidmode, vipw, wall, whereis, and write.

Get the utility from util-linux.

internal:/home/apptricity/backup_scripts# sfdisk --version
sfdisk (util-linux-ng 2.13.1.1)internal:/home/apptricity/backup_scripts#
internal:/home/apptricity/backup_scripts#
internal:/home/apptricity/backup_scripts# fdformat --version
fdformat (util-linux-ng 2.13.1.1)
internal:/home/apptricity/backup_scripts#

module-init-tools

These tools are needed if you want to add or remove kernel modules while the kernel is running. It is useful to compile device drivers as modules and then load only the ones that correspond to the hardware present in the system.

The module-init-tools (m-i-t) package provides many of the utilities needed by Linux systems for managing loadable Linux Kernel Modules. This includes depmod, insmod, rmmod, modprobe and tools like modinfo. You usually won't have to build and install this package for yourself because it's part of every major Linux distribution.

For 2.6 kernels you need module-init-tools package. get it from module-init-tools

internal:/home/apptricity/backup_scripts# depmod -V
module-init-tools 3.4

ncurses

You need

  • libncurses5-dev - Developer's libraries and docs for ncurses for make menuconfig

More tools

you also need fakeroot wget bzip2 tar e2fsprogs jfsutils reiserfsprogs xfsprogs quotatool nfs-common nfs-kernel-server unionfs-utils

  • udev - /dev/ and hotplug management daemon
  • procps - /proc file system utilities tools like ps and top as well as many others
  • pcmciautils - PCMCIA utilities for Linux 2.6
  • curl

Filesystem specific tools

A wide range of filesystems tools are necessary to create, format, configure and fix disk partitions.

ext2/ext3/ext4

This is the most common filesystem on linux based systems. You will need the e2fsprogs package. you may get it at e2fsprogs.

internal:/backup1/app_demo/d_drive/d_drive# tune2fs
tune2fs 1.41.3 (12-Oct-2008)

What kernel to download

Distro kernel is never the latest release. When 2.6.22 is released then work begins on 2.6.22-rc1 then 2.6.22.1 is released as stable and work beings on rc2 and so on all the way to rc5. After rc5 you move on to 2.6.23.

vmone:/usr/src# curl http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.35.1.tar.gz -o linux-2.6.35.1.tar.gz  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 84.2M  100 84.2M    0     0   303k      0  0:04:44  0:04:44 --:--:--  298k

Unzip the new kernel

  • vmone:/usr/src# tar -xvzf linux-2.6.35.1.tar.gz

Modifying the Config

You can copy your old config into your new kernel.

  • cp /boot/config-2.6.26-2-amd64 /usr/src/linux-2.6.35.1/config_old

Configure

  • make menuconfig

Compile

  • make
To make the build elsewhere
make O=~/linux/linux-2.6.17.11
you can make just the modules you need with
make M=drivers/usb/serial
to build one
make drivers/usb/serial/visor.ko

There are 5 steps to compiling:

  1. make menuconfig
  2. make
  3. make modules
  4. make modules_install (modules will be install in /lib/modules/<kernel version>)
  5. make install

make install runs a program called /sbin/installkernel which will create the System.map vmlinuz and config to /boot folder.

Create initrd image

  • # mkinitrd -o /boot/initrd.img-2.6.25 /lib/modules/2.6.25
  • # mkinitramfs -o /boot/initrd.img-2.6.35.1 /lib/modules/2.6.35.1/

Update Grub

#vi /boot/grub/menu.lst
 
title           Debian GNU/Linux, kernel 2.6.35.1
root            (hd0,0)
kernel          /boot/vmlinuz-2.6.35.1 root=/dev/hda1 ro quiet
initrd          /boot/initrd.img-2.6.35.1
vmone:/boot/grub# update-grub 
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-2.6.35.1
Found kernel: /boot/vmlinuz-2.6.26-2-amd64
Updating /boot/grub/menu.lst ... done

Reboot

vmone:~# uname -a
Linux vmone 2.6.35.1 #1 SMP Wed Aug 11 17:24:24 CDT 2010 x86_64 GNU/Linux

Where are the header files

  • /lib/modules
Personal tools