From fdbe7fe42aed31a81493a19482b6d8a8fb6a492d Mon Sep 17 00:00:00 2001
From: Thomas Schneider <thomas@fsmpi.rwth-aachen.de>
Date: Tue, 7 Nov 2023 11:48:53 +0100
Subject: [PATCH] Adadpt for Linux Kernel Programming lecture, some
 documentation

---
 launch.1      | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++
 launch.sh     | 48 ++++++++++++++++++++++++++++++++++++-------
 make-image.sh |  4 +++-
 3 files changed, 101 insertions(+), 8 deletions(-)
 create mode 100644 launch.1

diff --git a/launch.1 b/launch.1
new file mode 100644
index 0000000..38ed032
--- /dev/null
+++ b/launch.1
@@ -0,0 +1,57 @@
+.Dd November 07, 2023
+.Dt LAUNCH.SH 1
+.Os
+.Sh NAME
+.Nm launch.sh
+.Nd launch a Linux Kernel development VM
+.Sh SYNOPSIS
+.Nm
+.Op Fl k Ar kernel
+.Op Fl i Ar initrd
+.Op Fl a Ar append
+.Op Fl s Ar share
+.Op Fl -
+.Op Ar "extra qemu arguments"
+.Sh DESCRIPTION
+.Nm
+launches a prepared VM image using
+.Xr qemu 1 ,
+suitable for Linux Kernel development.
+.Pp
+The options are as follows:
+.Bl -tag -width Ds
+.It Fl k Ar kernel
+Use
+.Ar kernel
+to boot the VM.
+.It Fl i Ar initrd
+Provide
+.Ar initrd
+as initrd/initramfs image to the kernel.
+Use an empty argument to boot
+.Em without
+an initrd/initramfs.
+.It Fl a Ar append
+Append
+.Ar append
+to the default kernel command line.
+.It Fl s Ar share
+Provide the directory
+.Ar share
+to the VM using virtfs/9p.
+.It Ar "extra qemu arguments"
+Specify extra arguments to pass to
+.Xr qemu 1 .
+.El
+.Sh EXIT STATUS
+.Ex -std
+.Sh EXAMPLES
+The following invocation launches a VM with a custom kernel, no initramfs, and a
+share directory:
+.Dl $ ./ Ns Nm Fl k Pa ../linux/arch/x86/boot/bzImage Fl i Pa "''" Fl s Pa ../share
+.Sh SEE ALSO
+.Xr qemu 1
+.Sh AUTHORS
+.An Thomas Schneider Aq Mt thomas@fsmpi.rwth-aachen.de
+.Sh BUGS
+Currently, too many options that should be customisable are hard-coded.
diff --git a/launch.sh b/launch.sh
index 7c42af1..6f21754 100755
--- a/launch.sh
+++ b/launch.sh
@@ -1,5 +1,43 @@
-#!/bin/sh
+#!/usr/bin/env bash
 set -e
+
+H=$(dirname "$0")
+kernel="$H/vmlinuz-virt"
+initrd="$H/initramfs-virt"
+append=""
+share=""
+
+optstring="k:i:a:s:"
+while getopts $optstring opt; do
+    case $opt in
+	k)
+	    kernel="$OPTARG"
+	    ;;
+	i)
+	    initrd="$OPTARG"
+	    ;;
+	a)
+	    append="$OPTARG"
+	    ;;
+	s)
+	    share="$OPTARG"
+	    ;;
+	'?')
+	    printf "Usage: %s [-k KERNEL] [-i INITRD] [-a APPEND] [-s SHARE] [...]\n" "$0" 1>&2
+	    exit 1
+	    ;;
+    esac
+done
+shift $((OPTIND - 1))
+
+qargs=(
+    -kernel "$kernel"
+)
+test -n "$initrd" && qargs+=(-initrd "$initrd")
+qargs+=(-append "root=/dev/vda overlaytmpfs console=hvc0 earlyprintk=hvc0 $append")
+test -n "$share" && qargs+=(-virtfs "local,path=$share,mount_tag=share,security_model=mapped-file,id=share")
+qargs+=("$@")
+
 set -x
 exec qemu-system-x86_64 \
 	-M microvm,x-option-roms=off,pit=off,pic=off,rtc=off,isa-serial=off,pcie=on \
@@ -14,9 +52,6 @@ exec qemu-system-x86_64 \
 	-device virtio-rng-device \
 	-drive id=root,file=image.sqfs,format=raw,if=none \
 	-device virtio-blk-device,drive=root \
-	-kernel vmlinuz-virt \
-	-initrd initramfs-virt \
-	-append "root=/dev/vda overlaytmpfs console=hvc0 earlyprintk=hvc0" \
 	-chardev stdio,mux=on,id=stdio \
 	-device virtio-serial-device \
 	-device virtconsole,chardev=stdio,name=console.0 \
@@ -25,7 +60,6 @@ exec qemu-system-x86_64 \
 	-device virtio-net-device,netdev=net \
 	-chardev socket,path=qga,server=on,wait=off,id=qga \
 	-device virtserialport,chardev=qga,name=org.qemu.guest_agent.0 \
-	-chardev socket,id=virtfs,path=/tmp/virtfs.sock \
-	-device vhost-user-fs-pci,chardev=virtfs,tag=virtfs,queue-size=1024 \
 	-object memory-backend-memfd,id=mem,size=1536M,share=on \
-	-numa node,memdev=mem
+	-numa node,memdev=mem \
+	"${qargs[@]}"
diff --git a/make-image.sh b/make-image.sh
index bf4c68b..7f9341f 100755
--- a/make-image.sh
+++ b/make-image.sh
@@ -13,10 +13,12 @@ ln -s "$PWD/apk-cache" /etc/apk/cache
 ln -s "$PWD/apk-cache" /image/etc/apk/cache
 cp -r /etc/apk/repositories /etc/apk/keys /image/etc/apk
 apk add --root /image --update-cache --initdb alpine-base mkinitfs \
-	qemu-guest-agent git git-lfs gitlab-runner bash buildah
+	qemu-guest-agent
 echo 'features="base squashfs virtio"' > /image/etc/mkinitfs/mkinitfs.conf
 apk add --root /image linux-virt
 
+echo "share /mnt 9p trans=virtio,version=9p2000.L,nofail" >> /image/etc/fstab
+
 oldpwd="$PWD"
 cd /image/etc/runlevels
 for i in devfs dmesg mdev hwdrivers cgroups; do
-- 
GitLab