Skip to content

Instantly share code, notes, and snippets.

@anoduck
Created September 28, 2025 07:38
Show Gist options
  • Select an option

  • Save anoduck/d3715f297a93ff6a83f88972a18f61e9 to your computer and use it in GitHub Desktop.

Select an option

Save anoduck/d3715f297a93ff6a83f88972a18f61e9 to your computer and use it in GitHub Desktop.
Setup and install OpenBSD in Qemu with a Virtual Risc-V Instance
#!/usr/bin/env bash
# vim: set sw=4 sts=4 et ft=sh :
# -*- mode:bash; -*-
# -------------------------------------------------------
# Copyright (C) 2025 by Anoduck, The Anonymous Duck
# -------------------------------------------------------
# https://anoduck.mit-license.org
# -------------------------------------------------------
# Installation method derived from the below blog post:
# https://briancallahan.net/blog/20220418.html
# -------------------------------------------------------
# mirror list: https://www.openbsd.org/ftp.html
MIRROR="https://cdn.openbsd.org/pub/OpenBSD"
RELEASE="snapshots"
MINIROOT="miniroot78.img"
VMROOT="/var/vmm/risc-v"
LIBDIR="/usr/local"
VMDISK="risc-v_disk.raw"
FWJUMP="${LIBDIR}/share/opensbi/generic/fw_jump.bin"
UBOOT="${LIBDIR}/share/u-boot/qemu-riscv64_smode/u-boot.bin"
INSTALLFILE="${VMROOT}/.riscv-install"
if ! [[ -d "$VMROOT" ]]; then
sudo mkdir -m 770 -p "$VMROOT"
sudo chown -R nobody:staff "$VMROOT"
fi
cd "$VMROOT" || return
if [[ "$PWD" != "$VMROOT" ]]; then
cd "$VMROOT" || exit
fi
miniroot() {
test -f "${VMROOT}/${MINIROOT}" && return # download once
url="${MIRROR}/${RELEASE}/riscv64/${MINIROOT}"
curl -o "${MINIROOT}" "${url}"
}
createrootdisk() {
test -f "${VMROOT}/${VMDISK}" && return # create once
qemu-img create -f qcow2 "$VMDISK" 60G # create 10 GB disk
}
opensbi() {
test -f ${FWJUMP} && return # Do not install if already present
if [[ $(uname -s) == "OpenBSD" ]]; then
sudo pkg_add -U "opensbi"
else
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}" "${LIBDIR}share/opensbi/generic/fw_jump.bin"
fi
}
uboot() {
test -f ${UBOOT} && return # Do not install if already present
if [[ $(uname -s) == "OpenBSD" ]]; then
sudo pkg_add -U "u-boot-riscv64"
else
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}" "${LIBDIR}share/u-boot/qemu-riscv64_smode/u-boot.bin"
fi
}
setup() {
test -f ${INSTALLFILE} && return # Do not setup is already setup.
miniroot
createrootdisk
opensbi
uboot
touch "${INSTALLFILE}"
echo "Your image is ready to run"
}
run() {
qemu-system-riscv64 \
-machine virt \
-nographic \
-m 2048M \
-smp 2 \
-bios "${FWJUMP}" \
-kernel "${UBOOT}" \
-device virtio-net-device,netdev=net -netdev user,id=net \
-device virtio-blk-device,drive=sd -drive file=${MINIROOT},if=none,id=sd \
-device virtio-blk-device,drive=hd -drive file=${VMDISK},if=none,id=hd
}
clean() {
rm "${INSTALLFILE}"
rm "${VMDISK}"
echo "Your Cleaned"
}
case $1 in
setup)
setup
;;
run)
run
;;
clean)
clean
;;
*)
echo "Your choices are setup, run, and clean"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment