Sungju's Slow Life

Personal journal

How could I reduce kernel’s binary size as small as distribution’s size

The basic steps to compile kernel and modules are similar to following.

make modules && make bzImage && make modules_install && make install

It will install the kernel related files into the proper location after compile modules and kernel. But maybe you could be surprised by the final binaries size. Sometimes, it shows more than 10 times bigger size than distributor’s module and kernel size. It even happened with distribution’s configuration.

I will show some basic tips you can use to build your own kernel from distribution’s source files without any increase in size.

First, you should download source files from distributor’s site or get it from distribution media. In my case, I downloaded the kernel rpm from Red Hat Network (

After installing source rpms and you need to rebuild source codes from the spec.

$ rpm -ivh kernel-2.6.18-128.1.1.el5.src.rpm
$ cd /usr/src/redhat/SPECS
$ rpmbuild -bp --target=i686 kernel-2.6.spec
$ cd /usr/src/redhat/BUILD/kernel-2.6.18/linux-2.6.18.x86_64/

You can see the patched kernel codes in BUILD directory. After doing some modification for your purpose, it is time to rebuild kernel. To reduce kernel size, add ‘INSTALL_MOD_STRIP=1’ options in make command.

make modules && make bzImage && make INSTALL_MOD_STRIP=1 modules_install && make INSTALL_MOD_STRIP=1 install

It will strip off unnecessary debug information from the compiled images. If you can keep another kernel images separately which has debug information for the debugging purpose.

This methods work for RHEL5 and Fedora 11 or 12 (I couldn’t check this with earlier version but would be applied for earlier version of Fedora). If you want to reduce sizes in RHEL4 or earlier version, you should strip off DEBUG_* related options from the configuration file before do compile.

Following is the part of RHEL4 spec file.

# set the EXTRAVERSION to custom, so that people who follow a kernel building howto
# don't accidentally overwrite their currently working moduleset and hose
# their system
perl -p -i -e "s/^EXTRAVERSION.*/EXTRAVERSION = -%{release}custom/" $RPM_BUILD_ROOT/usr/src/linux-%{KVERREL}/Makefile

# some config options may be appropriate for an rpm kernel build but are less so for custom user builds,
# change those to values that are more appropriate as default for people who build their own kernel.
perl -p -i -e "s/^CONFIG_DEBUG_INFO.*/# CONFIG_DEBUG_INFO is not set/" $RPM_BUILD_ROOT/usr/src/linux-%{KVERREL}/configs/*
perl -p -i -e "s/^.*CONFIG_DEBUG_PAGEALLOC.*/# CONFIG_DEBUG_PAGEALLOC is not set/" $RPM_BUILD_ROOT/usr/src/linux-%{KVERREL}/configs/*
perl -p -i -e "s/^.*CONFIG_DEBUG_SLAB.*/# CONFIG_DEBUG_SLAB is not set/" $RPM_BUILD_ROOT/usr/src/linux-%{KVERREL}/configs/*
perl -p -i -e "s/^.*CONFIG_DEBUG_SPINLOCK.*/# CONFIG_DEBUG_SPINLOCK is not set/" $RPM_BUILD_ROOT/usr/src/linux-%{KVERREL}/configs/*
perl -p -i -e "s/^.*CONFIG_DEBUG_HIGHMEM.*/# CONFIG_DEBUG_HIGHMEM is not set/" $RPM_BUILD_ROOT/usr/src/linux-%{KVERREL}/configs/*
perl -p -i -e "s/^.*CONFIG_MODULE_SIG.*/# CONFIG_MODULE_SIG is not set/" $RPM_BUILD_ROOT/usr/src/linux-%{KVERREL}/configs/*

As you can see, it strips off some DEBUG related options to blank.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: