内核模块编程实验-HelloWorld
1、编写一个linux的Helloworld.c文件
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/kernel.h>
int init_hello_module(void)
{
printk("hello world, i started! ..! it is a kernelmodule in 703\n");
printf("And our teacher,Mr. YangLiang, is guiding us in how to write a kernel module\n");
return 0;
}
{
printk("hello world, i started! ..! it is a kernelmodule in 703\n");
printf("And our teacher,Mr. YangLiang, is guiding us in how to write a kernel module\n");
return 0;
}
void cleanup_hello_module(void)
{
printf("goodbye from zhongshan institute\n");
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(init_hello_module);
module_exit(cleanup_hello_module);
{
printf("goodbye from zhongshan institute\n");
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(init_hello_module);
module_exit(cleanup_hello_module);
2、编写了一个Makefile文件,代码如下:
# To build modules outside of the kernel tree, we run "make"
# in the kernel source tree; the Makefile these then includes this
# Makefile once again.
# This conditional selects whether we are being included from the
# kernel Makefile or not.
# called from kernel build system: just declare what our modules are
obj-m := YesOrNo.o
obj-m := YesOrNo.o
CROSS_COMPILE =
CC = gcc
# Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /usr/src/linux-headers-2.6.18-6-686
# The current directory is passed to sub-makes as argument
PWD := $(shell pwd)
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /usr/src/linux-headers-2.6.18-6-686
# The current directory is passed to sub-makes as argument
PWD := $(shell pwd)
all: modules
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend *.symvers .*.cmd *.ko *.mod.c .tmp_versions $(TARGET)
在Makefile中直接make是出错的,因为makefile中有2个错误的地方,需要改正过来
1、obj-m := YesOrNo.o 中的YesOrNo.o应该改成对应的.C文件名 ,obj-m := Helloworld.o,当然,也可以把Helloworld.c文件改成YesOrNo.c
2、KERNELDIR ?= /usr/src/linux-headers-2.6.18-6-686 这个地方出错了,读者需要把kerneldir路径改成你所安装linux的版本,查看本机版本号用uname -a命令,在/usr/src/目录下找到相应的版本号的文件路径,然后把它复制到KERNELDIR后面
以下是我的示例
所以我就要改成 KERNELDIR ?= /usr/src/linux-headers-4.10.0-42-generic
完成这两步骤,就可以使用make命令了。
u 编译
a) 写好了Makefile,直接输入make,即可完成编译
b) 检查当前目录下,是否出现helloworld.ko文件,如果生成了.ko文件,则表示内核模块生成成功
u 运行
a) insmod helloworld.ko
b) lsmod 检查是否helloworld已经出现在列表中
c) tail –f /var/log/syslog 查看是否有打印,修改helloworld.c中的打印,重新编译后,看是否打印有所不同
▲强化.Hack你的操作系统
修改helloworld.c文件,尝试让你的操作系统无响应。
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/kernel.h>
int init_hello_module(void)
{
printk("hello world, i started! ..! it is a kernelmodule in 703\n");
printf("And our teacher,Mr. YangLiang, is guiding us in how to write a kernel module\n");
while(1)
{
printk("hello world, i started! ..! it is a kernelmodule in 703\n");
printf("And our teacher,Mr. YangLiang, is guiding us in how to write a kernel module\n");
while(1)
{
;
}
return 0;
}
return 0;
}
void cleanup_hello_module(void)
{
printf("goodbye from zhongshan institute\n");
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(init_hello_module);
module_exit(cleanup_hello_module);
{
printf("goodbye from zhongshan institute\n");
}
MODULE_LICENSE("Dual BSD/GPL");
module_init(init_hello_module);
module_exit(cleanup_hello_module);
在文件中加了一个死循环,然后再按着刚才的步骤make编译helloworld.C,然后加载到内核里,系统马上崩溃掉 insmod helloworld.ko 当你敲完这一句回车,系统马上死掉。
强化建议在虚拟机里实验,系统挂掉可以重新启动虚拟机。