The best way to customize your own kernel is get your sources from the officiel Linux Kernel website. It is highly recommended use a stable version:
$ mkdir $HOME/linux-kernel
$ cd $HOME/linux-kernel
$ git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git #linux-next
$ git clone https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
$ git clone https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git
Prerequisites
Before you proceed, you must install some programs that will let you customize and comiple it. On Debin-based systems, you need the build-essential package, among others:
# apt-get install build-essential gcc libncurses5-dev libssl-dev
The libncurses5-dev package will make it easier to configure the kernel, so make sure to install it. Other Linux operating systems will have a similar program, but this may be under a different name. For example, if you’re using Fedora, it’s called ncurses.
If it’s not installed already, you’ll also need the GNU Compiler Collection (GCC). This is the tool responsible for translating the raw source code into something understandable to computers. That is to say, compiling.
Kernel Configuration File
Before compiling the kernel, you need to tell GCC how you want it done. You do this using a command line tool called make. Type in this command:
$ make localmodconfig
This creates a file called .config, which tells GCC what to compile from the kernel sources. The make localmodconfig command detects currently running kernel components, and marks them for compilation. Create a config based on current config and loaded modules (lsmod). IMPORTANT: If the kernel is inside a folder you own, you won’t need sudo for it to work.
NOTE: This option localyesconfig is similar to localmodconfig, except it will convert all module options to built in (=y) options.
You may come across some messages questions about add some features. Just press the Enter key to skip them. They’re usually just new kernel features.
However, localmodconfig is not perfect! If you’re not currently using some parts of your computer, it may not detect all the things it supports. As such, you need to enable them manually. Alternatively, you can skip this step entirely and use the next command instead. This will compile the new kernel with the same options as the one you’re currently using.
If you’d prefer that, but used the localmodconifg command before, do this:
$ make clean
This will give you a clean build. You should also use this command if you’ve compiled a kernel with these sources before.
Further Configuration
Inside the kernel source folder, there’s a file called .config. This is what GCC will use to choose what to compile. Instead of editing it manually (not recommended), you’ll instead use a few terminal tools. This will make selecting compile options much easier.
Having done this, fine tune it further:
$ make nconfig
You’ll see a colorful menu pop up. This is the kernel configuration menu. To navigate around, use the arrow keys. You can press the right arrow key to expand entries with a —> sign next to them. Navigate out of these sub-menus by pressing the left arrow key.
You can toggle menu entries that have the <> or [] sign next to them by pressing the space bar. This will cycle through the different menu options. If you see a * or M inside, that specific kernel component will be compiled. The only difference between them is that the M option will be loaded when it’s needed. This can be useful if you’re compiling a driver for example, that won’t be used often.
If you want to know more about what a specific switch does, press F2 over it. You’ll see a helpful description of what you’re compiling.
Once you’re all done, press the F9 button to save and exit.
IMPORTANT: The option CONFIG_LOCALVERSION_AUTO = y [ General setup ---> (*) Local version - append to kernel release] is very useful. Automatically appends version information to the version string.
Compiling & Installing the Kernel
Now that you’ve created a custom make file, you’ll need to compile the kernel. Type in this command:
$ make -j$(nproc --all)
You will not need sudo if you downloaded the kernel sources off the web. The second part of the command helps speed up the kernel compile time, by taking advantage of all your CPU cores. You may get rid of this, or change the number to something else (e.g. -j2), if you want to use your computer for other tasks without too many hiccups. However, this will also make the compiling slower!
This process can and will take a very long time. The less you chose to compile, the shorter it will be. Even so, you’ll probably need to be patient.
Afterwards, type this command in to finish compiling the rest of the kernel:
# make modules_install
To actually use this kernel, you’ll need to enter this command:
# make install
This will automatically copy the kernel to your /boot folder and generate the appropriate files to make it work.
Switching Kernels Using GRUB
If you reboot after installing your new kernel, your system might not work! This is usually due to an improperly compiled kernel (that is, not supporting your computer well enough). Case in point, my own laptop. Just in case this happens, I recommend editing GRUB so that it can switch back to older kernels. To do this, use this command:
# nano /etc/default/grub
Place a # sign in front of the GRUB_HIDDEN_TIMEOUT and GRUB_HIDDEN_TIMEOUT_QUIET lines. Afterwards, save these changes by running this command:
# grub-mkconfig -o /boot/grub/grub.cfg
Now if you reboot your computer, you’ll be able to switch to an older kernel if your new one doesn’t work. Simply go to the Advanced options menu item and select the kernel you want to boot. If everything works out fine, congratulations! You’ll be using the kernel you compiled by yourself.
TIPS
1. In order to know the release of the kernel you are going to compile:
$ make kernelrelease
scripts/kconfig/conf --silentoldconfig Kconfig
4.11.0
2. In order to save de config file:
$ make savedefconfig
$ cp defconfig arch/x86/configs/x86_64_oscargomezf_defconfig
3. To get all possible targets just run:
$ make help
4. So that check the coding style oa a regular file:
$ ~/linux-kernel/linux/scripts/checkpatch.pl --no-tree -f hello.c
--no-tree : By default checkpatch must be run from the top-level dir. of a kernel tree. This option overrides the default behaviour.
-f : By default checkpatch verifies kernel patches. This option instructs it to treat the file as a regular source file.