diff options
author | Helmut Grohne <helmut@subdivi.de> | 2022-12-20 14:29:17 +0100 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2022-12-20 14:29:17 +0100 |
commit | e3258ab831172b3760577507b0437365f343cc5e (patch) | |
tree | 9a007fca948beb324dc4ea5acd10382665d73586 /debvm-run | |
download | debvm-e3258ab831172b3760577507b0437365f343cc5e.tar.gz |
initial checkin
Diffstat (limited to 'debvm-run')
-rwxr-xr-x | debvm-run | 124 |
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" "$@" |