第一个内核模块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系统由于其出色的性能和稳定性,开放源代 ...
随机推荐
- 使用Secure Boot后,导致VMware无法启动虚拟机
最初安装vmware时就报错 Gtk-Message: Failed to load module "canberra-gtk-module": libcanberra-gtk- ...
- centos5.5 快速安装mysql
安装MySQL. [root@sample ~]# yum -y install mysql-server ← 安装MySQL[root@sample ~]# yum -y install php-m ...
- atitit.为什么 java开发要比php开发速度慢??
atitit.为什么 java开发要比php开发速度慢?? 1. 界面开发方面vs php 1 2. 架构繁琐 1 3. 环境配置复杂 1 4. 类库jar包冲突(严重) 1 5. 配置文件热部署的缺 ...
- How to Install Xcode, Homebrew, Git, RVM, Ruby & Rails on Snow Leopard, Lion, Mountain Lion, and Mavericks
After following many outdated and incomplete instructions for setting up a web development environme ...
- SAP ERP 6.0 EHP7 SR2(WINDOWS MSSQL版)安装说明
原文 by 枫竹丹青 ⋅ 1.安装准备 1.1.版本说明 本文是描述在一个Windows虚拟机.SQL Server数据库环境下,安装SAP ERP 6.0 EHP7 SR2服务器,安装完成虚拟机文件 ...
- Python 操作文件、文件夹、目录大全
# -*- coding: utf-8 -*- import os import shutil # 一. 路径操作:判断.获取和删除 #1. 得到当前工作目录,即当前Python脚本工作的目录路径: ...
- 突破MIME限制上传
方法:找一个正常的可上传的查看其的MIME类型,然后将马子的MIME改成合法的MIME即可.
- mysql数据库批量操作
批量KILL会话: 1.首先,根据条件将查询到要kill的进程写入文件:如:desc information_schema.processlist; SELECT concat('KILL ',id, ...
- C语言 · 求存款
算法提高 3-2求存款 时间限制:1.0s 内存限制:256.0MB 问题描述 见计算机程序设计基础(乔林)P50第5题. 接受两个数,一个是用户一年期定期存款金额,一个是按照百分比 ...
- am335x 打开内部 RTC
AM335X 打开内部 RTC 过程记录. kernel version: 3.2.0 先在 make menuconfig 里面打开内部RTC的配置: make menuconfig Device ...