How to Find The Version of a Compiled Kernel Module

How to find the version of a compiled kernel module?

$ apropos modinfo
modinfo (8) - display information about a kernel module
$ modinfo cpuid.ko
filename: cpuid.ko
author: H. Peter Anvin <hpa@zytor.com>
description: x86 generic CPUID driver
license: GPL
vermagic: 2.6.37 SMP preempt mod_unload PENTIUM4 gcc-4.4.5
depends:

Getting kernel version from linux kernel module at runtime

By convention, Linux kernel module loading mechanism doesn't allow loading modules that were not compiled against the running kernel, so the "running kernel" you are referring to is most likely is already known at kernel module compilation time.

For retrieving the version string constant, older versions require you to include <linux/version.h>, others <linux/utsrelease.h>, and newer ones <generated/utsrelease.h>. If you really want to get more information at run-time, then utsname() function from linux/utsname.h is the most standard run-time interface.

The implementation of the virtual /proc/version procfs node uses utsname()->release.

If you want to condition the code based on kernel version in compile time, you can use a preprocessor block such as:

#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,16)
...
#else
...
#endif

It allows you to compare against major/minor versions.

Linux Kernel Module development compile for other kernel

I've downloaded kernel version 3.16.xx from https://www.kernel.org/ and then pointed to extracted files from that archive.

You can't compile modules for some kernel if you have only its original source. You need point IDE to configured and partially build kernel. Actually, all files needed to build modules for some kernel version are in kbuild directory of compiled kernel, installed into /lib/modules/version/kbuild. There are linux-kbuild-version (https://packages.debian.org/jessie/kernel/linux-kbuild-3.16) package with some files https://packages.debian.org/jessie/amd64/linux-kbuild-3.16/filelist and linux-headers-version (https://packages.debian.org/jessie/linux-headers-3.16.0-4-amd64) which includes Module.symvers (https://packages.debian.org/jessie/amd64/linux-headers-3.16.0-4-amd64/filelist):

/usr/src/linux-headers-3.16.0-4-amd64/Module.symvers

Can the booted kernel version differ from the version against which a module has been built?

That's why you usually never find prebuilt kernel module distributed somewhere. You have to build kernel module with kernel headers of your running kernel. Common practice is always having the right kernel headers in your /usr/src

Building kernel modules for different linux version

So, building a new kernel is not a good option as it will require :

  • building kernel
  • building modules and firmware
  • building headers
    Moving all of above things in appropriate location (if your machine is not same on which you are going to develop module)

So if you have kernel headers for running system then you dont need to download a source code for any kernel version, and while making module use

make -C /lib/modules/kernel-headers-x.y.z/build M=`pwd` modules

and your module will be ready.

If there would be better answers, i will not hesitate to accept any of them.

Cross Compilation kernel and kernel modules problems

i'm not sure if it possible to compile just some modules or if it's necessary to compile the whole kernel every time

Well, you can compile just single modules, but compiling a module requires the kernel to be already built. Once you do that one time though, you should be able to compile other modules singularly. That is, of course, if you do not wish to embed them in the kernel itself (CONFIG_XXX=y instead of CONFIG_XXX=m). You should be able to compile only the module you want like this (assuming /path/to/linux is the directory where your already built kernel source resides):

$ cd /path/to/linux
$ cd path/to/module/folder
$ make -C /path/to/linux M=$(pwd) modules

I tried to find the .config of my device in order to have a smooth configuration but i don't have the /boot folder and it also was not in /proc. How can i get it?

Where did you look precisely? The presence of /proc/config.gz depends on CONFIG_IKCONFIG_PROC (see also here). If you cannot find the file then it's most likely because that configuration option was disabled when the kernel was built. You may try look under /boot (as you already did), or under /lib/modules/$(uname -r)/build/.config, but unfortunately there's not much else to do otherwise.

I've seen people suggest trying to run modprobe configs and then check /proc/config.gz, but that seems strange since as far as I know the kernel config shouldn't be configurable to be available as a loadable module.

What do you suggest me to do?

Well, the most important thing you want right now is to find the configuration file for your router (or a compatible one). If you cannot find that, it will be pretty hard to get everything right. You might want to search for OpenWRT versions available for your router (if any), or really anywhere else on the internet as long as you can find a suitable configuration. Include your router brand and/or model in your searches. StackOverflow can't really help you that much about this though.

You can try cross-compiling a 5.4 kernel with default config plus the module you want. For example, assuming you have the right cross-compilation toolchain ready:

cd /path/to/linux
make ARCH=mips CROSS_COMPILE=your-cross-toolchain-prefix- defconfig
make ARCH=mips CROSS_COMPILE=your-cross-toolchain-prefix- menuconfig

# ... enable the module, tune the config ...

make -j ARCH=mips CROSS_COMPILE=your-cross-toolchain-prefix-

In any case, consider the fact that jumping from a 2.6 to a 5.4 kernel is a pretty big change, and it's likely to end up breaking everything, so be sure to make a backup of your router's firmware before trying anything.



Related Topics



Leave a reply



Submit