How to compile the kernel-Debian way

How to compile the kernel-Debian way

Each distribution has some specific tools to build a custom kernel from source code. This article is about compiling the kernel on Debian (Etch) system. It describes how to build a custom kernel using the latest unmodified kernel source. www.kernel.org (Vanilla kernel) So that you can be independent of the kernel provided by the distribution. It also shows how to patch if you need a feature that does not exist in the kernel source code.

I do not guarantee that this will work for you!

1 Preliminary description

I will describe two ways to compile a new kernel. Using the first method, you will end up with a kernel .deb package that can be installed on the system and can be shared with others and installed on other Debian Etch systems.

The second method is to compile the kernel in a “traditional” way. This method works on any Linux distribution, but, of course, you won’t get the kernel .deb package in the end.

2 Build the kernel.deb package

This chapter describes how to build the kernel and finally get a .deb package, which you can install and share with others.

2.1 Install the software packages required for kernel compilation

First, we update the package database:

apt-get update

Then we install all the required packages as follows:

apt-get install kernel-package ncurses-dev fakeroot wget bzip2 build-essential

2.2 Download the kernel source code

Next, we download the required kernel to /usr/src.go with www.kernel.org And select the kernel you want to install, for example linux-2.6.21.3.tar.bz2 (you can find all 2.6 kernels here: https://www.kernel.org/pub/linux/kernel/v2.6/). Then you can download it to /usr/src like this:

cd /usr/srcwget https://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.21.3.tar.bz2

Then, we unzip the kernel source code and create a symlink linux to the kernel source code directory:

tar xjf linux-2.6.21.3.tar.bz2ln -s linux-2.6.21.3 linuxcd /usr/src/linux

2.3 Apply the patch to the kernel source (optional)

Sometimes, you need drivers for hardware that the new kernel does not support by default, or you need support for virtualization technology or some cutting-edge technologies that are not yet used in the kernel. In all these cases, you must patch the kernel source code (as long as there is a patch available…).

Now, suppose you have downloaded the required patch (called patch.bz2 in this example) to /usr/src. This is how to apply it to the kernel source (you still have to be in the /usr/src/linux directory):

bzip2 -dc /usr/src/patch.bz2 | patch -p1 --dry-runbzip2 -dc /usr/src/patch.bz2 | patch -p1

The first command is just a test and has no effect on your source code. If no errors are displayed, you can run a second command to actually apply the patch. If the first command shows an error, please do not perform this operation!

You can also apply kernel pre-patches to the kernel source. For example, if you need features that are only available in kernel 2.6.22-rc3, but the complete source code of the kernel has not been released yet. Instead, patch-2.6.22-rc3.bz2 is provided. You can apply the patch to the 2.6.21 kernel source, but not to the kernel 2.6.21.1 or 2.6.21.2, etc. https://kernel.org/patchtypes/pre.html:

The pre-patches are equivalent to the alpha distribution of Linux; they are located in the test directory of the archive. The patch(1) utility should be used to apply them to the source code of the previous full version with a three-part version number (for example, the 2.6.12-rc4 prepatch should be applied to the 2.6.11 kernel source, not, for example 2.6.11.10.)

Therefore, if you want to compile the 2.6.22-rc3 kernel, you must download the 2.6.21 kernel source code (https://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.21.tar.bz2) Instead of kernel 2.6.21.3!

This is how to apply the 2.6.22-rc3 patch to kernel 2.6.21:

cd /usr/src wget https://www.kernel.org/pub/linux/kernel/v2.6/testing/patch-2.6.22-rc3.bz2cd /usr/src/linuxbzip2 -dc /usr/src/patch-2.6.22-rc3.bz2 | patch -p1 --dry-runbzip2 -dc /usr/src/patch-2.6.22-rc3.bz2 | patch -p1

2.4 configure the kernel

It is best to use the configuration of the current working kernel as the basis for the new kernel. Therefore, we copy the existing configuration to /usr/src/linux:

make clean && make mrpropercp /boot/config-`uname -r` ./.config

Then we run

make menuconfig

The kernel configuration menu pops up. Go to load the alternate configuration file, and select .config (contains the configuration of the current working kernel) as the configuration file:

Save as .config file

Then browse the kernel configuration menu and make a selection. After you finish and choose to exit, click Yes to answer the following questions (Do you want to save the new kernel configuration?):

Save the new kernel configuration

2.5 Build the kernel

To build the kernel, execute the following two commands:

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

After –append-to-version=, you can write any string that can help you identify the kernel, but the string must start with a minus sign (-) and cannot contain spaces.

Please be patient now, kernel compilation may take several hours, depending on your kernel configuration and processor speed.

2.6 install new kernel

After successfully building the kernel, you can find two .deb packages in the /usr/src directory.

cd /usr/srcls -l

On my test system, they are called linux-image-2.6.21.3-custom_2.6.21.3-custom-10.00.Custom_i386.deb (contains the actual kernel) and linux-headers-2.6.21.3-custom_2.6.21. 3- custom-10.00.Custom_i386.deb (which contains the files needed if you want to compile other kernel modules in the future). I installed them like this:

dpkg -i linux-image-2.6.21.3-custom_2.6.21.3-custom-10.00.Custom_i386.debdpkg -i linux-headers-2.6.21.3-custom_2.6.21.3-custom-10.00.Custom_i386.deb

(You can now even transfer these two .deb files to other Debian Etch systems and install them here in exactly the same way, which means you don’t have to compile the kernel again there.)

That’s it. The GRUB boot loader configuration file /boot/grub/menu.lst has been automatically modified, and a ramdisk for the new kernel has been created in /boot.

Now restart the system:

shutdown -r now

At the boot prompt, select the new kernel (it should be selected by default):

Boot to the new kernel

If all goes well, you should use the new kernel.You can check if it is indeed using the new kernel by running the following command

uname -r

This should display something like

2.6.21.3-custom

If the system does not start, restart it, and then select your old kernel at the boot prompt. Now, you can try to compile a runnable kernel again. Don’t forget to delete the non-working kernel section from /boot/grub/menu.lst.

3Build the kernel in the traditional way

This chapter introduces another method that can be used on any Linux system (except for details such as creating a ramdisk or updating the boot loader). Of course, you won’t get the kernel .deb package in the end.

3.1 Install the software packages required for kernel compilation

First, we update the package database:

apt-get update

Then we install all the required packages as follows:

apt-get install kernel-package libncurses5-dev fakeroot wget bzip2 build-essential

3.2 Download the kernel source code

Next, we download the required kernel to /usr/src.go with www.kernel.org And select the kernel you want to install, for example linux-2.6.21.3.tar.bz2 (you can find all 2.6 kernels here: https://www.kernel.org/pub/linux/kernel/v2.6/). Then you can download it to /usr/src like this:

cd /usr/srcwget https://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.21.3.tar.bz2

Then, we unzip the kernel source code and create a symlink linux to the kernel source code directory:

tar xjf linux-2.6.21.3.tar.bz2ln -s linux-2.6.21.3 linuxcd /usr/src/linux

3.3 Apply the patch to the kernel source (optional)

Sometimes, you need drivers for hardware that the new kernel does not support by default, or you need support for virtualization technology or some cutting-edge technologies that are not yet used in the kernel. In all these cases, you must patch the kernel source code (as long as there is a patch available…).

Now, suppose you have downloaded the required patch (called patch.bz2 in this example) to /usr/src. This is how to apply it to the kernel source (you still have to be in the /usr/src/linux directory):

bzip2 -dc /usr/src/patch.bz2 | patch -p1 --dry-runbzip2 -dc /usr/src/patch.bz2 | patch -p1

The first command is just a test and has no effect on your source code. If no errors are displayed, you can run a second command to actually apply the patch. If the first command shows an error, please do not perform this operation!

You can also apply kernel pre-patches to the kernel source. For example, if you need features that are only available in kernel 2.6.22-rc3, but the full source code of that kernel has not been released. Instead, patch-2.6.22-rc3.bz2 is provided. You can apply the patch to the 2.6.21 kernel source, but not to the kernel 2.6.21.1 or 2.6.21.2, etc. https://kernel.org/patchtypes/pre.html:

The pre-patches are equivalent to the alpha distribution of Linux; they are located in the test directory of the archive. The patch(1) utility should be used to apply them to the source code of the previous full version with a three-part version number (for example, the 2.6.12-rc4 prepatch should be applied to the 2.6.11 kernel source, not, for example 2.6.11.10.)

Therefore, if you want to compile the 2.6.22-rc3 kernel, you must download the 2.6.21 kernel source code (https://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.21.tar.bz2) Instead of kernel 2.6.21.3!

This is how to apply the 2.6.22-rc3 patch to kernel 2.6.21:

cd /usr/src wget https://www.kernel.org/pub/linux/kernel/v2.6/testing/patch-2.6.22-rc3.bz2cd /usr/src/linuxbzip2 -dc /usr/src/patch-2.6.22-rc3.bz2 | patch -p1 --dry-runbzip2 -dc /usr/src/patch-2.6.22-rc3.bz2 | patch -p1

3.4 configure the kernel

It is best to use the configuration of the current working kernel as the basis for the new kernel. Therefore, we copy the existing configuration to /usr/src/linux:

make clean && make mrpropercp /boot/config-`uname -r` ./.config

Then we run

make menuconfig

The kernel configuration menu pops up. Go to load the alternate configuration file, and select .config (contains the configuration of the current working kernel) as the configuration file:

Configure the kernel

Save as .config file

Then browse the kernel configuration menu and make a selection. After you finish and choose to exit, click Yes to answer the following questions (Do you want to save the new kernel configuration?):

Save the new kernel configuration

3.5 Compile and install the kernel

To build the kernel, execute the following command:

make allmake modules_installmake install

Please be patient now, kernel compilation may take several hours, depending on your kernel configuration and processor speed.

3.6 Post-installation steps

Now that the new kernel is installed, we still need a ramdisk for the new kernel (otherwise the system will probably not boot!), and we need to inform the GRUB bootloader about our new kernel.

First, we do this:

depmod 2.6.21.3apt-get install yaird

Then, use the following command to create a virtual disk:

mkinitrd.yaird -o /boot/initrd.img-2.6.21.3 2.6.21.3

On Debian Etch, GRUB configuration is very simple.All we have to do is run

update-grub

This will detect the new kernel and ramdisk and update /boot/grub/menu.lst for us.

Now restart the system:

shutdown -r now

At the boot prompt, select the new kernel (it should be selected by default). If all goes well, you should use the new kernel.You can check if it is indeed using the new kernel by running the following command

uname -r

This should display something like

2.6.21.3

If the system does not start, restart it, and then select your old kernel at the boot prompt. Now, you can try to compile a runnable kernel again. Don’t forget to delete the non-working kernel section from /boot/grub/menu.lst.

Source

Related Posts