New kernel on Ubuntu 10.04

From Crashcourse Wiki

Jump to: navigation, search

Contents

What's this all about, then?

It's about building, installing and booting a bleeding-edge Linux kernel under Ubuntu 10.04. Don't be scared, it's easier than you think. And given that I'm writing this with precious little experience with Ubuntu, send any suggestions or corrections to rpjday@crashcourse.ca. And away we go.

What do I need?

Assuming you're already running Ubuntu 10.04 (Lucid Lynx), you'll need both some recent kernel source, plus a few development packages. Starting with the development tools, it would appear that you should be good to go with just:

$ sudo apt-get install git-core

Basically, you need the standard development utilities (gcc, make, perl), plus git since we're going to check out the latest kernel git repository, and everything but git appears to already be there with a standard desktop install. Am I overlooking anything?

Note carefully that I'm not installing any kernel-related packages, as other web pages suggest. If you work purely with a git clone of the kernel source, most of those packages are unnecessary.

Getting the kernel source

Pick a location under your home directory, and:

$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git

Note that this is not a Ubuntu-specific checkout. Whenever possible, I prefer to work with the pristine source, and not repos that have already been hacked to be distro-specific. So type the above command, and go for coffee. It's going to take a while.

What are the parts to a bootable kernel, anyway?

If you're unfamiliar with this, it's worth seeing the parts that relate to a bootable kernel, since it's a good sanity check after you configure, build and install to verify that you've created all those new parts. Mostly, all that's under /boot, so here's an abbreviated list of things that are currently necessary to boot a kernel:

$ ls -l /boot
total 88776
-rw-r--r-- 1 root root   634929 2010-04-16 07:32 abi-2.6.32-21-generic
-rw-r--r-- 1 root root   110365 2010-04-16 07:32 config-2.6.32-21-generic
drwxr-xr-x 3 root root     4096 2010-05-11 10:25 grub
-rw-r--r-- 1 root root  8323533 2010-05-06 18:47 initrd.img-2.6.32-21-generic
-rw-r--r-- 1 root root   160280 2010-03-23 05:40 memtest86+.bin
-rw-r--r-- 1 root root  2152657 2010-04-16 07:32 System.map-2.6.32-21-generic
-rw-r--r-- 1 root root     1336 2010-04-16 07:35 vmcoreinfo-2.6.32-21-generic
-rw-r--r-- 1 root root  4037888 2010-04-16 07:32 vmlinuz-2.6.32-21-generic
$

By the time we're done, you should be able to verify that you have new versions of the above corresponding to a much newer kernel.

In addition, the current GRUB configuration is in /boot/grub/grub.cfg, so you might want to save a copy of that file to the side for the chance to compare the differences later after you install your kernel and update GRUB. It's just a suggestion.

Oh, and there are the loadable kernel modules under /lib/modules, one subdirectory per kernel version. At the moment:

$ ls /lib/modules/
2.6.32-21-generic
$

Once we build and install a new kernel and new modules for it, we will of course verify that there is a new directory for those modules there.

Configuring your new kernel

Since you have your new kernel source directory from above (in my case, version 2.6.34-rc7), cd into that directory and configure your kernel source appropriately. The safest approach is to use the current config file (/boot/config-2.6.32-21-generic), as a starting point, so just:

$ cp /boot/config-2.6.32-21-generic .config
$ make oldconfig

The only drawback with the above is that, by default, you'll still be asked to configure those new kernel options that aren't addressed in the older config file. You can either sit there and tediously hit Enter if you want to take the defaults, or you can tell the configure step to just deal with all that automatically:

$ yes '' | make oldconfig

which is what I did. On the other hand, at some point, it's really probably worth it to spend some time and check out those newer options. Onward to ...

The build

$ make

Go for dinner.

Installing the new kernel and modules, and updating GRUB

If the build works (and we have no reason to believe it won't because that's the kind of perfect universe I live in), install all of that and update GRUB to recognize the new kernel and initrd image:

$ sudo make modules_install
$ sudo make install
$ sudo update-initramfs -c -k 2.6.34-rc7
$ sudo update-grub

Once all that's done, and if it worked, you can verify the following:

  • There's a new kernel image in /boot (in my case, vmlinuz-2.6.34-rc7)
  • In the same location, there will also be corresponding config, initrd.img and System.map files.
  • There should be a whole new directory of loadable modules under /lib/modules:
$ ls /lib/modules/
2.6.32-21-generic  2.6.34-rc7
  • Finally, if you made a copy of the original /boot/grub/grub.cfg, feel free to diff the two and note the whole new stanza referring to your new kernel.

Rebooting

The above will have made your new kernel the default, so all you need to do is reboot to come up under the new kernel. If bad things happen, simply reboot yet again, hold down the Shift key to get to the GRUB menu, and boot under your original, working kernel. Then debug. Repeat as long as necessary until you get a successful boot and can verify that with:

$ uname -a
Linux lynx 2.6.34-rc7 #2 SMP Tue May 11 08:56:21 EDT 2010 x86_64 GNU/Linux
           ^^^^^^^^^^ :-)
$

And there you have it.

Things you might have done differently

Variations on the above you might find useful:

  • Rather than blindly running make oldconfig, take some time to go through the default config settings and remove some of the bazillion settings you know you don't need.
  • The label assigned to your new kernel will be based on the variable settings at the top of the kernel source Makefile:
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 34
EXTRAVERSION = -rc7

If you plan on building more than one kernel from the source (possibly with different config settings or based on newer git pull operations), keep changing the value of EXTRAVERSION to distinguish between the kernels; otherwise, a new build will overwrite a previous result.

  • To massively reduce the size of installed modules, install them with:
$ sudo make INSTALL_MOD_STRIP=1 modules_install

as opposed to just:

$ sudo make modules_install

The difference is noticeable.

Issues

Unsurprisingly, all is not quite perfect. The obvious oddity right now is that when I select to shut down the system, it instead shuts down ... and immediately reboots. Thoughts?

Personal tools