1. 目录结构

内核源代码下载 https://www.kernel.org

arch目录

arch是architecture的缩写。

内核所支持的每种CPU体系,在该目录下都有对应的子目录。每个CPU子目录又进一步分解为boot,mm,kernel等子目录,分别包含控制系统引导,内存管理,系统调用等。

block目录

部分块设备驱动程序

crypto目录

加密、压缩、CRC校验算法

documentation

内核的文档

drivers目录

设备驱动程序

fs目录

存放各种文件系统的实现代码,每个子目录对应一种文件系统的实现,公用的源程序用于实现虚拟文件系统的vfs

  • devpts /dev/pts虚拟文件系统
  • ext2 第二的扩展文件系统
  • fat MS的fat32文件系统
  • isofs ISO9660光盘cd-rom上的文件系统
  • ...

include目录

内核所需要的头文件。与平台无关的放在include/linux子目录下,与平台相关的头文件则放在相应的子目录中。

lib目录

库文件代码

mm目录

mm目录中的文件用于实现内存管理中与体系结构无关的部分

net目录

网络协议的实现代码

samples目录

一些内核编程的实例

scripts目录

配置内核的脚本 security目录 SElinux的模块

sound目录

音频设备的驱动程序

usr目录

cpio命令的实现

virt目录

内核虚拟机

2. linux内核配置与编译

Linux内核具有可定制的优点,具体步骤如下:

1. 清除临时文件、中间文件和配置文件

删除产生的大部分文件,但保留配置

make clean

删除所有产生的文件和配置文件

make mrproper

删除所有产生的文件、配置文件以及编辑器备份文件和补丁文件

make distclean
2. 确定目标系统的软硬件的配置情况,比如CPU的类型、网上的型号,所需支持的网络协议等
3. 使用如下命令之一配置内核

基于文本模式的交互式配置

make config

基于文本模式的菜单型配置。(推荐使用)

make menuconfig

使用已有的配置文件(.config),但是会询问新增的配置选项

make oldconfig

图形化的配置(需安装图形化系统)

make xconfig

make config 是最为常用的内核配置方式,使用方法如下:

  1. 使用方向键在各选项中移动

  2. 使用“Enter”键进入下一层选单;每个选项上的高亮字母是键盘快捷方式,使用它可以快速地到达想要设置的选单项。

内核配置通常在一个已有的配置文件基础上,通过修改得到新的配置文件,linux内核提供了一系列可供参考的内核配置文件,位于arch/$cpu/configs

4. 编译内核
  • make zimage
  • make bzimage

区别:在x86平台,zimage只能用于小于512k的内核

如需获取详细编译信息,可使用:

  • make zimage V=1
  • make bzimage V=1

编译好的内核位于arch/$cpu/boot 目录下

5. 编译内核模块
make modules
6. 安装内核模块
make modules_install

将编译好的内核模块从内核源代码目录复制至/lib/modules下

7. 制作init ramdisk
mkinitrd initrd-$version $version

$version可以通过查询/lib/modules下的目录得到

3. 内核安装

  1. cp arch/x86/boot/bzimage /boot/vmlinuz-$version

  2. cp $initrd /boot/

  3. 修改/etc/grub.conf 或者 /etc/lilo.conf

4. 模块功能

linux内核的整体结构非常庞大,把所有的组件都编译进内核文件,即:zimage或bzimage,会导致两个问题:一是生成的内核文件过大;二是如果要添加或删除某个组件,需要重新编译整个内核。

linux提供了一种叫做“内核模块”的机制,能让内核文件(zimage或bzimage)本身并不包含某组件,而是在该组件需要被使用的时候,动态地添加到正在运行的内核中。

内核模块具有如下特点:

  • 模块本身并不被编译进内核文件(zimage或bzimage)
  • 可以根据需求,在内核运行期间动态地安装或卸载

程序结构

  1. 模块加载函数(必需)

    安装模块时被系统自动调用的函数,通过module_init宏来指定

  2. 模块卸载函数(必需)

    卸载模块时被系统自动调用的函数,通过module_init宏来指定

范例

#include <linux/init.h>
#include <linux/module.h> static int __init hello_init(void)
{
printk(KERN_WARNING"Hello World!\n");
return 0;
} static void __exit hello_exit(void)
{
printk(KERN_INFO "Goodbye\n");
} module_init(hello_init);
module_exit(hello_exit);

模块的编译

在linux2.6下编译内核模块,通常使用makefile

安装与卸载

  • 加载 insmod(insmod hello.ko)
  • 卸载 rmmod (rmmod hello)
  • 查看 lsmod
  • 加载 modprobe (modprobe hello)

modprobe如同insmod,也是加载一个模块到内核,它的不同之处在于它会根据文件/lib/modules/<$version>/modules.dep来查看要加载的模块,如果是,modprobe会首先找到这些模块,把它们先加载到内核。

模块可选信息

1. 许可证申明

宏MODULE_LICENSE用来告知内核,该模块带有一个许可证,有效的许可证有“GPL”、“GPLv2”、“GPL and additional rights”、“Dual BSD/GPL”、“Dual MPL/GPL”和“Proprietary”。

2. 作者声明

MODULE_AUTHOR("your name");

3. 模块描述

MODULE_DESCRIPTION("Hello world module");

4. 模块版本

MODULE_VERSION("V1.0");

5. 模块别名

MODULE_ALIAS("a simple module");

6.模块参数

通过宏module_paran指定模块参数,模块参数用于在加载模块时传递参数给模块。

module_paran(name, type, perm)

name 是模块参数的名称(变量名),type是这个参数的类型

perm 是模块参数的访问权限

type常见值:bool布尔型、int整型、charp字符串型

perm常见值:

  • S_IRUGO :任何用户都对于/sys/module中出现的该参数具有读权限
  • S_IWUSR :允许root用户修改/sys/module中出现的该参数

例如:

int a = 3;
char *st;
module_param(a, int, S_IRUGO);
module_param(st, charp, S_IRUGO);

内核符号导出

/poc/kallsyms 记录了内核中所有导出的符号的名字与地址。

使用:

EXPORT_SYMBOL(符号名)

EXPORTSYMBOLGPL(符号名)

其中EXPORTSYMBOLGPL只能用于包含GPL许可证的模块。

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/

table th {
font-weight: bold;
}

table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}

table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}

table tr:nth-child(2n) {
background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

最新文章

  1. bootstrap无限级分类 jq拓展 之前的无限级分类的封装版~
  2. Badboy录制脚本参数化
  3. ADO.NET Entity Framework,Code First简单示例
  4. 引擎设计跟踪(九.14.2e) DelayLoaded DLLs (/DELAYLOAD)
  5. ios协议调起app
  6. Toolkit Pro学习--Toolbar的创建
  7. Vue2.x中的Render函数
  8. yii 输出当前的sql语句
  9. Python调用ansible API系列(二)执行adhoc和playbook
  10. ManyToManyField 增加记录
  11. git 连接github的配置
  12. vue修改对象属性值视图上没有更新
  13. OneZero第五次站立会议(2016.3.25)
  14. #优化:Nginx防御DDOS和CC攻击
  15. spark 源码阅读博客
  16. bzoj2014
  17. Windows Server 2016 Essentials试用
  18. Unity开发 手机平台播放影片
  19. IO模型《五》异步IO
  20. 【实战】sqlmap显示有注入却无法爆出库名

热门文章

  1. html元素禁用disable or enable
  2. ios copy和strong,浅拷贝和深拷贝
  3. SQL 表值函数/标量函数
  4. Vue(五)Vue规范
  5. linux安装redis操作
  6. linux sed awk seq 正则使用 截取字符 之技巧
  7. MongoDB系列:三、springboot整合mongoDB的简单demo
  8. MariaDB多实例的安装配置
  9. C++中typedef enum 和 enum
  10. Linux系统中硬链接和软链接(符号链接)的区别