571
社区成员
发帖
与我相关
我的任务
分享本学期的linux操作系统分析这门课由孟宁老师和李春杰老师共同讲解,经过一个学期的学习,我对Linux内核的原理有了更进一步的理解,并且完成两个实验,在实践中理解linux内核的编译、运行,对操作系统的运行原理有了更深的理解,尤其是对函数的调用堆栈、系统调用的内核处理过程等。在本课程中,我们学习了x86和ARM64指令集架构,同时也第一次了解到LoongArch,有同学还完成了c代码编译成LoongArch的代码分析,在参考学习后有很大的收获。
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
wget https://raw.github.com/mengning/mykernel/master/mykernel-2.0_for_linux-5.4.34.patch
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
make defconfig
make -j$(nproc)

由于putty远程连接不支持显示图形界面,以下改用VNCviewer
执行
qemu-system-x86_64 -kernel arch/x86/boot/bzImage

前几步同实验一
make menuconfig

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

因为没有文件系统最终会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)

编译
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文件
#!/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
执行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、next、step....

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