Installing the linux kernel. Configuring and compiling the Linux kernel. Unpacking the kernel source

Good afternoon, readers and guests. In continuation of the last post about, today I want to tell you, how to build and configure your own kernel.

From the previous post, we already know that Linux kernel has minimal features and hardware support, but if necessary, we can extend the capabilities of the kernel with kernel modules. What might be needed building or rebuilding your own kernel? For example, to enable / disable any new features, or old kernel update to a newer one with support for new features, or just for experiments, as in our example.

Obtaining Kernel Sources and Preparing for Configuration

Can be done in several ways get sources:

    1. Get the original code archives from The Linux Kernel Archives , as gzip or bzip2 archives, with the command:
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.36.1.tar.bz2 cp linux-2.6.36.1.tar.bz2 /usr/src/
  1. Get sources from distribution repositories (for example: , CentOS i386) as a binary deb or rpm package (src.rpm) with patches from the distribution manufacturer. For example: kernel-server:/tmp/123# apt-cache search source | grep linux ketchup - update utility for linux-kernel sources linux-patch-grsecurity2 - grsecurity kernel patch - new major upstream version linux-wlan-ng-source - linux-wlan-ng driver linuxdoc-tools - convert LinuxDoc SGML source into other formats linux-patch-lustre - Linux kernel patch for the Luster Filesystem libcorelinux-dev - Foundation Classes, Design Patterns, IPC and Threads libcorelinux-doc - Foundation Classes, Design Patterns, IPC and Threads libcorelinux-examples - Foundation Classes, Design Patterns, IPC and Threads libcorelinuxc2a - Foundation Classes, Design Patterns, IPC and Threads linux-patch-openswan - IPSEC Linux kernel support for Openswan selinux-policy-src - Source of the SELinux reference policy for customization user-mode-linux-doc - User-mode Linux (Documentation) linux-patch-xenomai - Linux kernel patches for Xenomai linux-patch-debian-2.6.26 - Debian patches to version 2.6.26 of the Linux kernel linux-source-2.6.26 - Linux kernel source for version 2.6 .26 with Debian patches linux-tree-2.6.26 - Linux kernel source tree for building Debian kernel images Print-server:~# apt-get install linux-source-2.6.26 Reading package lists... Done Building dependency tree Reading status information... Done The following additional packages will be installed: binutils bzip2 cpp cpp-4.3 gcc gcc-4.3 libc6-dev libgmp3c2 libgomp1 libmpfr1ldbl linux-libc-dev make Suggested packages: binutils-doc bzip2-doc cpp-doc gcc-4.3 -locales gcc-multilib manpages-dev autoconf automake1.9 libtool flex bison gdb gcc-doc gcc-4.3-multilib libmudflap0-4.3-dev gcc-4.3-doc libgcc1-dbg libgomp1-dbg libmudflap0-dbg glibc-doc libncurses-dev ncurses -dev kernel-package libqt3-mt-dev make-doc NEW packages to be installed: binutils bzip2 cpp cpp-4.3 gcc gcc-4.3 libc6-dev libgmp3c2 libgomp1 libmpfr1ldbl linux-libc-dev linux-source-2.6.26 make updated 0, 13 new packages installed, 0 packages marked for removal, and 5 packages not updated. You need to download 50.2MB/63.2MB archives. After this operation, the amount of occupied disk space will increase by 89.4MB. Would you like to continue [Y/N]? y Fetched:1 http://ftp.debian.org lenny/main linux-libc-dev 2.6.26-26 Fetched:2 http://ftp.debian.org lenny/main linux-source-2. 6.26 2.6.26-26 Got 50.2MB in 1min49s (460kB/s) Selection of previously unselected binutils package. (Reading database... currently installed 16621 files and directories.) Unpacking binutils package (from file.../binutils_2.18.1~cvs20080103-7_i386.deb)... Selecting previously unselected bzip2 package. ..... The make package is unpacked (from file.../archives/make_3.81-5_i386.deb)... Man-db triggers are processed ... The binutils package (2.18.1~cvs20080103-7) is configured. .. ..... Setting up linux-source-2.6.26 (2.6.26-26) ... Setting up make (3.81-5) ... kernel-server:~# ls /usr/src/ linux -source-2.6.26.tar.bz2

I would like to point out that source archive name or package, looks something like this: linux-source-2.6.26 or kernel-source-2.6.18. From the listing of the second example, it can be seen that when installing sources from the repository, apt offers us to install other related packages, without which the kernel cannot be built.

So. After getting sources one of the above ways (copying the archive (in the first case) and installing the package (in the second case)), we get in the directory /usr/src/- archive with the same name linux-source-2.6.26.tar.bz2. Next, we need to unpack the melon archive with the command: tar -xjf linux .tar.bz2. As a result, we get a tree of kernel source directories located in the /usr/src/linux-2.6.36.1/ directory. It is also recommended to create a symbolic link /usr/src/linux to the above directory:

Kernel-server:~# cd /usr/src/ kernel-server:/usr/src# ln -s linux-2.6.36.1 linux kernel-server:/usr/src# ls -l lin* lrwxrwxrwx 1 root src Dec 14 3 16:31 linux -> linux-2.6.36.1 -rw-r--r-- 1 root src 70236745 Dec 3 15:57 linux-2.6.36.1.tar.bz2

Before trying to build and configure, you need to review the file Changes located in the Documentation directory. It contains, among other things, list of packages needed to build the kernel, with version numbers. Make sure that these packages are installed (if you installed the sources using a package manager, then most likely the necessary packages are already installed).

The unpacked subdirectory tree contains the file Makefile. makefile contains various build targets for configuring kernel options, building the kernel and its modules, installing modules, and building RPM or deb packages. Most recent versions of the kernel sources allow you to use make help to get brief help for each target. On older systems, it was necessary to consult the documentation or view the makefile. Below is part of the output of make help:

Kernel-server:/usr/src/linux-2.6.36.1# make help Cleaning targets: clean - Remove most generated files but keep the config and enough build support to build external modules mrproper - Remove all generated files + config + various backup files distclean - mrproper + remove editor backup and patch files Configuration targets: config - Update current config utilising a line-oriented program nconfig - Update current config utilising a ncurses menu based program menuconfig - Update current config utilising a menu based program xconfig - Update current config utilising a QT based front-end gconfig - Update current config utilising a GTK based front-end oldconfig - Update current config utilising a provided .config as base .......

Further, if for some reason it is necessary to apply a patch to the kernel, then it should be downloaded in advance and put in the directory /usr/src. If you do not need to patch the kernel, you can safely proceed to configuration. The core is patched as follows:

Kernel-server: /usr/src/linux-2.6.36# gzip -cd ../patch-2.6.xx.gz | patch -p1 #or Print-server:/usr/src/linux-2.6.36# bzip2 -dc ../patch-2.6.xx.bz2 | patch-p1

Configuration

The current kernel configuration is stored in a file .config. This file is formed using one of the configuration targets (the target is, in simple terms, a command executed in the form make target):

The config target uses a command line interface to answer many questions about creating or updating a .config file. In my opinion, in comparison with the purposes using the menu - very inconvenient piece.

cloneconfig

Copy current kernel settings to .config file. This target has been deprecated and has been replaced in newer kernels by . (handy for adding new features to the current kernel)

The menuconfig target uses a menu-driven program built on top of ncurses to create or update a .config file. You only have to answer the questions for the items you want to change. This approach has replaced the old config. Runs in a terminal window remotely or locally.

Target nconfig uses a menu-driven program built on top of ncurses to create or update a .config file. This version is based on but has a more modern look. Added after the release of the Linux kernel 2.6.35. You only have to answer the questions for the items you want to change. Runs in a terminal window remotely or locally.

The xconfig target uses a graphical menu system based on the QT front-end used by the KDE desktop.

The gconfig target uses a graphical menu system based on the GTK front-end used by GNOME desktop.

oldconfig

The oldconfig target allows you to create a configuration using an existing .config file that you created earlier or from another system. For example, you can copy the configuration file for your system from /lib/modules/$(uname -r)/build/.config to /usr/src/linux. Once you have done this, you can use one of the configuration menu targets to make changes if necessary. Also, when executing this command, if a lot of new features have been added to the new kernel, many questions will be asked about setting up new parameters. (handy for adding new features to the current kernel)

Again, a list of all command targets can be seen by typing make help. So, let's start configuring. The most convenient for the console, IMHO - .

After running the command, I got a terrible message:

Kernel-server:/usr/src/linux-2.6.36.1# make menuconfig *** Unable to find the ncurses libraries or the *** required header files. *** "make menuconfig" requires the ncurses libraries. *** *** Install ncurses (ncurses-devel) and try again. *** make: *** Error 1 make: *** Error 2

Something is missing, thought Stirlitz (C) ... Probably a package containing the name ncurses and since these are libraries, then most likely the package starts with lib. I got into the repository:

Kernel-server:/usr/src/linux-2.6.36.1# apt-cache search ncurses | grep ^lib libcunit1-ncurses-dev - Unit Testing Library for C (ncurses) -- development files libcunit1-ncurses - Unit Testing Library for C (ncurses) libncurses-gst - Ncurses bindings for GNU Smalltalk libkaya-ncurses-dev - Ncurses binding for kaya libkaya-ncursesw-dev - Ncurses binding for kaya libcurses-perl - Curses interface for Perl libcurses-widgets-perl - Curses widget interface for Perl libruby-extras - a bundle of additional libraries for Ruby libruby1.8-extras - a bundle of additional libraries for Ruby 1.8 libtexttools-dev - Ada and C++ library for writing console applications libtexttools2.0.5 - Ada and C++ library for writing console applications libncurses-ruby1.8 - ruby ​​Extension for the ncurses C library libncurses-ruby1.9 - ruby Extension for the ncurses C library libncurses-ruby - ruby ​​Extension for the ncurses C library lib64ncurses5-dev - developer's libraries for ncurses (64-bit) lib64ncurses5 - shared libraries for terminal handling (64-bit) libncurses5-dbg - debugging/ profiling libraries for ncurses libncurses5-dev - developer"s libraries and docs for ncurses libncursesw5-dbg - debugging/profiling libraries for ncurses libncursesw5-dev - developer"s libraries for ncursesw libcurses-ocaml-dev - OCaml bindings for the ncurses library libcurses- ocaml - OCaml bindings for the ncurses library libggi-target-terminfo - General Graphics Interface TermInfo display target libncurses5 - shared libraries for terminal control libncursesw5 - libraries for terminal control (support for double-byte characters)

This line: libncurses5-dev - developer's libraries and docs for ncurses seemed necessary to me. Let's install:

Kernel-server:/usr/src/linux-2.6.36.1# apt-get install libncurses5-dev Reading package lists... Done Building dependency tree Reading status information... Done NEW packages to be installed: libncurses5-dev 0 updated, 1 new packages installed, 0 packages marked for removal, and 5 packages not updated. You need to download 1546kB of archives. After this operation, the amount of occupied disk space will increase by 6599kB. Received:1 http://ftp.debian.org lenny/main libncurses5-dev 5.7+20081213-1 Received 1546kB in 4s (344kB/s) Selection of previously unselected libncurses5-dev package. (Reading database... 18098 files and directories currently installed.) Unpacking libncurses5-dev package (from file.../libncurses5-dev_5.7+20081213-1_i386.deb)... Processing man-db triggers ... Package libncurses5-dev (5.7+20081213-1) is being configured ...

Done, let's try :

Hooray! We see the coveted kernel configuration menu. The following shows the various options that allow you to include components in the kernel or create modules. When an option is highlighted, you can use the spacebar to navigate through the options for that component. To activate the option, press y to disable -- n to create, if possible, a module, click m. Exit the menu: Esc Esc.

  • [*] The component will be included in the kernel.
  • The component will not be included in the kernel.
  • [M] The component will be styled as a module.
  • < >The component will not be included in the core, but may be packaged as a module.

Below I will give a description of the main sections of configuring the change (it will be replenished over time):

Chapter paremeter Description
General setup This section allows you to add an identification string to your kernel, as well as a number of attributes that are not related to any sections, but still need to be described.
Enable loadable module support This section contains options to determine whether your kernel will support modules and whether they will be loaded and unloaded automatically. The "Enable loadable module support" option should be enabled.
Processor type and features This section contains configuration options specific to this processor type. Here you can select the processor and processor family that will be supported by your kernel. You can enable or disable kernel support for various features provided by this processor. Make sure you enable symmetric multi-processing support if your system has more than one processor or if the processor supports hyperthreading. In addition, to get better performance of the graphics subsystem in systems with AGP or PCI video cards, support for MTRR should be enabled.
Power management options This section contains options related to power management. They are especially important for laptops. In addition to monitoring the power status, you can find there tools to control and monitor parameters such as temperature or the status of the cooling fan.
Bus options (PCI etc.) This section contains options for the computer buses supported by your system, such as PCI, PCI Express, and PC Card. Here you can enable support for the /proc/pci file system, which can be used in conjunction with the commonly used lspci command.
Executable file formats / Emulations This section contains options regarding support for various binary file formats. Support for "ELF binary" should be enabled. In addition, you can enable support for DOS binaries to run them under DOSEMU, as well as other binaries supported by appropriate wrappers, such as Java ™, Python, Emacs-Lisp, etc. Finally, for 64-bit systems that support 32-bit emulation, you may want to enable support for 32-bit applications.
Networking The section regarding network settings is quite large. Here you can enable basic socket support, TCP/IP networking, network packet filtering, routing, and bridging, as well as support for various protocols such as IPV6, IPX, Appletalk, and X.25. In addition, you can enable support for wireless, infrared, and amateur radio.
device drivers This section is also very large. Here you can enable support for a large number of hardware devices, including IDE/ATAPI or SCSI drives, or flash drives. Enable DMA for your IDE devices; otherwise they will work in slower PIO mode. If you want to have support for multiple devices such as RAID or LVM, the corresponding options must also be enabled. Here you can also enable parallel port support to work with the printer through this interface. This is where you configure the wide range of supported network devices for the various network protocols we configured earlier. In addition, here you will find options for supporting audio and video capture devices, USB and IEEE 1384 (Firewire) devices, as well as various kinds of hardware monitoring devices. In the Character Devices section, you may want to enable parallel port printing support and direct rendering support.
Firmware drivers This section contains several options related to installing and updating the BIOS, such as using the Dell System Management features on some Dell systems.
File systems This section is for configuring the filesystems you want to support in your kernel, compiled as modules or not. You can also find here file systems for removable disk devices (floppies, CDs and DVDs) as well as network file systems such as NFS, SMB or CIFS. Support for various types of sections and national encodings Native Language Support is also located in this section.
Kernel hacking This section allows you to enable kernel debug mode and select which additional features will be enabled.
security options This section is for configuring security options and enabling and configuring SELinux (Security Enhanced Linux).
Cryptographic options In this section, you can configure support for various encryption algorithms such as MD4, DES, and SHA256.
library routines Here you can specify a number of checksum calculation (CRC) algorithms to be included in the kernel or compiled as modules.

I have given a very brief description of the kernel configuration sections. Specifically, on the choice of settings, I will probably make a separate post, because the current one has grown to a huge size. The best description of make menuconfig settings I found here Gentoo Handbook. From myself, I’ll say that it would be logical to execute make oldconfig (thereby copying the current.config of the installed OS), and then run make menuconfig and disable all unnecessary functions (let’s say I disabled support for WiFi, jfs file systems that I don’t need, etc.). And in general, I am a supporter of the opinion that it is necessary to engage in kernel optimization in very extreme cases, when the performance of the OS rests on the capabilities of the hardware. Accordingly, by reducing the size of the kernel (disabling unnecessary modules, including them in the kernel + disabling unnecessary features), you can add 1-2% to performance. And on modern servers, I think this (kernel reconfiguration) is not particularly relevant.

Building the Kernel

Now that we've configured the kernel, we're ready to build it. If you don't know what the state of the build tree is, run make clean before you start configuring a new kernel. For a more thorough cleanup, run make mrproper (this will remove the .config file as well as some other files used in the build process, make mrproper ).

As part of the configuration trial, it is advisable to give the new kernel a special name that will allow you to easily identify it. To do this, you must set the Local version value and activate the Automatically append version information to the version string option in the corresponding line of the General setup section.

In principle, no root permissions are required to build the kernel, even though installing a new kernel these powers are required.

To start building the 2.6 kernel, you need to run make .

To start building the 2.4 kernel, you need to run 3 commands:
make dep
make bzImage
make modules
The first one creates the required dependency files. The second collects the core. And the last collects modules.

Installing a new kernel

After the kernel is built, it must be installed. Before installing, you need to run make modules_install to install the kernel modules into a new /lib/modules subdirectory. After installing the modules, you need to run make install to install the new kernel and the initial RAM disk (initial RAM disk) in the /boot directory and update the bootloader configuration.

I would like to draw your attention to the fact that the necessary initial RAM disk (initial RAM disk or initrd) is automatically created during the build process. If you need to create it manually, you can do so with the mkinitrd command.

After executing make install, the bootloader configuration file should be updated. But for some reason it was updated for me after the update-grub command.

On this I will finish. It's a very big article. In the near future I will try to squeeze it.

Sometimes you may need to assemble your own Linux kernel. The reasons for this may be as follows:

  • you need a clean kernel, no distribution patches;
  • you want to apply your own patches (of which there are a lot);
  • you want to assemble the kernel for your hardware configuration, throw out the excess from it and / or sharpen it for certain tasks;
  • you want to include an experimental driver or file system in the kernel that is not in the kernel "vanilla" core (eg. ZFS or Raiser 4);
There is nothing difficult in assembling the kernel. It is only important to understand why this is being done, and also not to touch those parameters that you do not understand. In this note, I will describe two examples of building a kernel in Debian based distributions. In the first example, I will show how easy it is to build a clean, what is called a "vanilla" kernel (such as it is released by Linus Torvalds), and in the second - how to apply your own patches and optimize the kernel. I will immediately write two warnings:
  • you will need to rebuild the kernel every time you update it (download an "update patch", apply it and build the kernel);
  • the rebuilt kernel may not work if some hacks are used in your system to ensure the operability of one or another equipment;
  • if the kernel is configured incorrectly, especially in the case of illiterate or thoughtless patching, you can either get a system that slows down to horror, or lose it altogether.
ALL ACTIONS YOU PERFORM AT YOUR OWN RISK!

Easy kernel build without patching.

The source code for the Linux kernel is located at kernel.org. There are also "update patches". What do we need? Downloading from the site tarball(archive) with the latest stable version of the kernel (at the time of writing, this is the version 4.3 ). We download in any convenient way. Next, we need tools for assembly:

sudo apt install build-essential gcc kernel-package patch
sudo apt-get build-dep linux

After all the necessary tools are installed, unpack the archive with the kernel code into any convenient directory. Let it be /home/user/KERNEL, Where "user"- system user name. Next, open a terminal and go there:

cd /home/user/KERNEL

It remains to build the kernel:

fakeroot make-kpkg -j 3 --initrd --append-to-version=-custom kernel_image kernel_headers #-j 3

Number 3 after j- this is the number of cores of your processor + 1. That is, for a dual-core it is 3, for a 4-core it is 5, and so on.
-custom- here you can specify a convenient name for the kernel to make it easier to distinguish it from the distribution one.
kernel_image And kernel_headers are the kernel itself and its header files, respectively. headers necessary for building drivers and kernel modules, as well as for some other purposes. After executing this command, several kernel configuration questions will start to appear. Since we're leaving everything at default, just press Enter until the build starts. Depending on the power of your computer, assembly may take from 15-20 minutes to several hours. After assembly, in the directory /home/user there will be two deb package: core and headers. Install them with the command:

sudo dpkg -i linux-image-4.3*deb linux-headers-4.3*deb
sudo update-grub

And reboot. In the GRUB menu, it will now be possible to select a different kernel to boot the system.

Building the kernel using patches and additional configuration.

This time we will compile an optimized core for working with sound and video, as well as for greater system responsiveness. To do this, we will apply two patches: the so-called real-time patch ( PREEMPT RT) and a compiler patch GCC to add additional options for CPU optimizations. First off, what is a patch? A patch is a text file that is created by a program diff, containing code changes in certain parts, which, when applying the patch, are entered in the right places. Because RT patch comes out with a big delay, its latest version is for kernel 4.1. However, this is not so important. In the same way, we download the kernel 4.1 from kernel.org and unpack it into the directory /home/user/KERNEL-CUSTOM. Now download patches. PREEMPT_RT and GCC Patch . From the downloaded archives, we need files with the .patch extension, which must be placed in the kernel source directory. That is, in /home/user/KERNEL-CUSTOM. Before applying patches, you need to make sure that there will be no errors. Open terminal:

cd /home/user/KERNEL-CUSTOM
patch -p1 -i patch-4.1.13-rt15.patch --dry-run


Option --dry-run allows you to simulate the application of the patch, without making changes to the files. If no errors were found (see screenshot) - apply the patch without the option --dry-run. Do the same with the second patch. Do not apply more than one patch at the same time! Now we need to configure our kernel. We have the following options to choose from:

makeconfig- questions about the configuration of one or another kernel subsystem will be displayed in the terminal one by one. Extremely long and tedious process. Let's forget about it :)
make oldconfig- the configuration of the currently running kernel will be used. Since we are building our own from scratch, this method is also useless.
make defconfig- similar to the previous one, only the values ​​​​will be by default. The way it was set by the kernel developers. An analogue of the first assembly method.
make menuconfig- pseudo-graphical interface based on the library Ncurses. An interface with a convenient hierarchical menu will be displayed on the screen. Controls with direction keys, space bar and TAB key. Recommended if you are compiling the kernel on a system that does not have a graphical shell.
make gconfig GTK, recommended in GNOME, Mate, Xfce, Cinnamon, Unity, and other environments that use GTK.
make xconfig- GUI based Qt. Recommended in KDE. Since my system uses KDE, I will use this method. In addition, there are a couple of other methods, but their application is no different. So, after applying the patches, run make xconfig and this is what we have before us:


Turn off first dynticks. For this we go to Timer subsystem and choose Periodic timer ticks


Now the most delicious. Let's go to Processors type and features, looking for an item Processor family and specify your processor series. For example, if you have Intel Core i5-4xxx, specify Hasswell(4th generation of processor). If you are not sure exactly, you can select the item Native optimizations autodetected by GCC. In this case, when building, the compiler will determine what your processor supports, and include all its features.


Go below and turn on the option Full preemptible kernel (RT). Hard real time mode.


Scroll below and in paragraph Timer frequency set the system interrupt frequency to 1000 Hz


Completely turn off any power saving. It is important! On the left we are looking for an item Power management and ACPI options and uncheck ACPI. Also turn off CPU power saving.

That's all. If you wish (and carefully study the documentation), you can make additional changes to the configuration (disable unnecessary drivers, enable additional subsystems, and so on). Now save the config via File-Save, close the configurator and build the kernel:

fakeroot make-kpkg -j 3 --initrd --append-to-version=-rt-custom kernel_image kernel_headers #-j 3
sudo update-grub

On my computer with an Intel Core i3-550 (3.2 GHz) processor, the performance gain was quite noticeable. But most importantly, when working in LMMS And Kdenlive, periodic stuttering, desynchronization of audio and video tracks, as well as freezes under heavy load on the hard drive have disappeared. Conclusion - it works! Finally, I will describe two modified kernels that are very popular in Linux circles:

PF-kernel- the most popular set of patches from Ukrainian Alexander Natalenko (aka post-factum) . This is a set of patches that are not included in the main kernel, but provide increased system responsiveness, provide an alternative hibernation subsystem that is faster than the main one, and also reduce memory usage using the same page merging technique. The set includes:

  • BFS process scheduler by Con Kolivas with additional fixes by Alfred Chen;
  • BFQ I/O Scheduler by Paolo Valente, Arianna Avanzini and Mauro Marinoni;
  • the TuxOnIce hibernation subsystem by Nigel Cunningham;
  • implementation of the same page merging technique in UKSM by Nai Xia;
  • a patch from Graysky expanding the list of processors for kernel optimization by the compiler (the one we applied above)
Modified kernel repository. Official site .

zen-kernel- the second most popular, but the first set in terms of the number of patches. Zen Kernel uses a combination of multiple projects, updates code via a git repository, and has a few Zen-specific things that aim to meet most user needs in one core. Some patch features: drm-next, wireless-testing, CPU scheduler selection (CFS/BFS), BFQ I/O scheduler, aufs, unionfs, reiser4, tuxonice, PHC and many other things that are great for desktop optimization or laptops. All of this is available as a single patch to the vanilla core. Official site . GIT repository. Packages for Debian/Ubuntu .

For today, perhaps, everything. More information can be found in the links to the article. Everything described in the article has been tested by me on many configurations.

Core(English) kernel) is what everything else is built around. That is what is called Linux. Now the word Linux in everyday life is called the 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 kernel with a large number of patches, some of which add unstable and experimental features.

Each release of Ubuntu has its own kernel version. LTS releases since 10.04 have been able to upgrade the kernel to versions included in newer releases.

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

forks

The version numbering of the Ubuntu kernel and on the kernel.org site do not match, since Canonical developers add a micro version to indicate the added patches. For example, version 3.2.0-23 would mean that the kernel is based on the 3.2 branch, which has 23 patches.

The following types of kernels are supported in the Ubuntu repository:

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

The 64-bit kernel allows addressing 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, eliminate regressions), you can use the officially supported archive http://kernel.ubuntu.com/~kernel-ppa/mainline/ .

Compiling the kernel

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

Before you start building the kernel, you need to 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 by Ubuntu can be obtained by installing the linux-source package:

apt-get install linux source

After installation in the directory /usr/src there will be an archive called linux-source-verify_kernels.tar.bz2.

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

When downloading the kernel from the kernel.org site, 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.

Recently, new versions of kernels are released quite often. Every few months there is a stable release. Well, unstable release candidates come out even more often. Linus Torvalds and many developers around the world are constantly working to improve new kernels and add more and more functionality to them.

With each new version, the Linux kernel adds support for several new devices, such as new processors, video cards, or even touch screens. Recently, support for new hardware has improved a lot. Also, new file systems are included in the kernel, the network stack is improved, errors and bugs are fixed.

If you need more information about changes in a particular kernel version, see its Changelog on kernel.org, and in this article we'll look at updating the Linux kernel to the latest version. I will try not to tie the instruction to a specific kernel version, new kernels are released quite often and it will be relevant for each of them.

Consider updating the Ubuntu and CentOS kernel. First, let's look at how to update the kernel in Ubuntu 16.04.

Let's first see what kernel you have installed. To do this, open a terminal and run:

For example, I'm currently using version 4.3 and can upgrade to the newest version. The Ubuntu developers have already made sure that their users don't build the kernel by hand and make deb packages of the new kernel version. They can be downloaded from the official Canonical website.

I could have given the wget commands to download here if the kernel version was known, but in our case it would be better to use a browser. Go to http://kernel.ubuntu.com/~kernel-ppa/mainline/. Everything compiled by the Ubuntu kernel team is here. Kernels are built both for specific distributions, with the code name of the distribution, and general ones. Moreover, kernels from Ubuntu 16.10 will most likely work in 16.04, but from 9.04 on Ubuntu 16.04 you should not install the kernel.

Scroll to the bottom, that's where the newer versions of the kernels are located:

In addition, at the very top there is a daily/current folder, which contains the latest, nightly kernel builds. Select the desired kernel version and download the two files linux-headers and linux-image for your architecture:

After the download is complete, you can proceed with the installation. To do this, run the following in the terminal:

Navigate to the folder with installation packages, for example, ~/Downloads:

Run the installation:

If this command did not work, you can go the other way. Install the gdebi utility:

sudo apt-get install gdebi

Then use it to install the kernel:

sudo gdebi linux-headers*.deb linux-image-*.deb

The kernel is installed, it remains to update the bootloader:

sudo update-grub

Now you can restart your computer and see what happened. After the reboot, make sure that the Linux kernel update to the newest version was successful:

As you can see, the kernel is successfully installed and running. But do not rush to remove the old version of the kernel, it is recommended to have several versions of the kernel in the system so that in case of problems you can boot from the old working version.

Automatic Linux Kernel Update in Ubuntu

Above, we looked at how to install the desired kernel version manually. Ubuntu used to have a PPA for daily kernel builds, but it's closed now. Therefore, you can update the kernel only by downloading the deb package and installing it. But all this can be simplified with a special script.

Installing the script:

cd /tmp
$ git clone git://github.com/GM-Script-Writer-62850/Ubuntu-Mainline-Kernel-Updater
$ bash Ubuntu-Mainline-Kernel-Updater/install

Checking for updates:

KernelUpdateChecker -r yakkety

The -r option allows you to specify the distribution branch for which to search for kernels. For xenial, the kernels are no longer built, but the kernels from the next version will work fine here. In addition, the -no-rc option can tell the utility not to use release candidates, and the -v option specifies the exact kernel version to be installed. If you don't care which distribution the kernel is for, as long as it's the latest, use the --any-release option. The script will give the following output:

Before installing the kernel, you can see the details by opening the /tmp/kernel-update file:

Here we can see that the search for yakkety was performed, and the kernel version 4.7-rc6 is currently available. We can install:

sudo /tmp/kernel-update

The script will show us the version of the current kernel, as well as the version of the kernel that will be installed, its build date, and other details. You will also be asked if you need to keep a change log. Next comes the installation:

Old kernels, just in case, do not delete (n):

Done, updating the kernel to the newest version is complete, now restart your computer (y):

Checking if the Ubuntu kernel update actually worked:

Moreover, the script has been added to autoload and will now automatically check for updates 60 seconds after login. The autoload shortcut is in the file:

vi ~/.config/autostart/KernelUpdate.desktop

You can change it as you like or delete it. If you want to remove the script completely from the system, run:

rm ~/.config/autostart/KernelUpdate.desktop
$ sudo rm /usr/local/bin/KernelUpdate(Checker,ScriptGenerator)

Is not downloading

If any errors occurred during the installation or the kernel was updated incorrectly and now the system does not boot with the new kernel, you can use the old kernel. Also, the system may not start if you are using a proprietary driver for an NVIDIA video card; in this case, do not rush to download the newest version of the kernel, use only stable kernels, as a rule, support for this module has already been added to them.

And to restore the system, select the item Advanced options for Ubuntu in Grub menu:

And start the previous running kernel:

After downloading, it remains to remove the incorrectly installed kernel and update Grub again, substitute the desired kernel version instead of 4.7:

sudo apt remove linux-header-4.7* linux-image-4.7*

sudo update-grub

Your system is now back to its previous state. You can try installing an older kernel version or try again.

Upgrading the Linux kernel to 4.4 on CentOS

And now let's look at how to update the latest version of the Linux kernel in CentOS. The instructions have been tested on CentOS 7, but most likely will work on RedHat 7, Fedora and other similar distributions.

As a rule, new kernels are not included in the official CentOS repositories, so in order to get the latest stable version, we will need to add the ELRepo repository. This is a repository of commercial packages (Enterprise Linux Packages) and is also maintained on RedHat and Fedora.

To add a repository, follow these steps:

First you need to import the key:

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

Add the repository and required components to RHEL/Scientific Linux/CentOS-7:

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm

yum install yum-plugin-fastestmirror

On Fedora 22 and above:

The available information about building the kernel varies greatly, so we will describe the build of the kernel specifically for Ubuntu. Let's try. so that there are no errors in the commands written in this article. When describing the compilation process, we will not stop only at getting the kernel. Getting a working kernel is not enough. For owners of nVidia cards, here will be a description of how to get a system with working graphics on the new core. Moreover, the graphics will work both in the old core and in the new one.

1. Installing kernel sources

First we need the kernel sources. For Ubuntu 7.04 they are already in the repository, you need to find the package linux-source-waste(in our case it will be linux-source-2.6.20), and install it (all this can be done via Synaptic). After installing this pact, in the directory /usr/src a file named linux-source-2-6-20.tar.bz2.

We do under sudo command

chmod 777 /usr/src

Login as a regular user /usr/src and unpack this file

tar -jxvf linux-source-2-6-20.tar.bz2

The source directory will appear. /usr/src/linux-source-2-6-20. We delete the archive file (root rights will be needed).

2. Installing accompanying packages

More packages needed to build kernel-package, libncurses5-dev, fakeroot. Install them via Synaptic. Of course, the compiler must also be installed on the system. gcc And dev-packages for system libraries, such as libc6-dev.

3. Creating a kernel configuration file

Now we need the kernel config with which the ubunta kernel was built. Go to catalog /boot, and we see a file like config-2.6.20-15-generic. He is what we need. Copy it to the source directory /usr/src/linux-source-2-6-20 and rename it to .config. Please note that the file name starts with a dot, this is not a typo.

Now, being in the directory /usr/src/linux-source-2-6-20, we give the command as a normal user

this will launch the kernel configuration text interface. You can also run the setup in graphical mode

In both cases, an interface with checkboxes will open, through which we configure the kernel. It seems like the file is opened by default .config, which currently contains the standard Ubuntu kernel config.

What do you want to configure - think for yourself, there is a lot of Russian-language documentation on this issue on the Internet. The purpose of this article is only to describe the steps involved in compiling the Ubuntu kernel.

At the end of the configuration, select the "Save configuration configuration" item, specify a file name that is different from .config, For example .config_my_01. We leave.

Now rename .config V .config_ubuntu. And you get two files - .config_ubuntu And .config_my_01. You can see the differences between the default and your configuration like this

diff .config .config_my_01

Now copy your configuration .config_my_01 under the name .config. Those. you will get 3 config files. Compilation will use the file .config. Files .config_ubuntu And .config_my_01 we will be helped in the future for a new compilation. This is just in case the new kernel turns out to be inoperable or buggy.

4. Compilation

Before compiling, be sure to check the availability of free space (on the partition where the sources are located). It would be nice to mark 4-5Gb(!) in reserve. When compiling, the size of the source directory can grow to 3.5Gb. You can check the free space with the command

Now, being under a normal user in the directory /usr/src/linux-source-2-6-20, we give a command that will delete in the source the object files compiled by someone, which remained from the previous compilation and were in the archive.

Then through sudo we get the rights of the root and start the compilation.

make-kpkg --initrd --revision=mybuild.1.0 kernel_image kernel_headers

Where instead of " mybuild.1.0" write what you need. English letters, numbers, dots are allowed. Underscores and dashes are not allowed.

Actually, in an amicable way, compilation of the kernel should be done under the rights of a regular user. Formally, creating a kernel binary is no different from compiling a binary of any other program. But we do compilation not in manual mode (through commands like make bzImage modules), and in semi-automatic (through make-kpkg). And this program, after compiling, will run the program from under itself dpkg-deb To obtain deb kernel package. At this point, root rights are required.

Now let's see what the above command does. It starts compiling the kernel, and then creates deb-named package linux-image-version.deb, which will contain the kernel binary and kernel modules (this will be done thanks to the goal kernel_image). It will also be created deb-named package linux-headers-version.deb, it will contain the kernel header files (this will be done thanks to the goal kernel_headers). The resulting packages will be in the directory /usr/src.

See what files are in these deb-packages, can be in conqueror(in Kubuntu) by right-clicking on the one of interest deb-file and selecting " Kubuntu package menu" -> "Show package info". Information will be formed rather slowly, about a minute, since the packets are large.

5. Installing the kernel

Now install the kernel. Being with superuser rights in the directory /usr/src, give the command

dpkg -i linux-image-version.deb

after which your kernel (file vmlinuz-2.6.20.3-ubuntu1) will be placed in the directory /boot(all previous kernels will not go anywhere either, they will remain in their places), and in the directory /lib/modules, next to the directory with regular kernel modules (in Ubuntu 7.04 called /lib/modules/2.6.20-15-generic) a directory will appear with the modules of your new kernel (in our case it will be /lib/modules/2.6.20.3-ubuntu1). The new kernel will be automatically written to /boot/grub/menu.lst.

In principle, you can already reboot, and in the loading screen Grub a new item will appear with your kernel. The new core will appear at the top of the list. But we are not in a hurry, but we will give another command

dpkg -i linux-headers-version.deb

which will install the kernel headers in the directory /usr/src/linux-headers-version, in our case it will be a directory /usr/src/linux-headers-2.6.20.3-ubuntu1. We will need these headers, for example, to recompile drivers nVidia for the new kernel.

6. Restart

Reboot, and in the menu Grub you will see two new items - normal boot with your kernel, and boot in minimal console mode. Choose the first item. If the core did not immediately fall into Kernel panic, that's a good sign. Wait for the end of the download. If you're lucky, Ubuntu will boot into graphical mode and display a graphical login prompt. In this case, you can not read further.

But for card users nVidia, who used drivers installed through the "Proprietary Driver Manager" or used drivers from the package nvidia-glx(or there nvidia-glx-new), I give a 99% guarantee that you will not be lucky! And you will not see graphics under the new kernel!

7. Installing nVidia drivers from nvidia.com

To get working x's under the new kernel, the first thing that comes to mind is to install the drivers from nvidia.com under the new kernel. And this is the wrong decision! As soon as firewood is installed under the new kernel, the graphics in your old tested kernel will stop working (due to the fact that the nVidia driver files are tightly tied to the version and name of the kernel). And since you haven't really tested the performance of the new kernel yet, you can get a system "with a native kernel, but without graphics" and "a buggy kernel, but with graphics". I don't think anyone likes this situation.

In the popular article "Nuclear Physics for Housewives", there are recommendations on how to get graphics under both cores. The following way is proposed - to have the installation package of firewood from nvidia.com ready, and if you want to boot under a specific kernel, you must first boot in the console mode of this kernel, install the firewood, and then boot normally. I don't think anyone will be happy with this approach either.

We will make sure that the graphics will work both in the old kernel and in the new kernel, and for this it will not be necessary to run the installation (compilation) of firewood every time. To do this, we will need to fulfill only one condition - that the graphic firewood under different cores be of the same version.

Brief Action Plan- we put firewood from the site nvidia.com for the standard kernel in full. We make sure they work. Then, from the same package, we install firewood for a self-made kernel, but in the "only graphics module" mode.

8. Installing nVidia drivers for a regular kernel

Everything that is written below is also suitable for those who just decided to install new nVidia drivers for the standard kernel!

Downloading from the site nvidia.com firewood under linux. I downloaded the firewood version 96.43 . The file is called nvidia-linux-x86-96.43.01-pkg1.run. But you can try other stable releases that are present on the site. nVidia.

To install, create in the directory /usr/src subdirectory named nvidia, copy this file there. Being in this subdirectory, we give the file permission to execute

chmod 777 NVIDIA-Linux-x86-96.43.01-pkg1.run

Everything, on this work in the graphical interface ends for a while. Before exiting graph mode, run Synaptic and make sure you have

  • aptitude. This is a front-end wrapper over the package manager for text-mode.
  • linux-headers-2.6.20-15. These are the header files (headers) of your standard kernel.
  • linux-headers-2.6.20-15-generic. I won’t say exactly what the hell this package is for, but let it be.

A good tip is to have the text of this article printed out on paper, or save it to a text file that can be viewed from text mode.

We reboot into the console mode of a regular kernel (in Grub there is such a point). IN ubuntu you will automatically get root rights, you don’t even need to enter a password. Now we need to remove the wood nVidia that are in the system. If you installed firewood through the "Proprietary Driver Manager" (or by installing the package nvidia-glx or nvidia-glx-new), then the package nvidia-glx/nvidia-glx-new must be removed from the system, and not just uninstalled, but uninstalled in the mode purge.

I'm a rather dumb user, and instead of dealing with the options dpkg, in the console I use the program aptitude. Dial a team

and you will find yourself in a shell, reminiscent of Synaptic. There will be a key hint at the top. To access the menu, press ctrl+t(uncomfortable, but what to do). On the menu
arrows and keys Enter find and select " Find". We write the search string - nvidia-glx. The light will fall on the desired package. We call the menu again, and find the item " Purge". We press it, and the package on which the flash is located will be marked for complete uninstallation of all its files from the system (the package itself will remain in the cache, it can be reinstalled if necessary). A hint will appear below - " e - Examine, ! - remove". Press " e" - and see which packages will be removed. If from nvidia-glx packages depend on them, they will also be uninstalled. This is usually a package. nvidia-glx-dev. It's okay if he leaves too.

Now we press " ! "(for the especially gifted - shift+1), hereby we agree with our changes. Then we press " q" (exit). When exiting, the program will remove the packages we marked.

Now is such a moment. We are now at the level init 1(only console, a lot of services are not running). If you run the driver installation nVidia, then she will swear that you may not have started the service devfs, which usually starts at the level 3 . Therefore, we give the command

telinit 3

and the system will finish loading the necessary services, and at the same time exit the single-user mode (several consoles will appear that can be switched with the keys ALT+F1...ALT+F6). For further work, we just need two consoles. By the way, the system will try to load the graphics, but it will not succeed, because. we just uninstalled the driver. And she will stay 7 th console with a blank screen. Don't panic, push ALT+F1, we see the first console, we enter the login there, the password is the same as in the graphical input (only after the login and password we press Enter but not TAB).

After logging in, we call mc under superuser

Go to catalog /usr/src/nvidia

./NVIDIA-Linux-x86-96.43.01-pkg1.run -e

Option " -e" will allow us to see a report on actions and see conflicting files. Let's start the installation. We agree with the license. ftp nvidia. We speak OK that the modules will be created by us. For questions about entering paths, just click Enter.

In the end, before the installation itself, a list of files that will be installed will be shown. And at the beginning of this list (if found) will appear conflicting files. Will be written " backup file..."- this is what they are. In my case, these were files

/usr/lib/xorg/modules/extensions/libGLcore.so
/usr/lib/xorg/modules/extensions/libglx.so
/usr/lib/libGL.so
/usr/lib/libGL.so.1
/usr/lib/libGL.so.1.2
/lib/modules/2.6.20-15-generic/volatile/nvidia.ko
/usr/include/GL/glext.h
/usr/include/gl/gl.h
/usr/include/GL/glxext.h
/usr/include/GL/glx.h

These are the files from the package nvidia-restricted-modules. The fact is that if you simply remove this package, then along with these files, all files for nVidia- chipsets ( nVidia after all, it does not only make video cards). You will also need to remove dependent packages. linux-restricted-modules-2.6.20-15-generic, linux-restricted-modules-generic And linux generic. Therefore, it is undesirable to remove this package. Therefore, we will do it differently.

As soon as you see such a list, log in to the second console (transition - ALT+F2), run

and methodically transfer these conflicting files somewhere to a separate directory in the home directory, referring to the list in the first console. Why move and not delete? The fact is that file names in Linux are "human-readable", and you can easily make a mistake and delete the wrong file.

After deleting all files intended for Backup, return to the first console. Abort installation ( ctrl+c) and restart it. If " backup file..." will no longer be, then complete the installation. Everything should go smoothly. You can agree with the fix xorg.conf, still create a backup file.

Now attention! Most importantly, at this moment do not overload! And go to the file /etc/default/linux-restricted-modules-common, and add to option DISABLED_MODULES modules n.v. And nvidia_new. I have it done like this

DISABLED_MODULES="nv nvidia_new"

If this is not done, then the next time you load the file (which you deleted!) /lib/modules/2.6.20-15-generic/volatile/nvidia.ko will automatically restored from the package nvidia-restricted-modules. And your file that you compiled when you installed the drivers is called /lib/modules/2.6.20-15-generic/kernel/drivers/video/nvidia.ko. And so, at start ksov the first file will be found. And it will not reach your file. And X's won't be able to boot.

We reboot into the standard kernel in full mode. If everything is done correctly, X will start. Rejoice, at least you still have a working system.

9. Installing nVidia drivers for a custom kernel

Now the last step remains - to make the graphics work in the new kernel. Everything is quite simple here. We boot into the console mode of the homemade kernel. We give a command

log in, and in the first console run

Go to catalog /usr/src/nvidia and start installing firewood with the command

./NVIDIA-Linux-x86-96.43.01-pkg1.run -Ke

Options " -ke" allow you to build only the graphics module nvidia.ko under the current kernel (and the file will be placed in the directory /lib/modules/current_kernel_name/kernel/drivers). No other shared files, such as would be placed in /usr/lib... as when compiling with the "-e" option, will not be created.

Just like when compiling in the standard kernel, we agree with the paths by pressing Enter. We reach the screen where the files that will be installed will be listed. If there are conflicting files at the beginning of this list " backup file...", switch to the adjacent console and delete (transfer) these files.

After removing the conflicting files, in the first console, abort the installation ( ctrl+c), and run it again (with the option " -ke"of course). After the installation is complete, reboot by selecting from the menu Grub to full mode with your kernel.

X should start. You can rejoice a second time - you have a system with a custom kernel and working graphics.

At any time, you can now boot under the desired kernel, and graphics should work everywhere. That's all.