This commit is contained in:
Ciro Santilli
2016-12-15 22:53:13 +00:00
parent d45ceace5d
commit 8eb878c24e
5 changed files with 48 additions and 34 deletions

View File

@@ -5,3 +5,5 @@
1. [kmod](kmod.md) 1. [kmod](kmod.md)
1. Examples 1. Examples
1. [Host](host/) 1. [Host](host/)
TODO: use Buildroot as in this answer http://stackoverflow.com/a/38813400/895245 to install and test modules on QEMU.

7
TODO.md Normal file
View File

@@ -0,0 +1,7 @@
# TODO
- build kernel module against a given kernel tree
- build out of tree kernel module into the kernel image
- http://stackoverflow.com/questions/7353851/insert-linux-kernel-module-statically
- http://stackoverflow.com/questions/8421970/compiling-a-driver-as-a-part-of-a-kernel-not-as-a-module
- .ko file format vs .o http://stackoverflow.com/questions/10476990/difference-between-o-and-ko-file

View File

@@ -2,7 +2,23 @@
The module building system. The module building system.
Kernel modules are built using a makefile located at: Explained at: <https://www.kernel.org/doc/Documentation/kbuild/modules.txt>
Full method: get the kernel, build it to a directory `$KDIR`, and then run:
make -C "$KDIR" M="$(PWD)" modules
Quick method: no need for a full build, just:
make modules_prepare
but you lose some functionality. TODO: module insert on host then fails with:
insmod: ERROR: could not insert module hello.ko: Invalid module format
even though kernel tree was checked out to match the host.
Using your distro's kernel version:
/lib/modules/$(uname -r)/build /lib/modules/$(uname -r)/build

View File

@@ -1,33 +1,16 @@
OUT_EXT := .ko
OBJ_EXT := .o
RUN := hello
RUN_EXT := $(RUN)$(OUT_EXT)
obj-m += hello.o obj-m += hello.o
ccflags-y := -Wno-declaration-after-statement -std=gnu99 ccflags-y := -Wno-declaration-after-statement -std=gnu99
.PHONY: clean ins log rm run my-ins .PHONY: all clean
all: $(RUN_EXT) ins_rm_mod.out all: hello.ko ins_rm_mod.out
hello.ko: hello.c hello.ko: hello.c
make -C /lib/modules/$(shell uname -r)/build M="$(PWD)" modules make -C '/lib/modules/$(shell uname -r)/build' M='$(PWD)' modules
clean: clean:
make -C /lib/modules/$(shell uname -r)/build M="$(PWD)" clean make -C '/lib/modules/$(shell uname -r)/build' M='$(PWD)' clean
rm -f *.out rm -f ins_rm_mod.out
ins: all ins_rm_mod.out: ins_rm_mod.c hello.c
sudo insmod '$(RUN_EXT)' gcc -Wall -std=gnu99 -o ins_rm_mod.out ins_rm_mod.c
log:
dmesg
rm:
if lsmod | grep -Eq '^$(RUN) '; then sudo rmmod '$(RUN_EXT)'; fi
ins_rm_mod.out: ins_rm_mod.c
gcc -Wall -std=gnu99 -o '$@' '$<'
ins_rm_run: ins_rm_mod.out $(RUN_EXT)
sudo ./ins_rm_mod.out

View File

@@ -9,7 +9,7 @@ Simple things that can be demonstrated by inserting a module into the currently
This method easier to setup, but it is not recommended for development, as: This method easier to setup, but it is not recommended for development, as:
- it may break your system. - it may break your system
- you can't control which kernel version to use - you can't control which kernel version to use
Use VMs instead. Use VMs instead.
@@ -20,15 +20,21 @@ We only use it for super simple examples.
Build, insert and remove a hello world module: Build, insert and remove a hello world module:
make ins make
make rm
make log
The last lines of the log should contain: sudo insmod hello.ko
init_module # Our module should be there.
cleanup_module sudo lsmod | grep hello
Insert and remove a module from a C program: # Last message should be: init_module
dmest -T
make ins_rm_run sudo rmmod hello
# Last message should be: cleanup_module
dmest -T
Insert and remove the `hello.ko` module from a C program with system calls:
sudo ./ins_rm_mod.out