<?xml version='1.0' encoding='utf-8'?>
<!-- -*- DocBook -*- -->
<chapter id="ch-common-tasks">
<?dbhtml filename="ch-common-tasks.html" ?>
<title>Common kernel-related tasks</title>
<section id="s-common-getting"><title>Obtaining the Debian kernel source</title>
<para>
To get the Debian kernel source at the current maximum patchlevel, it is
sufficient to install the latest
<package>linux-source-<replaceable>version</replaceable></package> package and unpack
the source, for example:
</para>
<screen>
<prompt>#</prompt> <userinput>apt-get install linux-source-4.3</userinput>
<prompt>$</prompt> <userinput>tar xaf /usr/src/linux-source-4.3.tar.xz</userinput>
</screen>
<para>
The unpacked source tree then will be available in
<filename>linux-source-4.3</filename> directory.
</para>
</section>

<section id="s-common-official"><title>Rebuilding official Debian kernel packages</title>
<para>
You can build all or selected kernel packages by following these instructions.
You may be asked to do this in order to test a potential bug fix.
</para>
<section id="s4.2.1"><title>Preparation</title>
<para>
Run the following commands:
</para>
<variablelist>
<varlistentry>
<term><prompt>#</prompt> <userinput>apt-get install build-essential fakeroot</userinput></term>
<term><prompt>#</prompt> <userinput>apt-get build-dep linux</userinput></term>
<listitem>
<para>
This will install the packages required by the kernel build process.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><prompt>$</prompt> <userinput>apt-get source linux</userinput></term>
<listitem>
<para>
This will download and unpack the <package>linux</package> source package,
making the tree available in the
<filename>linux-<replaceable>version</replaceable></filename> directory.  As always,
the revision part of the version of this package (for example, 1 in 3.2.19-1)
will determine its patchlevel with respect to the original upstream kernel
source.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><prompt>$</prompt> <userinput>cd linux-<replaceable>version</replaceable></userinput></term>
<listitem>
<para>
Enter the source directory.
</para>
</listitem>
</varlistentry>
</variablelist>
<section id="s-common-size"><title>Disk space requirements</title>
<para>
Building binary packages for a single kernel flavour requires up to
15 GB space in the package directory and 300 MB in
<filename>/tmp</filename> (or <envar>$TMPDIR</envar>).
</para>
<para>
Building with debug info disabled requires about 2 GB and 25 MB respectively.
You can disable debug info by changing the value of
<literal>debug-info</literal> to <literal>false</literal> in
<filename>debian/config/defines</filename> (or
<filename>debian/config/<replaceable>arch</replaceable>/defines</filename>
in older package versions).
</para>
<para>
Building all binary packages for i386 or amd64 currently requires
about 50 GB space in the package directory.  Other architectures with
fewer drivers will require less space.
</para>
</section>

</section>

<section id="s4.2.2"><title>Simple patching and building</title>
<para>
The source package includes a script to simplify the process of building with
extra patches.  You can use this by running commands such as:
</para>
<screen>
<prompt>#</prompt> <userinput>apt-get install devscripts</userinput>
<prompt>$</prompt> <userinput>bash debian/bin/test-patches ../fix-bug123456.patch ../add-foo-driver.patch</userinput>
</screen>
<para>
This script has options to control the flavour, featureset, etc.  For a summary
of the options, run:
</para>
<screen>
<prompt>$</prompt> <userinput>bash debian/bin/test-patches</userinput>
</screen>
<para>
However, if you need to change the configuration or make other changes, you
should not use this script and should follow the instructions below.
</para>
</section>

<section id="s4.2.3"><title>Applying patches or configuration changes</title>
<para>
It is possible to apply extra patches to the source before starting the build.
In the <package>linux</package> source package, the default (non-featureset)
patches are automatically applied in the top level directory.
</para>
<para>
The patched source appears in the following directories.
</para>
<variablelist>
<varlistentry>
<term>default source:</term>
<listitem>
<para>
top level
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>source with featureset:</term>
<listitem>
<para>
<filename>debian/build/source_<replaceable>featureset</replaceable></filename>
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
You should apply the extra patches in the appropriate directory.  In the
<package>linux</package> source package you can use the
<command>quilt</command> utility to do this.
</para>
<para>
To change the configuration before building, for example for the 686-pae
flavour on i386, run the commands:
</para>
<screen>
<prompt>$</prompt> <userinput>fakeroot make -f debian/rules.gen setup_i386_none_686-pae</userinput>
<prompt>$</prompt> <userinput>make -C debian/build/build_i386_none_686-pae nconfig</userinput>
</screen>
<para>
If the patches or configuration changes alter type definitions for the kernel,
you may need to change the ABI name; see <xref linkend="s-abi-name"/>.
</para>
</section>

<section id="s4.2.4"><title>Building many packages</title>
<para>
To build all possible packages for this architecture, run:
</para>
<screen>
<prompt>$</prompt> <userinput>fakeroot debian/rules binary</userinput>
</screen>
<para>
To build all architecture-dependent packages, run:
</para>
<screen>
<prompt>$</prompt> <userinput>fakeroot debian/rules binary-arch</userinput>
</screen>
<para>
To build all architecture-independent packages, run:
</para>
<screen>
<prompt>$</prompt> <userinput>fakeroot debian/rules binary-indep</userinput>
</screen>
</section>

<section id="s4.2.5"><title>Building packages for one flavour</title>
<para>
For example, to build only the binary packages for 686-pae flavour on i386
architecture, use the following commands:
</para>
<screen>
<prompt>$</prompt> <userinput>fakeroot debian/rules source</userinput>
<prompt>$</prompt> <userinput>fakeroot make -f debian/rules.gen binary-arch_i386_none_686-pae</userinput>
</screen>
<para>
The target in this command has the general form of
<literal><replaceable>target</replaceable>_<replaceable>arch</replaceable>_<replaceable>featureset</replaceable>_<replaceable>flavour</replaceable></literal>.
Replace the <literal><replaceable>featureset</replaceable></literal> with
<literal>none</literal> if you do not want any of the extra featuresets.  This
command will build the linux image and kernel headers packages.  You may also
need the <package>linux-headers-<replaceable>version</replaceable>-common</package>
binary package, which can be built using the commands:
</para>
<screen>
<prompt>$</prompt> <userinput>fakeroot debian/rules source</userinput>
<prompt>$</prompt> <userinput>fakeroot make -f debian/rules.gen binary-arch_i386_none_real</userinput>
</screen>
<para>
The target in this command has the general form of
<literal><replaceable>target</replaceable>_<replaceable>arch</replaceable>_<replaceable>featureset</replaceable>_<replaceable>real</replaceable></literal>
</para>
</section>

</section>

<section id="s-common-official-vcs"><title>Building a development version of the Debian kernel package</title>
<para>
To build a kernel image based on the kernel team's unreleased development
version:
</para>
<variablelist>
<varlistentry>
<term><prompt>#</prompt> <userinput>apt-get install build-essential fakeroot rsync git</userinput></term>
<term><prompt>#</prompt> <userinput>apt-get build-dep linux</userinput></term>
<listitem>
<para>
The last two commands will install the build dependencies required by the
kernel build process.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><prompt>$</prompt> <userinput>git clone -b <replaceable>dist</replaceable> --single-branch https://salsa.debian.org/kernel-team/linux.git</userinput></term>
<listitem>
<para>
This will check out the Debian packaging.  <replaceable>dist</replaceable> is
normally the distribution codename such as <literal>wheezy</literal> or
<literal>sid</literal> (unstable).  For the very latest version, usually based
on an upstream release candidate, use <literal>master</literal>.  Note that
this will download several hundred megabytes of data.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><prompt>$</prompt> <userinput>apt-get source -d linux</userinput></term>
<listitem>
<para>
This will download the <package>linux</package> upstream source (and the last
released Debian patches).  Depending on which version you are trying to build,
you might need to override APT's version selection or download a tarball from
<ulink url="https://people.debian.org">people.debian.org</ulink> instead.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><prompt>$</prompt> <userinput>cd linux</userinput></term>
<term><prompt>$</prompt> <userinput>debian/rules orig</userinput></term>
<listitem>
<para>
This unpacks the upstream source and merges it with the Debian packaging.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><prompt>$</prompt> <userinput>debian/rules debian/control</userinput></term>
<listitem>
<para>
This generates a Debian package control file based on the current definitions
of the various kernel flavours which can be built.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><prompt>$</prompt> <userinput>fakeroot debian/rules <replaceable>target</replaceable></userinput></term>
<listitem>
<para>
Finally, build binary packages as explained in <xref
linkend="s-common-official"/>.
</para>
</listitem>
</varlistentry>
</variablelist>
</section>

<section id="s-gen-orig"><title>Generating orig tarball from newer upstream</title>
<para>
First you must add a changelog entry for the new upstream version.  If the new
version is a release candidate, change the string <literal>-rc</literal> to
<literal>~rc</literal>.  (In Debian package versions, a suffix beginning with
<literal>~</literal> indicates a pre-release.)
</para>
<para>
The 'orig' tarball is generated by the <command>genorig.py</command> script.
It takes either a tarball and optional patch from kernel.org, or a git
repository.  First ensure you have the necessary tools installed:
</para>
<screen>
<prompt>#</prompt> <userinput>apt-get install unifdef</userinput>
</screen>
<para>
Then if you have a tarball, run a command such as:
</para>
<screen>
<prompt>$</prompt> <userinput>debian/bin/genorig.py ../linux-4.3.tar.xz ../patch-4.4-rc1.xz</userinput>
</screen>
<para>
Or if you have a git repository, pass the name of its directory:
</para>
<screen>
<prompt>$</prompt> <userinput>debian/bin/genorig.py ~/src/linux</userinput>
</screen>
<para>
Either of these will generate a file such as
<filename>../orig/linux_3.5~rc1.orig.tar.xz</filename>.  You can then combine
this tarball with the Debian packaging by running:
</para>
<screen>
<prompt>$</prompt> <userinput>debian/rules orig</userinput>
</screen>
</section>

<section id="s-common-building"><title>Building a custom kernel from Debian kernel source</title>
<para>
This section describes the simplest possible procedure to build a custom kernel
the "Debian way".  It is assumed that user is somewhat familiar with kernel
configuration and build process.  If that's not the case, it is recommended to
consult the kernel documentation and many excellent online resources dedicated
to it.
</para>
<para>
The easiest way to build a custom kernel (the kernel with the configuration
different from the one used in the official packages) from the Debian kernel
source is to use the <package>linux-source</package> package and the
<command>make deb-pkg</command> target.  First, prepare the kernel tree:
</para>
<screen>
<prompt>#</prompt> <userinput>apt-get install linux-source-4.3</userinput>
<prompt>$</prompt> <userinput>tar xaf /usr/src/linux-source-4.3.tar.xz</userinput>
<prompt>$</prompt> <userinput>cd linux-source-4.3</userinput>
</screen>
<para>
The kernel now needs to be configured, that is you have to set the kernel
options and select the drivers which are going to be included, either as
built-in, or as external modules.
</para>
<para>
It is possible to reuse an old configuration file by placing it as a
<filename>.config</filename> file in the top-level directory.
Alternately, you can use the default configuration for the
architecture (<command>make defconfig</command>) or generate a
configuration based on the running kernel and the currently loaded
modules (<command>make localmodconfig</command>).
</para>
<para>
If you reuse a Debian kernel config file, you may need to disable
module signing (<command>scripts/config --disable
MODULE_SIG</command>) or enable signing with an ephemeral key
(<command>scripts/config --set-str MODULE_SIG_KEY
certs/signing_key.pem</command>).  The build will use less time and
disk space (see <xref linkend="s-common-size"/>) if debug information
is disabled (<command>scripts/config --disable DEBUG_INFO</command>).
Debuginfo is only needed if you plan to use binary object tools like
crash, kgdb, and SystemTap on the kernel.
</para>
<para>
The kernel build infrastructure offers a number of targets, which
invoke different configuration frontends.  For example, one can use
console-based menu configuration by invoking the command
</para>
<screen>
<prompt>$</prompt> <userinput>make nconfig</userinput>
</screen>
<para>
Instead of <literal>nconfig</literal> one can use <literal>oldconfig</literal>
(text-based line-by-line configuration frontend) or <literal>xconfig</literal>
(graphical configuration frontend).  Note that different frontends may require different additional
libraries and utilities to be installed to function properly.  For example, the
<literal>nconfig</literal> frontend requires the <literal>ncurses</literal>
library, which at time of writing is provided by the
<package>libncurses5-dev</package> package.
</para>
<para>
After the configuration process is finished, the new or updated kernel
configuration will be stored in <filename>.config</filename> file in the
top-level directory.  The build is started using the commands
</para>
<screen>
<prompt>$</prompt> <userinput>make clean</userinput>
<prompt>$</prompt> <userinput>make deb-pkg</userinput>
</screen>
<para>
As a result of the build, a custom kernel package
<filename>linux-image-3.2.19_3.2.19-1_i386.deb</filename> (name will reflect the
version of the kernel and build number) will be created in the directory one
level above the top of the tree.  It may be installed using
<command>dpkg</command> just as any other package:
</para>
<screen>
<prompt>#</prompt> <userinput>dpkg -i ../linux-image-3.2.19_3.2.19-1_i386.deb</userinput>
</screen>
<para>
This command will unpack the kernel, generate the initrd if necessary (see
<xref linkend="ch-initramfs"/> for details), and configure the bootloader to make
the newly installed kernel the default one.  If this command completed without
any problems, you can reboot using the
</para>
<screen>
<prompt>#</prompt> <userinput>shutdown -r now</userinput>
</screen>
<para>
command to boot the new kernel.
</para>
<para>
For much more information about bootloaders and their configuration please
check their documentation, which can be accessed using the commands
<command>man lilo</command>, <command>man lilo.conf</command>, <command>man
grub</command>, and so on.  You can also look for documentation in the
<filename>/usr/share/doc/<replaceable>package</replaceable></filename> directories,
with <replaceable>package</replaceable> being the name of the
package involved.
</para>
</section>

<section id="s-kernel-org-package"><title>Building a custom kernel from the "pristine" kernel source</title>
<para>
Building a kernel from the "pristine" (also sometimes called "vanilla") kernel
source, distributed from <ulink
url="https://www.kernel.org">www.kernel.org</ulink> and its mirrors, may be
occasionally useful for debugging or in the situations when a newer kernel
version is desired.  The procedure differs only in obtaining the kernel source:
instead of unpacking the kernel source from Debian packages, the "pristine"
source is downloaded using your favourite browser or using wget, as follows:
</para>
<screen>
<prompt>$</prompt> <userinput>wget https://kernel.org/pub/linux/kernel/v4.x/linux-4.3.tar.xz</userinput>
</screen>
<para>
The integrity of the downloaded archive may be verified by fetching the
corresponding cryptographic signature
</para>
<screen>
<prompt>$</prompt> <userinput>wget https://kernel.org/pub/linux/kernel/v4.x/linux-4.3.tar.sign</userinput>
</screen>
<para>
and running this command (<package>gnupg</package> package must be installed):
</para>
<screen>
<prompt>$</prompt> <userinput>unxz -c linux-4.3.tar.xz | gpg --verify linux-4.3.tar.sign -</userinput>
</screen>
<para>
Successful verification results in output similar to the one below:
</para>
<screen>
<computeroutput>gpg: Signature made Mon 21 May 2012 01:48:14 AM CEST using RSA key ID 00411886
gpg: Good signature from "Linus Torvalds &lt;torvalds@linux-foundation.org&gt;"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: ABAF 11C6 5A29 70B1 30AB  E3C4 79BE 3E43 0041 1886</computeroutput>
</screen>
<para>
After that the archive may be unpacked using
</para>
<screen>
<prompt>$</prompt> <userinput>tar xaf linux-4.3.tar.xz</userinput>
<prompt>$</prompt> <userinput>cd linux-4.3</userinput>
</screen>
<para>
The unpacked kernel tree (in <filename>linux-4.3</filename>) can now be
configured and built, in the same way described in the previous section.
</para>
</section>
<section id="s-common-out-of-tree"><title>Building out-of-tree kernel modules</title>
<para>
Some kernel modules are not included in the upstream or Debian kernel source,
but are provided as third-party source packages.  For some of the most popular
out-of-tree modules, the binary Debian packages with modules built against the
stock Debian kernels are provided.  For example, if you are running stock
Debian kernel <literal>3.2.0-2-686-pae</literal> (use the <command>uname
-r</command> command to verify the version) from the
<package>linux-image-3.2.0-2-686-pae</package> package, and would like to use
the squash filesystem, all you need to do is install
<package>squashfs-modules-3.2.0-2-686-pae</package> binary package, which
provides the necessary binary kernel modules.
</para>
<para>
If you are not so lucky, and there are no binary module packages in the
archive, there is a fair chance that the Debian archive contains the packaged
<replaceable role="strong">source</replaceable> for the kernel modules.  Names of
such packages typically end in <literal>-source</literal>, for example
<package>squashfs-source</package>, <package>thinkpad-source</package>,
<package>rt2x00-source</package> and many others.  These packages contain
debianized source code of the kernel modules, suitable for building using the
<command>module-assistant</command> (or <command>m-a</command>) script from the
<package>module-assistant</package> package.  Typical sequence to build a
custom binary module package, matching a kernel
<literal>3.2.0-2-686-pae</literal> (as returned by <command>uname -r</command>)
from the debianized source consists of the following steps:
</para>
<para>
Install a set of kernel headers, matching the kernel for which the modules are
going to be built:
</para>
<screen>
<prompt>#</prompt> <userinput>apt-get install linux-headers-3.2.0-2-686-pae</userinput>
</screen>
<para>
Install the package containing the source:
</para>
<screen>
<prompt>#</prompt> <userinput>apt-get install squashfs-source</userinput>
</screen>
<para>
Invoke <command>module-assistant</command> (aka <command>m-a</command>) to do
the heavy lifting:
</para>
<screen>
<prompt>#</prompt> <userinput>m-a build squashfs</userinput>
</screen>
<para>
As a result, a Debian package is going to be built and placed in
<filename>/usr/src</filename>.  It can be installed the usual way, using
<command>dpkg -i</command>.  Two last steps (building and installation) may be
combined using the invocation
</para>
<screen>
<prompt>#</prompt> <userinput>m-a auto-install squashfs</userinput>
</screen>
<para>
Check out the <command>module-assistant</command> documentation (<command>man
module-assistant</command>) for other options and much more information on how
to use it.
</para>
<para>
Finally, in some rare circumstances, you might need to build the kernel modules
from the upstream source packages.  In that case, follow the documentation
included with the package to build the modules.  If the build process will
require you to specify the directory with the kernel headers, matching the
currently running kernel, for stock Debian kernels this directory is
<filename>/usr/src/linux-headers-<replaceable>uname</replaceable></filename>, provided
by the <package>linux-headers-<replaceable>uname</replaceable></package> package.
Here <replaceable>uname</replaceable> is the output of the
<command>uname -r</command> command.  If you are building and running your own
custom kernels, it is a good idea to keep the original build tree around, as it
also can be used for out-of-tree module building.
</para>
</section>

</chapter>

