From 617d73fc9eff5b08a80b873fd97f66caa7e80fc9 Mon Sep 17 00:00:00 2001 From: johannst Date: Thu, 15 Jul 2021 21:16:42 +0200 Subject: added dockerfile for kernel dev --- content/20191027-kernel-debugging-qemu/Dockerfile | 32 ++++++++++++++++++++++ content/20191027-kernel-debugging-qemu/Makefile | 12 ++++++++ .../20191027-kernel-debugging-qemu/build_initrd.sh | 12 +++++--- .../20191027-kernel-debugging-qemu/build_kernel.sh | 2 +- content/20191027-kernel-debugging-qemu/run.sh | 24 ++++++++++++++++ 5 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 content/20191027-kernel-debugging-qemu/Dockerfile create mode 100644 content/20191027-kernel-debugging-qemu/Makefile mode change 100644 => 100755 content/20191027-kernel-debugging-qemu/build_initrd.sh mode change 100644 => 100755 content/20191027-kernel-debugging-qemu/build_kernel.sh create mode 100755 content/20191027-kernel-debugging-qemu/run.sh (limited to 'content/20191027-kernel-debugging-qemu') 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 + +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 old mode 100644 new mode 100755 index 74f9896..fd82990 --- 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 old mode 100644 new mode 100755 index f1e15bb..7ae3014 --- 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 -- cgit v1.2.3