How (recipe) to build only one kernel module?
The recipe to build a custom module might need to be split in three sections.
Setup once
$ cd ~
$ apt-get source linux-source-3.13.0
I am too lazy to copy the mvsas specific driver source files; just copy them all to your current working directory. If apt-get
results in an error message about missing source URIs then see note #4 at the bottom.
$ cd linux-3.13.0
$ make oldconfig
$ make prepare
$ make scripts
This will prepare some files necessary to build a kernel module.
Each kernel version
$ apt-get install linux-headers-$(uname -r)
This will install headers and the Ubuntu kernel configuration file for that kernel version in /lib/modules.
$ cd ~/linux-3.13.0
$ cp -v /usr/src/linux-headers-$(uname -r)/Module.symvers .
This to prevent the message "no symbol version for module_layout" when loading the module with insmod or modprobe.
$ mv -v /lib/modules/$(uname -r)/kernel/drivers/scsi/mvsas/mvsas.ko /lib/modules/$(uname -r)/kernel/drivers/scsi/mvsas/mvsas.ko.backup
This will rename the original (Ubuntu build) kernel module to make sure that the custom patched one will load.
Each edit
$ cd ~/linux-3.13.0/drivers/scsi/mvsas
$ nano mv_sas.h
$ nano mv_sas.c
These are for the edits.
$ make -C /lib/modules/$(uname -r)/build M=$(pwd) modules
This will compile and build the kernel module .ko
file using the kernel configuration from your stock Ubuntu distribution as stored in /lib/modules/$(uname -r)/
.
$ make -C /lib/modules/$(uname -r)/build M=$(pwd) modules_install
This will install the kernel module in /lib/modules/$(uname -r)/extra/
, not overwriting the distribution module in case you didn't rename the distribution kernel module file. In this mvsas case it will also run depmod.
$ lsmod | grep mvsas
If this results in any output, the mvsas module needs unloading with (modprobe -r mvsas
) first.
$ sudo modprobe -v mvsas
This should load the new kernel module.
Check the output to verify that /lib/modules/.../extra/mvsas.ko
is being loaded.
Modprobe error: could not insert
In some cases you might experience a modprobe: ERROR: could not insert 'xyz': Unknown symbol in module, or unknown parameter (see dmesg)
while in the verbose modprobe output you see that insmod
is trying to load the module from the kernel default location. For example:
# insmod /lib/modules/3.17.0-031700rc7-generic/kernel/drivers/scsi/pm8001/pm80xx.ko
modprobe: ERROR: could not insert 'pm80xx': Unknown symbol in module, or unknown parameter (see dmesg)
In that case you need to manually run depmod and try to load the module again:
# depmod
# sudo modprobe -v mvsas
Notes
- It may be the case that the resulting
.ko
module files are much (for example 20 times) larger in size than the original module files as distributed by Ubuntu; in that case themake prepare
step could have created a Linux developers debugging kernel configuration file and you are building from the source directory. Your-C
param might not function as expected. - I have seen guides with other commands like
make modules_prepare
andmake M=scripts/mod
but I don't think these are necessary for this case. - You can use the Linux developers debug config by replacing
-C /lib/modules/$(uname -r)/build
with-C /usr/src/linux-headers-$(uname -r)
- In a default setup,
apt-get source linux-sources
will return errorE: You must put some 'source' URIs in your sources.list
. To fix this issue you can modify file/etc/apt/sources.list
by uncommenting (removing the leading#
from) the firstdeb-src
line. Example for Ubuntu 17.10:deb-src http://ie.archive.ubuntu.com/ubuntu/ artful main restricted
. runsudo apt-get update
, and then the command will deliver sources for you. See also this question where a GUI method for doing this is described too.
Related videos on Youtube
Pro Backup
Updated on September 18, 2022Comments
-
Pro Backup over 1 year
I have a bug in a Linux kernel module that causes the stock Ubuntu 14.04 kernel to oops (crash).
That is why I want to edit/patch the source of only that single kernel module to add some extra debug output. The kernel module in question is
mvsas
and not necessary to boot. For that reason I don't see any need to update any initrd images.I have read a lot of information (as shown below) and find the setup and build process confusion. I need two recipes:
- to setup/configure the build environment once
- steps to do after editing any source file of this kernel module (
.c
and.h
) and converting that edit into a new kernel module (.ko
)
The sources that have been used are:
- build one kernel module - Google search
- http://www.linuxquestions.org/questions/linux-kernel-70/rebuilding-a-single-kernel-module-595116/
- https://stackoverflow.com/questions/8744087/how-to-recompile-just-a-single-kernel-module
- http://www.pixelbeat.org/docs/rebuild_kernel_module.html
- How do I build a single in-tree kernel module?
- http://ubuntuforums.org/showthread.php?t=1153067
- http://ubuntuforums.org/showthread.php?t=2112166
- http://ubuntuforums.org/showthread.php?t=1115593
- build one kernel module ubuntu - Google search
- 'make +single +kernel +module' - Ask Ubuntu
- 'make +kernel +module' - Ask Ubuntu
- My makefile results in: No rule to make target `arch/x86/tools/relocs.c', needed
- '"Invalid module format"' - Ask Ubuntu
- Driver installation: compiling source code for newer kernel
- Modprobe: 'Invalid nodule format', yet works after insmod
- "Symbol version dump" "is missing" - Google search
- https://stackoverflow.com/questions/9425523/should-i-care-that-the-symbol-version-dump-is-missing-how-do-i-get-one
- https://askubuntu.com/questions/369051/where-can-i-find-the-corresponding-module-symvers-and-config-files-for-ubutunu
- "no symbol version for module_layout" when trying to load usbhid.ko
- Broken links inside Linux header file folder
- 'make modules_install' - Ask Ubuntu
- 'modules_install' - Ask Ubuntu
- Empty build directory in custom compiled kernel
- https://askubuntu.com/questions/444345/not-able-to-see-pr-info-output
- https://askubuntu.com/questions/472496/in-which-directory-are-the-kernel-source-files-and-how-can-i-recompile-it
- How can I compile and install that patched libata-eh.c file?
- 'modules_install +depmod' - Ask Ubuntu
- modules_install depmod - Google search
- "make modules_install" - Google search
- http://www.csee.umbc.edu/courses/undergraduate/CMSC421/fall02/burt/projects/howto_build_kernel.html
- https://unix.stackexchange.com/questions/20864/what-happens-in-each-step-of-the-linux-kernel-building-process
- https://wiki.ubuntu.com/KernelCustomBuild
- http://www.cyberciti.biz/tips/build-linux-kernel-module-against-installed-kernel-source-tree.html
- http://www.linuxforums.org/forum/kernel/170617-solved-make-modules_install-different-path.html
- "make prepare" - Google search
- "make prepare" "scripts/kconfig/conf --silentoldconfig Kconfig" - Google search
- http://ubuntuforums.org/showthread.php?t=1963515
- ubuntu "make prepare" version - Google search
- https://stackoverflow.com/questions/8276245/how-to-compile-a-kernel-module-against-a-new-source
- https://help.ubuntu.com/community/Kernel/Compile
- How do I compile a kernel module?
- How to add a custom driver to my kernel?
- https://askubuntu.com/questions/426549/compile-and-loading-kernel-module-without-compiling-the-kernel
-
Ciro Santilli OurBigBook.com almost 8 yearsPossible duplicate of How do I build a single in-tree kernel module?
-
Oleg Kokorin about 6 yearsperhaps this article will help too: stackoverflow.com/questions/8744087/…
-
Dr.jacky over 8 yearsI got error: /bin/sh: arm-none-linux -gnueabi-gcc: not found
-
Albus Dumbledore over 8 years
$(uname-r)
is apparently wrong... You need to call theshell
built-in:$(shell uname -r)
-
Pro Backup about 8 years@AlbusDumbledore I also see cases where
$(shell uname -r)
doesn't work. Why is$(uname -r)
so apparently wrong? -
Martin Pecka over 7 yearsI had to also call
echo "search extra built-in" | sudo tee /etc/depmod.d/00-extra.conf
so thatdepmod
sees the newly updated file in.../extra
. -
Envek almost 7 yearsWhat to do with module signing error, how to avoid it? ``` INSTALL /home/envek/linux-4.10.0/drivers/hwmon/dell-smm-hwmon.ko At main.c:158: - SSL error:02001002:system library:fopen:No such file or directory: bss_file.c:175 - SSL error:2006D080:BIO routines:BIO_new_file:no such file: bss_file.c:178 sign-file: certs/signing_key.pem: No such file or directory ```
-
Tooniis over 5 yearsI get this when I try to build the
radeon
module: askubuntu.com/questions/1093718/… -
Tooniis about 5 yearsI have sorted out the problem above. Now I'm stuck here: askubuntu.com/q/1135475/632192
-
G Huxley about 5 yearsThis didn't work for building the multiple ko's in drivers/media/v4l2-core because some non-linux-header include dirs are not added to compilation. Doing the following in the root of the source dir makes it work: sudo make SUBDIRS=drivers/media/v4l2-core modules
-
doom over 3 yearsworks until this line :
$ make -C /lib/modules/$(uname -r)/build M=$(pwd) modules_install
, get a lot of errorssign-file: certs/signing_key.pem: No such file or directory
even if cert files have been created with superuser.com/a/1322832 . Seems that it searchescerts
directory from local dir (in my casedrivers/hwmon
) instead of fromlinux-5.8.0
folder -
doom over 3 yearsAlso
../crypto/bio
folder is missing =>SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:76
. Can you help to fix this please ? -
dshepherd about 3 yearsTo fix the crypto problem: askubuntu.com/questions/820883/…