Compile the kernel in ubuntu

Source: Internet
Author: User
Compile the kernel under ubuntu-general Linux technology-Linux programming and kernel information. The following is a detailed description. 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.

1.1 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


1.2 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


1.3 download the 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/... ux-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


1.4 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/... atch-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


2.1 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 ask "Do you wish to save your new kernel configuration? "Select yes.


2.2 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.


2.3 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"


3. Some Problems

If a problem occurs during restart, when you see "Press ESC to enter the menu... 1 "when this line is entered, Press ESC and the grub Startup menu will be displayed. Select your old version kernel to 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.