Skip to content

Commit

Permalink
publish debug linux kernel with qemu and busybox
Browse files Browse the repository at this point in the history
  • Loading branch information
dupeiran001 committed Sep 25, 2024
1 parent f306caa commit 71db5ca
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 13 deletions.
127 changes: 127 additions & 0 deletions content/posts/debug_linux_kernel.md
Original file line number Diff line number Diff line change
@@ -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
```
13 changes: 0 additions & 13 deletions content/posts/second_post.md

This file was deleted.

0 comments on commit 71db5ca

Please sign in to comment.