diff --git a/content/posts/debug_linux_kernel.md b/content/posts/debug_linux_kernel.md new file mode 100644 index 0000000..e8b8420 --- /dev/null +++ b/content/posts/debug_linux_kernel.md @@ -0,0 +1,127 @@ +--- +title: "How to Debug Linux Kernel (with `qemu` & `busybox`)" +date: 2024-09-25T17:00:55-05:00 +draft: false +toc: false +images: +tags: + - kernel +--- + +# How to Debug Linux Kernel (with `qemu` & `busybox`) + +## Outline + +- clone `linux` source code +- clone `busybox` +- check compile configs +- generate `initramfs` by `busybox` +- install `qemu` +- boot kernel by `qemu` attached with user space from `busybox` +- other configurations + +## Prepare Linux src + +> before get started, make sure you have `flex` & `bison` installed to config and compile linux kernel + +- clone [linux src](https://github.com/torvalds/linux.git) + +```bash +git clone https://github.com/torvalds/linux.git +cd linux +``` + +- config it for minimal: + +```bash +make allnoconfig +``` + +- and enable following config by: + +```bash +make menuconfig +``` + +> 64-bit kernel ---> yes +> General setup ---> Initial RAM filesystem and RAM disk (initramfs/initrd) support ---> yes +> General setup ---> Configure standard kernel features ---> Enable support for printk ---> yes +> Executable file formats / Emulations ---> Kernel support for ELF binaries ---> yes +> Executable file formats / Emulations ---> Kernel support for scripts starting with #! ---> yes +> Device Drivers ---> Generic Driver Options ---> Maintain a devtmpfs filesystem to mount at /dev ---> yes +> Device Drivers ---> Generic Driver Options ---> Automount devtmpfs at /dev, after the kernel mounted the rootfs ---> yes +> Device Drivers ---> Character devices ---> Enable TTY ---> yes +> Device Drivers ---> Character devices ---> Serial drivers ---> 8250/16550 and compatible serial support ---> yes +> Device Drivers ---> Character devices ---> Serial drivers ---> Console on 8250/16550 and compatible serial port ---> yes +> File systems ---> Pseudo filesystems ---> /proc file system support ---> yes +> File systems ---> Pseudo filesystems ---> sysfs file system support ---> yes +> Processor type and features ---> Linux guest support ---> Support for running PVH guests ---> yes + +you may check `linux/.config` to see whether its configured successfully + +- then compile it: + +```bash +make -j +``` + +## Prepare BusyBox + +- clone [BusyBox](https://github.com/mirror/busybox.git) + +```bash +git clone https://github.com/mirror/busybox.git +``` + +- config it: + +```bash +make defconfig +``` + +- we're willing to compile it statically. + so we need to enable the following features by: + +```bash +make menuconfig +``` + +> Settings ---> Build Options ---> Build static binary + +- then generate the rootfs by: + +```bash +make install +``` + +- modify rootfs: + > create a file named `init` at the root of **\_install** under busybox with following content: + +```bash +#!/bin/sh  +mount -t proc proc /proc +mount -t sysfs sysfs /sys +mount -t devtmpfs devtmpfs /dev  + +echo "Booting BusyBox..." +exec /bin/sh +``` + +- create initramfs by: + +```bash +cd _install +find . | cpio -H newc -o | gzip > ../ramdisk.img +``` + +## Install qemu + +```bash +sudo apt install qemu-kvm +``` + +## Boot the kernel and user space + +```bash +qemu-system-x86_64 -nographic --append "console=ttyS0" -kernel ./vmlinux -initrd ../busybox/ramdisk.img +``` diff --git a/content/posts/second_post.md b/content/posts/second_post.md deleted file mode 100644 index f13b6a4..0000000 --- a/content/posts/second_post.md +++ /dev/null @@ -1,13 +0,0 @@ ---- -title: "Second_post" -date: 2024-05-27T16:41:44+08:00 -draft: false -toc: false -images: -tags: - - untagged ---- - - -## Second post -