#!/bin/sh
# Copyright 2022 Helmut Grohne <helmut@subdivi.de>
# SPDX-License-Identifier: MIT

set -u

IMAGE=rootfs.ext2
SSHPORT=
GRAPHICAL=

die() {
	echo "$*" 1>&2
	exit 1
}

usage() {
	die "usage: $0 [-g] [-i image] [-s sshport] [-- qemu options]"
}

while test "$#" -gt 0; do
	case "$1" in
		-g)
			GRAPHICAL=1
			shift
		;;
		-i)
			test "$#" -eq 1 && usage
			IMAGE=$2
			shift 2
		;;
		-s)
			test "$#" -eq 1 && usage
			SSHPORT=$2
			shift 2
		;;
		--)
			shift
			break
		;;
		*)
			usage
		;;
	esac
done

test -f "$IMAGE" || die "image '$IMAGE' not found"
test -s "$IMAGE" || die "image '$IMAGE' is empty"

cleanup() {
	set +x
	test -n "$KERNELTMP" && rm -f "$KERNELTMP"
	test -n "$INITRDTMP" && rm -f "$INITRDTMP"
}

trap cleanup EXIT INT TERM QUIT

KERNELTMP=$(mktemp)
INITRDTMP=$(mktemp)

ARCHITECTURE=$(dpkg --print-architecture)
VMARCH=$ARCHITECTURE
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

KERNELNAME=$(/sbin/debugfs "$IMAGE" -R "stat $KERNELLINK" | sed 's/Fast link dest: "\(.*\)"/\1/;t;d')
INITRDNAME=$(/sbin/debugfs "$IMAGE" -R "stat initrd.img" | sed 's/Fast link dest: "\(.*\)"/\1/;t;d')

test -n "$KERNELNAME" || die "failed to discover kernel image"
test -n "$INITRDNAME" || die "failed to discover initrd image"

KERNEL_CMDLINE="root=/dev/vda rw"
NETDEV="user,id=net0"

set -- \
	-no-user-config \
	-name "debvm-run $IMAGE" \
	-m 1G \
	-smp "$(nproc)" \
	-kernel "$KERNELTMP" \
	-initrd "$INITRDTMP" \
	-object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0 \
	-drive "media=disk,format=raw,discard=unmap,file=$IMAGE,if=virtio,cache=unsafe" \
	-device "virtio-net-pci,netdev=net0" \
	"$@"

if test "$ARCHITECTURE" = "$VMARCH"; then
	QEMU=kvm
	# While kvm will fall back gracefully, the following options can only
	# be passed when kvm really is available.
	if test -w /dev/kvm; then
		set -- -enable-kvm -cpu host "$@"
	fi
	case "$VMARCH" in
		arm64)
			set -- -machine type=virt,gic-version=host "$@"
		;;
	esac
else
	case "$VMARCH" in
		arm64)
			QEMU=qemu-system-aarch64
			set -- -machine virt -cpu max "$@"
		;;
		arm|armel|armhf)
			QEMU=qemu-system-arm
			set -- -machine virt -cpu max "$@"
		;;
		ppc64el)
			QEMU=qemu-system-ppc64
		;;
		*)
			QEMU="qemu-system-$VMARCH"
		;;
	esac
fi

if test -z "$GRAPHICAL"; then
	set -- -nographic "$@"
	case "$VMARCH" in
		amd64|i386)
			KERNEL_CMDLINE="$KERNEL_CMDLINE console=ttyS0"
		;;
	esac
fi

if test -n "$SSHPORT"; then
	NETDEV="$NETDEV,hostfwd=tcp:127.0.0.1:$SSHPORT-:22"
fi
set -- \
	-append "$KERNEL_CMDLINE" \
	-netdev "$NETDEV" \
	"$@"

set -ex

/sbin/debugfs "$IMAGE" -R "cat $KERNELNAME" > "$KERNELTMP"
/sbin/debugfs "$IMAGE" -R "cat $INITRDNAME" > "$INITRDTMP"

"$QEMU" "$@"