0

I am currently writing a linux kernel module that needs to include a file from the linux driver source code. The particular file I am trying to include is: https://elixir.bootlin.com/linux/latest/source/drivers/nvme/host/nvme.h

But the /lib/modules/$(shell uname -r)/build directory does not contain the drivers folder. I tried doing sudo apt-get install linux-headers-$(shell uname -r) but that also does not include the driver header files. My Makefile looks like this:

obj-m += hello_world.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

I then tried to get a full kernel checkout of the kernel version closest to my kernel version (I couldn't find the exact source code for my kernel version.) I pointed my Makefile to use that version but then when I try to insert the kernel module, it throws Invalid module format error and dmesg shows no symbol version for module layout. My source directory does contain the Module.symvers file but it still throws this error. I believe this error could be resolved if I somehow use my current linux source.

So, what's the best way to get the driver header files and use them in a kernel module. Any help would be appreciated.

Basim Sahaf
  • 33
  • 1
  • 7

1 Answers1

1

I see you are using apt. In such case you can get the correct source for your kernel simply enabling source repositories (see here or here to know how) and then getting the source code of the package:

$ sudo apt-get update
$ sudo apt-get source linux-image-$(uname -r)

After this, you will have the following files in the current directory:

linux-XXX/
linux_XXX.tar.xz
linux_XXX.dsc
linux_XXX.orig.tar.xz

The first one is a folder containing the correct source code for your installed kernel. You'll then be able to #include the header you need for your module.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • Thanks Marco for your answer. Your approach successfully got me my linux distro's source code. Although, when I try to compile and load my kernel module, it still shows the same error - `Inavlid module format`. Any leads? – Basim Sahaf Jun 23 '20 at 15:46
  • @BasimSahaf have you tried doing a full clean and then rebuilting from scratch? Try compiling a simple hello world module that only has an init and exit function and does nothing, see if that compiles and inserts correctly (also, try with `modprobe` if `insmod` does not seem to work). If that does not work, add the steps that you made to reproduce the error. – Marco Bonelli Jun 23 '20 at 15:51
  • I figured out why it is happening. When I do `sudo apt-get source linux-image-$(uname -r)`, it gets a different version than my current kernel version. So, since the two kernel versions are different, therefore I am not able to insert the kernel module. Any idea why this is happening? – Basim Sahaf Jun 24 '20 at 18:26
  • @BasimSahaf Not sure, `uname -r` should get the currently running kernel version. I don't see how something different could happen normally. – Marco Bonelli Jun 24 '20 at 19:57