aboutsummaryrefslogtreecommitdiffhomepage
path: root/content/20191027-kernel-debugging-qemu
diff options
context:
space:
mode:
Diffstat (limited to 'content/20191027-kernel-debugging-qemu')
-rw-r--r--content/20191027-kernel-debugging-qemu/Dockerfile32
-rw-r--r--content/20191027-kernel-debugging-qemu/Makefile12
-rwxr-xr-x[-rw-r--r--]content/20191027-kernel-debugging-qemu/build_initrd.sh12
-rwxr-xr-x[-rw-r--r--]content/20191027-kernel-debugging-qemu/build_kernel.sh2
-rwxr-xr-xcontent/20191027-kernel-debugging-qemu/run.sh24
5 files changed, 77 insertions, 5 deletions
diff --git a/content/20191027-kernel-debugging-qemu/Dockerfile b/content/20191027-kernel-debugging-qemu/Dockerfile
new file mode 100644
index 0000000..42e1f05
--- /dev/null
+++ b/content/20191027-kernel-debugging-qemu/Dockerfile
@@ -0,0 +1,32 @@
+FROM ubuntu:20.04
+MAINTAINER Johannes Stoelp <johannes.stoelp@gmail.edu>
+
+RUN apt update \
+ && DEBIAN_FRONTEND=noninteractive \
+ apt install \
+ --yes \
+ --no-install-recommends \
+ # Download & unpack.
+ wget \
+ ca-certificates \
+ xz-utils \
+ # Build tools & deps (kernel).
+ make \
+ bc \
+ gcc g++ \
+ flex bison \
+ libelf-dev \
+ # Build tools & deps (initrd).
+ cpio \
+ # Run & debug.
+ qemu-system-x86 \
+ gdb \
+ telnet \
+ # Convenience.
+ ripgrep \
+ fd-find \
+ neovim \
+ && rm -rf /var/lib/apt/lists/* \
+ && apt-get clean
+
+WORKDIR /develop
diff --git a/content/20191027-kernel-debugging-qemu/Makefile b/content/20191027-kernel-debugging-qemu/Makefile
new file mode 100644
index 0000000..11e7c7b
--- /dev/null
+++ b/content/20191027-kernel-debugging-qemu/Makefile
@@ -0,0 +1,12 @@
+build:
+ scripts/build_kernel.sh
+ scripts/build_initrd.sh
+
+clean:
+ $(RM) -r linux-*
+ $(RM) -r busybox-*
+ $(RM) initramfs.cpio.gz
+
+docker:
+ DOCKER_BUILDKIT=1 docker build -t kernel-dev .
+ docker run -it --rm -v $(PWD):/develop/scripts -v $(PWD)/Makefile:/develop/Makefile kernel-dev
diff --git a/content/20191027-kernel-debugging-qemu/build_initrd.sh b/content/20191027-kernel-debugging-qemu/build_initrd.sh
index 74f9896..fd82990 100644..100755
--- a/content/20191027-kernel-debugging-qemu/build_initrd.sh
+++ b/content/20191027-kernel-debugging-qemu/build_initrd.sh
@@ -1,8 +1,12 @@
#!/bin/bash
+if test $(id -u) -ne 0; then
+ SUDO=sudo
+fi
+
set -e
-BUSYBOX=busybox-1.31.0
+BUSYBOX=busybox-1.33.1
INITRD=$PWD/initramfs.cpio.gz
## Build busybox
@@ -40,9 +44,9 @@ EOF
chmod +x init
# 3. create device nodes
-sudo mknod dev/tty c 5 0
-sudo mknod dev/tty0 c 4 0
-sudo mknod dev/ttyS0 c 4 64
+$SUDO mknod dev/tty c 5 0
+$SUDO mknod dev/tty0 c 4 0
+$SUDO mknod dev/ttyS0 c 4 64
# 4. created compressed initrd
find . -print0 \
diff --git a/content/20191027-kernel-debugging-qemu/build_kernel.sh b/content/20191027-kernel-debugging-qemu/build_kernel.sh
index f1e15bb..7ae3014 100644..100755
--- a/content/20191027-kernel-debugging-qemu/build_kernel.sh
+++ b/content/20191027-kernel-debugging-qemu/build_kernel.sh
@@ -2,7 +2,7 @@
set -e
-LINUX=linux-5.3.7
+LINUX=linux-5.13.2
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/$LINUX.tar.xz
unxz $LINUX.tar.xz && tar xf $LINUX.tar
diff --git a/content/20191027-kernel-debugging-qemu/run.sh b/content/20191027-kernel-debugging-qemu/run.sh
new file mode 100755
index 0000000..b0a84ae
--- /dev/null
+++ b/content/20191027-kernel-debugging-qemu/run.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+VER=5.13.2
+
+# Launch the emulator with our kernel.
+qemu-system-x86_64 \
+ -kernel ./linux-$VER/arch/x86/boot/bzImage \
+ -nographic \
+ -append "earlyprintk=ttyS0 console=ttyS0 nokaslr init=/init debug" \
+ -initrd ./initramfs.cpio.gz \
+ -serial telnet:localhost:12345,server,nowait \
+ -monitor none \
+ -gdb tcp::1234 \
+ -S &
+
+# Kill qemu when we exit.
+QEMU_PID=$!
+trap "kill $QEMU_PID" EXIT
+
+# Give qemu some time to come up.
+sleep 0.5
+
+# Attach debugger to qemu and load the kernel symbols.
+gdb -ex 'target remote :1234' ./linux-$VER/vmlinux