Compiling a 'stock' kernel.

With many many thanks to Greg Kroah-Hartman for the book LINUX KERNEL IN A NUTSHELL which helped separate the wood from the trees. The book's web site is at http://www.kroah.com/lkn.

You can download of all the chapters in pdf format from http://www.kernel.org/pub/linux/kernel/people/gregkh/lkn/lkn_pdf.tar.bz2 or, if you want to update the book yourself, get the books own source code in DocBook format from http://www.kernel.org/pub/linux/kernel/people/gregkh/lkn/lkn_xml.tar.bz2.

You have to love Open Source don't you? How many Microsoft books do you have whereby you can update the book itself?

Sometimes you simply have to compile a stock kernel for your distribution, just sometimes. I've had to do it to get my qdos module to compile because the installed one is too old - but I'm not allowed to change it - so this is what I did.

  • Download a stock kernel by browsing to http://www.kernel.org/.
  • Locate the line near the top that says The latest stable version of the Linux kernel is.
  • Click on the F on the same line, just after the date. This will download a full source tree for the latest stable kernel.

This kernel will be different from the one your distro supplied for you - in my case, Suse Linux Enterprise Server which uses a 2.6.16.54 because my qdos module needs a 2.6.18 or higher kernel, the installed one is no good to me!

We now need somewhere to put said kernel. I do my compilations in my home directory, so:

mkdir linux
mv linux-2.6.25.6.tar.bz2 linux/
cd linux

Warning: If you intend installing any software from source code, you better not overwrite your /usr/src/linux directory with the source code from the stock kernen. Leave that location for the installed kernel 's source code.

Now it's time to unpack the source tree. Remember this is a stock kernel and has not been adulterated by the distro supplier.

tar --bzip2 -xvf linux-2.6.25.6.tar.bz2

Because this is a stock kernel, you can configure it to be like your distro in as much as the configured options and modules are concerned. It is possible that your distro has made code changes to their supplied version. Not to worry. When developing for the kernel, it's best to start with a stock kernel and work on that.

From 2.6 onwards, building a kernel is quite a simple affair and no longer frightening! First check that the extract has completed and you should find in your new linux directory a new sub-directory called linux-2.6.25.6.

ls
linux-2.6.25.6  linux-2.6.25.6.tar.bz2

So far so good. We need a file named .config now. We can build this manually if you like - and know exactly what options your distro's kernel has configured, but it's easier to copy the configuration information directly from your running kernel.

ls /proc/config.gz
config.gz

That file, config.gz is a copy of the running kernel that has been created in the /proc virtual filesystem for you. Let's grab hold of it, and uncompress it.

cp /proc/config.gz ./
gunzip config.gz
ls
config  linux-2.6.25.6  linux-2.6.25.6.tar.bz2

The file needs to be in the root of our source tree, and needs to be called .config, so we copy it into the correct place next.

cp config linux-2.6.25.6/.config

Job done. You can now either build a stock copy of your running kernel, or make some changes. I removed a couple of built in options from my kernel that related to bluetooth as I don't have a bluetooth option on this PC. The command was:

make menuconfig

You can do this now if you like, and set the kernel up to remove things you don't need or want, or make them modules etc. When done, come back and we will compile the kernel.

make

Did that surprise you? A one word command to build a new kernel? Surely not! Welcome to Linux! You don't even have to be root to build a new kernel.

This will run for a wee while, depending on the speed of your system, so let it run. Unless you messed up your configuration, it should finish after it has done, and there should be no errors.

Drink lots of coffee, watch TV whatever you like. It will finish eventually, and probably just as soon as you go off to make another cup of coffee.

Once done, you have a new stock kernel. The chances are it's a different version from the one you are running, so the /lib/modules/<kernel_version> directory tree doesn't exist, so we have to create it properly.

ls /lib/modules
2.6.16.54-0.2.5-default
 
su root
password:
 
make modules_install
 
  INSTALL ...
...
...
  INSTALL sound/usb/snd-usb-lib.ko
  INSTALL sound/usb/usx2y/snd-usb-usx2y.ko
  DEPMOD  2.6.25.6-default
 
ls /lib/modules
2.6.16.54-0.2.5-default  2.6.25.6-default

And now, the moment of truth, installing our new kernel.

make install

The output from that command is as follows.

sh /media/usbdisk/norman/linux/linux-2.6.25.6/arch/x86/boot/install.sh 2.6.25.6-default arch/x86/boot/bzImage System.map "/boot"
Root device:    /dev/disk/by-id/scsi-SATA_WDC_WD800JD-60L_WD-WMAM9P907202-part2 (/dev/sda2) (mounted on / as ext3)
Module list:    ata_piix processor thermal fan jbd ext3 edd (xennet xenblk)
 
Kernel image:   /boot/vmlinuz-2.6.25.6-default
Initrd image:   /boot/initrd-2.6.25.6-default
Shared libs:    lib/ld-2.4.so lib/libacl.so.1.1.0 lib/libattr.so.1.1.0 lib/libblkid.so.1.0 lib/libc-2.4.so lib/libcom_err.so.2.1 lib/libdl-2.4.so lib/libext2fs.so.2.4 lib/libhistory.so.5.1 lib/libncurses.so.5.5 lib/libpthread-2.4.so lib/libreadline.so.5.1 lib/librt-2.4.so lib/libuuid.so.1.2 lib/libnss_files-2.4.so lib/libnss_files.so.2 lib/libgcc_s.so.1
 
Driver modules: scsi_mod sd_mod dock libata ata_piix processor thermal fan edd
Filesystem modules:     jbd ext3
Including:      initramfs fsck.ext3
Bootsplash:     SuSE-NLD (800x600)
28390 blocks

That's it. Next time you reboot you will (should have) a kernel named 2.6.25.6-default on your list of available kernels.

If you want to immediately start using the new kernel, reboot and select the newly installed kernel from the list.

How easy was that?

  • linux/kernel/start.txt
  • Last modified: 2008/06/17 10:29
  • by norman