How to build a custom kernel?
Environment
-
Red Hat Enterprise Linux 4
-
Red Hat Enterprise Linux 5
-
Red Hat Enterprise Linux 6
Issue
-
Even though Red Hat does not provide any support for the customized kernel, as a user, you want to rebuild a kernel (e.g. to test a change or apply a local fix until errata are released).
-
When you recompile the kernel, the size of each binary is a lot bigger than the one Red Hat provides. How can the binaries' size be reduced to sizes similar to those in the Red Hat shipped kernel binary RPM?
Resolution
The following information has been provided by Red Hat, but is outside the scope of the posted Service Level Agreements and support procedures (Production Support - Red Hat Customer Portal). The information is provided as-is and any configuration settings or installed applications made from the information in this article could make the Operating System unsupported by Red Hat Global Support Services. The intent of this article is to provide information to accomplish the system's needs. Use of the information in this article at the user's own risk.
To build a custom kernel, you need to download the source rpm first. The kernel-version.src.rpm can be downloaded from the Red Hat Customer Portal. The steps to rebuild the kernel are described below.
- Install source rpm and apply the Red Hat patches to the original kernel source
# rpm -ivh kernel-2.6.x-x.src.rpm
# cd /usr/src/redhat/SPECS
# rpmbuild -bp --target=*i686* kernel.spec
These commands will set up the kernel source in the /usr/src/redhat/BUILD/ directory.
- Move to the directory to rebuild kernel:
# cd /usr/src/redhat/BUILD/kernel-*2.6.x*/linux-*2.6.x*
- Build the kernel:
# make bzImage ; make modules ; make install; make modules_install
This will compile modules and bootable kernel image.
If the build succeeds, it will install the modules into the /lib/modules/2.6.x../ directory and install the kernel image into /boot directory together with some related files.
However, this process is not devoid of problems. if the user has limited resources for their file system because the kernel built with those commands create much bigger files compared to the Red Hat's files. It happens because Red Hat's kernel is stripped of some unnecessary information.
Alternative way for Step 3 :
- Build a kernel with
INSTALL_MOD_STRIPoption turned on:
# make bzImage && make modules && make INSTALL_MOD_STRIP=1 install && make INSTALL_MOD_STRIP=1 modules_install
- In Red Hat Enterprise Linux 4, the kernel Makefile doesn't have this
INSTALL_MOD_STRIPoption, so those commands cannot be used. The spec file in Red Hat Enterprise Linux 4 uses a different approach. The following is a snippet from thekernel-2.6.specfile:
# set the EXTRAVERSION to <version>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/*
In RHEL4, it doesn't use strip command. Instead, it uses compile options to remove debug information at the time of kernel compile. So, run the previous commands before kernel compilation.
If these steps feel tedious, reuse the original rpm spec file.
For detailed information for these steps at the following link: Content from fedoraproject.org is not included.Content from fedoraproject.org is not included.http://fedoraproject.org/wiki/Building_a_custom_kernel.
Comments
Refer to the Red Hat Global Support Services Production Support Scope of Coverage for further details and note that rebuilt RPMs are considered modified RPMs.
If you wish to see Red Hat make particular changes to the kernel, please open a support case with Red Hat Global Support Services and request that these changes be considered for a feature request.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.