Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Hacking the Linux 2.6 kernel, Part 1: Getting ready

Materials you'll need, and tips for configuring and compiling the kernel

Lina Mårtensson (linam@tyst.nu), Freelance writer
Lina Mårtensson
Lina Mårtensson is pursuing a M.Sc. in Computer Science and Engineering at Chalmers University of Technology, Sweden. Contact Lina at linam@tyst.nu.
Valerie Henson (val@nmt.edu), Software Engineer, IBM
Valerie Henson
Val Henson works for the Linux Technology Center at IBM. She has more than five years experience working on the Linux and Solaris operating systems, including a year as a maintainer of part of the PowerPC Linux kernel tree. Contact Val at val@nmt.edu.

Summary:  In this first of a two-part series, learn about system and environment requirements, the best ways to acquire Linux® source code, how to configure and boot your new kernel, and how to use the printk function to print messages during bootup.

View more content in this series

Date:  20 Jul 2005
Level:  Introductory PDF:  A4 and Letter (67 KB | 21 pages)Get Adobe® Reader®

Activity:  21359 views
Comments:  

Booting your new kernel

Before you boot the kernel

Now that you've successfully compiled your new kernel, you need to boot it. We're going to cover booting a new kernel on three different architectures: x86, PowerPC, and Alpha. Some similarities exist among all three, so we'll start with the similarities.

Rule number one: Never, ever delete your current working kernel or bootloader configuration files! Never copy your new kernel over your original kernel. You don't need to keep every kernel you ever compile, but do pick one or more "safe" kernels and keep them and their configuration files intact.


Copy your new kernel to a safe location

First, copy the new kernel you just compiled to a safe location. We suggest using /boot/mynewkernel as the destination. If you're on x86, you want to copy the bzImage:

# cp arch/i386/boot/bzImage /boot/mynewkernel

If you're on a PowerPC or Alpha, copy the vmlinux:

# cp vmlinux /boot/mynewkernel


Booting your new kernel with LILO on an x86

LILO is the most common bootloader for x86 machines. You are running LILO if you see "LILO:" when you boot. We'll just go over the bare minimum of information you need to boot a new kernel in LILO. (For more information on LILO, type man lilo and man lilo.conf).

You will need to be root to edit /etc/lilo.conf and run LILO.

The LILO configuration file is in /etc/lilo.conf. The basic idea here is to copy the chunk of your configuration file for your current kernel and then change the name of the kernel file to boot. Remember, you are copying the information for your current kernel and changing the copy, not editing the original.

Say you have something like this in your /etc/lilo.conf:

image=/boot/vmlinuz-2.2.14-5.0
        label=linux
        initrd=/boot/initrd-2.2.14-5.0.img
        read-only
        root=/dev/sda1

  • The image field tells LILO where to find the file containing your new kernel.
  • The label field is what you type at the LILO prompt to boot that kernel. It's a good idea to make the label something short and easy to type.
  • The initrd field is optional and specifies an initial ramdisk to load before mounting the root file system.
  • The read-only field says to initially mount the root file system read-only rather than read-write.
  • The root field tells LILO which device contains the root file system (your root file system is what is mounted at /).

Copy this information and change the following fields:

image=<file name of your new kernel>
         label=<choose whatever name you'd like here>

Remove the initrd field if it exists:

initrd=/boot/initrd-2.2.14-5.0.img

The reason you should remove the initrd field is because the initrd file does not contain anything that would be useful to your new kernel. In a normal Linux distribution, the initrd contains a lot of kernel modules which can only be loaded by the distribution kernel.

The new lines you just added to your configuration file should look something like this when you're done:

image=/boot/mynewkernel
        label=new
        read-only
        root=/dev/sda1

LILO does not know that you changed anything in /etc/lilo.conf until you run the lilo command. You will probably do this more than once: compile a new kernel, copy it to the new place, forget to run LILO, and reboot, only to find that LILO doesn't know about the new kernel. So, after you change the /etc/lilo.conf file, always run LILO:

# lilo -v

The -v says to be verbose about what it's doing. LILO will complain if it can't find the files you told it to use.

Now, reboot, and you'll see the LILO prompt. Type the label you chose (the label you entered in the label field) for your new kernel. LILO will then try to boot that new kernel. To avoid typing a command line into LILO, run this just before you reboot:

# lilo -v -R "<LILO command line>"

This tells LILO to use that command line the next time you reboot. It then erases that command line and goes back to normal behavior the next time you reboot. This is really useful for testing new kernels since you can boot your new kernel, see it crash, reset the machine, and automatically boot your working kernel without ever typing anything.

If you are having trouble with LILO, see Resources for more help.


Booting your new kernel with GRUB on an x86

GRUB is becoming a common bootloader for x86 machines. You are running GRUB if you see a pretty graphical menu which lets you select which image to boot with the arrow keys when you boot. We'll just go over the bare minimum of information you need to boot a new kernel in GRUB. For more information on GRUB, type info grub. You will need to be root to edit /etc/grub.conf.

The GRUB configuration file is in /etc/grub.conf. The basic idea here is to copy the chunk of your configuration file for your current kernel and then change the name of the kernel file to boot. Remember, you are copying the information for your current kernel and changing the copy, not editing the original. Say you have something like this in your /etc/grub.conf:

title Linux (2.4.9-31)
        root (hd0,0)
        kernel /vmlinuz-2.4.9-31 ro root=/dev/hda3
        initrd /initrd-2.4.9-31.img

  • The title field is the title in the GRUB menu that corresponds to your new kernel.
  • The root field tells GRUB where your root file system is (your root file system is what is mounted at /).
  • The kernel field tells GRUB where to find the file containing your kernel and, after the kernel file name, it gives some options to pass the kernel when it boots. Note: The file name of the kernel is given relative to the boot partition, which usually means that you have to remove the /boot part from the file name.
  • The initrd field is optional and specifies an initial ramdisk to load before mounting the root file system.

Copy this information and change the following fields:

title <choose whatever name you'd like here>
        kernel <file name of your new kernel> ro root=/dev/hda3

Remember, you should almost certainly remove the /boot part from the file name of the kernel unless you know what you're doing.

Remove the initrd field if it exists:

initrd /initrd-2.4.9-31.img

The reason you should remove the initrd field is because the initrd file does not contain anything that would be useful to your new kernel. In a normal Linux distribution, the initrd contains a lot of kernel modules which can only be loaded by the distribution kernel.

The new lines you just added to your configuration file should look something like this when you're done:

title new
        root (hd0,0)
        kernel /mynewkernel ro root=/dev/hda3

Note again, that in this case our new kernel is in /boot/mynewkernel, but since we have a separate partition mounted on /boot and since GRUB will only look inside that partition when we boot, we need to take off the mountpoint part of the name, which is /boot. If we had everything, including /boot, on one big partition, then we would tell GRUB that our kernel was named /boot/mynewkernel.

Now, reboot, and you'll see the GRUB menu. Select the title you chose earlier with the arrow keys and hit Enter. GRUB will now try to boot your new kernel. For more information on GRUB, see the Resources section.


Booting your new kernel with yaboot and ybin on a PowerPC

Once again, we'll only cover the bare minimum to boot a new kernel under yaboot. For more information on yaboot and ybin, try man yaboot, man yaboot.conf, and man ybin. You will need to be root to edit /etc/yaboot.conf and run yaboot and ybin.

The yaboot configuration file is in /etc/yaboot.conf and is quite similar to the LILO configuration file. The basic idea here is to copy the chunk of your configuration file for your current kernel and then change the name of the kernel file to boot. Remember, you are copying the information for your current kernel and changing the copy, not editing the original. Say you have something like this in your /etc/yaboot.conf:

image=/boot/vmlinux-2.2.15-2.7.0
        label=linux
        root=/dev/hda10
        novideo

  • The image field tells yaboot where to find the file containing your new kernel.
  • The label field is what you type at the yaboot prompt to boot that kernel. It's a good idea to make the label something short and easy to type.
  • The root field tells yaboot which device contains the root file system.
  • The novideo field works around a bug on some PowerMacs and may or may not be necessary on your system.

Just copy the yaboot entry that works for your particular machine. After you copy this information, change the following fields:

image=<file name of your new kernel>
        label=<choose whatever name you'd like here>

The new lines you just added to your configuration file should look something like this when you're done:

image=/boot/mynewkernel
        label=new
        root=/dev/hda10
        novideo

Next, run ybin if you have it. Ybin is a helper program that takes care of copying all the necessary files to the bootable partition, running yaboot, and all the little details that Apple's Open Firmware requires for booting a kernel. If you don't have ybin, consult the yaboot man page, man yaboot, for information about what you need to do.


Troubleshooting booting

At this point, if your kernel doesn't boot, reboot into your old kernel (this is why you never overwrite your old kernel or configuration) and try to figure out what's gone wrong.

A good way to troubleshoot is to start out by copying the lines for your current kernel from your bootloader's configuration file and only changing the label or title of the entry. See if you can boot that kernel (and initrd, if it has one). Once you're sure that's working, then try substituting the name of your new kernel (and removing the initrd entry, if there is one). At this point, you can be fairly sure that it's your kernel that doesn't boot rather than a problem in your bootloader configuration.

5 of 9 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Linux, Open source
ArticleID=471616
TutorialTitle=Hacking the Linux 2.6 kernel, Part 1: Getting ready
publish-date=07202005
author1-email=linam@tyst.nu
author1-email-cc=
author2-email=val@nmt.edu
author2-email-cc=