mayfrost-guides/KERNEL.md

231 lines
11 KiB
Markdown
Raw Normal View History

2018-03-03 08:21:00 +00:00
# KERNEL
Compiling a kernel has the advantage to make it as minimal and featureful as you want, you can test kernel patches and tweaks and optimize your system.
2018-03-03 09:16:25 +00:00
* You need the kernel source, build tools, the kernel configuration file (__optional__), and __root__ or __sudo__ privileges for the final stages of the process.
2018-03-03 08:21:00 +00:00
* Back up all of your files.
* Have a distro in a Live CD or USB at hand just in case.
2018-03-03 19:42:53 +00:00
---
## TOC
2018-03-03 09:30:20 +00:00
1. [PREPARATIONS](#preparations)
2018-03-03 09:34:02 +00:00
2. [GETTING THE KERNEL SOURCE](#getting-the-kernel-source)
2018-03-03 09:36:36 +00:00
2.1. [OPTIONAL: Symlink](#optional-symlink)
2018-03-03 09:34:02 +00:00
2.2. [DOWNLOAD THE KERNEL SOURCE](#download-the-kernel-source)
3. [GENERATING A CONFIGURATION FILE](#generation-a-configuration-file)
4. [CHANGING THE CONFIGURATION (OPTIONAL)](#changing-the-configuration-optional)
5. [COMPILATION](#compilation)
5.1. [OPTION A: One command](#option-a-one-command)
5.2. [OPTION B: One by one](#option-b-one-by-one)
5.3. [BUILD MODULES](#build-modules)
6. [INITRD](#initrd)
7. [MODULES](#modules)
7.1. [COMPILE A SINGLE MODULE](#compile-a-single-module)
7.2. [DKMS](#dkms)
8. [KERNEL PATCH](#kernel-patch)
2018-03-03 19:42:53 +00:00
8.1. [PRELIMINARY STEPS](#preliminary-steps)
8.2. [OPTION 1: Git](#option-1-git)
8.3. [OPTION 2: Patch command](#option-2-patch-command)
8.4. [OPTION 3: Patch with zcat](#option-2-patch-with-zcat)
---
2018-03-03 09:30:20 +00:00
2018-03-03 08:21:00 +00:00
## PREPARATIONS
2018-03-03 09:16:25 +00:00
* To see which version the new kernel source is.
2018-03-03 08:21:00 +00:00
`head /usr/src/linux/Makefile`
2018-03-03 09:16:25 +00:00
* To see which version the current kernel is.
2018-03-03 08:21:00 +00:00
`uname -r`
2018-03-03 09:16:25 +00:00
* List hardware and kernel modules in use (take note to know what kernel modules are appropriate).
`lspci -k`
`lspci -v`
`cat /proc/cpuinfo`
`cat /proc/meminfo`
`lsmod`
2018-03-03 08:21:00 +00:00
## GETTING THE KERNEL SOURCE
2018-03-03 09:36:36 +00:00
### OPTIONAL: Symlink
2018-03-03 19:08:20 +00:00
Is standard to download Linux sources under __/usr/src/__, then link the downloaded kernel tree to __/usr/src/linux-<VERSION_NUMBER>__ and work there, but is not a requirement.
2018-03-03 09:36:36 +00:00
* Change to __/usr/src/__.
2018-03-03 08:21:00 +00:00
`cd /usr/src/`
2018-03-03 09:36:36 +00:00
* Symlink.
2018-03-03 08:21:00 +00:00
`ln -s linux-<VERSION_NUMBER> linux`
2018-03-03 09:30:20 +00:00
### DOWNLOAD THE KERNEL SOURCE
Get latest kernel from kernel.org
2018-03-03 08:44:37 +00:00
* Get tarball.
2018-03-03 08:21:00 +00:00
`wget https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git/snapshot/linux-stable-rc-<VERSION>.tar.gz`
2018-03-03 08:44:37 +00:00
* Uncompress.
2018-03-03 08:21:00 +00:00
`tar -xvzf linux-<VERSION_NUMBER>.tar.xz`
2018-03-03 08:44:37 +00:00
* Enter directory.
2018-03-03 08:21:00 +00:00
`cd linux-<VERSION_NUMBER>/`
2018-03-03 09:16:25 +00:00
* Clean the directory (wont touch the config file just remove all compiled files from the kernel tree).
`make clean`
* Remove compiled files from the kernel tree AND the configuration file if there are any (backup your configuration file before this if you have one in the current directory).
2018-03-03 08:35:27 +00:00
`make mrproper`
2018-03-03 08:21:00 +00:00
## GENERATING A CONFIGURATION FILE
2018-03-03 09:39:59 +00:00
This process makes a "__.config__" file in the kernel source directory, this file determines which drivers are built and other support. There are three options here, you can turn on only the minimal set of options you need.
2018-03-03 08:21:00 +00:00
2018-03-03 09:16:25 +00:00
* __OPTION 1__: Use the config file of another kernel or the current system.
2018-03-03 08:21:00 +00:00
`cp -v /boot/config-$(uname -r) .config`
2018-03-03 08:56:04 +00:00
* Or in some distros you can take it from the running kernel.
2018-03-03 08:21:00 +00:00
`zcat /proc/config.gz > .config`
2018-03-03 09:16:25 +00:00
* __OPTION 2__: Make a default config file (may or may not not have the options you are currently using).
2018-03-03 08:21:00 +00:00
`make defconfig`
2018-03-03 09:16:25 +00:00
* __OPTION 3__: Generate a config file disabling all options not loaded by the currently running kernel and will make questions on what kernel options to support when it finds new kernel options. Could have problems with peripherals not in use at that time unless you plug all devices like __USBs__. Attach devices you use or insert modules manually with the insmod command before using this option. Available from kernel version 2.6.32 and up.
2018-03-03 08:21:00 +00:00
`make localmodconfig`
2018-03-03 09:16:25 +00:00
* __OPTION 4__: Create a minimal kernel config file which necessarily needs to manually enable options afterwards to have a working system. Available from kernel version 3.17-rc1 and up.
2018-03-03 08:24:49 +00:00
`make tinyconfig`
## CHANGING THE CONFIGURATION (OPTIONAL)
* Depending on your tastes this part can take minutes, hours, or days to enable proper options.
2018-03-03 19:16:40 +00:00
* The __Y__ (or "__*__" in __menuconfig__) flag in kernel configuration compiles options to be integrated into the kernel.
2018-03-03 19:15:24 +00:00
* The __M__ flag in kernel configuration compiles options as separate modules.
* The __N__ (or a blank space in __menuconfig__) flag in kernel configuration will not build selected option.
2018-03-03 08:24:49 +00:00
2018-03-03 09:16:25 +00:00
* __OPTION 1__: Start a menu and browse options. Requires curses library but likely is already on your computer. Press "__H__" or "__?__" to see help. You can use the space bar to cycle between the available choices or press the appropriate key mentioned above. Pressing "__/__" to search for keywords.
2018-03-03 08:35:27 +00:00
`make menuconfig`
2018-03-03 08:24:49 +00:00
2018-03-03 09:16:25 +00:00
* __OPTION 2__: Will make questions on what kernel options to support when it finds new kernel options not marked on an existing config file (takes time and and if you are using a striped down config file it requires you knowledge).
2018-03-03 08:24:49 +00:00
`make oldconfig`
2018-03-03 09:16:25 +00:00
* __OPTION 3__: Will fill questions with default answers on what kernel options to support beyond your provided config file.
2018-03-03 08:24:49 +00:00
`make olddefconfig`
## COMPILATION
2018-03-03 19:18:47 +00:00
Can take minutes to hours. Have a cup of tea or coffee while you wait to compile.
2018-03-03 19:12:11 +00:00
__NOTE__: The __-j\<X>__ flag, where __\<X>__ is the number of cores +1, __IS OPTIONAL__ and only goes if you have a processor with multiple cores. If you install __ccache__ (__THIS IS OPTIONAL__) you can speed up subsequent compilations by including __CC="ccache gcc"__ after the __-j\<X>__ flag.
2018-03-03 08:24:49 +00:00
`make -j<X> <OTHER_FLAGS>`
2018-03-03 19:14:24 +00:00
And with ccache.
2018-03-03 09:16:25 +00:00
`make -j<X> CC="ccache gcc" <OTHER_FLAGS>`
2018-03-03 08:24:49 +00:00
2018-03-03 19:13:02 +00:00
For compilation you have two options, __OPTION A__ is the easiest and is a single command. __OPTION B__ is here for didactic purposes.
2018-03-03 08:24:49 +00:00
2018-03-03 09:16:25 +00:00
### OPTION A: One command
Compile and move everything to its place. The "__all__" flag makes modules AND the __bzImage__ at the same time (replaces "__bzImage__" and "__modules__" flags).
`make -j<X> all modules_install install`
2018-03-03 08:24:49 +00:00
2018-03-03 09:16:25 +00:00
### OPTION B: One by one
2018-03-03 08:35:27 +00:00
* If your configuration does not contain answers for all of the options, especially if they are new and not currently included in your running kernel, you will need to answer the prompts for these options.
2018-03-03 08:24:49 +00:00
`make -j<X> bzImage`
2018-03-03 08:56:04 +00:00
* __OPTION B1__: Copy the new kernel to __/boot__.
2018-03-03 08:24:49 +00:00
`cp arch/<YOUR_ARCHITECTURE>/boot/bzImage /boot/vmlinuz`
2018-03-03 08:35:27 +00:00
* Not required for booting but some processes need it.
2018-03-03 08:24:49 +00:00
`cp System.map /boot`
2018-03-03 08:56:04 +00:00
* __OPTION B2__: Automatically move kernel to __/boot__ (and in certain distros, symlink the new kernel to __/boot/vmlinuz__ and update the bootloader configuration).
2018-03-03 08:24:49 +00:00
`make install`
2018-03-03 19:28:05 +00:00
__NOTE__: The __vmlinuz__ can be any name but that exact name has to be added to the configuration file of your bootloader of choice. Usually a version number is appended to the new kernel image. This has the advantage to avoid replacing a current kernel and having a fallback as backup to boot. You can set to boot from any image in the bootloader once you configure them in the bootloader.
2018-03-03 08:24:49 +00:00
2018-03-03 19:28:28 +00:00
### BUILD MODULES
2018-03-03 19:34:18 +00:00
Module files end with the __.ko__ file extension. They are individual files for each question you answered __M__ during kernel configuration. The object code is linked against your freshly built kernel as separate modules. Questions answered __Y__ were integrated into the kernel (__vmlinuz__), and for questions answered __N__ they were skipped (not compiled).
2018-03-03 19:33:32 +00:00
* To compile modules.
2018-03-03 08:24:49 +00:00
`make -j<X> modules`
2018-03-03 08:56:04 +00:00
* Copy generated kernel modules to __/lib/modules/<KERNEL_VERSION>/__.
2018-03-03 08:24:49 +00:00
`make modules_install`
## INITRD
2018-03-03 09:30:20 +00:00
The __initrd__ is used only while booting, unless you compile the kernel with the filesystem it resides on (__initfs__). There are three options.
* __OPTION 1__: Compile the kernel with support for the filesystem used in the __/boot__ partition (__initfs__).
* __OPTION 2__: Use __mkinitrd__.
2018-03-03 08:24:49 +00:00
`mkinitrd -o /boot/initrd.img`
2018-03-03 09:30:20 +00:00
* __OPTION 3__: Use __initramfs__.
2018-03-03 08:24:49 +00:00
`update-initramfs -u`
2018-03-03 19:42:53 +00:00
After this you'll only need to reboot to test your new kernel.
---
2018-03-03 08:24:49 +00:00
2018-03-03 09:16:25 +00:00
## MODULES
2018-03-03 19:42:53 +00:00
This section is optional. It shows how to get individual modules and what you can do with them,, how to install news, update them and automatize the process.
2018-03-03 09:16:25 +00:00
### COMPILE A SINGLE MODULE
2018-03-03 08:35:27 +00:00
* Only compile a module.
2018-03-03 08:21:00 +00:00
`cd linux-<VERSION_NUMBER>/`
2018-03-03 08:35:27 +00:00
* Create files required for compiling external modules.
2018-03-03 08:21:00 +00:00
`make modules_prepare`
2018-03-03 08:35:27 +00:00
* Compile modules.
`make M=<DIRECTORY_WHERE_MODULE_IS_LOCATED>`
2018-03-03 08:56:04 +00:00
* Move module to __<VERSION_NUMBER>__.
2018-03-03 08:35:27 +00:00
`cp <DIRECTORY_WHERE_MODULE_IS_LOCATED>/<NAME_OF_MODULE>.ko /lib/modules/$(uname -r)/`
* Or
2018-03-03 08:21:00 +00:00
`cp <DIRECTORY_WHERE_MODULE_IS_LOCATED>/<NAME_OF_MODULE>.ko /lib/modules/`
2018-03-03 08:35:27 +00:00
* Update the modules.
2018-03-03 08:21:00 +00:00
`depmod -a`
2018-03-03 09:16:25 +00:00
### DKMS
2018-03-03 08:56:04 +00:00
To update modules automatically when changing a kernel use __DKMS__. Requires the __dkms__ package and the proper module source code.
2018-03-03 08:35:27 +00:00
* Create a directory.
2018-03-03 08:21:00 +00:00
`mkdir /usr/src/<MODULE>-<MODULE_VERSION>/`
2018-03-03 08:35:27 +00:00
* Copy the source code of the module.
2018-03-03 08:21:00 +00:00
`cp -a * <DIRECTORY_WHERE_MODULE_IS_LOCATED>`
2018-03-03 08:56:04 +00:00
* Create a __dkms.conf__ file in the directory.
2018-03-03 08:21:00 +00:00
`vi /usr/src/<MODULE>-<MODULE_VERSION>/dkms.conf`
2018-03-03 08:56:04 +00:00
* Edit __dkms.conf__.
2018-03-03 08:21:00 +00:00
`PACKAGE_NAME="<MODULE>"`
`PACKAGE_VERSION="<MODULE_VERSION>"`
`BUILT_MODULE_NAME[0]="<MODULE>"`
`DEST_MODULE_LOCATION[0]="/kernel/<SECTION>/<MODULE>/"`
`AUTOINSTALL="yes"`
2018-03-03 08:56:04 +00:00
* Add __<MODULE>__ and __<MODULE_VERSION>__ to __DKMS__.
2018-03-03 08:21:00 +00:00
`dkms add -m <MODULE> -v <MODULE_VERSION>`
2018-03-03 08:56:04 +00:00
* Compile the module with __DKMS__.
2018-03-03 08:21:00 +00:00
`dkms build -m <MODULE> -v <MODULE_VERSION>`
2018-03-03 08:56:04 +00:00
* Install the module with __DKMS__.
2018-03-03 08:21:00 +00:00
`dkms install -m <MODULE> -v <MODULE_VERSION>`
2018-03-03 19:42:53 +00:00
---
2018-03-03 08:21:00 +00:00
## KERNEL PATCH
2018-03-03 20:24:22 +00:00
This section is also optional. There are cool patches for the kernel out there and here you'll find how to install them. You can find patch files on the Linux Kernel Mailing List https://lkml.org/. More patches are provided in other places.
Some available patches are:
* __Kernel_gcc_patch__, enable gcc optimizations for additional CPUs: https://github.com/graysky2/kernel_gcc_patch
* __Liquorix__, kernel for desktop, multimedia, and gaming workloads: https://liquorix.net/sources/
2018-03-03 20:37:40 +00:00
* __linux-tiny__, reduce the memory and disk footprint (only for 2.6 and down, not updated): https://elinux.org/Linux_Tiny
2018-03-03 20:24:22 +00:00
* __Linux-libre__, remove non-free, obfuscated or obscured software included without source code: http://linux-libre.fsfla.org/pub/linux-libre/releases/
* __pf-kernel__, patch set improving interactiveness and performance, includes __Kernel_gcc_patch__, __BFQ__ and others: https://pfactum.github.io/pf-kernel/
2018-03-03 08:21:00 +00:00
2018-03-03 19:42:53 +00:00
### PRELIMINARY STEPS
2018-03-03 08:35:27 +00:00
* Go to kernel source tree.
2018-03-03 08:21:00 +00:00
`cd /usr/src/linux-<VERSION_NUMBER>`
* Download the patch file there.
2018-03-03 19:49:44 +00:00
* Following instructions assume is __gzipped__.
2018-03-03 08:21:00 +00:00
2018-03-03 09:41:19 +00:00
You have three options to patch the kernel.
2018-03-03 09:16:25 +00:00
### OPTION 1: Git
Git can be used to fallback from the patch and revert to the upatched source.
2018-03-03 08:35:27 +00:00
* Uncompress patch file.
2018-03-03 08:21:00 +00:00
`gunzip <PATCH_FILE>.gz`
2018-03-03 08:35:27 +00:00
* Apply patch.
2018-03-03 08:21:00 +00:00
`git am <PATCH_FILE>`
2018-03-03 08:35:27 +00:00
* To revert a single patch.
2018-03-03 08:21:00 +00:00
`git apply -R <PATCH_FILE>`
2018-03-03 08:56:04 +00:00
* To revert several patches first see the __git log__.
2018-03-03 08:21:00 +00:00
`git log --pretty=oneline --abbrev-commit`
2018-03-03 08:35:27 +00:00
* Select the proper number from the first column.
2018-03-03 08:21:00 +00:00
`git reset --hard <FIRST_COLUMN_NUMBER>`
2018-03-03 09:16:25 +00:00
### OPTION 2: Patch command
2018-03-03 08:35:27 +00:00
* Uncompress patch file.
2018-03-03 08:21:00 +00:00
`gunzip <PATCH_FILE>.gz`
2018-03-03 08:56:04 +00:00
* The __-p__ flag specifies a number of leading directories to remove, location is at the top of the patch file filename and is relative to the current directory.
2018-03-03 08:21:00 +00:00
`patch -p1 < <PATCH_FILE>`
2018-03-03 09:30:20 +00:00
### OPTION 3: Patch with zcat
2018-03-03 19:51:29 +00:00
* To use the uncrompressed file directly.
2018-03-03 08:21:00 +00:00
`zcat <PATCH_FILE>.gz | patch -p1`
2018-03-03 19:51:29 +00:00
Compile the kernel with the patch now included.