Setup an OpenBSD RISCV64 VM in QEMU

Last modification on

This describes how to setup an OpenBSD RISCV64 VM in QEMU.

The shellscript below does the following:

  • Set up the disk image (raw format).
  • Patch the disk image with the OpenBSD miniroot file for the installation.
  • Downloads the opensbi and u-boot firmware files for qemu.
  • Run the VM with the supported settings.

The script is tested on the host GNU/Void Linux and OpenBSD-current.

IMPORTANT!: The signature and checksum for the miniroot, u-boot and opensbi files are not verified. If the host is OpenBSD make sure to instead install the packages (pkg_add u-boot-riscv64 opensbi) and adjust the firmware path for the qemu -bios and -kernel options.

Shellscript

#!/bin/sh
# mirror list: https://www.openbsd.org/ftp.html
mirror="https://ftp.bit.nl/pub/OpenBSD/"
release="7.0"
minirootname="miniroot70.img"

miniroot() {
	test -f "${minirootname}" && return # download once

	url="${mirror}/${release}/riscv64/${minirootname}"
	curl -o "${minirootname}" "${url}"
}

createrootdisk() {
	test -f disk.raw && return # create once
	qemu-img create disk.raw 10G # create 10 GB disk
	dd conv=notrunc if=${minirootname} of=disk.raw # write miniroot to disk
}

opensbi() {
	f="opensbi.tgz"
	test -f "${f}" && return # download and extract once.

	url="${mirror}/${release}/packages/amd64/opensbi-0.9p0.tgz"
	curl -o "${f}" "${url}"

	tar -xzf "${f}" share/opensbi/generic/fw_jump.bin
}

uboot() {
	f="uboot.tgz"
	test -f "${f}" && return # download and extract once.

	url="${mirror}/${release}/packages/amd64/u-boot-riscv64-2021.07p0.tgz"
	curl -o "${f}" "${url}"

	tar -xzf "${f}" share/u-boot/qemu-riscv64_smode/u-boot.bin
}

setup() {
	miniroot
	createrootdisk
	opensbi
	uboot
}

run() {
	qemu-system-riscv64 \
		-machine virt \
		-nographic \
		-m 2048M \
		-smp 2 \
		-bios share/opensbi/generic/fw_jump.bin \
		-kernel share/u-boot/qemu-riscv64_smode/u-boot.bin \
		-drive file=disk.raw,format=raw,id=hd0 -device virtio-blk-device,drive=hd0 \
		-netdev user,id=net0,ipv6=off -device virtio-net-device,netdev=net0
}

setup
run