第一个内核模块hello world
1、源码树的下载和编译(只是研究内核模块的话,应该不需要源码树的)
下载很简单,压缩包解压
编译:make menuconfig
make bzImage -j4
2、
cd /usr/src/linux-4.12.10/drivers/char
建立demo目录,用来存放自己的内核模块
demo目录下新建hello.c 和 Makefile文件
//hello.c #include <linux/init.h>
#include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void)
{
printk(KERN_ALERT "hello world!\n");
return ;
} static void hello_exit(void)
{
printk(KERN_ALERT "goodbye!\n");
} module_init(hello_init);
module_exit(hello_exit); //Makefile ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules*
.PHONY: modules modules_install clean
else
obj-m := hello.o
endif
编译结果:
ninjame@ubuntu1604:/usr/src/linux-4.12./drivers/char/demo$ sudo make
make -C /lib/modules/4.4.--generic/build M=/usr/src/linux-4.12./drivers/char/demo modules
make[]: Entering directory '/usr/src/linux-headers-4.4.80-040480-generic'
CC [M] /usr/src/linux-4.12./drivers/char/demo/hello.o
Building modules, stage .
MODPOST modules
CC /usr/src/linux-4.12./drivers/char/demo/hello.mod.o
LD [M] /usr/src/linux-4.12./drivers/char/demo/hello.ko
make[]: Leaving directory '/usr/src/linux-headers-4.4.80-040480-generic'
模块加载和效果
ninjame@ubuntu1604:/usr/src/linux-4.12./drivers/char/demo$ sudo insmod hello.ko
ninjame@ubuntu1604:/usr/src/linux-4.12./drivers/char/demo$ dmesg | tail
[ 15.300169] r8169 ::00.0 enp1s0: link down
[ 15.300185] r8169 ::00.0 enp1s0: link down
[ 15.300230] IPv6: ADDRCONF(NETDEV_UP): enp1s0: link is not ready
[ 16.840866] r8169 ::00.0 enp1s0: link up
[ 16.840873] IPv6: ADDRCONF(NETDEV_CHANGE): enp1s0: link becomes ready
[ 18.016626] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[ 18.016628] Bluetooth: BNEP filters: protocol multicast
[ 18.016631] Bluetooth: BNEP socket layer initialized
[12219.748060] hello: module verification failed: signature and/or required key missing - tainting kernel
[12219.748268] hello world!
第一个内核模块hello world的更多相关文章
- linux 驱动模块 内核编译环境
目录(?)[+] Linux设备驱动Hello World程序介绍 如何编写一个简单的linux内核模块和设备驱动程序.我将学习到如何在内核模式下以三种不同的方式来打印hello world,这三种方 ...
- Linux设备驱动Hello World程序介绍
自古以来,学习一门新编程语言的第一步就是写一个打印“hello world”的程序(可以看<hello world 集中营>这个帖子供罗列了300个“hello world”程序例子)在本 ...
- linux内核编译与开发
一.Linux内核简介linux kernel map: linux 系统体系结构: linux kernel体系结构: arm有7种工作模式,x86也实现了4个不同级别RING0-RING3,RIN ...
- WinDbg 图形界面功能(三)
1.4.调试菜单 调试相关操作的功能菜单在这个下面,比如单步执行等. Go 单击Go调试菜单恢复 (或开始) 在目标上的执行. 此执行将继续,直到抵达某个断点. 异常或事件发生时,该过程结束或调试器将 ...
- ApacheCN Linux 译文集 20211129 更新
笨办法学 Linux 中文版 练习 0:起步 练习 1:文本编辑器,vim 练习 2:文本浏览器,少即是多 练习 3:Bash:Shell..profile..bashrc..bash_history ...
- 《Linux内核设计与实现》读书笔记 第一、二章
第一章 Linux内核简介 1.1Unix历史 Unix特点:1.很简洁 2.所有东西都被当成文件对待 3.Unix内核和相关的系统工具软件都是用C语言编写而成 4.进程创建非常迅速 所以Uni ...
- Linux内核模块设计
内核的设计有两种方式:单内核和微内核,两者各有优劣,关于两者的比较可以参见wiki.windowds和Solaris采用微内核结构. Linux内核采用单内核结构,设计比较简单,但单内核的理念是把所有 ...
- Linux内核模块简介
一. 摘要 这篇文章主要介绍了Linux内核模块的相关概念,以及简单的模块开发过程.主要从模块开发中的常用指令.内核模块程序的结构.模块使用计数以及模块的编译等角度对内核模块进行介绍.在Linux系统 ...
- 如何增强 Linux 系统的安全性,第一部分: Linux 安全模块(LSM)简介
http://www.ibm.com/developerworks/cn/linux/l-lsm/part1/ 1.相关背景介绍:为什么和是什么 近年来Linux系统由于其出色的性能和稳定性,开放源代 ...
随机推荐
- ACCESS与MSSQL比较:SQL语句关于时间格式使用的注意点
ACCESS与MSSQL比较:SQL语句关于时间字符串的使用:ACCESS数据库使用 # 来控制时间格式字符串:mssql数据库使用单引号 ' 来控制时间格式字符串.例: ACCESS版本:UPDAT ...
- 线程池c3p0和dbcp2的配置初始化实例
一.c3p0 public class ConnectionManager { public static ComboPooledDataSource dataSource; static { try ...
- IT行业面试指导 计算机行业面试技巧 面试技巧
简历篇 简历是你的的第一张脸,简历写的是否合理,是否吸引人,将决定你能否赢得宝贵的面试机会,迈出第一步! l 姓名,性别,学历,居住地,工作年限,邮箱,手机号 l 填“现居住地”,不要填成“户籍所 ...
- ubuntu下载linux内核源码
ubuntu仓库里面关于源码部分配置的好全啊,什么都有,ps:包括vim的各种插件居然也有人打包放到仓库里,真是方便. 1.首先查看一下本系统使用的内核版本号: cat /proc/version L ...
- C#多枚举值的写法与读法
首先,定义枚举的时候必须是2,4,8,16这种2的次方的值. using System; using System.Collections.Generic; using System.Linq; us ...
- Experience on Namenode backup and restore --- checkpoint
Hadoop version: Hadoop 2.2.0.2.0.6.0-0009 Well, We can do this by building Secondary Namenode, Check ...
- Ownerdrawn ComboBox
[ToolboxBitmap(typeof(ComboBox))] class ComboBoxEx : ComboBox { public ComboBoxEx() { this.DrawMode ...
- Nokia Imaging SDK 的高级使用—实时滤镜拍照
有关 Nokia Imaging SDK 的初级使用,可以参考:Nokia Imaging SDK滤镜使用入门 本文的主题: 1.如何 PhotoCaptureDevice 类使用,以及如何在 Med ...
- DDR2基础
一. DDR2介绍 DDR2由JEDEC(电子设备工程联合委员会)开发的新生代内存技术标准.该标准定义了DDR2封装.寻址及操作.电气等所有特性. DDR相关技术对比 DDR DDR2 DDR3 电压 ...
- ngx_lua模块学习示例之waf
转自:http://www.tuicool.com/articles/FbQ3ymB WAF的主要功能为: ip黑白名单 url黑白名单 useragent黑白名单 referer黑白名单 常见web ...