so2学习日志1-准备


参考链接 github参考文档 so2内核教学

启动

物理机下

1
sudo ./local.sh docker interactive --privileged

docker下

1
2
3
/linux/tools/labs# LABS=<实验名称> make skels
make console
# make gui

源码在 docker下/linux 中

vim(Cscope插件)下查询

1
2
3
4
5
6
7
8
9
10
11
:cs find g task_struct
find : Query for a pattern (Usage: find a|c|d|e|f|g|i|s|t name)
a: Find assignments to this symbol
c: Find functions calling this function
d: Find functions called by this function
e: Find this egrep pattern
f: Find this file
g: Find this definition
i: Find files #including this file
s: Find this C symbol
t: Find this text string

ctrl+o跳回

练习

骨架代码是从位于 tools/labs/templates 的完整源代码示例中生成的。要解决任务,首先要为所有实验生成骨架代码:

1
2
tools/labs $ make clean
tools/labs $ LABS=<实验名称> make skels

你还可以使用以下命令为单个任务生成骨架代码:

1
tools/labs $ LABS=<实验名称>/<任务名称> make skels

生成骨架驱动程序后,构建源代码:

1
tools/labs $ make build

这里会出现报错,根据报错信息,把kernel_modules下的3-errer-mod下的kbuild中第三行删除就行
或者你可以看看报错原因,自行修改err_mod.c,跟着教程走就行
然后,启动虚拟机:

1
tools/labs $ make console

模块将放置在 /home/root/skels/kernel_api/<任务名称> 目录中。
重新构建模块时,无需停止虚拟机!本地 skels 目录与虚拟机共享。

在虚拟机中 ctrl+a z 再按 q 退出

在 docker 中用 vim 不是很方便,用 vscode 吧
用 root 用户启动 vscode

1
sudo code --no-sandbox --user-data-dir /root

在vscode安装Dev Containers插件,在docker运行时
打开 VSCode 命令面板 (Ctrl+Shift+P)。
输入并选择 “Dev Containers: Attach to Running Container…”。
从列表中选择您要连接的容器。

在root下的vscode中copilot大概率是没登录的

1
2
3
4
5
6
# 允许 root 访问当前 X 会话
xhost +SI:localuser:root
# 启动 firefox
sudo firefox
# 使用后关闭访问
xhost -SI:localuser:root

可以使得root打开图形化浏览器
即便可以打开浏览器了,还是无法在浏览器上验证账号,可能是一些环境问题,root下的浏览器没办法访问到root下的vscode,如果直接切换到root用户下可能可以(su),我没法切换,你们可以试一试
可以使用设备码的方式验证,但出现方式不明,多试试

Kbuild

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 编译到内核镜像
obj-y += foo.o

# 编译为可加载模块
obj-m += foo.o

# 不编译
obj-n += foo.o
obj- += foo.o

# 单文件模块
obj-m += module.o

# 多文件模块
obj-m += module.o
module.o := file1.o file2.o file3.o

# 进入子目录构建
obj-y += drivers/net/
obj-y += fs/
# 会在子目录中查找 Makefile/Kbuild
# 并依次构建 drivers/net/ 和 fs/ 目录下的内容

# 整个模块的编译标志
ccflags-y := -DDEBUG

$(src) # 源文件目录
$(obj) # 输出目录
$(srctree) # 内核源码根目录

# 条件编译
# 根据内核配置决定是否编译
obj-$(CONFIG_NET) += network.o
obj-$(CONFIG_USB) += usb/

# 头文件搜索路径
ccflags-y += -I$(src)/include
ccflags-y += -I$(srctree)/drivers/misc