Issue on Creating module? - linux

I am trying to learn module programming for that i used a sample program in c as
#include<linux/module.h>
#include<linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "Hello man .\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Good bye see you soon\n");
}
for compiling this c code i wrote a make file as below
obj-m + = Hello_module.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
Then i am compiling this one using make command i get following errors
joe#joe-O-E-M:~/Module_pgm$ make
make -C /lib/modules/3.19.0-25-generic/build M=/home/joe/Module_pgmmodules
make[1]: Entering directory `/usr/src/linux-headers-3.19.0-25-generic'
./scripts/Makefile.build:44: /home/joe/Module_pgmmodules/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/joe/Module_pgmmodules/Makefile'. Stop.
make[1]: *** [_module_/home/joe/Module_pgmmodules] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.19.0-25-generic'
make: *** [all] Error 2
How can i resolve this problem..

You are missing a space between $(PWD) and modules. Try:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
The "No such file or directory" error message shows the path it's trying to find.

Related

WSL2&Linux make: make[1]: *** /lib/modules//build: No such file or directory. Stop

I am using the WSL2 and have already downloaded the Linux kernel to compile the module, however, when I enter make: it shows
code:
TARGETNAME = hello
OBJ = $(TARGETNAME).o
MODULE = $(TARGETNAME).ko
obj-m += $(OBJ)
all:
make -j $(nproc) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
install:
#modprobe -r $(TARGETNAME)
#install $(MODULE) /lib/modules/$(shell uname -r)/kernel/drivers/hid
#depmod
#modprobe $(TARGETNAME)
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Error:
tony#tony:~/OS/testa$ make
make -j -C /lib/modules/$(shell uname -r)/build
M=/home/tony/OS/testa modules
/bin/sh: 1: shell: Permission denied
make[1]: *** /lib/modules//build: No such file or directory.
Stop.
make: *** [Makefile:8: all] Error 2
And this is my wsl2(Linux file menu)
I am wondering that The reason for the error is that the executable program (make) in the target path cannot be found. And what should I do?

How to write a Linux kernel module makefile?

I'm trying to write a simple hello world kernel module. I'm working in Ubuntu 18.04.2 LTS on Virtual Box. In the directory /usr/src I created a directory named hello and inside that hello directory I've created hello.c and a makefile. Here is my 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
When I make the command "sudo make" I get this output:
make -C /lib/modules/5.3.0-28-generic/build M= modules
make[1]: Entering directory '/usr/src/linux-headers-5.3.0-28-generic'
make[2]: *** No rule to make target 'arch/x89/tools/relocs_32.c', needed by 'arch/x86/tools/relocs_32.o'. Stop.
arch/x86/Makefile:232: recipe for target 'archscripts' failed
make[1]: *** [archscripts] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.3.0-28-generic'
makefile:4: recipe for target 'all' failed
make: *** [all] Error 2
I've tried a few variations of my makefile including changing "PWD" to "shell pwd". I've installed all the essential tools and libraries as far as I know. What could be the problem here?
Variable PWD is set by the shell, so your Makefile relies on make invocation to be performed from the shell, as we normally do when type make in the terminal.
But sudo make executes make from a plain user environment. This environment lacks for PWD variable, so your Makefile behaves incorrectly. This can be found from your output:
make -C /lib/modules/5.3.0-28-generic/build M= modules
Use $(shell pwd) expression instead, it is more reliable:
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
Alternatively, you may call make with root privileges using this way:
sudo /bin/sh -c make
This way make is executed not from the plain root environment, but under an intermediate shell. This shell sets PWD variable.
Update
As noted by #Ian Abbott in the comments, another alternative for $(PWD) is $(CURDIR). Variable CURDIR is set by the make itself (precisely, by GNU Make). See also that question: bash: What is the difference between PWD and CURDIR?.
Your Makefile should look like this
obj-m += hello.o
Then your build command will look like
make -C $KDIR M=$PWD
Where $KDIR is the source tree of your kernel build.
is the way. The rest is handled by the kbuild system.
Please refer to refer to the official documentation for building off-tree modules

No rule to make target 'lib/sha256.c' error when compiling kernel module

I am trying to compile kernel module in Fedora 28. My current kernel is 4.17.3-200.fc28.x86_64. My hello.c is
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
static int __init hello_2_init(void)
{
printk(KERN_INFO "Hello, world 2\n");
return 0;
}
static void __exit hello_2_exit(void)
{
printk(KERN_INFO "Goodbye, world 2\n");
}
module_init(hello_2_init);
module_exit(hello_2_exit);
and my Makefile is
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) modules
I get this error when I am trying to compile. Initially I thought that the error is due to ssl library. So I installed openssl-devel for fedora. But I still get the same error.
make -C /lib/modules/4.17.3-200.fc28.x86_64/build M= modules
make[1]: Entering directory '/usr/src/kernels/4.17.3-200.fc28.x86_64'
CHK include/config/kernel.release
CHK include/generated/uapi/linux/version.h
CHK include/generated/utsrelease.h
make[2]: *** No rule to make target 'lib/sha256.c', needed by 'arch/x86/purgatory/sha256.o'. Stop.
make[1]: *** [arch/x86/Makefile:263: archprepare] Error 2
make[1]: Leaving directory '/usr/src/kernels/4.17.3-200.fc28.x86_64'
make: *** [Makefile:4: all] Error 2
How can I solve this error?
obj-m := FILENAME.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build <br/>
PWD := $(shell pwd)
all:
default
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
Try this Makefile or write in Console:
make -C /lib/modules/`uname -r`/build M=`pwd` modules obj-m=FILENAME.o
Replace FILENAME with the name of your file!

make: no rule to make target

i have written a simple linux module & its make file
this is my module
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void) {
printk("<1> Hello world!\n");
return 0;
}
static void hello_exit(void) {
printk("<1> Bye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
this is my make file
obj-m :=Hello.o
KDIR = /usr/src/linux-headers-3.5.0-17
all:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.o *.ko *.mod.* *.symvers *.order
when i execute make -f MakeFile
it gives following o/p
make -C /usr/src/linux-headers-3.5.0-17 SUBDIRS=/home/linux/Desktop modules
make[1]: Entering directory `/usr/src/linux-headers-3.5.0-17'
WARNING: Symbol version dump /usr/src/linux-headers-3.5.0-17/Module.symvers
is missing; modules will have no dependencies and modversions.
scripts/Makefile.build:44: /home/linux/Desktop/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/linux/Desktop/Makefile'. Stop.
make[1]: *** [_module_/home/linux/Desktop] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.5.0-17'
make: *** [all] Error 2
can any one tell me how to get rid of these errors.
Thanks in advance
Make the below change in your Makefile
First check which kernel is running by typing uname -a
Then go to cd /usr/src/
then check your linux source-code name
for e.g
uname -a Linux vinay-VirtualBox 3.2.0-50-generic-pae #76-Ubuntu SMP Tue Jul 9 19:24:55 UTC 2013 i686 i686 i386 GNU/Linux
here its source-code name is linux-headers-3.2.0-50-generic-pae
same thing in your case
e.g
linux-headers-3.2.0-23 linux-headers-3.2.0-23-generic-pae
so use linux-headers-3.2.0-23-generic-pae instead of
linux-headers-3.2.0-23 i.e replace same in your makefile
i.e KDIR=/usr/src/linux-headers-3.5.0-17-generic-pae
or in order to avoid above problem use
KDIR == /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
KDIR == /lib/modules/$(shell uname -r)/build
$(MAKE) -C $(KDIR) M=$(PWD) modules
obj-m += xyz.o
KDIR:=/usr/src/linux-headers-3.5.0-46-generic
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
Are you sure /usr/src/ contains linux-headers-3.5.0-46-generic files?
if this is not the case, download:
sudo apt-get install linux-headers-3.5.0-46
sudo apt-get install linux-headers-3.5.0-46-generic

Compiling Linux kernel - hello world

I am trying to compile the Linux kernel:
http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html
I have a simple hello world program hello-1.cpp
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
return 0;
}
void cleanup_module(void)
{
}
But I am trying to build it using the Makefile:
obj-m += hello-1.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 get a couple of errors.
make -C /home/pacman/linux-2.6.34.11/2.6.35.6-45.fc14.i686/build M=/home/pacman/p1 modules
make: *** /home/pacman/linux-2.6.34.11/2.6.35.6-45.fc14.i686/build: No such file or directory. Stop.
make: * [all] Error 2
Am I forgetting to define something?
Rename hello-1.cpp to hello-1.c (modules must be written in C) and add the lines:
module_init(init_module);
module_exit(cleanup_module);

Resources