0

I am working on building a test kernel module in Buildroot for a Raspberry Pi target. My host machine is x86, running Ubuntu 22.04.1. I have been following along with several tutorials, including this one and this one and this one.

My buildroot system boots, and I can see my compiled kernel module at /lib/modules/5.15.92-v7l/extra/kernelmoduletest.ko.xz.

If I attempt to run insmod from the home directory, my module cannot be found. But, if I navigate to where it is installed and run insmod, I get 'invalid module format'.

# pwd
/root
# insmod kernelmoduletest.ko.xz 
insmod: can't insert 'kernelmoduletest.ko.xz': No such file or directory
# cd /lib/modules/5.15.92-v7l/extra/
# ls
dtbocfg.ko.xz           kernelmoduletest.ko.xz
# insmod kernelmoduletest.ko.xz 
insmod: can't insert 'kernelmoduletest.ko.xz': invalid module format

Looking into the invalid module format error seems to suggest that the kernel module was compiled for the wrong kernel?

I ran dmesg to get more info, and I found this:

[    3.820114] udevd[136]: starting version 3.2.11
[    3.852896] udevd[137]: starting eudev-3.2.11
[    4.497392] bcmgenet fd580000.ethernet: configuring instance for external RGMII (RX delay)
[    4.507484] bcmgenet fd580000.ethernet eth0: Link is Down
[    8.635423] bcmgenet fd580000.ethernet eth0: Link is Up - 1Gbps/Full - flow control rx/tx
[   33.754917] cam-dummy-reg: disabling
[   66.809984] Invalid ELF header magic: != ELF
[   66.814488] Invalid ELF header magic: != ELF

How do I compile my module correctly so it will run with insmod?


Here is my setup information for my kernel module, called kernelmoduletest:

buildroot/package/kernelmoduletest/Config.in:

config BR2_PACKAGE_KERNELMODULETEST
    bool "kernelmoduletest"
    help
        kernelmoduletest package.

        Kernel module test. Just a hello world package that runs as a kernel module.

buildroot/package/kerneltestmodule/kernelmoduletest.mk:

################################################################################
#
# kernelmoduletest package
#
################################################################################

KERNELMODULETEST_VERSION = 1.0
KERNELMODULETEST_SITE = package/kernelmoduletest/src
KERNELMODULETEST_SITE_METHOD = local# Other methods like git,wget,scp,file etc. are also available.

#KERNELMODULETEST_MODULE_SUBDIRS = driver/base
KERNELMODULETEST_MAKE_OPTS = KVERSION=$(LINUX_VERSION_PROBED)



define KERNEL_MODULE_BUILD_CMDS
        $(MAKE) -C '$(@D)' LINUX_DIR='$(LINUX_DIR)' CC='$(TARGET_CC)' LD='$(TARGET_LD)' modules
endef


#ifeq ($(BR2_PACKAGE_LIBBAR),y)
#KERNELMODULETEST_DEPENDENCIES += libbar
#KERNELMODULETEST_CONF_OPTS += --enable_bar
#KERNELMODULETEST_MODULE_SUBDIRS += driver/bar
#else
#KERNELMODULETEST_CONF_OPTS += --disable_bar
#endif

$(eval $(kernel-module))
$(eval $(generic-package))

buildroot/package/kerneltestmodule/src/kernelmoduletest.c:

#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("GPL");

static int myinit(void)
{
    printk(KERN_INFO "kernelmoduletest hello init\n");
    return 0;
}

static void myexit(void)
{
    printk(KERN_INFO "kernelmoduletest hello exit\n");
}

module_init(myinit)
module_exit(myexit)

buildroot/package/kerneltestmodule/src/Makefile:

obj-m += kernelmoduletest.o
kernelmoduletest.o += -DDEBUG

all:
    #make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C $(KDIR) M=$(PWD) modules
    $(MAKE) -C '/lib/modules/$(shell uname -r)/build' M='$(PWD)' modules

clean:
    #make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C $(KDIR) M=$(PWD) clean
    $(MAKE) -C '$(LINUX_DIR)' M='$(PWD)' clean

1 Answer 1

0

You need to decompress the module first:

xz -d <module_path>

Or just build the kernel without compressing the modules:

make linux-nconfig

Go to Enable loadable module support -> Module compression mode and select None

Now insmod should work. Hope it helps.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.