When they tell me about the severe brakes of Linux compared to Windows installed on the same machine, I ask one single question: “Do you have a self-assembled kernel or the bloated one that comes with the distribution by default?” Usually the answer is: “Naturally, what’s in the distribution!”

In addition, at the same time, it is perceived with great surprise that in order to improve the operation of the system, it would not hurt to rebuild the kernel for your specific computer in order to disable in it a huge amount of what is absolutely not necessary for your hardware, but only slows down its operation... A reason immediately arises The question is, why did the developers of the distribution include such a slow kernel that an ordinary user who installed this distribution must recompile it himself?.. And the question seems to be logical, but not quite...

Distribution developers usually include in it a kernel that could easily recognize the maximum possible hardware, both on the computer that your grandfather bought so that he could play Call of Duty when he wasn’t watching news on TV, and the latest system, purchased to get the most out of the computer's capabilities. Naturally, in this case, the kernel turns out to be extremely bloated, and during operation it is terribly slow, because the system every time has to “pick through” a large pile of entries and drivers inside the kernel, which cannot please either you or your computer... And compiling your own kernel allows you to disable everything unnecessary, leaving only the necessary functionality... The fact is that when running the Linux operating system, the kernel is the only memory section that is not swapped, so it is logical to make it as small as possible. I hope I have explained in a popular scientific way the absolute necessity of rebuilding the kernel?..

Usually, having first realized the importance of kernel compilation, novice penguin breeders give up, believing that this requires a lot of knowledge and time, and in general this is an activity for red-eyed administrators who fall asleep only for a couple of hours with a laptop under their arm, and certainly not for humans, not enough still understands Linux... However, I hasten to assure you that everything is very simple and you should not be afraid of the not so complex procedure. You just need to know the complete configuration of your computer and set aside an hour to carefully configure the kernel. The installation program will assemble the kernel, and you just have to leave the machine at its complete disposal for half an hour. 🙂

So, as promised earlier, I’ll tell you about the process of setting up and compiling your own kernel... Let's go!

The first thing we need is the actual source code of the new kernel, under which your Linux will soon run. You can download it completely freely at http://www.kernel.org. You need to download the latest stable kernel (it is usually the first one in the list, where it says “The latest stable version of the Linux kernel is...”). I advise you to take the tar.bz2 archive, but only because it is smaller in size. If you have already assembled the kernel, then you can simply download patches, that is, patches, so as not to download the entire kernel, which is quite large in volume... In fact, I recommend using the full kernel, because I read that when assembling the kernel using patches, various problems can arise , often directly related to simple curvature of the hands, however, without the appropriate knowledge it is better not to take risks...

So, the kernel has been downloaded... We issue the command:

sudo chmod 777 /usr/src

After that, drop it (the downloaded archive) into the /usr/src folder. Unpack the archive with the command:

tar -jxvf xxxxx.tar.bz2

In this command, xxxxx.tar.bz2 is the name of the archive file with the kernel... As soon as the unpacking is completed, your sources will be located in the /usr/src/xxxxx directory.

Before you start configuring the kernel, find out exactly the specifications of your computer:
1) processor type,
2) motherboard chipset,
3) type of video card,
4) type of network card,
5) sound card type,
6) type of hard drives,
7) USB devices and so on...

When everything is installed and studied, you can begin to actually configure the kernel.

We go to the /boot directory, and we see a file there with the name config-2.6.24-19-generic. That's what we need. Copy it to the source directory /usr/src/xxxxx and rename it to .config.

We also first open the Makefile file, and in the line “EXTRAVERSION=" we add our name for the kernel, for example, EXTRAVERSION=-mykernel. This line does not serve any other function except for convenience in identifying one’s own core.

This command launches the console configuration interface. If you prefer a graphical configurator, then to launch it you need to use the command:

There is also a console configuration option:

This option is very useful only when you already have a generated .config settings file (you can use the settings file from the old kernel). In this option, only questions are asked about features that appeared with the version of the kernel for which the settings file was made.

If you want the kernel to work stably and as quickly as possible, approach the configuration issue responsibly. Read each paragraph carefully. If you don’t understand something, look for answers on the Internet. For example, you can find descriptions of many settings and tips on how to best use them. By the way, in the section “3.4 Example of kernel configuration” there are also several tips on kernel optimization.

After configuration, select “Save configuration to a new file”, where we specify the name of a file other than .config, for example .config_mykernel. Then rename .config to .config_old. And you get two files - .config_old and .config_mykernel. By the way, it is possible to see the differences between the standard and your configuration like this:

diff .config .config_mykernel

Now copy your configuration .config_mykernel as .config. Those. you will end up with three configuration files. The .config file will be used during compilation. The .config_ubuntu and .config_mykernel files will help us in the future for a new compilation. This is in case the new kernel turns out to be inoperable.

Compiling the kernel should be done as a superuser. Why do we become root and start the kernel assembly process:

sudo -s -H
make-kpkg --initrd --revision=mykernel.1 kernel_image kernel_headers

in this line mykernel.1 denotes the name of the kernel. You can write whatever you want.

The compilation operation takes quite a long time, depending on the power of the computer; for me it usually took about half an hour...

At the end of the process, two deb files will be created with a kernel configured specifically for your needs. Congratulations, the kernel is ready.

Now you need to install the assembled kernel into the system:

sudo dpkg -i linux-image-xxxxx.deb
sudo dpkg -i linux-headers-xxxxx.deb

Replace with the exact names of the deb files you created. Next, we reboot and see our kernel in the menu. If you've got everything loaded as expected, I think you'll notice an immediate performance boost. However, if you have, for example, a card from nVidia installed, most likely this will all end... For example, after launching my version of the kernel, I simply had a black screen and zero emotions...

This was fixed by installing proprietary drivers from nVidia. There are two options for installing them: under a new kernel (in this case, the graphic server will work only under your kernel (recommended if you do not plan to work in any kernel other than yours) and under several existing kernels (in this case, the system will boot both in the new kernel you created, and in the previous one).But that’s a completely different story...

Why would someone want to compile a new kernel? It is often not necessary since the default kernel shipped with Debian handles most configurations. Also, Debian often offers several alternative kernels. So you may want to check first if there is an alternative kernel image package that better corresponds to your hardware. However, it can be useful to compile a new kernel in order to:

Don’t be afraid to try compiling the kernel. It's fun and profitable.

To compile a kernel the Debian way, you need some packages: , and a few others which are probably already installed (see for the complete list).

This method will make a .deb of your kernel source, and, if you have non-standard modules, make a synchronized dependent .deb of those too. It’s a better way to manage kernel images; will hold the kernel, the System.map, and a log of the active config file for the build.

Note that you don't have to compile your kernel the “Debian way”; but we find that using the packaging system to manage your kernel is actually safer and easier. In fact, you can get your kernel sources right from Linus instead of , yet still use the compilation method.

Note that you’ll find complete documentation on using under . This section just contains a brief tutorial.

Hereafter, we’ll assume you have free rein over your machine and will extract your kernel source to somewhere in your home directory. We'll also assume that your kernel version is 3.16. Make sure you are in the directory to where you want to unpack the kernel sources, extract them using and change to the directory that will have been created.

Now, you can configure your kernel. Run if X11 is installed, configured and being run; run otherwise (you’ll need installed). Take the time to read the online help and choose carefully. When in doubt, it is typically better to include the device driver (the software which manages hardware peripherals, such as Ethernet cards, SCSI controllers, and so on) you are unsure about. Be careful: other options, not related to a specific hardware, should be left at the default value if you do not understand them. Do not forget to select “Kernel module loader” in “Loadable module support” (it is not selected by default). If not included, your Debian installation will experience problems.

Clean the source tree and reset the parameters. To do that, do.

Now, compile the kernel: . The version number of “1.0” can be changed at will; this is just a version number that you will use to track your kernel builds. Likewise, you can put any word you like in place of “custom” (e.g., a host name). Kernel compilation may take quite a while, depending on the power of your machine.

Once the compilation is complete, you can install your custom kernel like any package. As root, do . The part is an optional sub-architecture, depending on what kernel options you set. will install the kernel, along with some other nice supporting files. For instance, the will be properly installed (helpful for debugging kernel problems), and will be installed, containing your current configuration set. Your new kernel package is also clever enough to automatically update your boot loader to use the new kernel. If you have created a modules package, you’ll need to install that package as well.

It is time to reboot the system: read carefully any warning that the above step may have produced, then .

For more information on Debian kernels and kernel compilation, see the Debian Linux Kernel Handbook. For more information on, read the fine documentation in.

If you remember, not so long ago we learned how to compile the FreeBSD kernel from source code. The question is, why not learn to do the same with the Linux kernel? The reasons for building the Linux kernel from source are generally the same - obtaining the latest version of the kernel, urgent application of security patches, optimization for specific tasks and specific hardware, as well as the desire to take part in the development of the kernel, even in the role of QA.

Important! Following the instructions in this post may result in the loss of your data. Make backups and remember that you do everything entirely at your own peril and risk. Everything described below has been tested on Ubuntu 14.04 LTS. But on other versions of Ubuntu, as well as other Linux distributions, the differences should be minimal.

Setting up the bootloader

Edit /etc/default/grub something like this:

GRUB_DEFAULT=0
# GRUB_HIDDEN_TIMEOUT=10
# GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=10
GRUB_DISTRIBUTOR=`lsb_release -i-s2>/dev/null ||echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""

After editing we say:

sudo update-grub

As a result, before the system boots, you will be prompted for 10 seconds to select the kernel you want to boot with. Very convenient if you mess up something with the kernel configuration and want to boot with the previous version!

Installing dependencies

We will need at least the following packages:

sudoapt-get installgitgccmakebc fakeroot dpkg-dev\
libncurses5-dev libssl-dev

On many systems, all of them, however, will already be present.

Getting the sources

wget https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.6.4.tar.xz
tar—xz-xvf linux-4.6.4.tar.xz
cd linux-4.6.4

Or, if you need the very latest, you can take the sources directly from Git:

# Mirror: https://github.com/torvalds/linux
git clone’git://git.kernel.org/pub/scm/linux/kernel/git/’\
'torvalds/linux.git'
cd linux

Judging by the fact that I could not find the v4.6.4 tag in Git, Linux kernel releases are issued exclusively in the form of compressed tar archives.

If, instead of the vanilla kernel, you would like to build a kernel with patches from Canonical:

git clone git://kernel.ubuntu.com/ubuntu/ubuntu-trusty.git
cd ubuntu-trusty
git tag|less
git checkout Ubuntu-lts-4.4.0-31.50_14.04.1

From my own experience, I will say that if you use Ubuntu, you can safely use the vanilla kernel. It is unlikely that you will have any problems with it.

Note: Interestingly, of the existing relatively popular Linux distributions, only Gentoo, Slackware and Arch Linux seem to use a kernel without their own patches.

One way or another, now you have the sources.

Compiling and installing the kernel

Select the options with which the kernel will be compiled:

If necessary, change the settings, click Save, then Exit. As a result, a file will be created containing the parameters we have selected.

At update kernel (are you already using some kind of kernel anyway?) it’s convenient to take the config of the current kernel and set the new options to default values:

zcat/proc/config.gz > ./.config
make olddefconfig

Finally, we collect:

make-j4 bindeb-pkg LOCALVERSION=-custom

The core takes quite a long time to assemble. On my laptop the assembly took 1 hour and 15 minutes. However, from this time O The bulk is spent on building a giant kernel package with debugging symbols.

Manually building and configuring the Linux kernel

The build of this package can be disabled by commenting out the CONFIG_DEBUG_INFO parameter in the config. Just keep in mind that this package is required by SystemTap and other useful tools.

In addition to the kernel itself, you can also collect documentation:

# there is also `make pdfdocs` and others, see `make help`
make htmldocs
chromium-browser Documentation/DocBook/index.html

Upon completion of assembly in subsidiary directory we see something like:

linux-firmware-image-4.4.13-custom_4.4.13-custom-1_amd64.deb
linux-headers-4.4.13-custom_4.4.13-custom-1_amd64.deb
linux-image-4.4.13-custom_4.4.13-custom-1_amd64.deb
linux-image-4.4.13-custom-dbg_4.4.13-custom-1_amd64.deb
linux-libc-dev_4.4.13-custom-1_amd64.deb

We install all deb packages except the dbg version of the kernel as usual and reboot. After the reboot, look at the command output. We make sure that we really booted with the new kernel. If there is something wrong with the new kernel, in the bootloader we simply select the one with which the system was booted before. After booting with the old kernel, we quickly remove the packages of the new kernel, and voila - the system is back to its previous state.

Additionally, after loading the new kernel, you can run tests on it:

sudomake kselftest
sudomake kselftest-clean

Congratulations, you now know how to build a Linux kernel!

Conclusion

Materials on the internal structure of the Linux kernel:

  • KernelNewbies.org, a site for newcomers to the Linux kernel with its own mailing list, IRC channel, forum, wiki, etc.;
  • LKML.org, a site for comfortable reading the mailing list of Linux kernel developers via a browser. Be careful, it often spoils the formatting of letters! In this sense, the mailing list archive on marc.info is much better;
  • Linux Cross Reference, a site for comfortable reading Linux kernel code through a browser;
  • The book Linux Kernel Development, 3rd Edition is the most recent book on the Linux kernel in English (2010, kernel 2.6.x), there is a translation into Russian;
  • The book Linux-Treiber entwickeln, 4th Edition is more recent (2015, kernel 4.x), but is written in German and has no translation;
  • The free book Linux Insides by Alexander Kuleshov a.k.a @0xAX looks the most accessible and up-to-date, but is currently still in the process of being written;
  • Sometimes you can find something useful in the Linux Kernel HTML Documentation, the official documentation from the Linux kernel sources;
  • Examples for the book Linux Device Drivers, 3rd Edition, which they are trying to keep up to date, plus sources for the 4th edition, which is currently still being written;

Are you building the kernel from source, and if so, why? 🙂

Addition: You may also be interested in the article The scandalous truth about debugging kernel code in Linux.

Tags: Linux, Ring0.

Compiling the Linux kernel


Many consider this a pointless waste of time in order to show themselves as a smart and advanced Linux user. In fact, compiling the kernel is a very important matter. Let's say you bought a new laptop and your webcam doesn't work. Your actions? You look into the search engine and look for a solution to the problem on this issue. Quite often it may turn out that your webcam is running on a newer version of the kernel than yours. If you don't know what version you have, enter uname -r in the terminal, as a result you will get the kernel version (for example, linux-2.6.31-10). Kernel compilation is also widely used to increase performance: the fact is that by default, kernel distributions compile “for everyone,” which is why it includes a huge number of drivers that you may not need. So if you know the hardware you are using well, you can disable unnecessary drivers at the configuration stage. It is also possible to enable support for more than 4 GB of RAM without changing the system bit depth.

How to build a Linux kernel? Building the Linux kernel

So, if you still need to have your own kernel, let's start compiling!


The first thing you need to do is get the source code for the required kernel version. Usually you need to get the latest stable version. All official kernel versions are available at kernel.org.

If you already have an X server installed (home computer), then you can go to the site in your favorite browser and download the desired version in the tar.gz archive (gzip compressed). If you are working in the console (for example, you have not yet installed the X server or are configuring the server), you can use a text browser (for example elinks).

You can also use the standard download manager wget:


tar -zxvf path_to_archive

Kernel configuration.

Compilation.
make && make install
We wait from 20 minutes to several hours (depending on the power of the computer). The kernel is installed. To make it appear in the grub(2) list, enter (as superuser)
update-grub


Articles archive:
May 2017
March 2017
April 2016
March 2016
October 2013
September 2013
May, 2013
March 2013
November 2012
July 2012
June 2012
April 2012
March 2012
February 2012
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010

Random:

Stellarium virtual planetarium: overview of new features

BOINC: Computing for Science

Gambas: Development in Basic on Linux

Sequential Diagrams SSH 2.0

Linuxnow.ru: Plans for the future

Suddenly running out of free disk space? Perhaps the problem is in the logs.

Colleagues: everyone
Linux for everyone

Our banners:


Set banner

Linux Kernel Configuration

Checking source packages

To recompile the kernel, its source code is required.

Blog about system administration. Articles about Linux, Windows, NetApp storage systems and virtualization.

In Red Hat, the kernel sources are placed in one rpm package, but are not installed by default. The name of this package is:

kernel-source-2.4.2-2.1386.rpm

In addition to the source code, to recompile the kernel you need a compiler consisting of two components:

make utility;

The C language compiler itself is gcc or egcs.

Creating a backup copy of the current kernel is carried out in three stages:

1. Creating a backup copy of the kernel source code, which allows you to restore it if the configuration is damaged.

2. Creating a backup copy of the kernel itself as a known working means of starting the system.

3. Creating a new entry for the operating system loader, which will allow you to boot from a kernel backup.

Creating a backup copy of the source code is done by copying the directory with the kernel source code to disk. Restoration is performed by copying in the opposite direction.

When a kernel is recompiled, the old kernel is written to a file with the .old extension. However, this copy of the kernel cannot yet be used to boot the system. This explains the need to perform the above operations.

The LILO operating system boot loader, commonly used to boot Linux, is configured by setting a link to a kernel file on the root file system.

After creating a kernel backup, you need to add one more entry to the /etc/lilo.conf file to allow you to start Linux with the old kernel:

open the file /etc/lilo. conf in a text editor;

create a copy of the entire partition;

change two positions in the copy:

replace the name of the kernel file with the name of its backup copy (together with the extension);

replace the partition label with something like linux. original (original Linux) or linux-previous (former Linux).

lines starting with initrd should be replaced with corresponding reserve ones, for example, initrd-2 .4.2-2. orig. Img;

write changes to /etc/lilo. conf.

enter the command /sbin/lilo to introduce a new entry into the bootloader. When you run the lilo command, the labels of the images entered into the operating system loader are displayed on the screen.

The next time you reboot the system, the new kernel identifier will appear on the screen in the LILO bootloader graphical prompt.

Configuring a new kernel

During configuration, it is decided which functions to include in the kernel, which not to include, etc. You can choose between configuring the old kernel and installing/configuring the new one. For example, using Red Hat Linux 7.1, you can reconfigure an existing 2.4.2 kernel by specifying new parameters. You can also download and install the new kernel version 2.4.4. Although the configuration details are different in these two cases, the utilities used and the configuration methodology are the same.

Linux has three separate configuration utilities, each with its own features.

A utility with a command line interface. The user consistently answers questions about the features that need to be included in the kernel. This utility is convenient for specialists who know how to work with the kernel, and for those who have configuration scripts for this utility.

A utility with a text mode menu. The multi-level menu of this utility allows you to install and reinstall kernel parameters in any order.

GUI utility. This is the most attractive utility, but it only runs on the X Window graphical system.

The listed utilities create the same configuration file, which is used by the make utility when compiling the kernel completely or partially.

Kernel parameters

When viewing parameters in any configuration program (command line, text or graphical interface), you must clearly understand the impact of these parameters on the operation of the kernel.

The identification of parameters in each program is different, but all three present the same set of them. The parameters are divided into two main groups:

modular;

non-modular.

If the program block corresponding to this parameter does not load as a kernel module, it can be one of two things:

[*] an integral part of the kernel;

An integral part of the core.

Characters in square brackets (along with brackets) correspond to option marks in the menu of configuration programs (except for the command line utility).

For modular parameters, three installation options are possible (according to their presentation in the configuration utilities menu):

<>is not included in the kernel and is not created as a module that can be loaded later:

<*>included in the kernel, so there is no need to load it later as a module:

<М>Included as a module, but not as part of the kernel. A block can be installed or removed from the kernel at any time.

Sometimes the value of a parameter cannot be changed before setting another parameter. For example, you can only install support for a specific SCSI device after you have generally enabled support for those devices.

When the necessary tools (the make utility and the gcc compiler) and source codes are installed on the system, you can launch one of the configuration utilities and begin configuring the kernel.

GUI configurator

The configuration utility with a graphical interface is launched in the following sequence.

Launch the X Windows system (with any graphical environment and desktop).

Open a terminal emulator window (command line window).

As when launching the previous utility, several messages about the compilation of the utility will flash on the screen, and after a few minutes its main window will open.

Rice. 8. Kernel configuration utility with graphical interface: menu buttons for setting parameter values

The graphical utility is not very different from the menuconf ig utility. Here the options are also divided into categories; After clicking on a specific category, a dialog box opens with a list of parameters, each of which can be set to a value that determines whether the corresponding tool should be used as part of the kernel, as a loadable module, or not at all.

Such a dialog box is shown in Fig.

Rice. 9. Dialog boxes like this set kernel configuration parameters

The 2.4.2 kernel in Red Hat Linux supports symmetric multiprocessor architecture by default. You must set the corresponding option to n if your computer has only one processor.

To the right of each option is a Help button.

A big advantage of the graphical configuration utility (especially those who are configuring the kernel for the first time) is the reflection in the menu of the dependencies between various parameters. For example, in the Block Devices section, you cannot install the support for RAID systems built into the kernel until the Multiple Device Driver Support option is enabled.

If in the menuconfig utility the dependence of parameters on each other is shown by indenting the parameter names in the menu, then in the graphical utility it is simply impossible to set unauthorized combinations of parameters. Working with a graphical utility helps you understand the dependencies between different modules.

At the end of the main menu of the graphical utility there are commands similar to the corresponding commands of the utility with the text mode menu.

Save and Exit. Create a kernel configuration file and exit the utility.

Quit Without Saving. Completing the utility without creating a kernel configuration file.

Without creating a kernel configuration file using one of the listed utilities, recompiling the kernel is impossible.

Load Configuration From File. Loading the configuration file created earlier.

Store Configuration To File. Write configuration data to a file with the specified name (for further use or transfer to a colleague). This does not affect the need to run the Save and Exit command, which creates a configuration file for recompiling the kernel.

Compiling and running a new kernel

After configuration is complete, you need to check for a new configuration file (.config) located in the /usr/src/linux-2 .4.2 directory (depending on the version number). If the .config file is in place, you can use the make command to recompile the kernel.

Typically, kernel recompilation takes from 15 minutes to several hours. This depends on the speed of the processor, the amount of RAM and a number of other factors, so it is convenient to combine all commands entered during compilation into one, separated by a semicolon, so that they are executed sequentially.

Starting the recompilation process

The commands below are designed to create a new kernel by recompiling all its modules and writing them to the appropriate system directories. (There they will be available to kernel commands.)

To recompile the system, enter the following commands.

# make dep; make clean; make bzlmage; make modules; make modules__install

Each make command can be entered separately after the previous one has completed.

After entering these commands, lines will begin to flash on the screen describing the directories accessed by the make program, launching the gcc compiler. to compile various source files and link various blocks. Each of these commands will take a few minutes to complete.

You can now create a boot disk for the new kernel using the command

Before running this command, you must insert a formatted floppy disk into the drive. The finished boot disk must be tested. Restart your computer without removing the floppy disk from the drive.

After running the kernel compilation commands and returning to the command line, a new kernel is created. To boot the system with a new kernel, it must be moved to the standard directory from which it will be launched. This is done by entering the command

# cf /usr/src/linux-2.4.2/arch/i386/boot/bzlmage /boot/vmlinuz-2.4.2-2

Finally, to update the download map, run the lilo command:

You can specify the version number in the name of the copied kernel image. It is important that the name of this file matches the name specified in the /etc/lilo.conf file.

Testing the new kernel

After moving the new kernel file to the default directory (specified in the lilo.conf file), the system can be rebooted with that kernel.

Immediately after the reboot, check the operation of the new tools, for the sake of which the recompilation was started. You can do the following:

Comparison of the volumes of old and new cores. You should check the amount of memory occupied by the operating system by entering the free command.

Installing a file system or attempting to access a device without loading a kernel module to support it (if support for the device is built into the kernel).

Use of network resources (for example, IP aliases) that were not present in the previous kernel.

It may be necessary to check the timestamp of the current kernel file. To do this, enter the uname command. This allows you to verify that the system is currently running with the recompiled kernel. The time and date stamp of the kernel must match the time it was recompiled.

If the response from the uname command indicates that the system is not loaded with a new kernel, look into the LILO boot loader. Check that the boot kernel name is correct in the /etc/lilo.conf file.

SEE MORE:

Compiling and installing the Linux kernel from source in Debian

Material from Bryansk Linux Users Group and www.rm.pp.ru

Each distribution has its own specifics for assembling the kernel, and this article is focused specifically on how to do this in Debian Etch. It also reveals the question of how to apply a particular patch to the kernel when support for certain functionality or new equipment in your system is needed. The article is intended primarily for more trained users and there are no guarantees that this method will work as it should and all described actions and responsibility fall on you.

  1. Note
  2. Method one. Building the kernel into .deb packages
  3. Applying patches
  4. Kernel configuration
  5. Compiling the kernel
  6. Installing a new kernel
  7. Method two. "traditional" way
  8. Problems
  9. Links

Note

Two methods of assembling the kernel will be described. The first option will be described for assembling .deb packages that can be installed on your or another system.

The second method is the so-called "traditional" way.

Method one. Building the kernel into .deb packages

Installing the necessary packages to compile the kernel

First, let's update the package lists:

# apt-get update

Let's install the packages we need:

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

Download the kernel sources

Go to the /usr/src directory, go to www.kernel.org and select the desired kernel version. In this case, the version linux-2.6.23.1.tar.bz2 will be considered. Download:

# cd /usr/src # wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.23.1.tar.bz2

Let's unpack the sources and create a symbolic link:

# tar xjf linux-2.6.23.1.tar.bz2 # rm linux (remove the previous symlink) # ln -s linux-2.6.23.1 linux # cd /usr/src/linux

Applying patches

Don't do this optionally or unnecessarily!

Sometimes drivers or tools are required that are not supported in the existing kernel, for example virtualization technology or other other specifics that are not in the current release. In any case, this can be corrected by applying so-called patches (if any).

So, let's assume you downloaded the required patch (for example, let's call it patch.bz2) to /usr/src. Let's apply the downloaded patch to our sources (You must still be in the /usr/src/linux directory):

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

The first command is only a test and no changes will be applied to the sources. If no errors were thrown after the first command, you can run the second command to apply the patch. Under no circumstances should you execute the second command if errors were generated after the first!

This way you can apply patches to the kernel sources. For example, there are some features that are only available in the 2.6.23.8 kernel, and the source code did not contain the necessary functionality, but patch-2.6.23.8.bz2 was released. You can apply this patch to 2.6.23 kernel sources, but not 2.6.23.1 or 2.6.23.3, etc. You can read more about this at:

Prefixes (prepatches) are equivalent to alpha releases; patches must be applied to the sources of the complete previous release with a 3-digit version (for example, patch 2.6.12-rc4 can be applied to the sources of version 2.6.11, but not to version 2.6.11.10.)

This means, if we want to build kernel 2.6.23.8, we need to download the sources of version 2.6.23 (http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.23.tar.gz) applied in the second method "traditonal" way!

Apply patch-2.6.23.8.bz2 to kernel 2.6.23:

# cd /usr/src # wget http://www.kernel.org/pub/linux/kernel/v2.6/patch-2.6.22.8.bz2 # cd /usr/src/linux # bzip2 -dc /usr/ src/patch-2.6.23.8.bz2 | patch -p1 --dry-run # bzip2 -dc /usr/src/patch-2.6.23.8.bz2 | patch -p1

Kernel configuration

It's a good idea to use the existing configuration file of the running kernel for the new one as well. Therefore, we copy the existing configuration to /usr/src/linux:

# make clean && make mrproper # cp /boot/config-`uname -r` ./.config

# make menuconfig

after which the graphical kernel configuration menu will load. Select the “Load an Alternate Configuration File” item in the configurator menu and click “Ok”. Then (if required) make the necessary changes to the kernel configuration by navigating through the menu (kernel configuration details can be found at www.google.com). When you finish and click “Exit”, the question “Do you wish to save your new kernel configuration?” will be asked, answer in the affirmative “Yes”.

Building the Linux kernel

Compiling the kernel

Building the kernel is done in just two commands:

# make-kpkg clean # fakeroot make-kpkg —initrd —append-to-version=-cybermind kernel_image kernel_headers

After --append-to-version=, you can write any name you like, but it must start with a minus sign (-) and have no spaces.

The process of compiling and building .deb packages can take quite a long time. Everything will depend on the kernel configuration and the capabilities of your processor.

Solving the problem with creating initrd.img

Recently, a bug has appeared in Debian in which, after installing packages with kernels assembled using the method described here, the corresponding /boot/initrd.img file is not created. To fix it for an already installed kernel, you will have to create initrd.img manually:

update-initramfs -c -k<полная-версия-ядра>

To solve the problem “for the future,” comment out, as shown, the second of the lines quoted below in the file /etc/kernel/postinst.d/initramfs-tools:

# kernel-package passes an extra arg; hack to not run under kernel-package #[ -z "$2" ] || exit 0

Installing a new kernel

When the kernel build is completed successfully, two .deb packages will be created in the /usr/src directory:

# cd /usr/src # ls -l

linux-image-2.6.23.1-cybermind_2.6.23.1-cybermind-10.00.Custom_i386.deb - the actual kernel itself and linux-headers-2.6.23.1-cybermind_2.6.23.1-cybermind-10.00.Custom_i386.deb - headers kernels necessary for assembling other modules (for example, when assembling nVidia driver modules). Install them:

# dpkg -i linux-image-2.6.23.1-cybermind_2.6.23.1-cybermind-10.00.Custom_i386.deb # dpkg -i linux-headers-2.6.23.1-cybermind_2.6.23.1-cybermind-10.00.Custom_i386.deb

(These packages can now be installed on another system and there will be no need to build them again.)

That's it, the installation is complete, the bootloader menu, installation of a new RAM disk and kernel will be done automatically. All that remains is to reboot:

Method two. "traditional" way

We carry out all the points described above BEFORE the “Kernel Compilation” point.

# make all # make modules_install # make install

As usual, the build may take a long time, depending on the kernel configuration and processor capabilities.

The main disadvantage of this method is that if you often update kernels, then after a while a large number of them will accumulate and you will want to remove the unused ones. To make this easier, you can assemble the kernel and other files that are installed into the system using the commands “make modules_install” and “make install” into a deb package (or rather, two starting from kernel 2.6.27) similar to the first method, but we’ll use Here are the scripts of the kernel itself:

# make all # make deb-pkg

Two .deb files will appear in the directory one level above the source directory. I assembled the kernel in the /usr/src/linux-2.6.27.10 directory and files appeared in the /usr/src/ directory

# linux-2.6.27.10_2.6.27.10-1_amd64.deb # linux-firmware-image_2.6.27.10-1_all.deb

The kernel is installed with the command

# dpkg -i linux-2.6.27.10_2.6.27.10-1_amd64.deb

Old kernels can be removed, for example, from synaptic

Next steps

The kernel has been compiled and installed, but now you need to create a RAM disk (without which the kernel simply will not boot) and you need to update the GRUB boot loader. To do this, do the following:

# depmod 2.6.23.1 # apt-get install yaird

Install the RAM disk:

# mkinitrd.yaird -o /boot/initrd.img-2.6.23.1 2.6.23.1

Let's update the bootloader easily and painlessly:

That's it, the bootloader and the new kernel are ready, all that remains is to reboot:

Problems

If, after rebooting, the new kernel you selected does not load, reboot and select your previous kernel and you can try to do the whole process again to build a working kernel. In this case, do not forget to delete the lines of the non-working kernel in /boot/grub/menu.lst.

Compiling the Linux kernel

Why compile the kernel yourself?
Perhaps the main question asked about kernel compilation is: “Why should I do this?”
Many consider this a pointless waste of time in order to show themselves as a smart and advanced Linux user. In fact, compiling the kernel is a very important matter. Let's say you bought a new laptop and your webcam doesn't work. Your actions? You look into the search engine and look for a solution to the problem on this issue. Quite often it may turn out that your webcam is running on a newer version of the kernel than yours. If you don't know what version you have, enter uname -r in the terminal, as a result you will get the kernel version (for example, linux-2.6.31-10). Kernel compilation is also widely used to increase performance: the fact is that by default, kernel distributions compile “for everyone,” which is why it includes a huge number of drivers that you may not need. So if you know the hardware you are using well, you can disable unnecessary drivers at the configuration stage.

It is also possible to enable support for more than 4 GB of RAM without changing the system bit depth. So, if you still need to have your own kernel, let's start compiling!

Obtaining the kernel source code.
The first thing you need to do is get the source code for the required kernel version. Usually you need to get the latest stable version. All official kernel versions are available at kernel.org. If you already have an X server installed (home computer), then you can go to the site in your favorite browser and download the desired version in the tar.gz archive (gzip compressed). If you are working in the console (for example, you have not yet installed the X server or are configuring the server), you can use a text browser (for example elinks). You can also use the standard download manager wget:
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.33.1.tar.gz
But keep in mind that you must know the exact version number you need.

Unpacking the source code archive.
Once you have received the source code archive, you need to extract the archive into a folder. This can be done from graphical file managers (dolphin, nautilus, etc.) or through mc. Or use the traditional tar command:
tar -zxvf path_to_archive
Now you have a folder with your source code, navigate to it using the command cd kernel_source_directory (to list the directories in the folder, use the ls command).

Kernel configuration.
Once you have navigated to the kernel source directory, you need to perform a “20 minute” kernel configuration. Its goal is to leave only the necessary drivers and functions. All commands must already be executed as a superuser.

make config - console mode of the configurator.

make menuconfig - console mode in the form of a list.

make xconfig - graphical mode.

After making the necessary changes, save the settings and exit the configurator.

Compilation.
It's time for the final stage of assembly - compilation. This is done with two commands:
make && make install
The first command will compile all files into machine code, and the second will install the new kernel on your system.
We wait from 20 minutes to several hours (depending on the power of the computer).

Configuring and Compiling the Linux Kernel

The kernel is installed. To make it appear in the grub(2) list, enter (as superuser)
update-grub
Now after reboot, press "Escape" and you will see the new kernel in the list. If the kernel does not turn on, then simply boot with the old kernel and configure it more carefully.

KernelCheck - compiles the kernel without going to the console.
KernelCheck allows you to build the kernel in fully graphical mode for Debian and distributions based on it. After launch, KernelCheck will offer the latest kernel versions and patches, and after your consent, download the source code and launch the graphical configurator. The program will compile the kernel into .deb packages and install them. All you have to do is reboot.

Articles archive:
May 2017
March 2017
April 2016
March 2016
October 2013
September 2013
May, 2013
March 2013
November 2012
July 2012
June 2012
April 2012
March 2012
February 2012
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010

Random:

Aurorae: window decoration engine for KDE

Sblog will be rewritten in .NET

Linuxnow.ru: Plans for the future

Bolgenos. Telecon's refutation

Fast GRUB 2 recovery

Free Software Foundation and Copyleft.

Colleagues: everyone
Linux for everyone

Our banners:


Set banner

Core(English) kernel) is what everything else is built around. This is what is called Linux. Nowadays, the word Linux in everyday life refers to an operating system built on it, although in a good way it is called GNU/Linux (the Linux kernel and software from the GNU project, which has been in development for many decades).

Ubuntu uses a heavily patched kernel, some of which add unstable and experimental features.

Each release of Ubuntu has its own version of the kernel. LTS releases starting from 10.04 have the opportunity to update the kernel to the versions included in newer releases.

Ubuntu versionKernel version
4.10 2.6.9
5.04 2.6.11
5.10 2.6.13
6.06 LTS 2.6.15
6.10 2.6.18
7.04 2.6.19
7.10 2.6.20
8.04 LTS 2.6.24
8.10 2.6.27
9.04 2.6.28
9.10 2.6.31
10.04 LTS 2.6.32
10.10 2.6.35
11.04 2.6.38
11.10 3.0.4
12.04 LTS 3.2
12.10 3.5
13.04 3.8
13.10 3.11
14.04 LTS 3.13
14.10 3.16
15.04 3.19

Forks

The numbering of kernel versions in Ubuntu and on the kernel.org website does not match, since Canonical developers add a microversion to indicate added patches. For example, version 3.2.0-23 would mean that the kernel is based on the 3.2 branch, which received 23 patches.

The following kernel types are supported in the Ubuntu repository:

The generic-pae kernel allows a 32-bit system to use up to 64GB of total RAM, allocating no more than 4GB for the needs of a particular process, while a simple generic kernel works with no more than 4GB of RAM.

The 64-bit kernel allows you to address up to 1TB of memory consumed by processes.

If you need to update the kernel to a newer major version (usually this is due to the fact that new versions add support for new hardware and eliminate regressions), you can use the officially supported archive http://kernel.ubuntu.com/~kernel-ppa/mainline/ .

Compiling the kernel

Building the kernel from source code requires some skills and knowledge of how the OS works.

Before you start building the kernel, you must install the following packages:

Build-essential fakeroot ncurses-dev libssl-dev

All further actions must be performed on behalf of the superuser:

sudo su

Getting the source code

The source code for the kernel used in Ubuntu can be obtained by installing the linux-source package:

apt-get install linux-source

After installation in the directory /usr/src an archive will appear called linux-source-verify_kernel.tar.bz2.

You can also download the archive with the kernel source code from the website kernel.org.

When downloading the kernel from kernel.org, you will have to apply patches to it

Configuration

Unpack the resulting archive and, for convenience, create a symbolic link to the resulting directory:

cd / usr/ src tar xjf ./ linux-source-3.2.0.tar.bz2 ln -s ./ linux-source-3.2.0 ./ linux cd ./ linux

To simplify the kernel configuration process, you can copy the settings of the current one.

Building the Linux kernel
Hello, dear readers. Today I will tell you about such an interesting activity How to build Linux kernel. Why might you need to assemble the kernel yourself? In fact, there can be many reasons: the need to use additional kernel capabilities, optimize the kernel for your computer, update the kernel to the latest version. In this article, I will demonstrate the process of obtaining source codes, configuring, compiling and installing the Linux kernel into the system, as part of solving the problem of including cryptoloop support (loopback encrypting devices) in the kernel.

Getting the source code
First of all we need to get the source code, this can be done in different ways and from different sources. I propose to consider only two: system repositories, the official kernel website. The repositories will most likely contain versions of the kernel older than those on the official site, but these sources should include patches and fixes from the manufacturer of your Linux distribution. This approach is preferable unless you need some new technology or feature that is only supported by newer kernels. You can view all versions of the kernel sources that are contained in the repositories of your system by entering in the terminal (true for Ubuntu Linux, the package name may differ in other distributions):

Apt-cache search linux-source

The command will list the available packages:


As you can see, I only have a package with the current version and a package with version 3.5 (in fact, the current kernel version is also 3.5). To expand the list of kernels available in this way, it is worth connecting additional repositories. We can get the kernel with the command: sudo apt-get install linux-source

linux-source - the name of the source package, it may be different in your case.
After the command completes, a file will appear in the /usr/src directory, in my case - linux-source-3.5.0.tar.bz2. Let's go to the folder, unpack the archive and, for convenience, create a symbolic link:

Cd /usr/src sudo tar -xjvf linux-source-3.5.0.tar.bz2 sudo ln -s linux-source-3.5.0 linux

If you need the latest version of the kernel, you can always download it from the website kernel.org. It is worth noting that the site contains both stable versions of kernels and versions intended for testing and modification (usually their name contains the abbreviation “RC” - Release candidate). If you do not want unnecessary problems with the system, I advise you to download the stable version:

Let's save the archive with the sources in the /usr/src folder. To unpack the resulting archive you may need to install additional utilities:

Sudo apt-get install xz-utils

Now, as in the case of downloading the kernel from the repositories, we must unpack the source archive and create a link:

Cd /usr/src sudo tar -xpJf linux-3.8.5.tar.xz sudo ln -s linux-3.8.5.tar.xz linux

Configuration and compilation.
Now we come to the most interesting part. Before we get started, let's install a few additional packages:

sudo apt-get install build-essential kernel-package libncurses-dev

We will create a new configuration based on the kernel currently used by the system:

Cd /usr/src/linux sudo make oldconfig

If you are configuring a newer version of the kernel than is present in the system, then it is likely that it contains parameters that are not in the configuration of the current kernel. In this case, the program will ask you to make a choice; you can leave the default values ​​by simply pressing the Enter key. In any case, the configuration is not yet complete. Now we can make the settings we need through the configuration creation menu:

Sudo make menuconfig

The configuration program will start in the terminal:

Here, the configuration options are divided into sections to make them easier to navigate. As I said above, I need to include cryptoloop support in the kernel. To do this, go to the “Device Drivers” section, and from there to the “Block Devices” subsection:

We find the “Cryptoloop Support” parameter, next to it there is the letter “M” which means that support for encryption devices will be added as a kernel module that can be enabled with the modprobe command. We need to include support for this technology directly in the kernel so that it is always supported. We move the focus to the “Cryptoloop Support” parameter and press the space bar. The letter “M” should be replaced by the symbol “*”, this means that support for this technology will be “hardwired” into the kernel. Be careful, a space means that the technology will not be supported at all.

Press the “Tab” key and click on the “Exit” button until you are prompted to save the changes:

We answer “Yes”. We have successfully completed the configuration!
Let's start compiling. First, we delete the files remaining from previous assemblies; if you are starting the assembly for the first time, it is not necessary to run this command: sudo make-kpkg clean

Let's start the compilation:

Sudo make-kpkg -j4 --initrd --append-to-version=-mykernel kernel_image kernel_headers

J4 - flag indicates how many threads to use for compilation. This will greatly speed up compilation on multi-core processors. The number 4 here indicates 4 threads. Install as many threads as the number of cores of your processor that the system “sees”.
-mykernel - a prefix indicating that the kernel was compiled manually, you can change it, it actually does not affect anything.

Now the compilation process has begun. It can take from 10 minutes to several hours depending on how powerful your computer is:

Installing the kernel on the system

After compilation is complete, two files with the “deb” extension should appear in the /usr/src directory. They are installation packages of our new kernel and can be installed using the dpkg utility:

sudo dpkg -i linux-image-3.8.5-mykernel_3.8.5-mykernel-10.00.Custom_i386.deb
sudo dpkg -i linux-headers-3.8.5-mykernel_3.8.5-mykernel-10.00.Custom_i386.deb

Congratulations! The kernel is installed, now the system will boot with this kernel by default, but if you have problems with the new kernel, you can always boot with the old one by selecting it on the boot screen - Grub. With this I conclude today’s article and wish you success, dear readers!

I have long wanted to write an article on how to compile the Linux kernel. And now this moment has come.

Actually, why build a kernel if the distribution provides a completely usable kernel?

For example, in order to use the latest drivers and solutions presented in the new kernel, when only the old branch is available from the distribution. Also, for example, to customize it for your hardware and speed up the work a little. I assembled 3.11 for myself because it contains a set of patches that improve work with ATI video cards, and I am the owner of one of them.

A lot of articles have been written about assembling the kernel, so I won’t go into details, but will just write how I assemble the kernel for myself.

First of all, you need to obtain an archive with the source codes of the desired version of the Linux kernel.

You can download the desired version on the website https://www.kernel.org/. I will describe the assembly and installation process using version 3.11.0 as an example.

It is advisable to compile the kernel in a directory specially created for this purpose under an unprivileged user. I am compiling in a folder ~src/linux/linux-version

Before compiling, you should make sure that all the dependencies necessary for the build are installed and there is about 3GB of free space in the compilation directory.

Here is a list of packages for successful compilation (for Debian/Ubuntu):

gcc, make- necessary assembly and linking tools. Preferably gcc one of the latest available versions.

libncurses5-dev- needed for menuconfig to work

ccache- allows you to speed up reassembly

If you want to use the graphical kernel configurator, then you should also install packages for QT development, for example libqt4-dev, g++, pkg-config.

Lzop, lz4c- if you are interested in alternative kernel compression mechanisms and initramfs.

It is assumed that the current directory is the directory of the unpacked kernel.

After the build environment is prepared, you need to generate the kernel configuration. The current configuration can be viewed like this:

Cat /boot/config-`uname -r`

Zcat /proc/config.gz

In principle, you can use the current config by editing it in one of the configuration programs. Copy it to the directory where the archive with the kernel is unpacked and rename it as .config

Cp /boot/config-`uname -r` .config

I like xconfig, I find it the most convenient.

Make xconfig

Automatically loads the previously copied .config, which serves as our basis for configuration. There are a lot of tips for configuring the kernel, I just recommend choosing your processor version, disabling hardware drivers that are not there, you can also select additional modules, such as zram and a compression algorithm, I chose lz4 as the fastest.

After saving the configuration, you can start compiling.

If you are too lazy to configure the kernel manually, it is possible to perform automatic configuration using information about loaded modules: make localmodconfig

Now the second main stage is compiling the kernel and modules. Executed in one command:

Make -j4 CC="ccache gcc" bzImage modules

Where -j4 corresponds to the number of processor cores in your configuration.

Compilation will not take long if the hardware is powerful enough and the distribution kernel config is not used. On my laptop with an AMD Phenom P820 processor and six gigabytes of RAM, compilation takes about half an hour.

The last step is installing the kernel and modules.

Sudo sed -i.bak "s/MODULES=most/MODULES=dep/" /etc/initramfs-tools/initramfs.conf

This is necessary in order to reduce the size of the initrd file by including only the modules necessary for loading.

Installation can also be done with one command:

Sudo make modules_install install

Or install everything manually. Modules first

Sudo make modules_install

Then the core

Version=`awk "NR<=3 {printf "%s.",$NF}" < Makefile | sed "s/\.$//"`

this is how we find out the kernel version from the Makefile

Sudo cp arch/`uname -m`/boot/bzImage /boot/vmlinuz-$version sudo cp .config /boot/config-$version sudo cp System.map /boot/System.map-$version sudo update-initramfs - c -k $version sudo update-grub

Finally, I am attaching a script to automate the process.

During the configuration of the config, he can ask a couple of questions; to answer by default, you just need to press Enter.

Happy compilation.