summaryrefslogtreecommitdiff
path: root/debvm-run
diff options
context:
space:
mode:
Diffstat (limited to 'debvm-run')
-rwxr-xr-xdebvm-run124
1 files changed, 124 insertions, 0 deletions
diff --git a/debvm-run b/debvm-run
new file mode 100755
index 0000000..112bd40
--- /dev/null
+++ b/debvm-run
@@ -0,0 +1,124 @@
+#!/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"
+
+KERNELNAME=$(/sbin/debugfs "$IMAGE" -R "stat vmlinuz" | sed 's/Fast link dest: "\(.*\)"/\1/;t;d')
+INITRDNAME=$(/sbin/debugfs "$IMAGE" -R "stat initrd.img" | sed 's/Fast link dest: "\(.*\)"/\1/;t;d')
+
+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
+ arm64|s390x)
+ DISKDEV=vda
+ ;;
+ *)
+ DISKDEV=sda
+ ;;
+esac
+
+KERNEL_CMDLINE=root=/dev/$DISKDEV
+NETDEV="user,id=net0"
+
+set -- \
+ -m 1G \
+ -smp "$(nproc)" \
+ -kernel "$KERNELTMP" \
+ -initrd "$INITRDTMP" \
+ -drive "media=disk,format=raw,discard=unmap,file=$IMAGE" \
+ -device "virtio-net-pci,netdev=net0" \
+ "$@"
+
+if test "$ARCHITECTURE" = "$VMARCH"; then
+ QEMU=kvm
+ set -- -enable-kvm -cpu host "$@"
+else
+ case "$VMARCH" in
+ arm64)
+ QEMU=qemu-system-aarch64
+ set -- -machine virt -cpu max "$@"
+ ;;
+ *)
+ QEMU="qemu-system-$VMARCH"
+ ;;
+ esac
+fi
+if test "$VMARCH" = arm64; then
+ set -- -nographic "$@"
+elif test "$VMARCH" = s390x; then
+ set -- -M s390-ccw-virtio -nographic "$@"
+elif test -z "$GRAPHICAL"; then
+ KERNEL_CMDLINE="$KERNEL_CMDLINE console=ttyS0"
+ set -- -nographic "$@"
+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" "$@"