一、Linux操作系统分析中的学习收获和感想
在本门课程中我们跟随孟宁、李春杰两位老师由浅入深的学习Linux操作系统知识,学习了Linux基本概念,并以实验为驱动深入学习中断、异常、时钟、进程管理、系统调用、驱动程序、文件系统、内核文件挂载等方面的内容,收获颇丰。
我们以《庖丁解牛Linux操作系统分析》为学习材料,从宏观微观等多角度分析Linux整体架构。了解了Linux系统的由来和发展。并深入其中,解读内核源代码,搭建调试环境。之后我们学习了在X86和ARM不同架构下的系统调用,进程的描述和创建,程序的编译装载过程,中断线程进程调度等关键问题,了解了操作系统最核心的功能和运行机制。
通过本次课程理论知识学习以及实验的实践,更加理解了Linux底层工作原理。
实验一:mykernel
配置步骤如下:
wget https://raw.github.com/mengning/mykernel/master/mykernel-2.0_for_linux-5.4.34.patch
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
patch -p1 < ../mykernel-2.0_for_linux-5.4.34.patch
sudo apt install build-essential gcc-multilib
sudo apt install qemu # install QEMU
sudo apt install libncurses5-dev bison flex libssl-dev libelf-dev
make defconfig # Default configuration is based on 'x86_64_defconfig'
make -j$(nproc)
qemu-system-x86_64 -kernel arch/x86/boot/bzImage
结果如下:
实验二:debug mykernel
(1)安装开发工具
(2)下载内核源代码
(3)准备配置文件.config
(4)make menuconfig:配置内核选项
make defconfig # Default configuration is based on 'x86_64_defconfig'
make menuconfig
# 打开debug相关选项
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)
(5)编译运行
make -j$(nproc) # nproc gives the number of CPU cores/threads available
# 测试一下内核能不能正常加载运行,因为没有文件系统最终会kernel panic
qemu-system-x86_64 -kernel arch/x86/boot/bzImage
(6)制作根文件系统
从https://www.busybox.net下载 busybox源代码解压,解压后配置编译,并安装。
axel -n 20 https://busybox.net/downloads/busybox-1.31.1.tar.bz2
tar -jxvf busybox-1.31.1.tar.bz2
cd busybox-1.31.1
make menuconfig
# 编译成静态链接
Settings --->
[*] Build static binary (no shared libs)
# 编译安装
make -j$(nproc) && make install
(7)制作内存根文件系统镜像`
mkdir rootfs
cd rootfs
cp ../_install/* ./ -rf
mkdir dev proc sys home
sudo cp -a /dev/{null,console,tty,tty1,tty2,tty3,tty4} dev/
准备init脚本文件放在根文件系统跟目录下(rootfs/init)
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
echo "Wellcome MengningOS!"
echo "--------------------"
cd home
/bin/sh
给init脚本添加可执行权限,打包成镜像文件并测试
chmod +x init
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../rootfs.cpio.gz
qemu-system-x86_64 -kernel linux-5.4.34/arch/x86/boot/bzImage -initrd rootfs.cpio.gz
(8)debug linux kernel
qemu-system-x86_64 -kernel linux-5.4.34/arch/x86/boot/bzImage -initrd rootfs.cpio.gz -S -s -nographic -append "console=ttyS0"
启动gdb,把内核符号表加载进来,建立连接:
cd linux-5.4.34/
gdb vmlinux
(gdb) target remote:1234
(gdb) b start_kernel
c、bt、list、next、step....
参考
本实验资料来自《庖丁解牛Linux操作系统分析》https://gitee.com/mengning997/linuxkernel ,感谢孟宁老师的帮助与指导!