一.编译linux内核
$ cd linux
$ make ARCH=x86_64 x86_64_defconfig
$ make ARCH=x86_64 menuconfig
# 打开 `Kernel hacking ->
Compile-time checks and compiler options ->
Compile the kernel with debug info ->
Provide GDB scripts for kernel debugging`
$ make -j
二.编译 rootfs
使用 buildroot 编译 rootfs
在使用 buildroot 编译 rootfs 时,若在内网使用,记得设置代理服务器。
$ git clone git://git.buildroot.net/buildroot
$ cd buildroot
$ make menuconfig
# select `Target Options ->
Target Architecture ->
x86_64`
# select `Filesystem images ->
ext2/3/4 root file system ->
ext4`
$ make -j
三.编译 qemu
configure --enable-kvm --target-list=x86_64-softmmu --enable-virtfs --prefix=/home/tyrion_shi/.bin
提示无法找到某个lib 但是lib的确已经安装 pk-config 也可以找到
qemu-system-x86_64 -s -S -no-kvm -kernel ~/work/test/linux/linux-master/arch/x86_64/boot/bzImage -boot c -m 2048M -hda ~/work/test/buildroot-2023.05.x/output/images/rootfs.ext4 -append "root=/dev/sda rw console=ttyS0,115200 acpi=off nokaslr" -serial stdio -display none
-fsdev local,security_model=passthrough,id=fsdev0,path=/home/tyrion_shi/share/qemushare -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=hostshare
qemu-system-x86_64 -s -S -no-kvm -kernel ~/work/test/linux/linux-master/arch/x86_64/boot/bzImage -boot c -m 2048M -hda ~/work/test/buildroot-2023.05.x/output/images/rootfs.ext4 -append "root=/dev/sda rw console=ttyS0,115200 acpi=off nokaslr" -serial stdio -display none -fsdev local,security_model=passthrough,id=fsdev0,path=/home/tyrion_shi/share/qemushare -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=hostshare
四.使用 gdb 调试内核启动流程
重新开窗口运行
1 $ gdb vmlinux
(gdb) target remote localhost:1234
(gdb) break start_kernel
(gdb) c
(gdb) layout src
挂载外部共享文件夹到虚拟机内部
2.mount -t 9p -o trans=virtio,version=9p2000.L hostshare /mnt/host_files