Unable to compile a kernel module - linux-kernel

I am trying to build a kernel module while compiling the kernel image, by changing the config symbol value to `'m'. But I do not see any module file generated. Please let me know if I need to take some extra steps to generated a module. If I change the flag to 'y' the code works fine.
Also, in online tutorials I have seen both of the following
>insmod temp.ko
also,
>insmod temp.o
Which one is correct type for a dynamically loadable module?

For compiling loadable kernel modules in Linux tree apply the following command
make modules
.ko is the proper kernel module extension. If .o is tried with insmod, then Invalid module format error will be displayed.

if your module has dependencies to other modules, then:
make modules to build modules
make modules_install to install them
modprobe temp.ko to load temp module and it's dependencies
if your module is simple and has no dependencies, then:
make modules to build modules
cd /path/to/module
insmod temp.ko

Wasn't using modules target in make.

First, run make menuconfig, search the module you want to build using /. Next choose 'm' or 'y' depending on when you want to compile it as a part of the kernel or build it as a module.
(If you don't want to build the entire kernel and just a single module):
Next, in the linux directory (assuming you are using vanilla kernel) run the following commands which will generate the scripts and required configuration files based on your .config
make prepare
make scripts
Now simply build the module using:
make M=<path/to/module/dir>
Finally you should have a kernel object/module (.ko) in the directory if you have selected m
hope this helps.

Related

How can I prepare a Linux source tree so an external module can be compiled against it?

I am keeping a WIFI driver alive by patching compilation errors for new Kernel versions. I can build it against a source tree, so I do not have to boot the kernel for which I want to fix it.
Unfortunately for this I have to fully compile the entire kernel. I know how to build a small version by using make localmodconfig, but that still takes very long.
Recently, I learned about the prepare target. This allows me to "compile" the module, so I learn about compilation problems. However, it fails in the linking phase, which prevents using make prepare in a Git bisect run. I also had the impression that it requires to clean the source tree from time to time due to spurious problems.
The question is: What is the fastest way to prepare a source tree so I can compile a Wifi module against it?
The target you are looking for is modules_prepare. From the doc:
An alternative is to use the "make" target "modules_prepare." This will make sure the kernel contains the information required. The target exists solely as a simple way to prepare a kernel source tree for building external modules.
NOTE: "modules_prepare" will not build Module.symvers even if CONFIG_MODVERSIONS is set; therefore, a full kernel build needs to be executed to make module versioning work.
If you run make -j modules_prepare (-j is important to execute everything in parallel) it should run pretty fast.
So what you need is basically something like this:
# Prepare kernel source
cd '/path/to/kernel/source'
make localmodconfig
make -j modules_prepare
# Build your module against it
cd '/path/to/your/module/source'
make -j -C '/path/to/kernel/source' M="$(pwd)" modules
# Clean things up
make -j -C '/path/to/kernel/source' M="$(pwd)" clean
cd '/path/to/kernel/source'
make distclean
The last cleaning up step is needed if you are in a bisect run before proceeding to the next bisection step, otherwise you may leave behind unwanted object files that might make other builds fail.

Adding only one driver to linux kernel

I know that on the internet I can find same information about "adding one driver to linux kernel" but I can not get it working.
I need to compile an ADV7800driver. It is based on adv7180 driver code.
I put my driver file (adv7800.c) into /linux_source_folder/drivers/media/platform/mxc/capture folder. I also add
adv7800_converter-objs := adv7800.o
obj-$(CONFIG_MXC_ADV7800_CONVERTER)+=adv7800_converter.o
in makefile in the same folder and add information in Kconfigfile.
Then I go back to /linux_source_folder and do sudo make menuconfig and set adv7800 as a module and save config. Then sudo make defconfig, then sudo make -j4 modules (now waiting about 2 hours) and then sudo make modules install.
As a result I can see every module which I configure in menuconfig but I can not see my own module (only .c file exists).
How can I do this correctly and how can I build only this one module without building others (to same much time) ?
I do not remember exactly what happens in terms of instructions executed, but the idea of defconfig is to set a default configuration for a given architecture/platform.
If, as you said, you run sudo make defconfig after you configure your module to be compiled, most likely you loose your configuration. The defconfig should be executed first (once) and then you customize the configuration.
Regarding the compilation of a single module, I point you to an old answer
How to "make" existing Linux kernel module driver after modifying the driver source code.
One note: you should not use sudo to compile

Using populate_sdk to include kernel headers

How do I include the linux kernel headers as part of the SDK package in Yocto?
I'm using Yocto 1.8 (fido) in an embedded project and want to do out-of-tree kernel module development. Currently, I can build my kernel modules (apart from bitbake) by pointing my $KERNEL_PATH to the poky/build/tmp/work-shared/<machine>/kernel-source/ directory when I run make. I don't want to do this in the long term, however, since others need to easily build the modules without installing and building a full image from bitbake.
I can generate an SDK using bitbake myimage -c populate_sdk. However, this doesn't include the kernel headers (all I ever see is sysroots/<mach>/usr/include/linux). How do I cause the kernel headers to be included in the SDK package? Also, I don't want the kernel headers to show up as part of my target image.
[Edit]
My image recipe is as follows:
EXTRA_IMAGE_FEATURES_append = " eclipse-debug debug-tweaks"
TOOLCHAIN_HOST_TASK_append = " nativesdk-cmake"
IMAGE_INSTALL = "packagegroup-core-boot ${CORE_IMAGE_EXTRA_INSTALL} util-linux kernel-modules netbase busybox base-passwd base-files sysvinit initscripts bash gdbserver strace sysfsutils dtc gawk ethtool grep sed wget iptables oprofile net-tools dropbear rsync stress-ng rt-tests i2c-tools"
inherit core-image
The kernel I'm using is linux-altera-ltsi-rt in the meta-altera layer.
From the fido release, the handling of kernel builds has been changed. In previous releases, you could usually just skip down to the usage example below.
In fido or any 1.8+, if you want the kernel src and build system available in the SDK, you should add
TOOLCHAIN_TARGET_TASK_append = " kernel-devsrc"
to your image recipe. That will ensure that the new, kernel-devsrc package is installed into your toolchain.
The procedure below is just to ensure that the rest of the workflow is fully understood (even though it's not strictly part of the original question).
Usage Example
Lets assume a module Makefile as follows:
obj-m += hello-1.o
all:
make -C $(KERNEL_SRC) M=$(PWD) modules
clean:
make -C $(KERNEL_SRC) M=$(PWD) clean
Example taken from The Linux Kernel Module Programming Guide (Note that the actual commands needs to have a tab character for indentation).
Then you'll have to define KERNEL_SRC to be sysroots/<mach>/usr/src/kernel/, either in the Makefile, or from your make call. (Using a variable like KERNEL_SRC will ensure that your module recipe automatically picks the right location when building using bitbake).
To manually build your kernel module:
Source the environment-* file for your SDK.
Go to you modules directory.
KERNEL_SRC=<sdk-install-path>/sysroots/<mach>/usr/src/kernel LDFLAGS="" make However, this will fail, as fixdep can't be found. We'll fix this manually.
cd <sdk-install-path>/sysroots/<mach>/usr/src/kernel
make modules_prepare
If this needs to be run with sudo, be sure to source the environment file in the sudo environment: sudo bash -c "source <sdk-install-path>/environment-setup-<mach> && make modules_prepare"
Go back to your modules directory.
KERNEL_SRC=<sdk-install-path>/sysroots/<mach>/usr/src/kernel LDFLAGS="" make
This should now allow you to build your modules.
If you don't have the kernel source under sysroots/<mach>/usr/src/kernel/, we'll have to look into that.
anders answer is very good, but in recent versions of yocto the way to add kernel-devsrc seems to be
IMAGE_INSTALL += "kernel-devsrc"
which I found here: https://www.mail-archive.com/yocto#yoctoproject.org/msg36448.html
With Yocto Zeus (3.0.x) add this to your image recipe:
TOOLCHAIN_TARGET_TASK += "kernel-devsrc"
EDIT: Same for Gatesgarth (3.2.x) but the make scripts command has a new host dependency to libyaml-dev

Kernel Version Error, insmod fails

I am running with kernel version-2.6.35
When I hit uname -r it gives as 2.6.35-22-generic
Compiled a module from Kernel-2.6.35 source tree,
But it fails to insert the module in my running kernel.
I don't have any clue.
can anybody help me out of this !!
Thank you.
Have to compile LKM against the correct kernel version i.e. output of uname -r. In your case you have downloaded the kernel version-2.6.35 source tree and compiled your LKM against it. While inserting LKM, checks for the KERNEL_VERSION, if they match will not get any errors while module insertion but if they mismatch fails to insert the module.
You want to ensure that CONFIG_MODVERSIONS is enable in the running Kernel, 2.6.35-22-generic in your case. When you the build a Kernel Module from the 2.6.35 sources the running Kernel will allow modules with matching symbols to be loaded or if symbols are missing, it'll fail to load.
Not having CONFIG_MODVERSIONS enabled means that you MUST match the version between the Kernel version and the module.
I am supposing that you are using the official kernel tree, but you are trying to load your module in your distribution. You must you the kernel source/header from your Linux distribution. I am supposing this because of this version 2.6.35-22-generic, -22-generic it is not part of the official version name.

Build kernel module for existing binary kernel

How can one build a kernel module that loads into an exiting binary kernel?
It seems that loading depends on the BuildID stored inside the module, but what is needed to match those? I expect the binary kernel to be the result of some git revision, using a default .config (copied from arch/.../configs). How can I decide which revision and .config is needed to build module that would be accepted by the existing kernel?
It seems that matching the of the ARCH and KERNELRELEASE properties as shown by uname -a is not enough.
You will need the source code for the current kernel you are running. These are usually packaged separately (you don't mention your distro). However once done you should be able to build the external module against that kernel following the instructions: Documentation/kbuild/modules.txt

Resources