Linux操作系统分析学习总结

weixin_42705053 2022-07-05 15:29:55

一、Linux操作系统分析中的学习收获与感想

本学期的linux操作系统分析这门课由孟宁老师和李春杰老师共同讲解,经过一个学期的学习,我对Linux内核的原理有了更进一步的理解,并且完成两个实验,在实践中理解linux内核的编译、运行,对操作系统的运行原理有了更深的理解,尤其是对函数的调用堆栈、系统调用的内核处理过程等。在本课程中,我们学习了x86和ARM64指令集架构,同时也第一次了解到LoongArch,有同学还完成了c代码编译成LoongArch的代码分析,在参考学习后有很大的收获。

二、课程实验

实验一 mykernel

实验过程

安装所需工具
sudo apt install axel
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev
sudo apt install qemu # install QEMU
sudo apt install qemu-system-x86 
下载mykernel
wget https://raw.github.com/mengning/mykernel/master/mykernel-2.0_for_linux-5.4.34.patch
下载linux内核
axel -n 20 https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.34.tar.xz

img

解压linux内核
xz -d linux-5.4.34.tar.xz
tar -xvf linux-5.4.34.tar

img

给内核打mykernel补丁
cd linux-5.4.34
patch -p1 < ../mykernel-2.0_for_linux-5.4.34.patch
编译
make defconfig
make -j$(nproc)

img

实验结果

由于putty远程连接不支持显示图形界面,以下改用VNCviewer

执行

qemu-system-x86_64 -kernel arch/x86/boot/bzImage

img

实验二 debug linux kernel

前几步同实验一

在linux-5.4.34目录中执行
make menuconfig
选择 Kernel hacking

img

配置如下选项
Kernel hacking  --->
  Compile-time checks and compiler options  --->
        [*] Compile the kernel with debug info
        [*]   Provide GDB scripts for kernel debugging
  [*] Kernel debugging
Processor type and features ---->
  [ ] Randomize the address of the kernel image (KASLR)
编译并运行内核
make -j$(nproc) # nproc gives the number of CPU cores/threads available
qemu-system-x86_64 -kernel arch/x86/boot/bzImage

img

因为没有文件系统最终会kernel panic!

制作内存根文件系统

首先从https://www.busybox.xn--net-128dw501a/ 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) 

img

编译

make -j$(nproc) && make install

img

制作内存根文件系统镜像

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文件

#!/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
debug linux kernel

执行qemu-system-x86_64 -kernel linux-5.4.34/arch/x86/boot/bzImage -initrd ./busybox-1.31.1/rootfs.cpio.gz -S -s -nographic -append "console=ttyS0" ,再打开一个窗口,启动gdb,把内核符号表加载进来,建立连接

gdb vmlinux

(gdb) target remote:1234

(gdb) b start_kernel

c、bt、list、nextstep....

img

三、参考资料

庖丁解牛Linux操作系统分析 https://gitee.com/mengning997/linuxkernel

269

...全文
162 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

571

社区成员

发帖
与我相关
我的任务
社区描述
软件工程教学新范式,强化专项技能训练+基于项目的学习PBL。Git仓库:https://gitee.com/mengning997/se
软件工程 高校
社区管理员
  • 码农孟宁
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧