
Building the Oprofile Kernel Module from Source
-----------------------------------------------
There are two ways to go about this: (1) building the kernel by hand,
and (2) using the Debian package 'kernel-package'.  For those that
typically use the kernels provided by the Debian archive, the latter
method is preferred.  If you build your own kernels often, you probably
know what to do already -- and probably have to use the first option.

You will either have to have root permission from here on out, our
have your system administrator follow these steps to install oprofile.

Credit for much of the text below goes to Alex Tsariounov, the
project lead for prospect, a performance tool that uses oprofile
(see http://prospect.sourceforge.net for details).


The OProfile Kernel Module and Linux 2.6
----------------------------------------
You do NOT have to build an oprofile module if you are using a Debian
'kernel-image' package.  It is included as part of the 'kernel-image'
package by default.  If you are building a 2.6 kernel from source,
you will need to make sure that CONFIG_OPROFILE is set (OProfile is
part of the 2.6 kernel source).  You will only need to create an 
OProfile kernel module with the 'oprofile-source' package when using
non-Debian kernels prior to 2.5.


Building OProfile with kernel-package
-------------------------------------
To build the oprofile kernel module from the contents of the
oprofile-source package, follow these steps:

   1) Install your kernel sources by installing these packages:
      kernel-image-2.4.24-1-686, and kernel-source-2.4.24.  This can
      be done with:

      # apt-get install kernel-image-2.4.24-1-686
      # apt-get install kernel-source-2.4.24
      # apt-get install kernel-patch-debian-2.4.24

      NOTE: a 2.4.18 or better kernel is recommended, but not required
      for i386.  For the woody/stable release, the version of the
      kernel binaries for that kernel are shown above.  Note, too, that
      the '-1-686' tag shown above is only meant to be indicative;
      that string may be different for the kernel you're using.
      The instructions that follow assume that we're working with
      the 2.4.24-1-686 kernel.

   2) To make sure you can build a kernel, make sure the right tools
      are already installed:

      # apt-get build-dep kernel-source-2.4.24
      # apt-get install build-essential

      This will pick up everything necessary to build a kernel
      from source.

   3) Unpack the kernel source:

      # cd /usr/src
      # tar xvjf kernel-source-2.4.24.tar.bz2

      This will create and populate the directory 
      /usr/src/kernel-source-2.4.24.

   4) Install the oprofile-source package so that we can build the kernel
      module needed:

      # apt-get install oprofile-source

      If doing this from a deb file:

      # dpkg -i oprofile-source_0.5.3-3_i386.deb

      You will also have to unpack the source file for the module:

      # cd /usr/src
      # tar xvzf oprofile-source.tar.gz

      This will create /usr/src/modules/oprofile, the directory containing
      the needed module source.

   5) Install all the tools that oprofile-source needs to build a module:

      # apt-get build-dep oprofile-source

      If you installed directly from a deb file, the apt-get will not
      work.  You'll need to do the following instead:

      # apt-get install debhelper kernel-package automake1.9 libpopt-dev

      Some of these may have already been installed by this point, but
      it can't hurt to be sure.

      NOTE: you will have to get automake1.9 out of the sarge/testing
      release (it also needs a newer version of autoconf).  Unfortunately,
      oprofile only works on 1.9 and newer versions of automake.  A quick
      and dirty way to do this is to (1) add a testing archive into your
      /etc/apt/sources.list, (2) do apt-get update, (3) do an apt-get 
      install automake1.9, then (4) remove testing from sources.list, 
      and (5) apt-get update again. 

   6) Create or copy in a config file.  You can use the config file
      from your running kernel:

      # cd /usr/src/kernel-2.4.24
      # cp /boot/config-2.4.24-1-686 .config

      Or you can use a config file that you've created and copy it
      into .config.

   7) Modify the kernel debian/changelog file to reflect the kernel
      you're building (if it hasn't been changed already):

      # cd /usr/src/kernel-source-2.4.24
      # vi debian/changelog
      
      The very first part of the very first line of text may say
      "kernel-source-2.4.24" but needs to reflect the kernel you're
      building for; in our case, change that part of the line to
      "kernel-source-2.4.20-1-686".

      If you do not have a 'debian' subdirectory, you'll need to first
      run the following commands, and the edit the changelog:

      # cd /usr/src/kernel-source-2.4.20
      # PATCH_THE_KERNEL=YES make-kpkg debian

   8) Build the oprofile module for your kernel

      # cd /usr/src/kernel-source-2.4.24
      # make-kpkg --append-to-version=-1-686 modules_image

      When done, there will be a .deb file containing the newly
      built oprofile kernel module in the directory /usr/src/modules.
      The resulting package for what we've done so far will be called
      'oprofile-modules-2.4.24-1-686_0.5.3-3_i386.deb' (the '0.5.3-3'
      part is the oprofile version number, and the '2.4.24-1-686' is
      the kernel version, so you may see something slightly different).

   9) Install and use the oprofile kernel module just built:

      # dpkg -i oprofile-modules-2.4.24-1-686.5.3-3_i386.deb


Building By Hand on i386
------------------------
To build the oprofile kernel module from the contents of the
oprofile-source package, follow these steps:

   1) Install your kernel sources by installing these packages:
      kernel-image-2.4.24-1-686, and kernel-source-2.4.24.  This can
      be done with:

      # apt-get install kernel-image-2.4.24-1-686
      # apt-get install kernel-source-2.4.24

      NOTE: a 2.4.24 or better kernel is recommended, but not required
      for i386.  For the woody/stable release, the version of the
      kernel binaries for that kernel are shown above.  Note, too, that
      the '-1-686' tag shown above is only meant to be indicative;
      that string may be different for the kernel you're using.
      The instructions that follow assume that we're working with
      the 2.4.24-1-686 kernel.

   2) To make sure you can build a kernel, make sure the right tools
      are already installed:

      # apt-get build-dep kernel-source-2.4.24
      # apt-get install build-essential

      This will pick up everything necessary to build a kernel
      from source.

   3) Unpack the kernel source:

      # cd /usr/src
      # tar xvjf kernel-source-2.4.24.tar.bz2

      This will create and populate the directory 
      /usr/src/kernel-source-2.4.24.

   4) Edit the kernel Makefile.  In order to make sure we build
      a usable version of oprofile, we need to set up the Makefile
      so that the kernel version matches the results of `uname -r`
      for the machine we'll be running on.

      # cd /usr/src/kernel-2.4.24
      # vi Makefile
        Edit the fourth line in the Makefile
            from: EXTRAVERSION =
              to: EXTRAVERSION = -1-686

   5) Create or copy in a config file.  You can use the config file
      from your running kernel:

      # cd /usr/src/kernel-2.4.24
      # make mrproper
      # cp /boot/config-2.4.24-1-686 .config

      Or you can use a config file that you've created and copy it
      into .config.

   6) Configure the kernel source tree properly:

      # cd /usr/src/kernel-2.4.24
      # make oldconfig
      # make dep

      This is all that needs to be done to the actual kernel source
      tree.  It is not necessary to actually rebuild the kernel.

   7) Install the oprofile-source package so that we can build the kernel
      module needed:

      # apt-get install oprofile-source

      If doing this from a deb file:

      # dpkg -i oprofile-source_0.5.3-3_i386.deb

   8) Install all the tools that oprofile-source needs to build a module:

      # apt-get build-dep oprofile-source

      If you installed directly from a deb file, the apt-get will not
      work.  You'll need to do the following instead:

      # apt-get install debhelper kernel-package automake1.9 libpopt-dev

      Some of these may have already been installed by this point, but
      it can't hurt to be sure.

      NOTE: you will have to get automake1.9 out of the sarge/testing
      release (it also needs a newer version of autoconf).  Unfortunately,
      oprofile only works on 1.9 and newer versions of automake.  A quick
      and dirty way to do this is to (1) add a testing archive into your
      /etc/apt/sources.list, (2) do apt-get update, (3) do an apt-get 
      install automake1.9, then (4) remove testing from sources.list, 
      and (5) apt-get update again. 

   9) Build the oprofile module for your kernel

      # cd /usr/src/modules/oprofile
      # debian/rules binary-modules

      When done, there will be a .deb file containing the newly
      built oprofile kernel module in the directory /usr/src/modules.
      The resulting package for what we've done so far will be called
      'oprofile-modules-2.4.24-1-686_0.5.3-3_i386.deb' (the '0.5.3-3'
      part is the oprofile version number, and the '2.4.24-1-686' is
      the kernel version, so you may see something slightly different).

  10) Install and use the oprofile kernel module just built:

      # dpkg -i oprofile-modules-2.4.24-1-686.5.3-3_i386.deb



 -- Al Stone <ahs3@debian.org>, Tue, 17 June 16:13:29 -0600
