Linux kernel module compiling - c

I try to compile simple linux kernel module:
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk("Hello world 1.\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_ALERT "Goodbye world 1.\n");
}
My makefile:
obj-m = testmodule.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
Now i haven't errors in my .c file.
But when i try make in terminal: make: Nothing to be done for `all'.
What's wrong?
Thank you.

The default command in your makefile is
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
That instructs make to cd to /lib/modules/$(KVERSION)/build and run
make module m=YOUR_CURRENT_DIR
In turn, that makefile is not finding anything to do. Presumably, that makefile expects to find some particular structure in your current directory.
You need to more carefully read whatever instructions led you to set up this makefile.

You really don't give enough information to determine the correct answer, but I'll give you some things to look into.
How are you compiling it? Do you have the correct include path in your build? Build paths are specified with the -I option to gcc. Make sure you are pointing to your kernel source tree.
Have you build the kernel? When you do a make, certain things are setup that allows you to build. Doing a make doesn't build everything (like modules) but will get the initial stuff setup for you.

Make sure your have your distributions the kernel header/development packages installed that provide these include files.
If they are installed, search where these include files are located on your computer and add these directories to your compilers include search path (the -I option).

This is because your Makefile contains no tab before giving make command
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
So normally your file should be like below:
obj-m = test.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

Related

What do I need to point my makefile at to make it build a kernel driver properly?

I am working on a simple driver for a class. I am using this makefile:
obj-m := mysourcefile.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/kernel
all default: modules
install: modules_install
modules modules_install help clean:
$(MAKE) -C $(KERNELDIR) M=$(pwd) $#
When I run this using the line:
make
I get the error:
no rule to make target 'modules'. Stop.
I do want to note, my textbook suggests this line:
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
However, I have no "build" directory in my version of Debian (10.6.0-3) (Kernel v. 4.19.0-10-amd64), so I changed the file to point to kernel, since I believe the makefile is supposed to point to the kernel. I don't know if this was the correct choice or not. My active directory IS the directory which contains my source code.
I am unsure of how to proceed from this point.

Problem with Makefile compilation on Ubuntu, -lhidapi-libusb library

I have a slight problem with a Linux module compilation. No matter where I put a -lhidapi-libusb library reference in the make command, the module just refuses to compile. I know I'm doing something wrong, please help me, if you have some time.
Thanks
obj-m += light.o
all:
make -lhidapi -libusb -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -lhidapi -libusb -C /lib/modules/$(shell uname -r)/build M=$(PWD)
Output:
fatal error: hidapi/hidapi.h: No such file or directory
#include <hidapi/hidapi.h>
What you're trying to do doesn't make sense, and will not work.
libusb and HIDAPI are userspace libraries. They cannot be used within a kernel module.
You need to actually install the hidapi header library:
$ sudo apt install libhidapi-dev
https://github.com/libusb/hidapi#installing-hidapi

error in loadable kernel module as make: nothing to be done for all?

The functions you write to provide procfs interfaces is just code that is part of your LKM source.
http://linux.die.net/lkmpg/x769.html has a simple example using procfs, reproduced here:
I copied the code from above link -
You'll find a tutorial for building kernel modules at http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html. The summary of that is:
1) Ensure you have kernel source installed in /usr/src.
2) Create a makefile that looks like:
obj-m = procfs2.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
3) build the module with the command make 4) load the module into memory with the command insmod procfs2.ko (do this as the root user)
I copied the code and created the MAKEFILE and later if I give the make command from the console then it is showing as : make: nothing to be done for all. could someone please tell me what could be the error ??
Here is an example of a Makefile for a kernel module.
the important thing here to note is that the dots shown below must be replaced by a TAB, replacing them by spaces will cause Makefile to malfunction.
obj-m += hello.o
all:
.......make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
.......make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Adding userspace header files to make file

I am building a linux device using make and i need to use string.h in my device. I tried to add /usr/include to make file but it does not work. can any one help me on adding another include path to make file.
my make file is
KBUILD_CFLAGS += -w
obj-m += netlinkKernel.o
all:
make -w -C /lib/modules/$(shell uname -r)/build CPPFLAGS="usr/include/" M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
That's because just usr/include/ isn't a proper compiler flag. You need to use e.g. -I/usr/include/.
However, using libraries from userspace in a kernel driver might not work as you expect it to, like not at all. The kernel should have have it's own string library (including the "string.h" header file) that you should use.
it seems like we can not use standard header files in kernel programming. Thanks for your help

"FATAL: Module not found error" using modprobe

I have a problem with modprobe command... I compiled the hello world module and loaded it with insmod, it works fine and when I do lsmod, I can see it in the output list. But when I insert this module using modprobe I am getting a FATAL error:
root#okapi:/home/ravi# modprobe ./hello.ko
FATAL: Module ./hello.ko not found.
root#okapi:/home/ravi#
Here is the module code:
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
and Makefile
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
The reason is that modprobe looks into /lib/modules/$(uname -r) for the modules and therefore won't work with local file path. That's one of differences between modprobe and insmod.
The best thing is to actually use the kernel makefile to install the module:
Here is are snippets to add to your Makefile
around the top add:
PWD=$(shell pwd)
VER=$(shell uname -r)
KERNEL_BUILD=/lib/modules/$(VER)/build
# Later if you want to package the module binary you can provide an INSTALL_ROOT
# INSTALL_ROOT=/tmp/install-root
around the end add:
install:
$(MAKE) -C $(KERNEL_BUILD) M=$(PWD) \
INSTALL_MOD_PATH=$(INSTALL_ROOT) modules_install
and then you can issue
sudo make install
this will put it either in /lib/modules/$(uname -r)/extra/
or /lib/modules/$(uname -r)/misc/
and run depmod appropriately
i think there should be entry of your your_module.ko in /lib/modules/uname -r/modules.dep and in /lib/modules/uname -r/modules.dep.bin for "modprobe your_module" command to work
Try insmod instead of modprobe. Modprobe
looks in the module directory /lib/modules/uname -r for all the modules and other
files
Ensure that your network is brought down before loading module:
sudo stop networking
It helped me - https://help.ubuntu.com/community/UbuntuBonding
Insert this in your Makefile
$(MAKE) -C $(KDIR) M=$(PWD) modules_install
it will install the module in the directory /lib/modules/<var>/extra/
After make , insert module with modprobe module_name (without .ko extension)
OR
After your normal make, you copy module module_name.ko into directory /lib/modules/<var>/extra/
then do modprobe module_name (without .ko extension)

Resources