Product SiteDocumentation Site

8.10. Mengompilasi sebuah Kernel

Kernel yang disediakan oleh Debian menyertakan banyak fitur, sebagaimana penggerak (driver) penuh, agar dapat menangani spektrum yang luas konfigurasi perangkat keras. Oleh karena itu, beberapa pengguna lebih memilih mengompilasi ulang kernel agar hanya menyertakan beberapa yang mereka butuhkan saja. Ada dua alasan untuk pilihan ini. Pertama, mungkin untuk mengoptimalisasi konsumsi memori, karena kode kernel, bahkan jika tak pernah digunakan, memakan memori yang tidak berguna (dan tak pernah “hilang” pada ruang swap, karena dia menggunakan RAM sebenarnya), di mana dapat mengurangi performa sistem. Kernel lokal yang dikompilasi dapat membatasi resiko masalah keamanan karena hanya sebagian kode kernel yang dikompilasi dan dijalankan.
Kompilasi ulang kernel juga diperlukan jika Anda ingin menggunakan fitur tertentu yang hanya tersedia sebagai patch (dan tidak disertakan dalam versi kernel standar).

8.10.1. Perkenalan dan Prasyarat

Tidak mengejutkan Debian mengelola kernel dalam bentuk paket, yang mana tidak secara tradisional kernel dikompilasi dan diinstall. Selama kernel berada dalam kontrol sistem paket, dia dapat dicopot bersih, atau disebarkan pada beberapa mesin. Lagipula, skrip yang berhubungan dengan paket ini secara mengotomatisasi interaksi dengan bootloader dan pembuat initrd.
The upstream Linux sources contain everything needed to build a Debian package of the kernel. But you still need to install build-essential to ensure that you have the tools required to build a Debian package. Furthermore, the configuration step for the kernel requires the libncurses-dev package (formerly libncurses5-dev, which is now a transitional package). Finally, the fakeroot package will enable creation of the Debian package without using administrator's rights.

8.10.2. Mendapatkan Sumber

Seperti apapun yang dapat berguna pada sebuah sistem Debian, sumber kernel Linux tersedia dalam sebuah paket. Untuk mendapatkannya, install saja paket linux-source-versi. Perintah apt search ^linux-source menampilkan berbagai variasi versi paket kernel oleh Debian. Versi terkini tersedia dalam distribusi Unstable: Anda dapat mengunduhnya tanpa banyak resiko (khususnya jika APT Anda dikonfigurasi berdasarkan instruksi Bagian 6.2.6, “Bekerja dengan Beberapa Distribusi”). Catat bahwa kode sumber yang ada dalam paket ini tidak secara persis sama dengan yang dipublikasikan oleh Linus Torvalds dan pengembang kernel; seperti seluruh distribusi, Debian menerapkan berbagai patch, yang mungkin (mungkin tidak) ditemukan pada versi hulu Linux. Modifikasi ini termasuk backport perbaikan/fitur/penggerak (driver) dari versi kernel terbaru, fitur baru yang belum (sepenuhnya) digabungkan dalam upstream Linux tree, dan terkadang bahkan perubahan spesifik Debian.
The remainder of this section focuses on the 5.10 version of the Linux kernel, but the examples can, of course, be adapted to the particular version of the kernel that you want.
We assume the linux-source-5.10 package has been installed. It contains /usr/src/linux-source-5.10.tar.xz, a compressed archive of the kernel sources. You must extract these files in a new directory (not directly under /usr/src/, since there is no need for special permissions to compile a Linux kernel): ~/kernel/ is appropriate.
$ mkdir ~/kernel; cd ~/kernel
$ tar -xaf /usr/src/linux-source-5.10.tar.xz
To build a kernel from the pristine sources, just download the tarball of the version of your choice from kernel.org, verify the integrity after importing the kernel maintainers key, and then proceed as described in the following chapters.

$ wget https://kernel.org/pub/linux/kernel/v5.x/linux-5.10.62.tar.xz
[..]
$ wget https://kernel.org/pub/linux/kernel/v5.x/linux-5.10.62.tar.sign
[..]
$ unxz -c linux-5.10.62.tar.xz | gpg --verify linux-5.10.62.tar.sign -
gpg: Signature made Fri 03 Sep 2021 10:11:35 AM CEST
gpg:                using RSA key 647F28654894E3BD457199BE38DBBDC86092693E
gpg: Good signature from "Greg Kroah-Hartman <gregkh@linuxfoundation.org>" [unknown]
gpg:                 aka "Greg Kroah-Hartman (Linux kernel stable release signing key) <greg@kroah.com>" [unknown]
gpg:                 aka "Greg Kroah-Hartman <gregkh@kernel.org>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 647F 2865 4894 E3BD 4571  99BE 38DB BDC8 6092 693E

8.10.3. Mengonfigurasi Kernel

Langkah selanjutnya mengonfigurasi kernel berdasarkan kebutuhan Anda. Prosedur tepatnya tergantung pada tujuannya.
When recompiling a more recent version of the kernel (possibly with an additional patch), the configuration will most likely be kept as close as possible to that proposed by Debian. In this case, and rather than reconfiguring everything from scratch, it is sufficient to copy the /boot/config-version file (the version is that of the kernel currently used, which can be found with the uname -r command) into a .config file in the directory containing the kernel sources. Make sure to read sidebar TIP Missing debian/certs/debian-uefi-certs.pem in this case.
$ cp /boot/config-5.10.0-8-amd64 ~/kernel/linux-source-5.10/.config
Kecuali Anda perlu mengubah konfigurasi, Anda dapat berhenti di sini dan melompat ke Bagian 8.10.4, “Kompilasi dan Membangun Paket”. Namun jika Anda perlu mengubahnya, atau jika Anda memutuskan untuk mengonfigurasi semuanya dari awal, Anda perlu meluangkan waktu untuk mengonfigurasi kernel Anda. Ada berbagai antarmuka terdedikasi dalam direktori sumber kernel yang dapat digunakan dengan memanggil perintah make target, dimana target merupakan salah satu dari nilai-nilai yang dijelaskan di bawah.
make menuconfig compiles and executes a text-mode interface (this is where the libncurses-dev package is required) which allows navigating the options available in a hierarchical structure. Pressing the Space key changes the value of the selected option, and Enter validates the button selected at the bottom of the screen; Select returns to the selected sub-menu; Exit closes the current screen and moves back up in the hierarchy; Help will display more detailed information on the role of the selected option. The arrow keys allow moving within the list of options and buttons. To exit the configuration program, choose Exit from the main menu. The program then offers to save the changes you've made; accept if you are satisfied with your choices.
Other interfaces have similar features, but they work within more modern graphical interfaces; such as make xconfig which uses a Qt graphical interface, and make gconfig which uses GTK+. The former requires qtbase5-dev, while the latter depends on libglade2-dev and libgtk2.0-dev.
Ketika menggunakan salah satu antarmuka konfigurasi tersebut, sebaiknya dimulai dengan konfigurasi standar. Kernel menyediakan konfigurasi tersebut dalam arch/arch/configs/*_defconfig dan Anda dapat menaruh konfigurasi pilihan dengan perintah seperti make x86_64_defconfig (dalam hal ini PC 64-bit) atau make i386_defconfig (dalam hal ini PC 32-bit).

8.10.4. Kompilasi dan Membangun Paket

Once the kernel configuration is ready, a simple make deb-pkg will generate up to 5 Debian packages:
linux-image-version
contains the kernel image and the associated modules,
linux-headers-version
contains the header files required to build external modules,
linux-firmware-image-version
contains the firmware files needed by some drivers (this package might be missing when you build from the kernel sources provided by Debian),
linux-image-version-dbg
contains the debugging symbols for the kernel image and its modules (only created if CONFIG_DEBUG_INFO=y), and
linux-libc-dev
contains headers relevant to some user-space libraries like GNU glibc.
The version is defined by the concatenation of the upstream version (as defined by the variables VERSION, PATCHLEVEL, SUBLEVEL, and EXTRAVERSION in the Makefile), of the LOCALVERSION configuration parameter, and of the LOCALVERSION environment variable. The package version reuses the same version string with an appended revision that is regularly incremented (and stored in .version), except if you override it with the KDEB_PKGVERSION environment variable.
$ make deb-pkg LOCALVERSION=-falcot KDEB_PKGVERSION=$(make kernelversion)-1
[...]
$ ls ../*.deb
../linux-headers-5.10.46-falcot_5.10.46-1_amd64.deb
../linux-image-5.10.46-falcot_5.10.46-1_amd64.deb
../linux-image-5.10.46-falcot-dbg_5.10.46-1_amd64.deb
../linux-libc-dev_5.10.46-1_amd64.deb
The whole process requires around 20 GB of free space, at least 8 GB of RAM, and several hours of compilation (utilizing one core) for a standard amd64 Debian kernel. These requirements can be drastically reduced by disabling the debug information using CONFIG_DEBUG_INFO=n, but this will make it impossible to trace kernel errors (“oops”) using gdb and also stop the creation of the linux-image-version-dbg package.

8.10.5. Kompilasi Modul Eksternal

Some modules are maintained outside of the official Linux kernel. To use them, they must be compiled alongside the matching kernel. A number of common third party modules are provided by Debian in dedicated packages, such as vpb-driver-source (extra modules for Voicetronix telephony hardware) or leds-alix-source (driver of PCEngines ALIX 2/3 boards).
Paket-paket ini ada banyak dan bervariasi, perintah apt-cache rdepends module-assistant$ dapat menampilkan daftar yang disediakan oleh Debian. Akan tetapi, daftar yang lengkap terkadang tidak berguna karena ada alasan khusus untuk mengompilasi modul eksternal kecuali ketika Anda tahu Anda memerlukannya. Dalam kasus tersebut, dokumentasi perangkat keras akan secara khas merinci modul spesifik yang diperlukannya agar berfungsi di Linux.
For example, let's look at the dahdi-source package: after installation, a .tar.bz2 of the module's sources is stored in /usr/src/. While we could manually extract the tarball and build the module, in practice we prefer to automate all this using the DKMS framework (Dynamic Kernel Module Support). Most modules offer the required DKMS integration in a package ending with a -dkms suffix. In our case, installing dahdi-dkms is all that is needed to compile the kernel module for the current kernel provided that we have the linux-headers-* package matching the installed kernel. For instance, if you use linux-image-amd64, you would also install linux-headers-amd64.
$ sudo apt install dahdi-dkms
[...]
Setting up dkms (2.8.4-3) ...
Setting up linux-headers-5.10.0-8-amd64 (5.10.46-4) ...
/etc/kernel/header_postinst.d/dkms:
dkms: running auto installation service for kernel 5.10.0-8-amd64:.
Setting up dahdi-dkms (1:2.11.1.0.20170917~dfsg-7.4) ...
Loading new dahdi-2.11.1.0.20170917~dfsg-7.4 DKMS files...
Building for 5.10.0-8-amd64
Building initial module for 5.10.0-8-amd64
Done.

dahdi_dummy.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/5.10.0-8-amd64/updates/dkms/

dahdi_dynamic_eth.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/5.10.0-8-amd64/updates/dkms/

[...]
DKMS: install completed.
$ sudo dkms status
dahdi, 2.11.1.0.20170917~dfsg-7.4, 5.10.0-8-amd64, x86_64: installed
$ sudo modinfo dahdi_dummy
filename:       /lib/modules/5.10.0-8-amd64/updates/dkms/dahdi_dummy.ko
license:        GPL v2
author:         Robert Pleh <robert.pleh@hermes.si>
description:    Timing-Only Driver
depends:        dahdi
retpoline:      Y
name:           dahdi_dummy
vermagic:       5.10.0-8-amd64 SMP mod_unload modversions 
parm:           debug:int

8.10.6. Menerapkan Patch Kernel

Beberapa fitur tidak disertakan dalam kernel standar karena belum maksimalnya atau ketidaksepakatan dengan maintainer kernel. Beberapa fitur mungkin didistribusikan sebagai patch di mana setiap orang bebas untuk menerapkan ke sumber kernel.
Debian kadang mendistribusikan beberapa patch ini dalam paket linux-patch-*, tapi seringkali mereka tidak mencapai rilis stabil (kadang untuk alasan yang sama dengan mengapa mereka tidak digabung ke dalam kernel hulu resmi). Paket ini menginstall berkas dalam direktori /usr/src/kernel-patches/.
To apply one or more of these installed patches, use the patch command in the sources directory then start compilation of the kernel as described above. The following shows an old example using linux-patch-grsecurity2 and linux-source-4.9.
$ cd ~/kernel/linux-source-4.9
$ make clean
$ zcat /usr/src/kernel-patches/diffs/grsecurity2/grsecurity-3.1-4.9.11-201702181444.patch.gz | patch -p1
Catat bahwa patch yang diberikan mungkin tidak bekerja dengan setiap versi kernel; mungkin saja sebuah patch gagal ketika diterapkan ke sumber kernel. Sebuah pesan error akan ditampilkan dan memberikan beberapa rincian tentang kesalahan; dalam kasus ini, mengacu ke dokumentasi yang tersedia dalam paket Debian dari patch tersebut (dalam direktori /usr/share/doc/linux-patch-*/). Dalam banyak kasus, maintainer menunjukkan versi kernel mana patch mereka ditujukan.