diff options
author | johannst <johannes.stoelp@gmail.com> | 2021-07-15 21:20:14 +0200 |
---|---|---|
committer | johannst <johannes.stoelp@gmail.com> | 2021-07-15 21:20:14 +0200 |
commit | 82e9ac4163b46b59e121194f84ac370818482923 (patch) | |
tree | b52adf8f5b2bafe7904f563c33ec23f46cb7c87c /content/2019-10-27-kernel-debugging-qemu | |
parent | 617d73fc9eff5b08a80b873fd97f66caa7e80fc9 (diff) | |
download | blog-82e9ac4163b46b59e121194f84ac370818482923.tar.gz blog-82e9ac4163b46b59e121194f84ac370818482923.zip |
use proper date fmt in content file names that zola can automatically can derive the date
Diffstat (limited to 'content/2019-10-27-kernel-debugging-qemu')
5 files changed, 160 insertions, 0 deletions
diff --git a/content/2019-10-27-kernel-debugging-qemu/Dockerfile b/content/2019-10-27-kernel-debugging-qemu/Dockerfile new file mode 100644 index 0000000..42e1f05 --- /dev/null +++ b/content/2019-10-27-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/2019-10-27-kernel-debugging-qemu/Makefile b/content/2019-10-27-kernel-debugging-qemu/Makefile new file mode 100644 index 0000000..11e7c7b --- /dev/null +++ b/content/2019-10-27-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/2019-10-27-kernel-debugging-qemu/build_initrd.sh b/content/2019-10-27-kernel-debugging-qemu/build_initrd.sh new file mode 100755 index 0000000..fd82990 --- /dev/null +++ b/content/2019-10-27-kernel-debugging-qemu/build_initrd.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +if test $(id -u) -ne 0; then + SUDO=sudo +fi + +set -e + +BUSYBOX=busybox-1.33.1 +INITRD=$PWD/initramfs.cpio.gz + +## Build busybox + +echo "[+] configure & build $BUSYBOX ..." +[[ ! -d $BUSYBOX ]] && { + wget https://busybox.net/downloads/$BUSYBOX.tar.bz2 + bunzip2 $BUSYBOX.tar.bz2 && tar xf $BUSYBOX.tar +} + +cd $BUSYBOX +make defconfig +sed -i 's/# CONFIG_STATIC .*/CONFIG_STATIC=y/' .config +make -j4 busybox +make install + +## Create initrd + +echo "[+] create initrd $INITRD ..." + +cd _install + +# 1. create initrd folder structure +mkdir -p bin sbin etc proc sys usr/bin usr/sbin dev + +# 2. create init process +cat <<EOF > init +#!/bin/sh + +mount -t proc none /proc +mount -t sysfs none /sys + +exec setsid cttyhack sh +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 + +# 4. created compressed initrd +find . -print0 \ + | cpio --null -ov --format=newc \ + | gzip -9 > $INITRD diff --git a/content/2019-10-27-kernel-debugging-qemu/build_kernel.sh b/content/2019-10-27-kernel-debugging-qemu/build_kernel.sh new file mode 100755 index 0000000..7ae3014 --- /dev/null +++ b/content/2019-10-27-kernel-debugging-qemu/build_kernel.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +set -e + +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 + +cd $LINUX + +cat <<EOF > kernel_fragment.config +# 64bit kernel +CONFIG_64BIT=y +# enable support for compressed initrd (gzip) +CONFIG_BLK_DEV_INITRD=y +CONFIG_RD_GZIP=y +# support for ELF and #! binary format +CONFIG_BINFMT_ELF=y +CONFIG_BINFMT_SCRIPT=y +# /dev +CONFIG_DEVTMPFS=y +CONFIG_DEVTMPFS_MOUNT=y +# tty & console +CONFIG_TTY=y +CONFIG_SERIAL_8250=y +CONFIG_SERIAL_8250_CONSOLE=y +# pseudo fs +CONFIG_PROC_FS=y +CONFIG_SYSFS=y +# debugging +CONFIG_DEBUG_INFO=y +CONFIG_PRINTK=y +CONFIG_EARLY_PRINTK=y +EOF + +make tinyconfig +./scripts/kconfig/merge_config.sh -n ./kernel_fragment.config +make -j4 diff --git a/content/2019-10-27-kernel-debugging-qemu/run.sh b/content/2019-10-27-kernel-debugging-qemu/run.sh new file mode 100755 index 0000000..b0a84ae --- /dev/null +++ b/content/2019-10-27-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 |