0
votes

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.

1

1 Answers

1
votes

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.