- Compile kernel module
- Contents
- Build environment
- Traditional compilation
- Arch Build System
- Source configuration
- Module compilation
- out-of-tree module compilation
- Module installation
- possible errors
- Kernel module
- Contents
- Obtaining information
- Automatic module loading with systemd
- Manual module handling
- Setting module options
- Manually at load time using modprobe
- Using files in /etc/modprobe.d/
- Using kernel command line
- Aliasing
- Blacklisting
- Using files in /etc/modprobe.d/
- Using kernel command line
- Troubleshooting
- Modules do not load
- 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
Compile kernel module
Sometimes you may wish to compile Linux’s Kernel module without recompiling the whole kernel.
Contents
Build environment
Firstly you will need to install build dependencies such as compiler ( base-devel ) and linux-headers .
Next you will need to get the source code for the kernel version the module is intended to run on. You may try use newer kernel sources but most likely compiled module will not load.
In case the intended kernel version is the installed kernel, find its version with
There are two main options to acquire the required source. Each option has slightly different usage methods and directory structure.
Traditional compilation
See Kernels/Traditional compilation#Download the kernel source. If you fetch latest source using Git you will need to checkout needed version using tag (eg. v4.1).
Arch Build System
For a general overview on Arch Build System read ABS. See Kernel/Arch Build System for acquiring the kernel source, as well as the directory structure, and other details.
Source configuration
When you have the source code, enter its directory. For the #Arch Build System case, that directory would be src/archlinux-linux/ down from where the PKGBUILD is.
The output from make help is beneficial here. Start by cleaning with
An appropriate .config file is now required. If no config file is to be seen nearby, perhaps from a saved .config , and the intended kernel version is the running kernel, you can use its configuration file:
Next ensure the .config file is adjusted for the kernel version. If you are using kernel sources for the exact current version then it should not ask anything. But for another version than the current kernel you might be asked about some options. In any case, for the #Arch Build System option, you might want to examine the PKGBUILD::prepare() function.
If the module you want to compile have some compilation options such as debug build, or it was not compiled before, you can also, possibly must, adjust the kernel configuration. You can do this with one of the many configuration targets mentioned by make help.
Module compilation
In order to compile and load our module cleanly, we must find the value of the EXTRAVERSION component of the current kernel version number so we can match the version number exactly in our kernel source. EXTRAVERSION is a variable set in the kernel top-level Makefile, but the Makefile in a vanilla kernel source will have EXTRAVERSION empty; it is set only as part of the Arch kernel build process. If relevant, the value of the current kernel’s EXTRAVERSION can be found by looking at the output of the uname -r command. In general, the kernel version is the concatenation of three components. Namely, the numeric version, the EXTRAVERSION, and the LOCALVERSION. The numeric version itself is a concatenation of three numbers. If built by a PKGBUILD file, the LOCALVERSION will be taken from the pkgrel variable, prefixed by a hyphen. And the EXTRAVERSION will be the suffix of the pkgver variable, where the period character to the right of the third numeric number of the numeric version is replaced by a hyphen. For example, with the linux package linux 5.5.8.arch1-1 , the LOCALVERSION is -1 . The EXTRAVERSION is -arch1 . The output of uname -r will be 5.5.8-arch1-1 in that example.
Once the EXTRAVERSION value is known, we prepare the source for module compilation:
Alternatively, if you are happy to load modules with modprobe using the —force-vermagic option to ignore mismatches in the kernel version number, you can simply run:
Finally, compile wanted module by specifying its directory name. You can find the module location, thus also its directory name, with modinfo or find.
As a last resort, if nothing else has worked, you can
Which will build all the modules from the kernel configuration.
out-of-tree module compilation
get the official source code of the current running linux kernel as described in Kernel/Arch Build System:
then point to the checked out source when compiling the module:
Module installation
Now after successful compilation you just need to gzip and copy it over for your current kernel.
If you are replacing some existing module you will need to overwrite it (and remember that reinstalling linux will replace it with default module)
Or alternatively, you can place the updated module in the updates folder (create it if it does not already exist).
However if you are adding a new module you can just copy it to extramodules (note, this is just example as btrfs will not get loaded from here)
You need to rebuild the module dependency tree with «depmod» to use installed modules.
If you are compiling a module for early boot (e.g. updated module) which is copied to Initramfs then you must remember to regenerate it with (otherwise your compiled module will not be loaded).
possible errors
If EXTRAVERSION is not set correctly the following errors may occur
adding force-vermagic makes it ignore the version mismatch
Источник
Kernel module
Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system.
To create a kernel module, you can read The Linux Kernel Module Programming Guide. A module can be configured as built-in or loadable. To dynamically load or remove a module, it has to be configured as a loadable module in the kernel configuration (the line related to the module will therefore display the letter M ).
Contents
Obtaining information
Modules are stored in /usr/lib/modules/kernel_release . You can use the command uname -r to get your current kernel release version.
To show what kernel modules are currently loaded:
To show information about a module:
To list the options that are set for a loaded module:
To display the comprehensive configuration of all the modules:
To display the configuration of a particular module:
List the dependencies of a module (or alias), including the module itself:
Automatic module loading with systemd
Today, all necessary modules loading is handled automatically by udev, so if you do not need to use any out-of-tree kernel modules, there is no need to put modules that should be loaded at boot in any configuration file. However, there are cases where you might want to load an extra module during the boot process, or blacklist another one for your computer to function properly.
Kernel modules can be explicitly listed in files under /etc/modules-load.d/ for systemd to load them during boot. Each configuration file is named in the style of /etc/modules-load.d/program.conf . Configuration files simply contain a list of kernel modules names to load, separated by newlines. Empty lines and lines whose first non-whitespace character is # or ; are ignored.
See modules-load.d(5) for more details.
Manual module handling
Kernel modules are handled by tools provided by kmod package. You can use these tools manually.
To load a module:
To load a module by filename (i.e. one that is not installed in /usr/lib/modules/$(uname -r)/ ):
To unload a module:
Setting module options
To pass a parameter to a kernel module, you can pass them manually with modprobe or assure certain parameters are always applied using a modprobe configuration file or by using the kernel command line.
Manually at load time using modprobe
The basic way to pass parameters to a module is using the modprobe command. Parameters are specified on command line using simple key=value assignments:
Using files in /etc/modprobe.d/
Files in /etc/modprobe.d/ directory can be used to pass module settings to udev, which will use modprobe to manage the loading of the modules during system boot. Configuration files in this directory can have any name, given that they end with the .conf extension. The syntax is:
Using kernel command line
If the module is built into the kernel, you can also pass options to the module using the kernel command line. For all common bootloaders, the following syntax is correct:
Simply add this to your bootloader’s kernel-line, as described in Kernel Parameters.
Aliasing
Aliases are alternate names for a module. For example: alias my-mod really_long_modulename means you can use modprobe my-mod instead of modprobe really_long_modulename . You can also use shell-style wildcards, so alias my-mod* really_long_modulename means that modprobe my-mod-something has the same effect. Create an alias:
Some modules have aliases which are used to automatically load them when they are needed by an application. Disabling these aliases can prevent automatic loading but will still allow the modules to be manually loaded.
Blacklisting
Blacklisting, in the context of kernel modules, is a mechanism to prevent the kernel module from loading. This could be useful if, for example, the associated hardware is not needed, or if loading that module causes problems: for instance there may be two kernel modules that try to control the same piece of hardware, and loading them together would result in a conflict.
Some modules are loaded as part of the initramfs. mkinitcpio -M will print out all automatically detected modules: to prevent the initramfs from loading some of those modules, blacklist them in a .conf file under /etc/modprobe.d and it shall be added in by the modconf hook during image generation. Running mkinitcpio -v will list all modules pulled in by the various hooks (e.g. filesystems hook, block hook, etc.). Remember to add that .conf file to the FILES array in /etc/mkinitcpio.conf if you do not have the modconf hook in your HOOKS array (e.g. you have deviated from the default configuration), and once you have blacklisted the modules regenerate the initramfs, and reboot afterwards.
Using files in /etc/modprobe.d/
Create a .conf file inside /etc/modprobe.d/ and append a line for each module you want to blacklist, using the blacklist keyword. If for example you want to prevent the pcspkr module from loading:
However, there is a workaround for this behaviour; the install command instructs modprobe to run a custom command instead of inserting the module in the kernel as normal, so you can force the module to always fail loading with:
This will effectively blacklist that module and any other that depends on it.
Using kernel command line
You can also blacklist modules from the bootloader.
Simply add module_blacklist=modname1,modname2,modname3 to your bootloader’s kernel line, as described in Kernel parameters.
Troubleshooting
Modules do not load
In case a specific module does not load and the boot log (accessible by running journalctl -b as root) says that the module is blacklisted, but the directory /etc/modprobe.d/ does not show a corresponding entry, check another modprobe source folder at /usr/lib/modprobe.d/ for blacklisting entries.
A module will not be loaded if the «vermagic» string contained within the kernel module does not match the value of the currently running kernel. If it is known that the module is compatible with the current running kernel the «vermagic» check can be ignored with modprobe —force-vermagic .
Источник
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.
Источник