How to re-compile the kernel in Ubuntu

Source: Internet
Author: User

Each release has some special tools usedSource codeCreate a custom kernel.ArticleIt is about how to compile the kernel in the Ubuntu system. The content includes how to use the latest and unmodified sources downloaded from www.kernel.org.CodeTo build a custom system, so that you can be independent from the kernel provided by the release. This article also covers how to fix the kernel source code if you need features not included in the existing kernel.

Each release comes with some special tools used to create a custom kernel from the source code. This article describes how to compile the kernel in the Ubuntu system. The content includes how to use the latest and unmodified source code downloaded from www.kernel.org to build a custom system, you can be independent from the kernel provided by the release. This article also covers how to fix the kernel source code if you need features not included in the existing kernel.

I have tested the steps described in Ubuntu 6.10 server ("edgy EFT") and Ubuntu 6.06 desktop ("Dapper Drake. However, it must be stated in advance that this is not the only way to build a system. In fact, there are many ways to achieve the same purpose. What is called a great path to Rome, I just took this path. Therefore, I cannot guarantee that this method applies to you.

By the way, I would like to execute all the following steps as root. Therefore, if you have not created a root account that can be logged in, Please create one immediately.

Sudo passwd Root

Then, log in as root:

Su

It doesn't matter if you have friends who like to perform operations as normal users. You just need to note that you can add sudo to all the commands in this article. For example, if I run

Apt-Get update

Then you should run

Sudo apt-Get update

Repeat this.

OK. Next, go to the specific steps.

Basic installation steps
Modify/bin/sh on Ubuntu 6.10
On ubuntu6.10,/bin/sh is linked to/bin/dash by default. When you compile software from source code, dash may cause some errors. At least I have encountered similar problems, therefore, I changed the/bin/sh connection to/bin/bash. (It is estimated that most users in China use Bash)

If you are using 6.10, you should do this:

Rm-F/bin/sh

Ln-S/bin/bash/bin/sh

Install the package required for Kernel Compilation
First, let's update the installation package database on our machine.

Apt-Get update

Then, use this command to install all the required packages:

Apt-Get install kernel-package libncurses5-dev fakeroot wget Bzip2

Download kernel source code
Next, we will download the required kernel to/usr/src. All kernels of version 2.6 can be obtained from http://www.kernel.org/pub/linux/kernel/v2.6. I downloaded it like this:

CD/usr/src

Wget

Http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.18.1.tar.bz2

You can also use different download methods to download unused kernel versions as needed.

After that, unpackage the source code and create a connection Linux to the source code Software Directory:

Tar xjf linux-2.6.18.1.tar.bz2

Ln-s linux-2.6.18.1 Linux

CD/usr/src/Linux

Patch the kernel source code
Sometimes, you need some hardware drivers, but the new kernel is not provided by default, or you may need to use virtual technologies or other cutting-edge technologies, but they are not added to the new kernel. In these cases, You have to patch your kernel (the premise is that there is a patch ).

Now, I assume you have downloaded the corresponding patch. Here we will replace patch.bz2. Put it in the/usr/src directory, and then use the following command to make it take effect:

Bzip2-DC/usr/src/patch.bz2 | patch-P1-dry-run

Bzip2-DC/usr/src/patch.bz2 | patch-p1

Note! At this time, you should always be in the/usr/src/Linux directory.

The first command is only experimental, and does not change your source code. If no error message is generated, you can use the second command to generate a true patching action for the source code. If the First Command has an error, please do not continue to execute it to see where the error is.

Similarly, you can provide pre-patches to your kernel. For example, once you find that you have a feature that you need to renew in 2.6.19 rc4, but this kernel does not have any source code to issue, only patch-2.6.19-rc4.bz2 is optional. Therefore, you can provide the patch to the kernel 2.6.18, but not 2.6.8.1 or 2.6.8.2.

Http://kernel.org/patchtypes/pre.htmlto explain this:

In fact, the pre-patch is equivalent to an alpha version of Linux, which exists in the test directory in the repository. They can only be submitted by the patch tool for the source code of the previous version. The last fully released version must be composed of three versions. For example, 2.6.12-RC4 prepatch can be provided to kernel source code 2.6.11, but 2.6.11.10 is useless.

So if you want to compile the 2.6.19rc4 kernel, you should download the kernel source code (http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.18.tar.bz2) of 2.6.18 ). The steps are as follows:

CD/usr/src

Wget

Http://www.kernel.org/pub/linux/kernel/v2.6/testing/patch-2.6.19-rc4.bz2

CD/usr/src/Linux

Bzip2-DC/usr/src/patch-2.6.19-rc4.bz2 | patch-P1-dry-run

Bzip2-DC/usr/src/patch-2.6.19-rc4.bz2 | patch-p1

Configure and install
Configure the kernel
Using the existing Kernel configuration file as the basis of the new Kernel configuration file is undoubtedly a good idea. Therefore, copy the existing configuration file to the/usr/src/Linux directory.

CP/boot/config-'uname-R'./. config

Then run

Make menuconfig

The Kernel configuration menu is displayed. Select "load an alternate configuration file", and then select the. config file you just copied as the configuration file. Then confirm. When you choose to exit, you will be prompted to "do you wish to save your new Kernel configuration ?" Select Yes.

Create a kernel
To create a kernel, run the following two commands:

Make-kpkg clean

Fakeroot make-kpkg-initrd-append-to-version =-custom kernel_image kernel_headers

After "-append-to-version =", you can write any string that helps to mark the kernel, provided that it must start with the-symbol and cannot contain any blank characters.

Now, please be patient. It may take you several hours to compile the kernel, depending on your Kernel configuration and machine performance.

Install a new kernel
After the kernel is built successfully, you can see two. Deb files in the/usr/src directory. In my testing systems, they are called linux-image-2.6.18.1-custom_2.6.18.1-custom-10.00.Custom_i386.deb (containing real Kernels) and linux-headers-2.6.18.1-custom_2.6.18.1-custom-10.00.Custom_i386.deb (containing the files needed to compile additional kernel modules ).

I use this method to install:

Dpkg-I linux-image-2.6.18.1-custom_2.6.18.1-custom-10.00.Custom_i386.deb

Dpkg-I linux-headers-2.6.18.1-custom_2.6.18.1-custom-10.00.Custom_i386.deb

You can even transfer the two Deb packages to other Ubuntu systems and install them in the same way. This means that you no longer need to compile them again.

Now, you can check your menu. LST file. There are two more parts related to the new kernel:

Title Ubuntu, kernel 2.6.18.1-custom

Root (hd0, 0)

Kernel/boot/vmlinuz-2.6.18.1-custom root =/dev/sda1 Ro quiet splash

Initrd/boot/initrd. img-2.6.18.1-custom

Savedefault

Boot

Title Ubuntu, kernel 2.6.18.1-custom (recovery mode)

Root (hd0, 0)

Kernel/boot/vmlinuz-2.6.18.1-custom root =/dev/sda1 Ro single

Initrd/boot/initrd. img-2.6.18.1-custom

Boot

Now, restart the computer and shutdown-R. If everything goes well, after the restart, you will use the new kernel. You can run the "uname-R" command to check the current kernel version. It should be represented as "2.6.18.1-custom"

Some Problems
If a problem occurs during restart, when you see "Press ESC to enter the menu..." on the screen... When you press ESC in this line, the grub Startup menu is displayed. Select your old version kernel and start the system. Now, you have to re-compile the kernel so that it can be used properly. Do not forget to remove the previous two abnormal kernel regions from menu. lst before compilation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.