From Fedora Project Wiki

< Building a custom kernel

Revision as of 18:11, 19 September 2016 by Labbott (talk | contribs) (Created page with "This patch contains instructions for building a kernel from the source RPM. Make sure you have installed all dependencies = Get the Source = {{Admon/warning |Do Not Build Pac...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This patch contains instructions for building a kernel from the source RPM. Make sure you have installed all dependencies

Get the Source

Warning.png
Do Not Build Packages as root.
Configuring and building packages as root is inherently dangerous and not required, even for the kernel. The following instructions allow any normal user to configure and build kernels from the source packages.

If you've never built an RPM before, prepare an RPM package-building environment in your (non-root) home directory by running the following command:

$ rpmdev-setuptree

This command creates a new directory ~/rpmbuild, with several empty subdirectories including SPECS, SOURCES, BUILD and others. It also creates an initial ~/.rpmmacros customization file for you. This only needs to be done once.

Download the kernel-<version>.src.rpm file:

$ koji download-build --arch=src kernel-<version>

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
Warning.png
Space Required. The full kernel building process requires several gigabytes of extra space on the file system containing your home directory.

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

2. Open the kernel.spec file for editing.
3. 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 any 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>

4. 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

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:

  • Ensure /usr/sbin is in your path (to pull in /usr/sbin/modinfo):

$ export PATH=/usr/sbin:$PATH

  • 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 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 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 will be located in the ~/rpmbuild/RPMS directory.

Warning.png
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 into the running system.

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

su -c "rpm -ivh $HOME/rpmbuild/RPMS/<arch>/kernel-<version>.<arch>.rpm"

This command will install your kernel in /boot, create a new initramfs to bootstrap your kernel, and automatically add your new kernel to your grub bootloader "menu.lst". At this point, you can reboot to give control to your new kernel.