1 安装开发工具
sudo apt install build-essential
sudo apt install qemu # install QEMU
sudo apt install libncurses5-dev bison flex libssl-dev libelf-dev
2 下载内核源代码
sudo apt install axel
axel -n 20 https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.34.tar.xz
xz -d linux-5.4.34.tar.xz
tar -xvf linux-5.4.34.tar
cd linux-5.4.34
![](https://img-blog.csdnimg.cn/img_convert/ce4a86a6ed0d1d006e24b5cb771558f4.png)
3 配置内核选项
make defconfig # Default configuration is based on 'x86_64_defconfig'
![](https://img-blog.csdnimg.cn/img_convert/f2ae8538803aba4ed23bcda618631973.png)
make menuconfig
打开界面后,按enter键是进入,按space键选择[]或[*]
Kernel hacking --->
Compile-time checks and compiler options --->
[*] Compile the kernel with debug info
[*] Provide GDB scripts for kernel debugging
[*] Kernel debugging
# 关闭KASLR,否则会导致打断点失败
Processor type and features ---->
[] Randomize the address of the kernel image (KASLR)
Kernel hacking在最后一行
![](https://img-blog.csdnimg.cn/img_convert/a4f71fa62572914f1eb54c0bdce24367.png)
进入Kernel hacking之后,就直接会显示Kernel debugging,这里可以看到已经是[*]了
![](https://img-blog.csdnimg.cn/img_convert/aeeddf6ee25977bfe0aedb6811b8e638.png)
修改Compile-time checks and compiler options
![](https://img-blog.csdnimg.cn/img_convert/3a427f566a6ff0f889ea69dbdab69d99.png)
4 编译和运行内核
make -j$(nproc) # nproc gives the number of CPU cores/threads available
# 测试一下内核能不能正常加载运行,因为没有文件系统最终会kernel panic
qemu-system-x86_64 -kernel arch/x86/boot/bzImage
![](https://img-blog.csdnimg.cn/img_convert/ad98ddce009795967b18f494c4c881d0.png)
5 制作根文件系统
安装busybox
axel -n 20 https://busybox.net/downloads/busybox-1.31.1.tar.bz2
# 1.31.0 老是出bug这里改成了1.35.0
tar -jxvf busybox-1.35.0.tar.bz2
cd busybox-1.35.0
make menuconfig
Settings --->
[*] Build static binary (no shared libs)
make -j$(nproc) && make install
然后制作内存根文件系统镜像
mkdir rootfs
cd rootfs
cp ../busybox-1.31.1/_install/* ./ -rf
mkdir dev proc sys home
sudo cp -a /dev/{null,console,tty,tty1,tty2,tty3,tty4} dev/
#准备init脚本文件放在根文件系统跟目录下(rootfs/init),添加如下内容到init文件。
vim init
==========复制到init中============
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
echo "Wellcome MengningOS!"
echo "--------------------"
cd home
/bin/sh
==========复制到init中============
给init脚本添加可执行权限
chmod +x init
#打包成内存根文件系统镜像
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../rootfs.cpio.gz
#测试挂载根文件系统,看内核启动完成后是否执行init脚本
qemu-system-x86_64 -kernel ./arch/x86/boot/bzImage -initrd rootfs.cpio.gz
![](https://img-blog.csdnimg.cn/img_convert/59c084cf16a9091ce029fdc5baefe9d1.png)
6 配置VS Code调试linux
安装vscode,到官网下载安装包
sudo apt install ./code_1.76.1-1678294265_amd64.deb
sudo apt install global
在linux根目录下创建.vscode,然后将配置文件复制过去
配置文件连接https://github.com/mengning/linuxkernel/tree/master/src/kerneldebuging
![](https://img-blog.csdnimg.cn/img_convert/dcc84983bb79fb0e83b48aca50aa2a2a.png)
修改tasks.json,这里的命令对应之前的qemu-system-x86_64 -kernel arch/x86/boot/bzImage
![](https://img-blog.csdnimg.cn/img_convert/713fd3b10251c0d05b7cbbabdc95d465.png)
vscode下载插件
![](https://img-blog.csdnimg.cn/img_convert/c623e71792b7f70d6257ea3020a6bf9a.png)
使用vscode打开linux目录
![](https://img-blog.csdnimg.cn/img_convert/da09d0ed358f7c0ebc279fa0706db752.png)
添加断点
![](https://img-blog.csdnimg.cn/img_convert/4b7471f847434720786b76e722b71e7d.png)
点击启动调试
![](https://img-blog.csdnimg.cn/img_convert/41a2d3cb533278f765e4f8f3c70ccc45.png)
![](https://img-blog.csdnimg.cn/img_convert/aa50862ee52222cc5eb23b2582fca661.png)
分析
0号进程init_task被设置整个系统的第一个进程
start_kernel会执行一些初始化操作,包括初始化各种重要的数据结构、驱动程序、中断处理程序等
start_kernel的结尾arch_call_reset_init(),这个点开这个函数的定义是执行了reset_init()函数
遇到了kernel_init,它是所有用户进程的祖先,由kernel_thread函数创建,kernel_thread函数创建一个新的内核线程
kernel_thread函数是通过_do_fork函数来创建进程的