Wednesday, May 04, 2022

RISC-V Ubuntu Virtual Machine

I'm pretty enthused with the open RISC-V architecture and have been meaning to get a RISC-V virtual machine up and going so I can learn the assembly language. Like many pieces of open source software, it didn't go that easy.

 

There is Debian, Fedora and Ubuntu versions of Linux for the RISC-V. I randomly chose Ubuntu.

 

I had trouble with U-Boot not being able to find the Device Tree Binary, the .dtb file, and found a post, which indicated I should get a specific version of U-Boot and compile it.

 

From:

 

https://discourse.ubuntu.com/t/ubuntu-server-on-risc-v-documentation-needs-updating/23927/4

 

The user xypron, supplied a script to build U-Boot.

 

#!/bin/sh

set -e

if test ! -f opensbi/build/platform/generic/firmware/fw_payload.bin; then
wget https://cdimage.ubuntu.com/releases/20.04/release/ubuntu-20.04.3-preinstalled-server-riscv64+unmatched.img.xz
xz -dk ubuntu-20.04.3-preinstalled-server-riscv64+unmatched.img.xz
export CROSS_COMPILE=riscv64-linux-gnu-
git clone https://source.denx.de/u-boot/u-boot.git
cd u-boot/
git reset --hard v2021.10-rc3
make qemu-riscv64_smode_defconfig
make -j$(nproc)
cd ..
git clone https://github.com/riscv/opensbi.git
cd opensbi/
make PLATFORM=generic FW_PAYLOAD_PATH=../u-boot/u-boot.bin
cd ..
fi
qemu-system-riscv64 -machine virt -m 1G -nographic \
-bios opensbi/build/platform/generic/firmware/fw_payload.bin \
-smp cores=2 -gdb tcp::1234 \
-device virtio-net-device,netdev=net0 \
-netdev user,id=net0,tftp=tftp \
-drive if=none,file=ubuntu-20.04.3-preinstalled-server-riscv64+unmatched.img,format=raw,id=mydisk \
-device ich9-ahci,id=ahci -device ide-hd,drive=mydisk,bus=ahci.0 \
-device virtio-rng-pci

 

I had already downloaded the ubuntu-20.04.4 image, so I removed the wget and changed the 20.04.3 references to 20.04.4.

 

This script built a version of U-Boot and OpenSBI that allowed the Ubuntu RISC-V image to boot.

 

Another piece that was helpful to me was to resize the disk image.

 

From the helpful Ubuntu Wiki:

 

https://wiki.ubuntu.com/RISC-V


Optionally, if you want larger disk, you can expand the disk (filesystem will be automatically resized too).

qemu-img resize -f raw focal-preinstalled-server-riscv64.img +5G