From Fedora Project Wiki

m (→‎Copy the Source Tree and Generate a Patch: remove -v from example cp options; consistently use $ver in cp and diff sections)
(→‎Configure Kernel Options: replaced <version> with 2.6.$ver and <arch> with $arch to be consistent with previous section)
Line 72: Line 72:
This step is for modifying the options the kernel is configured with. This step is optional. If no configuration changes are needed, proceed to "Prepare Build Files".
This step is for modifying the options the kernel is configured with. This step is optional. If no configuration changes are needed, proceed to "Prepare Build Files".


# Change to the kernel source tree directory:<pre>cd ~/rpmbuild/BUILD/kernel-<version>/linux-<version>.<arch>/</pre>
# Change to the kernel source tree directory:<pre>cd ~/rpmbuild/BUILD/kernel-2.6.$ver/linux-2.6.$ver.$arch/</pre>
# Select the desired configuration file from <code>~/rpmbuild/BUILD/kernel-<version>/linux-<version>.<arch>/configs</code>. Copy the desired config file to <code>~/rpmbuild/BUILD/kernel-<version>/linux-<version>.<arch>/.config</code>: <pre>cp configs/<desired-config-file> .config</pre>
# Select the desired configuration file from <code>~/rpmbuild/BUILD/kernel-2.6.$ver/linux-2.6.$ver.$arch/configs</code>. Copy the desired config file to <code>~/rpmbuild/BUILD/kernel-2.6.$ver/linux-2.6.$ver.$arch/.config</code>: <pre>cp configs/<desired-config-file> .config</pre>
# Run the following command: <pre>make oldconfig</pre>
# Run the following command: <pre>make oldconfig</pre>
# Then run the following command, selecting and saving the desired kernel options from the text-based UI:<pre>make menuconfig</pre>
# Then run the following command, selecting and saving the desired kernel options from the text-based UI:<pre>make menuconfig</pre>
#* For a graphical UI, instead run: <pre>make xconfig</pre>
#* For a graphical UI, instead run: <pre>make xconfig</pre>
# Add a new line to the top of the config file that contains the hardware platform the kernel is built for (the output of <code>uname -i</code>). The line is preceded by a <code>#</code> sign. For example, an x86_64 machine would have the following line added to the top of the config file:<pre># x86_64</pre>
# Add a new line to the top of the config file that contains the hardware platform the kernel is built for (the output of <code>uname -i</code>). The line is preceded by a <code>#</code> sign. For example, an x86_64 machine would have the following line added to the top of the config file:<pre># x86_64</pre>
# Copy the config file to <code>~/rpmbuild/SOURCES/</code>: <pre>cp .config ~/rpmbuild/SOURCES/config-<arch></pre>
# Copy the config file to <code>~/rpmbuild/SOURCES/</code>: <pre>cp .config ~/rpmbuild/SOURCES/config-$arch</pre>


=== Prepare Build Files ===
=== Prepare Build Files ===

Revision as of 22:58, 29 March 2009


Building a Kernel from the src RPM

Note these instructions *only* apply to Fedora 8 and later releases

This document provides instructions to advanced users who want to rebuild the kernel. Advanced users build custom kernels for a variety of reasons:

  • To apply patches for testing that they either generated or obtained from another source
  • To reconfigure the existing kernel
  • To learn more about the kernel and kernel development

These instructions can also be used for simply preparing the kernel source tree.

Before starting, make sure the system has all the necessary packages installed, including the following:

  • rpmdevtools
  • yum-utils

To install these packages, use the following command:

su -c 'yum install yum-utils rpmdevtools'

Get the Source

Do Not Build Packages as root.
Building packages as root is inherently dangerous and not required, even for the kernel. The following instructions allow any normal user to install and build kernels from the source packages.
  1. Prepare a RPM package building environment in your home directory. Run the following command:
    rpmdev-setuptree
    This command creates different directories ${HOME}/rpmbuild/SOURCES, ${HOME}/rpmbuild/SPECS, and ${HOME}/rpmbuild/BUILD. Where ${HOME} is your home directory.
  2. Download the kernel-<version>.src.rpm file. Enable the appropriate source repositories with the --enablerepo switch.
    yumdownloader --source kernel
  3. Install build dependencies for the kernel source with the yum-builddep command (root is required to install these packages):
    su -c 'yum-builddep kernel-<version>.src.rpm'
  4. Install kernel-<version>.src.rpm with the following command:
    rpm -Uvh kernel-<version>.src.rpm
    This command writes the RPM contents into ${HOME}/rpmbuild/SOURCES and ${HOME}/rpmbuild/SPECS, where ${HOME} is your home directory. It is safe to ignore any messages similar to the following:
warning: user kojibuilder does not exist - using root
warning: group kojibuilder does not exist - using root
Space Required. The full kernel building process requires several gigabytes of extra space on the file system containing your home directory.

Prepare the Kernel Source Tree

This step expands all of the source code files for the kernel. This is required to view the code, edit the code, or to generate a patch.

1. Prepare the kernel source tree using the following commands:

cd ~/rpmbuild/SPECS
rpmbuild -bp --target=`uname -m` kernel.spec

The kernel source tree is now located in the ~/rpmbuild/BUILD/kernel-<version>/linux-<version>.<arch> directory.

Copy the Source Tree and Generate a Patch

This step is for applying a patch to the kernel source. If a patch is not needed, proceed to "Configure Kernel Options".

Copy the source tree to preserve the original tree while making changes to the copy:

cp -r ~/rpmbuild/BUILD/kernel-2.6.$ver/linux-2.6.$ver.$arch ~/rpmbuild/BUILD/kernel-2.6.$ver.orig
cp -al ~/rpmbuild/BUILD/kernel-2.6.$ver.orig ~/rpmbuild/BUILD/kernel-2.6.$ver.new
The second cp command hardlinks the .orig and .new trees to make diff run faster. Most text editors know how to break the hardlink correctly to avoid problems.

Make changes directly to the code in the .new source tree, or copy in a modified file. This file might come from a developer who has requested a test, from the upstream kernel sources, or from a different distribution.

After the .new source tree is modified, generate a patch. To generate the patch, run diff against the entire .new and .orig source trees with the following command:

cd ~/rpmbuild/BUILD
diff -uNrp kernel-2.6.$ver.orig kernel-2.6.$ver.new > ../SOURCES/linux-2.6-my-new-patch.patch

Replace 'linux-2.6-my-new-patch.patch' with the desired name for the new patch.

For more information on patching refer to the man pages for diff(1) and patch(1).

Configure Kernel Options

This step is for modifying the options the kernel is configured with. This step is optional. If no configuration changes are needed, proceed to "Prepare Build Files".

  1. Change to the kernel source tree directory:
    cd ~/rpmbuild/BUILD/kernel-2.6.$ver/linux-2.6.$ver.$arch/
  2. Select the desired configuration file from ~/rpmbuild/BUILD/kernel-2.6.$ver/linux-2.6.$ver.$arch/configs. Copy the desired config file to ~/rpmbuild/BUILD/kernel-2.6.$ver/linux-2.6.$ver.$arch/.config:
    cp configs/<desired-config-file> .config
  3. Run the following command:
    make oldconfig
  4. Then run the following command, selecting and saving the desired kernel options from the text-based UI:
    make menuconfig
    • For a graphical UI, instead run:
      make xconfig
  5. Add a new line to the top of the config file that contains the hardware platform the kernel is built for (the output of uname -i). The line is preceded by a # sign. For example, an x86_64 machine would have the following line added to the top of the config file:
    # x86_64
  6. Copy the config file to ~/rpmbuild/SOURCES/:
    cp .config ~/rpmbuild/SOURCES/config-$arch

Prepare Build Files

This step makes the necessary changes to the kernel.spec file. This step is required for building a custom kernel.

1. Change to the ~/rpmbuild/SPECS directory:

cd ~/rpmbuild/SPECS

1. Open the kernel.spec file for editing. 1. Give the kernel a unique name. This is important to ensure the custom kernel is not confused with any released kernel. Add a unique string to the kernel name by changing the 'buildid' line. Optionally, change ".local" to your initials, a bug number, the date or anything other unique string.

Change this line:

#% define buildid .local

To this (note the extra space is removed in addition to the pound sign):

%define buildid .<custom_text>

1. If you generated a patch, add the patch to the kernel.spec file, preferably at the end of all the existing patches and clearly commented.

# cputime accounting is broken, revert to 2.6.22 version
Patch2220: linux-2.6-cputime-fix-accounting.patch

Patch9999: linux-2.6-samfw-test.patch

The patch then needs to be applied in the patch application section of the spec file. Again, at the end of the existing patch applications and clearly commented.

ApplyPatch linux-2.6-cputime-fix-accounting.patch

ApplyPatch linux-2.6-samfw-test.patch


Build the New Kernel

This step actually generates the kernel RPM files. This step is required for building a custom kernel.

Use the rpmbuild utility to build the new kernel:

  • To build all kernel flavors:
rpmbuild -bb --target=`uname -m` kernel.spec
  • To disable specific kernel flavors from the build (for a faster build):
rpmbuild -bb --without <option> --target=`uname -m` kernel.spec

Valid values for "option" above include xen, smp, up, pae, kdump, debug and debuginfo. Specifying --without debug strips out some debugging code from the kernels, where specifying --without debuginfo disables the building of the kernel-debuginfo packages.

  • To specify that only a specific kernel should be built:
rpmbuild -bb --with <option> --target=`uname -m` kernel.spec

Valid values for "option" above include xenonly, smponly, and baseonly.

  • For example, to build just the kernel and kernel-devel packages, the command would be:
rpmbuild -bb --with baseonly --without debuginfo --target=`uname -m` kernel.spec

The build process takes a long time to complete. A lot of messages will be printed to the screen. These messages can be ignored, unless the build ends with an error. If the build completes successfully, the new kernel packages in the ~/rpmbuild/RPMS directory.

TO DO
add a troubleshooting section

Following Generic Textbooks

Many of the tutorials, examples, and textbooks about Linux kernel development assume the kernel sources are installed under the /usr/src/linux/ directory. If you make a symbolic link, as shown below, you should be able to use those learning materials with the Fedora packages. Install the appropriate kernel sources, as shown earlier, and then run the following command:

su -c 'ln -s /usr/src/kernels/<version>.<release>-<arch> /usr/src/linux'

Enter the root password when prompted.

Install the New Kernel

This step actually installs the new kernel onto the running system.

To install the new kernel, use the rpm -ivh command, not the -U or --upgrade options:

rpm -ivh ~/rpmbuild/RPMS/<arch>/kernel-<version>.<arch>.rpm

Building Only Kernel Modules (Out Of Tree Modules)

This section needs to be updated and fleshed out

This section is for users who are only interested in working on a kernel module, and who do not wish to build an entire custom kernel. It is not necessary to download and rebuild the entire kernel to build a module. To build a module for the currently running kernel, only the matching kernel-devel package is required. Run the following command to install the kernel-devel package using yum.

su -c 'yum install kernel-devel'

You can build against any kernel version, as long as you have kernel and kernel-devel packages installed for that version. The rest of this section assumes we're building for the running kernel; if not, replace uname -r with the actual desired version number.

The kernel-doc package contains official Kbuild documentation - see files under Documentation/kbuild, in particular the modules.txt file.

As a simple example, to build the foo.ko module from foo.c, create the following Makefile in the directory containing the foo.c file:

obj-m := foo.o

KDIR  := /lib/modules/$(shell uname -r)/build
PWD   := $(shell pwd)

default:
$(MAKE) -C $(KDIR) M=$(PWD) modules

Then, issue the make command to build the foo.ko module.

The above is a helpful local Makefile wrapper invoking kbuild; in general you can simply do things like

# make -C /lib/modules/`uname -r`/build M=`pwd` modules
# make -C /lib/modules/`uname -r`/build M=`pwd` clean
# make -C /lib/modules/`uname -r`/build M=`pwd` modules_install

etc to build those targets.