王铁柱14

导航

 

一、下载Linux内核源码

wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.1.tar.xz #下载linux-5.0.1内核压缩包
xz -d linux-5.0.1.tar.xz
tar -xvf linux-5.0.1.tar #解压
cd linux-5.0.1

二、安装内核编译工具

sudo apt install build-essential flex bison libssl-dev libelf-dev libncurses-dev

三、配置编译内核

make x86_64_defconfig #生成64位配置文件
make menuconfig #开启文本菜单选项,对窗口有限制,尽量调大窗口
#Kernel hacking -> Compile-time checks and compiler options -> [*] Compile the kernel with debug info
#配置内核使之携带调试信息,保存并退出 make #编译,时间较长

四、升级当前系统内核

sudo make modules_install #安装前通过系统快照备份,以防出现故障前功尽弃
sudo make install
sudo update-grub
reboot
uname -a #检查当前内核版本,确认升级成功

五、通过QEMU虚拟机加载内核,构造MenuOS

sudo apt install qemu
git clone https://github.com/mengning/menu.git
cd menu
sudo apt-get install libc6-dev-i386 #在64位环境下编译32位程序需安装
vim Makefile #将Makefile中"qemu..."那一行进行对应修改
qemu-system-x86_64 -kernel ../linux-5.0.1/arch/x86/boot/bzImage -initrd ../rootfs.img #打开qemu
make rootfs

如下图所示:

六、构建Linux内核的gdb调试环境

qemu-system-x86_64 -kernel ../linux-5.0.1/arch/x86/boot/bzImage -initrd ../rootfs.img -append  nokaslr -s -S #在qemu中启动gdb server
#可以看到新打开的qemu虚拟机显示黑屏,此时它正在等待gdb的链接
#关于-s和-S选项的说明:
#-S freeze CPU at startup (use 'c' to start execution)
#-s shorthand for -gdb tcp::1234
#nokaslr KASLR是kernel address space layout randomization的缩写
gdb #在另外一个终端运行gdb
file linux-5.0.1/vmlinux #加载符号表
break start_kernel #在start_kernel处设置断点
target remote:1234 #建立gdb与gdbserver之间的连接
c
list #显示断点处相关源代码

 

如下图所示:

 

 七、验证MenuOS的网络能正常工作

git clone https://github.com/mengning/linuxnet.git
cd linuxnet/lab2 #打开TCP网络通信server端程序目录
make
cd ../../menu/
make rootfs
cd ../linuxnet/lab3 #打开TCP网络通信client端程序目录
vim Makefile #修改Makefile
make rootfs

在MenuOS窗口中依次输入replyhi和hello指令,如下图所示:

posted on 2019-12-11 23:23  王铁柱14  阅读(203)  评论(0编辑  收藏  举报