Skip to content

Instantly share code, notes, and snippets.

@chuanwang66
Last active August 29, 2015 14:07
Show Gist options
  • Save chuanwang66/3493e85ebc921d25f063 to your computer and use it in GitHub Desktop.
Save chuanwang66/3493e85ebc921d25f063 to your computer and use it in GitHub Desktop.
1. How to build Linux Kernel:
http://www.thegeekstuff.com/2013/06/compile-linux-kernel/
http://linux.vbird.org/linux_basic/0540kernel.php
http://haifux.org/lectures/88-sil/kernel-compilation.html
2. How to Create, Compile, Load Linux LKM (Loadable Kernel Modules)
<Linux Loadable Kernel Module HOWTO>
http://www.tldp.org/HOWTO/Module-HOWTO/index.html
<How to Create, Compile, Load Linux LKM Loadable Kernel Modules>
http://www.thegeekstuff.com/2012/04/linux-lkm-basics/
最详尽的资料==>
http://www.makelinux.net/ldd3/
http://www.makelinux.net/ldd3/chp-2-sect-2
w00273652@Build-149-126:~/test/Linux/LKM> cat hellomod.c
#include <linux/module.h>
#include <linux/kernel.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);
w00273652@Build-149-126:~/test/Linux/LKM> cat Makefile
obj-m += hellomod.o
modules:
sudo make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
modules_install:
sudo make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules_install
clean:
sudo make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
执行make命令完成编译,
(1)insmod hellomod.ko
在/var/log/message中可以看到相关信息, lsmod可以看到hellomod模块
(2)rmmod hellomod
在/var/log/message中可以看到相关信息, lsmod可以看到hellomod模块被删除
3. dive into the "Makefile" above
这里obj-m += hellomod.o应该也可以携程obj-m := hellomod.o
:= 定义变量,make会先把整个Makefile文件展开,找出该变量最后一个被指定的值并且赋值给他,i.e.
x := foo
y := $(x)
x := foobar
最后y的结果为foobar
?= 定义变量,如果变量已经被定义过,则不会再被赋值
$(shell uname -r): 取得kernel的版本号
obj-m: 指定需要编译成模块的目标文件名集合,编译方式为“把对象编译成模块”,它同时也代表被编译的文件名称(即hellomod.c或hellomod.S);
obj-y: 把对象编译进内核
obj-y += somedir/ : kbuild进入目录somedir后去找Makefile来决定需要编译出哪些目标文件。相当于进入somedir目录,然后执行make
如果一个模块包括了多个.c文件(如 file1.c , file2.c),则应该以如下方式编写Makefile
obj-m := modulename.o
module-objs := file1.o file2.o
make -C: 跟make说这次kernel module include的目录在哪里。简言之,-C用来指定内核源码的目录
make M=$(PWD): 描述哪个目录要被编译(早期的指令是SUBDIRS)。简言之,M=用来指定LKM所在的目录
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment