Skip to content
Tags

,

How to compile custom kernel on Centos/Xen or optimize CS:S server

by admin on May 20th, 2011

To be able to perform a successful kernel build, you will need to install the following packages:

  • yum groupinstall "Development Tools" # This will ensure that you have all the required tools for the build.
  • yum install ncurses-devel # This is required to enable a make *config command to execute correctly.
  • yum install qt-devel # This is only necessary if you wish to use make xconfig instead of make gconfig or make menuconfig.
  • The full kernel source tree.
yum install kernel-devel
or
yum install kernel-xen-devel

As root, install the packages rpm-build, redhat-rpm-config and unifdef:

[[email protected]]# yum install rpm-build redhat-rpm-config unifdef

As an ordinary user, not root, create a directory tree based on ~/rpmbuild:

[[email protected]]$ mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

[[email protected]]$ echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros

Once you have located the source file, you can install it by running, for example:

[[email protected]]$ rpm -i http://mirror.centos.org/centos/5/updates/SRPMS/kernel-2.6.18-238.9.1.el5.src.rpm 2>&1 | grep -v mockb

Now that the source rpm is installed, unpack and prepare the source files:

[[email protected]]$ cd ~/rpmbuild/SPECS

[[email protected] SPECS]$ rpmbuild -bp --target=`uname -m` kernel-2.6.spec 2> prep-err.log | tee prep-out.log

The kernel source tree will now be found in the directory ~/rpmbuild/BUILD/.

Configuring the kernel

With the buildroot correctly set up, it’s time to modify the kernel configuration. Change directory to ~/rpmbuild/BUILD/kernel-2.6.18/linux-2.6.18.`uname -m`/ and copy into this directory, as .config, one of two files:

[[email protected]]$ cd ~/rpmbuild/BUILD/kernel-2.6.18/linux-2.6.18.`uname -m`

Either –

[[email protected]]$ cp configs/kernel-2.6.18-`uname -m`[-type].config .config

– or –

[[email protected]]$ cp /boot/config-`uname -r` .config

First run make oldconfig. Now you should run either make menuconfig, make gconfig or make xconfig to customize the kernel configuration. Once complete, remember to save your changes.

For CS:S server :

############################

if this doesnt pop up a nice wizard, you will need to install ncurses-devel and ncurses.

Now we are inside menuconfig wizard change these settings:
Processor type and features —>
NO Tickless System (Dynamic Ticks)
YES High Resolution Timer Support
Processor family (Hit Enter and choose your processor) —>
Preemption Model (Hit Enter and choose: Preemptible Kernel (Low-Latency Desktop))
Timer frequency (Hit enter and choose: 1000 HZ) —>

Power management options (ACPI, APM) —>
Disable everything except ACPI
ACPI (Advanced Configuration and Power Interface) Support
YES ACPI Support
CPU Frequency scaling —> OFF

First off, lets edit include/linux/jiffies.h

scroll down and find this section:

Code:
#elif HZ >= 384 && HZ < 768
# define SHIFT_HZ       9
#elif HZ >= 768 && HZ < 1536
# define SHIFT_HZ       10
#else
# error You lose.
#endif

and change it to this:

Code:
#elif HZ >= 384 && HZ < 768
# define SHIFT_HZ       9
#elif HZ >= 768 && HZ < 1536
# define SHIFT_HZ       10
#elif HZ >= 1536 && HZ < 3072
# define SHIFT_HZ       11
#else
# error You lose.
#endif

basically adding the 11th statement. Save that

Next we want to edit include/asm-<distro>/param.h
where distro is your architecture type, basically asm-x86_64 for 64bit or asm-i386 for 32bit.

At the top of the file you will find this section

Code:
#ifdef __KERNEL__
# define HZ             CONFIG_HZ       /* Internal kernel timer frequency */
# define USER_HZ        100            /* .. some user interfaces are in "ticks" */
# define CLOCKS_PER_SEC         (USER_HZ)       /* like times() */
#endif

Change the USER_HZ from 100 to 1500 and only that:

Code:
#ifdef __KERNEL__
# define HZ             CONFIG_HZ       /* Internal kernel timer frequency */
# define USER_HZ        1500            /* .. some user interfaces are in "ticks" */
# define CLOCKS_PER_SEC         (USER_HZ)       /* like times() */
#endif

Save it. And thats it.

#####################################

Now you will need to modify the kernel specification file.

[[email protected]]$ cd ~/rpmbuild/SPECS

[[email protected] SPECS]$ cp kernel-2.6.spec kernel-2.6.spec.distro
[[email protected] SPECS]$ vi kernel-2.6.spec

 

Add, as the first line of the .config file, either:

# i386

– or –

# x86_64

Copy the .config file back to the configs/ directory. This is basically the opposite of the earlier copy command:

[[email protected]]$ cp .config configs/kernel-2.6.18-`uname -m`[-type].config

The final step is to copy the entire contents of the configs/ directory to the ~/rpmbuild/SOURCES/ directory.

[[email protected]]$ cp configs/* ~/rpmbuild/SOURCES

Now you will need to modify the kernel specification file.

[[email protected]]$ cd ~/rpmbuild/SPECS

[[email protected] SPECS]$ cp kernel-2.6.spec kernel-2.6.spec.distro
[[email protected] SPECS]$ vi kernel-2.6.spec

At line 73, the definition of buildid is commented out. This must be uncommented and given a value to avoid a conflict with your currently installed kernel. Change the line in similar manner to the example below:

 

%define buildid .your_identifier
There should be no space between the “%” and the word “define”.

Starting at line 12418, there is a block of code that has to be commented out. This block of code begins with the statement #if a rhel kernel, apply the rhel config options. Comment out the following 25 lines to build customised CentOS-5 kernels ( <!> This step is not required to build customised CentOS-4 kernels.):

 

#if a rhel kernel, apply the rhel config options
#%if 0%{?rhel}
#  for i in %{all_arch_configs}
#  do
#    mv $i $i.tmp
#    $RPM_SOURCE_DIR/merge.pl $RPM_SOURCE_DIR/config-rhel-generic $i.tmp > $i
#    rm $i.tmp
#  done
#%ifarch x86_64 noarch
#  for i in kernel-%{kversion}-x86_64*.config
#  do
#    mv $i $i.tmp
#    $RPM_SOURCE_DIR/merge.pl $RPM_SOURCE_DIR/config-rhel-x86_64-generic $i.tmp > $i
#    rm $i.tmp
#  done
#%endif
#%ifarch ppc64 noarch
#  #CONFIG_FB_MATROX is disabled for rhel generic but needed for ppc64 rhel
#  for i in kernel-%{kversion}-ppc64.config
#  do
#    mv $i $i.tmp
#    $RPM_SOURCE_DIR/merge.pl $RPM_SOURCE_DIR/config-rhel-ppc64-generic $i.tmp > $i
#    rm $i.tmp
#  done
#%endif
#%endif

Finally, if you have any patches to apply, you need to make reference to them by adding two lines for each patch. After line 6320, which should be near the end of the patch declarations, add your declaration starting with the number 40000, so that your patch is not in any danger of conflicting with the RHEL/CentOS kernel patch space. For example:

 

Patch40000: my-custom-kernel.patch

After line 12394 add the line to apply your patch. All you need to do is add is the patch number you declared earlier and rpmbuild will automagically apply it for you. For example:

 

%patch40000 -p1

Building the new kernel

Start the build:

 

[[email protected] SPECS]$ rpmbuild -bb --target=`uname -m` kernel-2.6.spec 2> build-err.log | tee build-out.log

For kernels >= 2.6.18-53.el5, you can add some useful options to the rpmbuild command by using the –with and/or –without flags and associated arguments. The options to note are:

 

--with baseonly
--with xenonly
--without up
--without xen
--without debug
--without debuginfo
--without fips
--without kabichk

For example, to build just the base kernel packages use:

 

--with baseonly --without debug --without debuginfo

To build just the xen kernel packages use:

 

--with xenonly --without debug --without debuginfo

To build just the PAE kernel packages use:

--without up --without xen --without debug --without debuginfo

When the build completes, your custom kernel rpm files will be found in the ~/rpmbuild/RPMS/`uname -m`/ directory. Make sure that you install those files, as root, using a rpm -ivh kernel-*.rpmcommand. Note: If you have built a kernel version that is older than a currently installed version you will also have to use the –oldpackage flag with the rpm command.

UNDER NO CIRCUMSTANCES use a rpm -Uvh command to install your kernel as this will update (overwrite) the currently installed version. Hence if you have a problem with your custom kernel, you will not be able to revert to the previous, working, version.

Sources from:

http://wiki.centos.org/HowTos/Custom_Kernel

http://wiki.centos.org/HowTos/I_need_the_Kernel_Source

and

http://forums.srcds.com/viewtopic/8378/%7Bpage%7D

 

One Comment
  1. Wow, fantastic blog format! How long have you ever
    been blogging for? you make running a blog look easy. The total glance of your web site is fantastic, let alone the content!

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS