You are not logged in.

#1 2020-11-09 10:22:09

tigerjack
Member
Registered: 2017-08-20
Posts: 99

[SOLVED] Custom kernel module fails to build

Hi, as per the title, I was trying to compile a simple kernel module using this Makefile

obj-m := helloaxo.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD       := $(shell pwd)

all:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) clean

but I always get the error message

make
make -C /lib/modules/5.9.6-arch1-1/build M=/mnt/internal/LinuxData/prog_mgmt/c_c++/ACSO/kernel modules
make[1]: Entering directory '/usr/lib/modules/5.9.6-arch1-1/build'
make[2]: *** No rule to make target '/mnt/internal/LinuxData/prog_mgmt/c_c++/ACSO/kernel/helloaxo.o', needed by '/mnt/internal/LinuxData/prog_mgmt/c_c++/ACSO/kernel/helloaxo.mod'.  Stop.
make[1]: *** [Makefile:1784: /mnt/internal/LinuxData/prog_mgmt/c_c++/ACSO/kernel] Error 2
make[1]: Leaving directory '/usr/lib/modules/5.9.6-arch1-1/build'
make: *** [Makefile:6: all] Error 2

both linux-headers and base-deval are installed. Also

$>ll -d /usr/lib/modules/5.9.6-arch1-1/
drwxr-xr-x 5 root root 4.0K Nov  8 19:35 /usr/lib/modules/5.9.6-arch1-1/
$>ll -d /usr/src/linux
lrwxrwxrwx 1 root root 34 Nov  5 22:00 /usr/src/linux -> ../lib/modules/5.9.6-arch1-1/build

Any idea on what I am doing wrong?

Last edited by tigerjack (2020-11-10 16:01:55)

Offline

#2 2020-11-09 20:12:47

loqs
Member
Registered: 2014-03-06
Posts: 19,041

Re: [SOLVED] Custom kernel module fails to build

Makefile as listed.  Empty helloaxo.c.  Produces:

make
make -C /lib/modules/5.9.5-1-stable/build M=/tmp/test modules
make[1]: Entering directory '/usr/lib/modules/5.9.5-1-stable/build'
  CC [M]  /tmp/test/helloaxo.o
  MODPOST /tmp/test/Module.symvers
WARNING: modpost: missing MODULE_LICENSE() in /tmp/test/helloaxo.o
  CC [M]  /tmp/test/helloaxo.mod.o
  LD [M]  /tmp/test/helloaxo.ko
make[1]: Leaving directory '/usr/lib/modules/5.9.5-1-stable/build'

Although I would suggest updating the Makefile to follow modules.html,  it does work for me.

Offline

#3 2020-11-10 10:49:02

tigerjack
Member
Registered: 2017-08-20
Posts: 99

Re: [SOLVED] Custom kernel module fails to build

My bad, I didn't know that the source file should have the same name specified by obj-m. After renaming the file to helloaxo.c everything worked.

Regarding the Makefile, following your suggestion, I changed it to

obj-m := helloaxo.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD       := $(shell pwd)
# EXTRAVERSION=-arch1-1

all:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

build:
	# $(MAKE) EXTRAVERSION=$(EXTRAVERSION) --debug=v -C $(KERNELDIR) M=$(PWD) modules
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) clean

Of course, now the make (all) requires sudo to install the module. But on the positive side, I can easily add it using modprobe instead of insmod. Is there any other benefit on using the modules_install?

Offline

#4 2020-11-10 15:47:17

loqs
Member
Registered: 2014-03-06
Posts: 19,041

Re: [SOLVED] Custom kernel module fails to build

How were you installing the module before using modules_install?

Last edited by loqs (2020-11-10 15:47:27)

Offline

#5 2020-11-10 15:49:05

tigerjack
Member
Registered: 2017-08-20
Posts: 99

Re: [SOLVED] Custom kernel module fails to build

loqs wrote:

How were you installing the module before using modules_install?

sudo insmod ./helloaxo.ko

Offline

#6 2020-11-10 15:57:00

loqs
Member
Registered: 2014-03-06
Posts: 19,041

Re: [SOLVED] Custom kernel module fails to build

tigerjack wrote:
loqs wrote:

How were you installing the module before using modules_install?

sudo insmod ./helloaxo.ko

That loads the module from the current location without installing it to one the of the standard locations searched by modprobe.

Offline

#7 2020-11-10 16:01:37

tigerjack
Member
Registered: 2017-08-20
Posts: 99

Re: [SOLVED] Custom kernel module fails to build

loqs wrote:
tigerjack wrote:
loqs wrote:

How were you installing the module before using modules_install?

sudo insmod ./helloaxo.ko

That loads the module from the current location without installing it to one the of the standard locations searched by modprobe.

You're right, the module was just compiled, then I loaded it into the current kernel without installing in the standard directories. Since it was just a simple example, I just didn't need the install procedure at all. But it's always good to know how it's supposed to be done. Thanks again.

Offline

Board footer

Powered by FluxBB