#!/bin/sh
#
# Copyright (c) 2026 Kirill A. Korinsky <kirill@korins.ky>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

disk_size=${DISK_SIZE:-128G}
memory=16G
cpus=4

i386_memory=4G      # OpenBSD/i386 supports up to 4Gb ram
i386_cpus=2         # More CPU is supported, but RAM suggest keep it small
armv7_memory=3G     # more than 3G makes U-boot broken
armv7_cpu=1         # OpenBSD/armv7 does not support SMP
hppa_memory=4G      # OpenBSD/hppa is 32bit, so 4G is max
hppa_cpu=2          # Like i386, more CPU probably useless
macppc_memory=2G    # qemu mac99 supports at most 2G of RAM
macppc_cpus=1       # qemu mac99 doesn't support SMP
octeon_cpus=4       # OpenBSD/octeon numcores is hardcoded in pflash files
sparc64_cpu=1       # qemu doesn't support SMP in sparc64

ssh_port=

host_addr=10.0.2.1
http_addr=10.0.2.1

default_installurl=https://cdn.openbsd.org/pub/OpenBSD
installurl_file='/etc/installurl'
examples_dir='/usr/local/share/examples/qobsd'
assets_dir='/usr/local/share/qobsd'

usage() {
	echo "usage: $0 [-n] [-d directory] [-f image] [-H hostname] [-p port] <arch> [qemu args ...]" >&2
	echo "       $0 [-n] [-k] [-d directory] [-f image] [-H hostname] (-m|-i|-u) (-s|-R version) <arch> [installurl] [qemu args ...]" >&2
	echo "       $0 [-n] [-d directory] [-f image] -z <arch> [qemu-img convert args ...]" >&2
	echo "supported arch: amd64 arm64 armv7 hppa i386 macppc octeon powerpc64 riscv64 sparc64" >&2
	exit 2
}

cleanup() {
	[ "$dry_run" = yes ] && return
	[ -n "$tmpdir" ] && rm -rf -- "$tmpdir"
	[ -n "$tmpdisk" ] && rm -f -- "$tmpdisk"
}

run_command() {
	if [ "$dry_run" = yes ]; then
		echo "$*"
	else
		"$@"
	fi
}

require_file() {
	[ "$dry_run" = yes ] && return
	[ -f "$1" ] && [ -r "$1" ] && return

	echo "$1 is not a readable file" >&2
	exit 1
}

resolve_asset() {
	asset_path=$script_dir/$1
	if [ ! -e "$asset_path" ] && [ ! -L "$asset_path" ]; then
		asset_path=$assets_dir/$1
	fi
	require_file "$asset_path"
}

set_project_dir() {
	project_dir=$examples_dir
	case "$mode" in
	run)	set -- "$qemu_pflash";;
	*)	set -- install.conf disklabel site site.perms "$qemu_pflash";;
	esac

	for project_file do
		[ -n "$project_file" ] || continue
		if [ -e "$run_dir/$project_file" ] ||
		    [ -L "$run_dir/$project_file" ]; then
			project_dir=$run_dir
			return
		fi
	done
}

require_project() {
	[ "$dry_run" = yes ] && return

	for project_file in install.conf disklabel; do
		if [ -e "$project_dir/$project_file" ] ||
		    [ -L "$project_dir/$project_file" ]; then
			require_file "$project_dir/$project_file"
		fi
	done

	if [ -e "$project_dir/site" ] || [ -L "$project_dir/site" ] ||
	    [ -e "$project_dir/site.perms" ] ||
	    [ -L "$project_dir/site.perms" ]; then
		if [ ! -d "$project_dir/site" ] ||
		    [ ! -r "$project_dir/site" ] ||
		    [ ! -x "$project_dir/site" ]; then
			echo "$project_dir/site is not a readable directory" >&2
			exit 1
		fi
		require_file "$project_dir/site.perms"
	fi
}

set_assets() {
	if [ -n "$qemu_bios" ]; then
		resolve_asset "$qemu_bios"
		qemu_bios=$asset_path
	fi

	if [ -n "$qemu_pflash" ]; then
		qemu_pflash=$project_dir/$qemu_pflash
		require_file "$qemu_pflash"
		qemu_pflash_readonly=off
		[ "$project_dir" != "$examples_dir" ] ||
		    qemu_pflash_readonly=on
	fi

	case "$mode" in
	manual|autoinstall|autoupgrade)
		resolve_asset dummy-httpd.sh
		httpd=$asset_path
		if [ "$dry_run" != yes ] && [ ! -x "$httpd" ]; then
			echo "$httpd is not executable" >&2
			exit 1
		fi
		;;
	esac
}

setup_tmpdir() {
	[ -n "$tmpdir" ] && return

	if [ "$dry_run" = yes ]; then
		tmpdir=$tmp_parent/openbsd-vm.XXXXXXXXXX
	else
		tmpdir=$(mktemp -d "$tmp_parent/openbsd-vm.XXXXXXXXXX") || exit 1
		trap cleanup EXIT HUP INT TERM
		tmpdir=$(realpath "$tmpdir") || exit 1
	fi
}

set_arch() {
	case "$arch" in
	amd64)
		qemu=qemu-system-x86_64
		qemu_args="-nographic"
		qemu_memory=$memory
		qemu_cpus=$cpus
		net_device=virtio-net-pci
		netboot=pxe
		;;
	arm64)
		qemu=qemu-system-aarch64
		qemu_args="-machine virt -cpu cortex-a57 -nographic -bios /usr/local/share/u-boot/qemu_arm64/u-boot.bin"
		qemu_memory=$memory
		qemu_cpus=$cpus
		net_device=virtio-net-device
		qemu_disk_args=if=none,id=hd0
		qemu_disk_device_args="-device virtio-blk-pci,drive=hd0,bootindex=1"
		netboot=efi
		efi_boot=BOOTAA64.EFI
		;;
	armv7)
		qemu=qemu-system-arm
		qemu_args="-machine virt,virtualization=on -cpu cortex-a15 -nographic -bios /usr/local/share/u-boot/qemu_arm/u-boot.bin"
		qemu_memory=$armv7_memory
		qemu_cpus=$armv7_cpu
		net_device=virtio-net-device
		qemu_disk_args=if=none,id=hd0
		qemu_disk_device_args="-device virtio-blk-device,drive=hd0,bootindex=1"
		netboot=efi
		efi_boot=BOOTARM.EFI
		;;
	hppa)
		qemu=qemu-system-hppa
		qemu_args="-machine B160L -nographic"
		qemu_memory=$hppa_memory
		qemu_cpus=$hppa_cpu
		net_device=e1000
		installer=cdrom
		qemu_disk_args=if=scsi,index=0
		qemu_cdrom_disk_args=if=scsi,index=2,media=cdrom
		qemu_disk_boot_args="-boot c"
		;;
	i386)
		qemu=qemu-system-i386
		qemu_args="-nographic"
		qemu_memory=$i386_memory
		qemu_cpus=$i386_cpus
		net_device=virtio-net-pci
		netboot=pxe
		;;
	macppc)
		qemu=qemu-system-ppc
		qemu_args="-machine mac99,via=cuda,graphics=off,usb=off -global cuda.x-sr-delay-ns=0 -cpu g4 -nographic -prom-env input-device=ttya -prom-env output-device=ttya"
		qemu_bios=openbios-ppc-qobsd-ba2f9b9.elf
		qemu_memory=$macppc_memory
		qemu_cpus=$macppc_cpus
		net_device=e1000
		installer=cdrom
		qemu_disk_args=if=ide,index=0
		qemu_cdrom_disk_args=if=ide,index=2,media=cdrom,readonly=on
		qemu_disk_boot_args="-boot c"
		if [ "$mode" = run ]; then
			qemu_boot_command='boot hd:,ofwboot /bsd'
		fi
		;;
	octeon)
		qemu=qemu-system-mips64
		install_arch=octeon
		qemu_args="-machine octeon3 -accel tcg,thread=multi -nographic -bios /usr/local/share/u-boot/octeon_ebb7304/u-boot.bin"
		qemu_memory=$memory
		qemu_cpus=$octeon_cpus
		net_device=e1000
		netboot=octeon
		qemu_disk_args=if=none,id=hd0
		qemu_disk_device_args="-device usb-storage,drive=hd0"
		if [ "$mode" = run ]; then
			qemu_pflash=u-boot-octeon3-qobsd-boot.pflash
		else
			qemu_pflash=u-boot-octeon3-qobsd-install.pflash
		fi
		;;
	powerpc64)
		qemu=qemu-system-ppc64
		qemu_args="-machine powernv9 -cpu power9 -nographic -bios /usr/local/share/qemu/skiboot.lid -kernel /usr/local/share/talos-ii-pnor/pnor.BOOTKERNEL -device ich9-ahci,id=ahci0,bus=pcie.0"
		qemu_memory=$memory
		qemu_cpus=$cpus
		net_device=e1000e,bus=pcie.1
		installer=miniroot
		qemu_disk_args=if=none,id=hd0
		qemu_disk_device_args="-device ide-hd,bus=ahci0.0,drive=hd0,bootindex=1"
		qemu_installer_disk_device_args="-device ide-hd,bus=ahci0.1,drive=hd0"
		qemu_miniroot_disk_args=if=none,id=miniroot0
		qemu_miniroot_device_args="-device ide-hd,bus=ahci0.0,drive=miniroot0"
		;;
	riscv64)
		qemu=qemu-system-riscv64
		qemu_args="-machine virt -nographic -bios /usr/local/share/opensbi/generic/fw_jump.bin -kernel /usr/local/share/u-boot/qemu-riscv64_smode/u-boot.bin"
		qemu_memory=$memory
		qemu_cpus=$cpus
		net_device=virtio-net-device
		netboot=efi
		efi_boot=BOOTRISCV64.EFI
		;;
	sparc64)
		qemu=qemu-system-sparc64
		qemu_args="-machine sun4u -nographic -prom-env boot-device=disk"
		qemu_bios=openbios-sparc64-qobsd-daa1b25.elf
		qemu_memory=$memory
		qemu_cpus=$sparc64_cpu
		net_legacy_model=sunhme
		installer=miniroot
		qemu_installer_disk_args=if=ide,index=0
		qemu_miniroot_disk_args=if=ide,index=1
		miniroot_boot_device=/pci@1fe,0/pci@1,1/ide@3/ide@0/disk@1
		miniroot_boot_file=bsd
		;;
	*)	usage
		;;
	esac

	disk=${disk:-$arch.qcow2}
}

set_mode() {
	[ "$mode" = run ] || usage
	mode=$1
}

ensure_disk() {
	[ "$dry_run" = yes ] && return
	[ -f "$disk" ] && return
	qemu-img create -f qcow2 "$disk" "$disk_size" || exit 1
}

require_disk() {
	[ "$dry_run" = yes ] && return
	[ -f "$disk" ] && return
	echo "$disk does not exist" >&2
	exit 1
}

shrink_disk() {
	tmpdisk=$disk.shrink.$$
	trap cleanup EXIT HUP INT TERM
	run_command qemu-img convert -p "$@" -O qcow2 "$disk" "$tmpdisk" ||
	    exit 1

	if [ "$dry_run" = yes ]; then
		echo "mv $tmpdisk $disk"
	else
		mv "$tmpdisk" "$disk" || exit 1
	fi
	tmpdisk=
}

set_installurl() {
	if [ -z "$installurl" ] && [ -f "$installurl_file" ]; then
		installurl=$(sed 's/#.*//;/^[	 ]*$/d;s/^[	 ]*//;s/[	 ]*$//;q' "$installurl_file")
	fi

	[ -n "$installurl" ] || installurl=$default_installurl

	if [ "$snapshot" = yes ]; then
		setdir=snapshots/$install_arch
	else
		setdir=$release/$install_arch
	fi

	srcdir=${installurl%/}/$setdir
}

fetch_file() {
	case "$srcdir" in
	ftp://*|http://*|https://*)
		ftp -o "$2" "$srcdir/$1" || exit 1
		;;
	*)
		fetch_source=$(realpath "$srcdir/$1") || exit 1
		ln -s "$fetch_source" "$2" || exit 1
		;;
	esac
	[ "$1" = SHA256.sig ] || verify_file "$@"
}

verify_file() {
	[ "$verify_downloads" = no ] && return
	setup_tmpdir
	[ -f "$tmpdir/SHA256.sig" ] ||
	    fetch_file SHA256.sig "$tmpdir/SHA256.sig"
	(
		cd "${2%/*}" || exit 1
		signify -C -t base -x "$tmpdir/SHA256.sig" "$1"
	) || exit 1
}

require_tftp() {
	[ -n "$netboot" ] && return

	echo "TFTP netboot is not implemented for $arch" >&2
	exit 1
}

find_install_file() {
	prefix=$1
	suffix=$2
	pattern=$3

	if [ -n "$release" ]; then
		install_version=$release
		case "$install_version" in
		*.*)	install_version=${install_version%.*}${install_version#*.};;
		esac
		install_file=$prefix$install_version$suffix
		return
	fi

	case "$srcdir" in
	ftp://*|http://*|https://*)
		install_file=$(ftp -Vo - "$srcdir/" |
		    sed -n "s/.*\($pattern\).*/\1/p" |
		    sort -u |
		    sed -n '$p')
		;;
	*)
		set -- "$srcdir"/$prefix*$suffix
		[ -f "$1" ] || {
			echo "$prefix*$suffix does not exist in $srcdir" >&2
			exit 1
		}
		for install_path do
			install_file=${install_path##*/}
		done
		;;
	esac

	[ -n "$install_file" ] || {
		echo "$prefix*$suffix does not exist in $srcdir" >&2
		exit 1
	}
	install_version=${install_file#"$prefix"}
	install_version=${install_version%"$suffix"}
}

fetch_install_media() {
	prefix=$1
	suffix=$2
	pattern=$3

	find_install_file "$prefix" "$suffix" "$pattern"

	if [ "$dry_run" = yes ]; then
		case "$srcdir" in
		ftp://*|http://*|https://*)
			setup_tmpdir
			install_media=$tmpdir/$install_file
			;;
		*)
			install_media=$srcdir/$install_file
			;;
		esac
		return
	fi

	case "$srcdir" in
	ftp://*|http://*|https://*)
		setup_tmpdir
		install_media=$tmpdir/$install_file
		fetch_file "$install_file" "$install_media"
		;;
	*)
		install_media=$srcdir/$install_file
		[ -f "$install_media" ] || {
			echo "$install_media does not exist" >&2
			exit 1
		}
		verify_file "$install_file" "$install_media"
		;;
	esac
}

setup_miniroot() {
	set_installurl

	fetch_install_media miniroot .img 'miniroot[0-9][0-9]*\.img'
	miniroot=$install_media
	qemu_media=$miniroot
	qemu_media_disk_args=$qemu_miniroot_disk_args
	qemu_media_device_args=$qemu_miniroot_device_args
	[ -n "$miniroot_boot_device" ] &&
	    qemu_media_boot_args="-prom-env boot-device=$miniroot_boot_device $qemu_media_boot_args"
	[ -n "$miniroot_boot_file" ] &&
	    qemu_media_boot_args="-prom-env boot-file=$miniroot_boot_file $qemu_media_boot_args"
	[ -n "$qemu_installer_disk_args" ] &&
	    qemu_disk_args=$qemu_installer_disk_args
	[ -n "$qemu_installer_disk_device_args" ] &&
	    qemu_disk_device_args=$qemu_installer_disk_device_args
}

setup_cdrom() {
	set_installurl

	fetch_install_media cd .iso 'cd[0-9][0-9]*\.iso'
	cdrom=$install_media
	qemu_media=$cdrom
	qemu_media_disk_args=$qemu_cdrom_disk_args
	qemu_media_boot_args="-boot d"
}

setup_tftp() {
	require_tftp

	set_installurl
	setup_tmpdir
	tftproot=$tmpdir/tftp

	if [ "$dry_run" = yes ]; then
		case "$netboot" in
		pxe)	bootprog=pxeboot;;
		efi)	bootprog=$efi_boot;;
		octeon)	bootprog=bsd.rd;;
		*)	echo "TFTP netboot is not implemented for $arch" >&2
			exit 1
			;;
		esac
		bootfile=$bootprog
		case "$mode" in
		autoinstall)	bootfile=auto_install;;
		autoupgrade)	bootfile=auto_upgrade;;
		esac
		return
	fi

	mkdir -p "$tftproot/etc" || exit 1

	fetch_file bsd.rd "$tftproot/bsd.rd"

	case "$netboot" in
	pxe)
		fetch_file pxeboot "$tftproot/pxeboot"
		bootprog=pxeboot
		cat >"$tftproot/etc/boot.conf" <<__EOF
stty com0 115200
set tty com0
boot tftp:bsd.rd
__EOF
		;;
	efi)
		fetch_file "$efi_boot" "$tftproot/$efi_boot"
		bootprog=$efi_boot
		cat >"$tftproot/etc/boot.conf" <<__EOF
boot tftp0a:bsd.rd
__EOF
		;;
	octeon)
		bootprog=bsd.rd
		;;
	*)	echo "TFTP netboot is not implemented for $arch" >&2
		exit 1
		;;
	esac

	bootfile=$bootprog
	case "$mode" in
	autoinstall)	bootfile=auto_install;;
	autoupgrade)	bootfile=auto_upgrade;;
	esac

	if [ "$bootfile" != "$bootprog" ]; then
		ln -s "$bootprog" "$tftproot/$bootfile" || exit 1
	fi
}

setup_http() {
	setup_tmpdir
	httproot=$tmpdir
	[ "$dry_run" = yes ] && return

	if [ -d "$project_dir/site" ]; then
		[ -n "$install_version" ] ||
		    find_install_file base .tgz 'base[0-9][0-9]*\.tgz'
		bsdtar -czf "$httproot/site.tgz" \
		    -C "$project_dir/site" "@$project_dir/site.perms" || exit 1
		[ -f "$httproot/SHA256.sig" ] ||
		    fetch_file SHA256.sig "$httproot/SHA256.sig"
		if grep -q '^SHA256 (BUILDINFO) = ' "$httproot/SHA256.sig"; then
			fetch_file BUILDINFO "$httproot/BUILDINFO"
		fi
		printf '0 INSTALL.%s\n0 SHA256.sig\n0 site%s.tgz\n' "$install_arch" \
		    "$install_version" >"$httproot/index.txt" || exit 1
		http_site_version=$install_version
	fi
}

quote_arg() {
	printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")"
}

set_netdev() {
	net_user="user,host=$host_addr,hostname=$vm_hostname,domainname=$vm_domain"

	if [ -n "$ssh_port" ]; then
		net_user="$net_user,hostfwd=tcp:127.0.0.1:${ssh_port}-:22"
	fi

	if [ -n "$tftproot" ]; then
		net_user="$net_user,tftp=$tftproot,bootfile=$bootfile"
	fi

	if [ -n "$httproot" ]; then
		http_command="$(quote_arg "$httpd") $(quote_arg "$project_dir") $(quote_arg "$httproot") $(quote_arg "$http_site_version")"
		http_command=$(printf '%s' "$http_command" | sed 's/,/,,/g')
		net_user="$net_user,guestfwd=tcp:$http_addr:80-cmd:$http_command"
	fi
}

set_qemu_defaults() {
	qemu_default_args=
	has_memory=no
	has_cpus=no

	for arg do
		case "$arg" in
		-m|-m=*)		has_memory=yes;;
		-smp|-smp=*)	has_cpus=yes;;
		esac
	done

	[ "$has_memory" = yes ] ||
	    qemu_default_args="$qemu_default_args -m $qemu_memory"
	[ "$has_cpus" = yes ] ||
	    qemu_default_args="$qemu_default_args -smp $qemu_cpus"
}

mode=run
dry_run=no
verify_downloads=yes
snapshot=no
release=
run_dir=
disk=
vm_hostname=

while getopts nd:f:H:kp:zmiuR:s opt; do
	case "$opt" in
	n)	dry_run=yes;;
	d)	run_dir=$OPTARG;;
	f)	disk=$OPTARG;;
	H)	vm_hostname=$OPTARG
		case "$vm_hostname" in
		''|.*|*.|*..*|*[!a-zA-Z0-9.-]*) usage;;
		esac
		;;
	k)	verify_downloads=no;;
	p)	ssh_port=$OPTARG;;
	z)	set_mode shrink;;
	m)	set_mode manual;;
	i)	set_mode autoinstall;;
	u)	set_mode autoupgrade;;
	R)	release=$OPTARG;;
	s)	snapshot=yes;;
	*)	usage;;
	esac
done
shift $((OPTIND - 1))

[ "$snapshot" = no ] || [ -z "$release" ] || usage
case "$mode" in
run|shrink)
	[ "$snapshot" = no ] && [ -z "$release" ] || usage
	;;
*)
	[ "$snapshot" = yes ] || [ -n "$release" ] || usage
	[ -z "$ssh_port" ] || usage
	;;
esac
[ $# -ge 1 ] || usage

arch=$1
shift
net_legacy_model=
netboot=
efi_boot=
installer=tftp
qemu_disk_args=
qemu_disk_boot_args=
qemu_disk_device_args=
qemu_installer_disk_args=
qemu_installer_disk_device_args=
qemu_miniroot_disk_args=if=ide,index=1
qemu_cdrom_disk_args=
qemu_miniroot_device_args=
qemu_bios=
qemu_pflash=
qemu_pflash_readonly=
qemu_media=
qemu_media_disk_args=
qemu_media_device_args=
qemu_media_boot_args=
qemu_boot_command=
tmpdir=
tmpdisk=
tftproot=
httproot=
httpd=
http_site_version=
bootfile=
miniroot=
cdrom=
miniroot_boot_device=
miniroot_boot_file=
installurl=
install_version=
install_arch=$arch
script_dir=$(dirname "$(realpath "$0")") || exit 1
if [ -n "$run_dir" ]; then
	cd "$run_dir" || exit 1
fi
run_dir=$(pwd -P) || exit 1
tmp_parent=${TMPDIR:-/tmp}
vm_hostname=${vm_hostname:-qobsd-$arch}
case "$vm_hostname" in
*.*)	vm_domain=${vm_hostname#*.};;
*)	vm_domain=$(hostname)
	vm_hostname=$vm_hostname.$vm_domain;;
esac

case "$mode" in
manual|autoinstall|autoupgrade)
	if [ $# -gt 0 ] && [ "${1#-}" = "$1" ]; then
		installurl=$1
		shift
	fi
	;;
esac

set_arch
set_qemu_defaults "$@"

if [ "$mode" = shrink ]; then
	require_disk
	shrink_disk "$@"
	exit
fi

set_project_dir
case "$mode" in
run)
	require_disk
	;;
manual)
	require_project
	[ -f "$project_dir/install.conf" ] || require_disk
	;;
autoinstall)
	require_tftp
	require_project
	require_file "$project_dir/install.conf"
	;;
autoupgrade)
	require_tftp
	require_project
	require_file "$project_dir/install.conf"
	require_disk
	;;
esac
set_assets

case "$mode" in
manual)
	ensure_disk
	if [ "$installer" = miniroot ]; then
		setup_miniroot
	elif [ "$installer" = cdrom ]; then
		setup_cdrom
	else
		setup_tftp
	fi
	setup_http
	;;
autoinstall)
	ensure_disk
	setup_tftp
	setup_http
	;;
autoupgrade)
	setup_tftp
	setup_http
	;;
esac

set_netdev

qemu_boot_args=$qemu_media_boot_args
if [ -z "$qemu_boot_args" ] && [ -n "$tftproot" ]; then
	[ "$netboot" = pxe ] &&
	    qemu_boot_args="-boot once=n,reboot-timeout=1 $qemu_boot_args"
elif [ -z "$qemu_boot_args" ]; then
	qemu_boot_args=$qemu_disk_boot_args
fi
if [ "$mode" != run ]; then
	qemu_boot_args="$qemu_boot_args -no-reboot"
fi
qemu_shutdown_args="-action shutdown=poweroff"
qemu_media_args=
qemu_target_args=

if [ -n "$qemu_media" ]; then
	qemu_media_args="-drive file=$qemu_media,format=raw,$qemu_media_disk_args"
	qemu_media_args="$qemu_media_args $qemu_media_device_args"
fi
if [ -n "$qemu_disk_args" ]; then
	qemu_target_option=-drive
	qemu_target_args="file=$disk,format=qcow2,$qemu_disk_args"
else
	qemu_target_option=-hda
	qemu_target_args=$disk
fi
if [ -n "$net_legacy_model" ]; then
	set -- -net "nic,model=$net_legacy_model" -net "$net_user" "$@"
else
	set -- -netdev "user,id=net0,${net_user#user,}" \
	    -device "$net_device,netdev=net0" "$@"
fi
if [ -n "$qemu_pflash" ]; then
	set -- -drive "file=$qemu_pflash,format=raw,if=pflash,readonly=$qemu_pflash_readonly" "$@"
fi
if [ -n "$qemu_bios" ]; then
	set -- -bios "$qemu_bios" "$@"
fi
if [ -n "$qemu_boot_command" ]; then
	set -- -prom-env "boot-command=$qemu_boot_command" "$@"
fi

run_command "$qemu" $qemu_args $qemu_default_args \
    $qemu_boot_args $qemu_shutdown_args \
    $qemu_media_args \
    "$qemu_target_option" "$qemu_target_args" \
    $qemu_disk_device_args \
    "$@"
