From f48cc92c8c187b92e8072a10a256695879e3bd43 Mon Sep 17 00:00:00 2001
From: Helmut Grohne <helmut@subdivi.de>
Date: Sun, 15 Jan 2023 19:46:58 +0100
Subject: debvm-run: guess the location of the kernel

---
 README.md     |  8 ++++----
 bin/debvm-run | 36 +++++++++++++++---------------------
 2 files changed, 19 insertions(+), 25 deletions(-)

diff --git a/README.md b/README.md
index 870e26d..ec1fcb3 100644
--- a/README.md
+++ b/README.md
@@ -45,10 +45,10 @@ contains the root filesystem of a (Debian) installation including an init
 system and a kernel. There is no partition table or bootloader. The following
 paths are assumed inside:
  * `/bin/true` is used to detect the architecture of an image
- * `(|/boot)/vmlinu[xz]` (depending on the architecture) must be a symbolic
-   link pointing to a regular file containing the kernel.
- * `/initrd.img` must be a symbolic link pointing to a regular file containing
-   the initrd image.
+ * `(|/boot)/vmlinu[xz]` must be a symbolic link pointing to a regular file
+   containing the kernel.
+ * `initrd.img` must be a symbolic link in the same directory as the kernel
+   image pointing to a regular file containing the initrd image.
 
 Why?
 ====
diff --git a/bin/debvm-run b/bin/debvm-run
index 65eb65c..39928d2 100755
--- a/bin/debvm-run
+++ b/bin/debvm-run
@@ -16,7 +16,7 @@ B<debvm-run> [B<-g>] [B<-i> F<image>] [B<-s> I<sshport>] [B<--> I<qemu options>]
 B<debvm-run> is essentially a thin wrapper around B<qemu> for running a virtual machine image created by B<debvm-create> or something compatible.
 The virtual machine image is expected to be a raw ext4 image with file system label B<debvm>.
 The architecture of the machine is detected from the contained F</bin/true>.
-It must contain a symbolic link pointing to a kernel image at F<(|/boot)/vmlinu[xz]> depending on the architecture and a symbolic link pointing to an initrd image at F</initrd.img>.
+It must contain a symbolic link pointing to a kernel image at one of F<(|/boot)/vmlinu[xz]> a symbolic link pointing to an initrd image at F<initrd.img> in the same directory as the kernel image.
 Both are extracted and passed to B<qemu>.
 A net interface configured for user mode is added automatically.
 
@@ -186,26 +186,20 @@ if command -v elf-arch >/dev/null 2>&1; then
 	/sbin/debugfs "$IMAGE" -R "cat /bin/true" > "$KERNELTMP"
 	VMARCH=$(elf-arch "$KERNELTMP")
 fi
-case "$VMARCH" in
-	mips*|ppc64el)
-		KERNELLINK=vmlinux
-	;;
-	*)
-		KERNELLINK=vmlinuz
-	;;
-esac
-
-KERNELPREFIX=""
-KERNELLINKFILE=$(/sbin/debugfs "$IMAGE" -R "stat $KERNELLINK")
-if test -z "$KERNELLINKFILE"; then
-	KERNELPREFIX=boot/
-	KERNELLINKFILE=$(/sbin/debugfs "$IMAGE" -R "stat ${KERNELPREFIX}$KERNELLINK")
-fi
-
-KERNELNAME=$(echo "$KERNELLINKFILE" | sed 's/Fast link dest: "\(.*\)"/\1/;t;d')
-INITRDNAME=$(/sbin/debugfs "$IMAGE" -R "stat ${KERNELPREFIX}initrd.img" | sed 's/Fast link dest: "\(.*\)"/\1/;t;d')
 
+for KERNELLINK in vmlinuz vmlinux boot/vmlinuz boot/vmlinux; do
+	KERNELNAME=$(/sbin/debugfs "$IMAGE" -R "stat $KERNELLINK" | sed 's/Fast link dest: "\(.*\)"/\1/;t;d')
+	test -n "$KERNELNAME" && break
+done
 test -n "$KERNELNAME" || die "failed to discover kernel image"
+
+if test "${KERNELLINK#boot/}" = "$KERNELLINK"; then
+	INITRDLINK=initrd.img
+else
+	INITRDLINK=boot/initrd.img
+fi
+INITRDNAME=$(/sbin/debugfs "$IMAGE" -R "stat $INITRDLINK" | sed 's/Fast link dest: "\(.*\)"/\1/;t;d')
+
 test -n "$INITRDNAME" || die "failed to discover initrd image"
 
 KERNEL_CMDLINE="root=LABEL=debvm rw"
@@ -293,7 +287,7 @@ set -- \
 
 set -ex
 
-/sbin/debugfs "$IMAGE" -R "cat ${KERNELPREFIX}$KERNELNAME" > "$KERNELTMP"
-/sbin/debugfs "$IMAGE" -R "cat ${KERNELPREFIX}$INITRDNAME" > "$INITRDTMP"
+/sbin/debugfs "$IMAGE" -R "cat $KERNELNAME" > "$KERNELTMP"
+/sbin/debugfs "$IMAGE" -R "cat $INITRDNAME" > "$INITRDTMP"
 
 "$QEMU" "$@"
-- 
cgit v1.2.3