Differences between revisions 107 and 109 (spanning 2 versions)
Revision 107 as of 2015-11-27 11:09:27
Size: 9936
Comment: added warning icon
Revision 109 as of 2016-05-16 18:23:15
Size: 471
Editor: BenHutchings
Comment: Replace outdated and incorrect instructions with a link to the kernel handbook
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
{{{#!wiki caution
'''This page outdated in some parts.'''

It should be reworked and refer to https://debian-handbook.info/browse/en-US/stable/sect.kernel-compilation.html
}}}

= Building custom kernel packages =

This page is about rebuilding an official Debian kernel package with custom changes. There is the Kernel Handbook about this: http://kernel-handbook.alioth.debian.org/ (which explains how to "Rebuild an official Debian linux kernel package", hopefully : http://kernel-handbook.alioth.debian.org/ch-common-tasks.html ch 4.2 : "Rebuilding an official Debian kernel package").

We are following section 4.2: '''Rebuilding an official Debian kernel package'''

<<Anchor(reasons)>> '''Reasons to do this:'''

 1. Modifying the Debian configuration to change the '''preemption model'''.
 1. Modifying the Debian configuration to change the '''timer frequency'''.
 1. Add a patch fix.
 1. Add your favorite patchset.
What you'll get is exactly the kernel that Debian releases but with those added changes. The new packages will not step on the official kernel images' toes.


== For the impatient ==

Example with [[http://www.frankvm.com/nfs-ngroups/|NFS ngroups patch]] and the [[https://packages.debian.org/source/stable/linux|latest stable kernel]] (here with Jessie):
{{{
# Run this in the same distro version than the recompiled kernel, possibly in a chroot
apt-get install devscripts equivs wget
wget http://www.frankvm.com/nfs-ngroups/3.16-nfs-ngroups-4.61.patch
# Make sure you have deb-src addresses for stable *and* security in /etc/apt/sources.list
apt-get update
apt-get source linux
cd linux-3.16.7-cktXX/
mk-build-deps -i
sed -i -e 's/~test/~nfs/g' debian/bin/test-patches # version suffix
sed -i -e 's/^abiname:.*/&nfs/' debian/config/defines # package suffix
time debian/bin/test-patches -f amd64 -j$(nproc) ../3.16-nfs-ngroups-4.61.patch # maybe use --fuzz 3, but be careful
fakeroot make -f debian/rules.gen binary-arch_amd64_none_real # if you need the linux-headers-XXX-common package e.g. for DMKS modules
# As of 2015-03, after ~55mn using -j4, and 12Go disk space, you get your .deb's :)
}}}

<<Anchor(add-a-patch)>>
== Add a patch to linux and build new kernel packages ==
 1. Create the directory where you will build the kernel package as a normal user and make sure you have all necessary packages installed: {{{
apt-get install fakeroot build-essential devscripts
apt-get build-dep linux
}}}

 1. Get the package source. For this to work, you need to have {{{deb-src}}} lines for the official archive in {{{/etc/apt/sources.list}}}: {{{
apt-get source linux
cd linux-*
}}}
 Now you are in the tree that holds the source and all changes Debian made to it. As an alternative, you could get a kernel tarball, e.g. from buildserver (see DebianKernel), unpack it, and get the latest Debian patches + config settings: {{{
tar xzf linux_3.0.0~rc6.orig.tar.gz
cd linux-*
svn export svn://svn.debian.org/svn/kernel/dists/trunk/linux/debian
}}}

 1. Look at the top of {{{debian/changelog}}} and think of an appropriate version number. You cannot {{{dch}}} yet, though. If you are adding a single patch "foo", you might want to append "+foo.1". If you are adding multiple patches for your company or yourself, use "+somename.1".

 1. Append the same string to the {{{abi.abiname}}} field in {{{debian/config/defines}}} to ensure that your custom kernel package name doesn't bite with the official ones. In our case, we're now creating {{{linux-image-3.x.0-y+foo.1-zzz}}}.

 1. Use quilt to add your patch (see UsingQuilt)

 1. Optional : If it's not the first build attempt, and should you need to clean things in case of build failures : {{{
make -f debian/rules clean
}}}

 1. Now check that your patchset still applies and fix any conflicts:
 Note: on the first run, the target fails. Make sure you run it twice.
 {{{
This target is made to fail intentionally, to make sure
that it is NEVER run during the automated build. Please
ignore the following error, the debian/control file has
been generated SUCCESSFULLY.
}}}

 {{{
make -f debian/rules source-all
}}}

 1. Now that the above error message has occured after the first “debian/rules clean”, {{{dch}}} will work because {{{debian/control}}} has been (re-)generated. (Note that, after editing {{{debian/changelog}}}, “debian/rules clean” must again be run because its hash changes.) Start a new stanza at the top of {{{debian/changelog}}} and use an appropriate version number. Ensure that the distribution is '''not''' set to the regular release names. Change it to something like '''UNRELEASED.''' You could do this with the {{{dch}}} tool from the {{{devscripts}}} package like so: {{{
dch --local +foo.
}}}

 1. Document the addition of this patch in the new stanza in {{{debian/changelog}}}.

<<Anchor(build-all)>>
=== Building all kernel-related packages ===
 0. Make sure you have at least 40 GB of disk space free (version 2.6.32-17 took 30 GB).

 1. Assuming you want to build '''all''' kernel-related packages, build with the appropriate {{{DEBIAN_KERNEL_JOBS}}} parameter (the number of processors you can use for the build): {{{
~/src/linux-3.2.31$ debuild -e DEBIAN_KERNEL_JOBS=$(nproc)
}}}

If you plan to do multiple rebuilds you might want to use ccache by adding the --prepend-path=/usr/lib/ccache option to the above debuild command.

<<Anchor(build-one)>>
=== Building only a single kernel variant ===
 0. Make sure you have at least 10 GB of disk space free (setup_amd64_none_amd64 took 7.3 GB in version 2.6.32-17).

 1. Prepare the rules file for the one build you want to make, e.g.: {{{
fakeroot make -f debian/rules.gen setup_i386_none_686-pae
}}}
 This will build a tree in the {{{debian/build/build_i386_none_686-pae}}} directory. Go to that directory.

 1. Change the {{{.config}}} file, using e.g. {{{
make menuconfig
cd ../../..
}}}

 1. Compile the kernel and generate the kernel packages. Replace {{{$NR_CPUS}}} with the number of CPUs of the build machine (keeping it all on the same line) and run e.g. {{{
fakeroot make -f debian/rules.gen binary-arch_i386_none_686-pae binary-indep \
DEBIAN_KERNEL_JOBS=${NR_CPUS}
}}}
 Some time later the package files will be generated, e.g.: {{{
linux-image-3.2.0-4+foo.1-686-pae_3.2.31-1+foo.1_i386.deb
linux-image-3.2.0-4+foo.1-686-pae-dbg_3.2.31-1+foo.1_i386.deb
linux-headers-3.2.0-4+foo.1-686-pae_3.2.31-1+foo.1_i386.deb
xen-linux-system-3.2.0-4+foo.1-686-pae_3.2.31-1+foo.1_i386.deb
}}}
 '''Problem''': In this case, {{{linux-headers-3.2.0-4+foo.1-common}}} will be missing. One needs to invoke the {{{binary-arch_i386}}} target, which will yield all feature sets (RT) and flavours to be generated, and obviously takes a lot longer. See [[http://lists.debian.org/debian-kernel/2008/04/msg00190.html|this thread]].
 Solution: {{{
fakeroot make -f debian/rules.gen binary-arch_i386_none_real
}}}

 1. If you apply further changes to the source package and need to rebuild the binary packages without rebuilding each and every binary objects, you can remove the build stamp before running the above mentioned building command. e.g. {{{
rm debian/stamps/build_i386_none_686-pae_plain
}}}

== The story of linux-tools ==

The {{{linux-headers-*}}} packages created with the above method depend on {{{linux-kbuild-*}}}, which is a '''not''' built from the {{{linux}}} source package, but from {{{linux-tools}}}.

Often, when a new kernel comes around, {{{linux-kbuild-3.xx}}} isn't yet available in the archive, so you either have to build it yourself, or wait.

A [[http://lists.debian.org/debian-kernel/2012/08/msg00390.html|Mini-Howto]] from Sedat (dileks) gives detailed instructions how to build {{{linux-kbuild-3.5}}}. An integration here would blow-up this wiki unnecessarily.

=== How to build linux-tools yourself ===

Checkout the source from SVN repository: {{{
svn co svn://anonscm.debian.org/svn/kernel/dists/trunk/linux-tools/
}}}
Then, download the vanilla kernel tarball (usually the 3.x version, no 3.x.y version): {{{
VERSION="3.6"
wget http://ftp.kernel.org/pub/linux/kernel/v3.x/linux-${VERSION}.tar.bz2
}}}
Now, you prepare the Debian package:
{{{
cd linux-tools
./debian/bin/genorig.py ../linux-${VERSION}.tar.bz2
cd ..
tar -xzf orig/linux-tools_${VERSION}.orig.tar.gz
cd linux-tools-${VERSION}
cp -a ../linux-tools/* ./
./debian/bin/gencontrol.py
}}}

NOTE: Optionally, you have to change Debian-version in debian/changelog if $VERSION of Linux-kernel is higher, think of 3.5 (latest version in linux-tools SVN-branch) is smaller than 3.6-rcX (version of tarball), otherwise the genorig.py line fails.

Finally, adjust the Debian version and add comments like "Non-maintainer upload" plus "New upstream version" via dch command. Last but not least, build the package itself after you installed eventually missing build-dependencies: {{{
dch -i
make -f debian/rules clean
dpkg-checkbuilddeps
cp -av ../orig/linux-tools_${VERSION}.orig.tar.gz ../ <--- Necessary to get a diff file.
dpkg-buildpackage -us -uc (or debuild)
}}}
and you are done.

== How to rebuild a single module of Debian kernel ==

{{{
apt-get install linux-headers-$(uname -r)
apt-get source linux-image-$(uname -r)
cd linux-*
# Modify a module if you want
make -C /lib/modules/$(uname -r)/build M=$(pwd) drivers/usb/host/xhci-hcd.ko
}}}

Notes:
 * As only modification, I needed to go to /usr/src/linux-headers-3.2.0.4-common/ and do {{{
ln -s ../linux-headers-$(uname -r)/scripts .
}}}
See the Kernel Handbook section [[https://kernel-handbook.alioth.debian.org/ch-common-tasks.html#s-common-official|Rebuilding official Debian kernel packages]].

Translation(s): English - Italiano

(!) ?Discussion

See the Kernel Handbook section Rebuilding official Debian kernel packages.