- Kernel/Traditional compilation
- Contents
- Preparation
- Install the core packages
- Create a kernel compilation directory
- Download the kernel source
- Unpack the kernel source
- Kernel configuration
- Default Arch configuration
- Advanced configuration
- Compilation
- Installation
- Install the modules
- Copy the kernel to /boot directory
- Make initial RAM disk
- Automated preset method
- Manual method
- Copy System.map
- Bootloader configuration
- Howto: Linux Add or Remove a Linux Kernel Modules / Drivers
- Task: Add a Module (driver) Called foo
- Find out info about loaded module
- Task: List all loaded modules
- Task: Remove a module called foo
Kernel/Traditional compilation
This article is an introduction to building custom kernels from kernel.org sources. This method of compiling kernels is the traditional method common to all distributions. It can be, depending on your background, more complicated than using the Kernels/Arch Build System. Consider the Arch Build System tools are developed and maintained to make repeatable compilation tasks efficient and safe.
Contents
Preparation
It is not necessary (or recommended) to use the root account or root privileges (i.e. via Sudo) for kernel preparation.
Install the core packages
Install the base-devel package group, which contains necessary packages such as make and gcc . It is also recommended to install the following packages, as listed in the default Arch kernel PKGBUILD: xmlto , kmod , inetutils , bc , libelf , git , cpio , perl , tar , xz .
Create a kernel compilation directory
It is recommended to create a separate build directory for your kernel(s). In this example, the directory kernelbuild will be created in the home directory:
Download the kernel source
Download the kernel source from https://www.kernel.org. This should be the tarball ( tar.xz ) file for your chosen kernel.
It can be downloaded by simply right-clicking the tar.xz link in your browser and selecting Save Link As. , or any other number of ways via alternative graphical or command-line tools that utilise HTTP, TFTP, Rsync, or Git.
In the following command-line example, wget has been installed and is used inside the
/kernelbuild directory to obtain kernel 4.8.6:
You should also verify the correctness of the download before trusting it. First grab the signature, then use that to grab the fingerprint of the signing key, then use the fingerprint to obtain the actual signing key:
Note the signature was generated for the tar archive (i.e. extension .tar ), not the compressed .tar.xz file that you have downloaded. You need to decompress the latter without untarring it. Verify that you have xz installed, then you can proceed like so:
Do not proceed if this does not result in output that includes the string «Good signature».
If wget was not used inside the build directory, it will be necessary to move the tarball into it, e.g.
Unpack the kernel source
Within the build directory, unpack the kernel tarball:
To finalise the preparation, ensure that the kernel tree is absolutely clean; do not rely on the source tree being clean after unpacking. To do so, first change into the new kernel source directory created, and then run the make mrproper command:
Kernel configuration
This is the most crucial step in customizing the default kernel to reflect your computer’s precise specifications. Kernel configuration is set in its .config file, which includes the use of Kernel modules. By setting the options in .config properly, your kernel and computer will function most efficiently.
You can do a mixture of two things:
- Use the default Arch settings from an official kernel (recommended)
- Manually configure the kernel options (optional, advanced and not recommended)
Default Arch configuration
This method will create a .config file for the custom kernel using the default Arch kernel settings. If a stock Arch kernel is running, you can use the following command inside the custom kernel source directory:
Otherwise, the default configuration can be found online in the official Arch Linux kernel package.
Advanced configuration
There are several tools available to fine-tune the kernel configuration, which provide an alternative to otherwise spending hours manually configuring each and every one of the options available during compilation.
Those tools are:
- make menuconfig : Command-line ncurses interface superseded by nconfig
- make nconfig : Newer ncurses interface for the command-line
- make xconfig : User-friendly graphical interface that requires packagekit-qt5 to be installed as a dependency. This is the recommended method — especially for less experienced users — as it is easier to navigate, and information about each option is also displayed.
- make gconfig : Graphical configuration similar to xconfig but using gtk. This requires gtk2 , glib2 and libgladeAUR .
The chosen method should be run inside the kernel source directory, and all will either create a new .config file, or overwrite an existing one where present. All optional configurations will be automatically enabled, although any newer configuration options (i.e. with an older kernel .config ) may not be automatically selected.
Once the changes have been made save the .config file. It is a good idea to make a backup copy outside the source directory. You may need to do this multiple times before you get all the options right.
If unsure, only change a few options between compilations. If you cannot boot your newly built kernel, see the list of necessary config items here.
Running lspci -k # from liveCD lists names of kernel modules in use. Most importantly, you must maintain cgroups support. This is necessary for systemd. For more detailed information, see Gentoo:Kernel/Gentoo Kernel Configuration Guide and Gentoo:Intel#Kernel or Gentoo:Ryzen#Kernel for Intel or AMD Ryzen processors.
Compilation
Compilation time will vary from as little as fifteen minutes to over an hour, depending on your kernel configuration and processor capability. Once the .config file has been set for the custom kernel, within the source directory run the following command to compile:
Installation
Install the modules
Once the kernel has been compiled, the modules for it must follow. First build the modules:
Then install the modules. As root or with root privileges, run the following command to do so:
This will copy the compiled modules into /lib/modules/ — . For example, for kernel version 4.8 installed above, they would be copied to /lib/modules/4.8.6-ARCH . This keeps the modules for individual kernels used separated.
Copy the kernel to /boot directory
The kernel compilation process will generate a compressed bzImage (big zImage) of that kernel, which must be copied to the /boot directory and renamed in the process. Provided the name is prefixed with vmlinuz- , you may name the kernel as you wish. In the examples below, the installed and compiled 4.8 kernel has been copied over and renamed to vmlinuz-linux48 :
Make initial RAM disk
If you do not know what making an initial RAM disk is, see Initramfs on Wikipedia and mkinitcpio.
Automated preset method
An existing mkinitcpio preset can be copied and modified so that the custom kernel initramfs images can be generated in the same way as for an official kernel. This is useful where intending to recompile the kernel (e.g. where updated). In the example below, the preset file for the stock Arch kernel will be copied and modified for kernel 4.8, installed above.
First, copy the existing preset file, renaming it to match the name of the custom kernel specified as a suffix to /boot/vmlinuz- when copying the bzImage (in this case, linux48 ):
Second, edit the file and amend for the custom kernel. Note (again) that the ALL_kver= parameter also matches the name of the custom kernel specified when copying the bzImage :
Finally, generate the initramfs images for the custom kernel in the same way as for an official kernel:
Manual method
Rather than use a preset file, mkinitcpio can also be used to generate an initramfs file manually. The syntax of the command is:
- -k ( —kernel ): Specifies the modules to use when generating the initramfs image. The name will be the same as the name of the custom kernel source directory (and the modules directory for it, located in /usr/lib/modules/ ).
- -g ( —generate ): Specifies the name of the initramfs file to generate in the /boot directory. Again, using the naming convention mentioned above is recommended.
For example, the command for the 4.8 custom kernel installed above would be:
Copy System.map
The System.map file is not required for booting Linux. It is a type of «phone directory» list of functions in a particular build of a kernel. The System.map contains a list of kernel symbols (i.e function names, variable names etc) and their corresponding addresses. This «symbol-name to address mapping» is used by:
- Some processes like klogd, ksymoops, etc.
- By OOPS handler when information has to be dumped to the screen during a kernel crash (i.e info like in which function it has crashed).
If your /boot is on a filesystem which supports symlinks (i.e., not FAT32), copy System.map to /boot , appending your kernel’s name to the destination file. Then create a symlink from /boot/System.map to point to /boot/System.map- :
After completing all steps above, you should have the following 3 files and 1 soft symlink in your /boot directory along with any other previously existing files:
- Kernel: vmlinuz-
- Initramfs: Initramfs- .img
- System Map: System.map-
- System Map kernel symlink
Bootloader configuration
Add an entry for your new kernel in your bootloader’s configuration file. See Arch boot process#Feature comparison for possible boot loaders, their wiki articles and other information.
Источник
Howto: Linux Add or Remove a Linux Kernel Modules / Drivers
=> Under MS-Windows you use term device driver for modules.
=> Under Linux you use term modules for device drivers.
Tutorial details | |
---|---|
Difficulty level | Advanced |
Root privileges | Yes |
Requirements | modprobe/lsmod/modinfo utilities |
Est. reading time | N/A |
=> The Linux kernel has a modular design.
=> At boot time, only a minimal resident kernel is loaded into memory.
=> If you add new hardware you need to add driver i.e. modules.
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
=> The modprobe command intelligently adds or removes a module from the Linux kernel
=> Usually, all Linux kernel modules (drivers) are stored in the module directory located that /lib/modules/$(uname -r) directory. To see current modules, type:
$ ls /lib/modules/$(uname -r)
Output:
Use the following command to list all drivers for various devices:
$ ls /lib/modules/$(uname -r)/kernel/drivers/
Sample outputs:
Fig.01: Device drivers on my Linux based system
Task: Add a Module (driver) Called foo
Type the following command as root user:
# modprobe foo
In this example, I am loading a module called i8k, enter:
# modprobe -v i8k
Sample outputs:
Find out info about loaded module
You need to use the modinfo command to see information about a Linux Kernel module. The syntax is:
# modinfo -v
# modinfo i8k
Sample outputs:
Fig.02: Displaying information about a Linux Kernel module called i8k
Task: List all loaded modules
Use the lsmod command to show the status of modules in the Linux Kernel:
# lsmod
Sample outputs:
Task: Remove a module called foo
Pass the -r option to modprobe command to remove a module, type:
# modprobe -r foo
You can also use the rmmod command, which is simple program to remove a module from the Linux Kernel:
# rmmod foo
Recommended readings
- man pages – modinfo, lsmod, insmod, and modprobe
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
when the system is rebooted the module inside the kernel will not be present.But i want the modules to be seen permenently.what should i do.
Thanks alot for this, very helpful for teh newbz.
Thank’s first for the helpfull command , where can i get the new modules other then my OS,
how to build linux kernel module with new device driver module during build
Thanks a lot for the info…:) 🙂
Hi there…
I am trying to remove TCP IP from a linux kernel, and want to recompile the LINUX kernel. But being a novice with the administrations of the linux (UBUNTU 10.4), I know a little about it. Agter recompilation can I again design mu own TCPIP using the C language code?
Guys please help me out…waiting for the reply .
Regards…
Thanks for this very short but very clear information. That helps me to understand the concept (of add or remove module on Linux) very much.
One question remain: how do we check to know what modules are available to add on a existing system?
Thank you in advance!
One question remain: how do we check to know what modules are available to add on a existing system?
cd to /lib/modules/$(uname -r) directory and you can see the list of available modules (run as root):
The following will list all drives
To find out more info about a module called foo:
Hope this helps!
Hi
thanks for your comments, I am trying to write a printer driver for linux, what should I do?
Please help me on this task.
Does anyone knows step by step guide for how to install ip_conntrack support into kernel Linux linux 2.6.35.14-106.fc14.i686 #1 SMP Wed Nov 23 13:57:33 UTC 2011 i686 i686 i386 GNU/Linux
I am unable to remove the following modules after issuing the commands.
$ rmmod usbhid and
$rmmod hid..
After issuing the commands the modules are not shown in “lsmod” but as soon as a device is added they again get loaded.
thanks, very clear article.
Hello there. I have an old computer Celeron, 128MB RAM and 28MB of VGA. What I want to do is keep the drivers that are needed for my system. Like if I use the Realtek Chipset for Network Card why should the Atheros driver be present on the system? Is there any way to accomplish this task?
Regards.
I have a serious problem, my Linux does not have these comands:
apt-get
uname
modprobe
mknod
man
…etc… and also has not a lot of typical commands
And there is no /dev/loop*
And mount -o loop blablabla bleble say incorrect option, does not undertand loop.
Of course, all problem seem that Kernel has no loop device support.
How can i fix it?
Please have in mind Kernel is on ROM (a chip not writteable), it is not a flasheable chip, i can not modify it on any way, … read only memory chip!!
I wish if it could be possible to add loop device support at run time as a module…
But it does not have modprobe command… neither a lot of clasic Linux commands… so i got to fail.
Need some help, i am not an expert.
Step 1: try to create /dev/loop0 (it does not exists) with mknod but mknod command not found
Try to add such coomands with apt-get, wget, etc… all says such commands not exists
I am getting mad…
Please note it is an ARM processor based, and Kernel is on a ReadOnly chip not flashable.
Thanks in advance for any help… i am getting really mad…
If I were you, rather than finding Linux Kernel modulos to have “uname”, “modprobe”, “man” recovered (maybe you have played around some kernel rebuild and screwed up some basic binaries?), I will re-install the entire Linux OS from scratch. You can download CentOS (Red Hat) or SUSE, Fedora or whatever Linux to have all these basic utilities included.
trying to add slcan module to kernel 2.6.32-504.30.3.el6.i686 but having problems with the following response … question is how to get it added
FATAL: Module slcan not found.
Thank you so much…
i want to know how many mouldes in linus and brief explanation of them.i will glad if my question is been answered.
hi all,
iam unable to remove nvme module in primary drive(ssd) by using
following comment:
rmmod nvme
Источник