Downloading and using kernel source code (2.6.32.12 and newer) on Gentoo

If you know you need to get your kernel source (or at least its headers) for a kernel version 2.6.32.12 and newer, you can find instructions to do so on Gentoo here.


Why would I want kernel source code?

There may come a time when you need the kernel source code, usually to build kernel headers, compile a module or some other server voodoo. Most users won't need to mess with the kernel source on a virtual server, so if you don't know that you need it, you probably don't and can safely skip this article. If you know you do, though, read on.

Note that the instructions in this article apply to kernels version 2.6.32.12 and newer. We'll cover checking the version in a bit. If you do have an older kernel version you'll need to check another article, or pop into support chat for help finding the right source code package.

Check the kernel version

Before hitting the repository let's check the version of the kernel you're running. You can do this through your management control panel (which is where you can install a different kernel version), but it's most reliable to check the version right on the server. Just run:

uname -r

The output will be the version of the kernel running on your virtual server. Something like:

2.6.32.12-rscloud

The kernel repository

Slicehost and the Rackspace Cloud use the same kernels, so you'll see both names when you visit our kernel repository:

http://kernel.slicehost.com

When you visit that site in a web browser you'll see a simple list of directories for the kernel versions that have source code available there. Click the one that corresponds to your kernel.

A choice of sources

Inside each of those directories are some other directories that offer you a choice of "flavor" when selecting your kernel source, something like:

Index of /2.6.32.12-rscloud/

Name                Last Modified           Size    Type
Parent Directory/                           -       Directory
patched_source/     2010-May-19 20:35:30    -       Directory
patches/            2010-May-19 20:40:47    -       Directory
vanilla_source/     2010-May-19 20:39:05    -       Directory

If you're looking for the source code of the kernel you're actually running on your server, you'll want what's in the "patched_source" directory.

The "vanilla_source" directory holds the original kernel source, before it was patched for our virtual environment. The "patches" directory contains the patches that were applied to the vanilla_source to make the patched_source.

A choice of formats

Finally, in that patched_source directory, you'll find a package with the source code you want. Or...maybe more than one package, with different extensions at the end.

Index of /2.6.32.12-rscloud/patched_source/

Name                        Last Modified           Size    Type
Parent Directory/                                   -       Directory
2.6.32.12-rscloud.tar.bz2   2010-May-17 19:03:51    62.3M   application/octet-stream
2.6.32.12-rscloud.tar.gz    2010-May-17 19:03:52    80.4M   application/octet-stream

Each of those packages contains the same source code, they're just compressed in different formats. The ".tar.gz" package is tarred and gzipped, which is a more widely compatible package format. The ".tar.bz2" package was tarred and compressed with bzip, which makes a smaller package but isn't compatible with as many systems as gzip.

You can safely pick either one, since pretty much any Linux distribution will be using a version of tar that can unpack both formats.

Downloading the source

So now you've identified the package you need. Next it's a matter of getting it onto your server.

If you like, you can copy the link and download the package directly to your server using wget. For example:

wget http://kernel.slicehost.com/2.6.32.12-rscloud/patched_source/2.6.32.12-rscloud.tar.bz2

Alternatively you can download the package in your browser instead, then copy it to your server from there using scp or sftp. So long as the package eventually winds up on your server you're good.

Unpack the source

Let's change our working directory to a standard location for linux source code:

cd /usr/src

And then unpack your package (known as a "tarball", in the jargon). If you downloaded the bz2 file, run:

sudo tar -xjvf ~/2.6.32.12-rscloud.tar.bz2

Substitute the path to the tarball for the "~/" above if it isn't in your home directory, of course.

If you downloaded the gz file, run:

sudo tar -xzvf ~/2.6.32.12-rscloud.tar.gz

Note that the difference between the commands is that a "j" option is passed to tar for the bz2 file where a "z" is passed to tar for the gz file.

Either way, after watching a bunch of filenames scroll past you'll wind up with a directory named after your kernel version, containing the kernel source. Nice work.

Make the headers

One possible use for the kernel source is to build the kernel headers needed to compile some other program. If that's what you're after, it's a pretty easy task to accomplish now. Change your working directory to the kernel source directory:

cd /usr/src/2.6.32.12-rscloud

And then run the following command:

sudo make headers_install

That's it, you're set.

Preparing to compile a module

If you want to compile a module there's more than one step involved, alas. And you'll actually need more than just the kernel source for this. You'll also need the configuration file that was used to build your kernel.

Thankfully, that's easy to find and put in place. Make sure you're in the kernel source directory, and run:

sudo zcat /proc/config.gz > .config

There you go. No need to muck about in a configuration menu or answer a bunch of yes/no questions, all the work's been done for you.

One more step. Run:

sudo make modules_prepare

You'll see where it's copying files, and maybe a few warnings. Skim the warnings, but if they're just advising you that it's "ignoring return value" of some function you don't need to worry about them.

Now your system should be ready to compile your module. From there follow the directions that (hopefully) came with the module's source code.

Summary

You should have the basic steps for getting the source code for your kernel down now: Check your kernel version, hit the kernel repository, pick the package you want, and then download and unpack it on your server. You still won't be able to install any kernel you compile from source, but you should be well-prepared to grab the source again if you upgrade your kernel through the control panel later.

  • -- Jered

Article Comments:

TheWoollyBully commented Fri Jul 30 21:58:26 UTC 2010:

also don't forget to update the symlink so that portage knows how to find the new kernel

ln -sfn /usr/src/2.6.32.12-rscloud /usr/src/linux

Jered commented Wed Aug 04 15:51:50 UTC 2010:

I admit, I made a conscious choice not to include instructions to add a symlink for /usr/src/linux. While there are builds that will want it, it's not recommended that anything actually look for headers there. It's more reliable for packages to use glibc for the kernel headers. There's been a recommendation against adding a /usr/src/linux symlink in the kernel source readme since the 2.4 tree.

Of course, if you need to compile something that needs a /usr/src/linux symlink, then you'll certainly want to set it up (practicality trumps best practices). Just be aware that whatever the package is doing that requires /usr/src/linux, it's probably doing it wrong.

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)