In my last post I talked about my excitement for RISC-V. Since then I have been experimenting to see what I can do before getting my hands on real hardware. I was able to get an emulated RISC-V environment running on Windows 10 via WSL2 (Windows Subsystem for Linux).

Debian RISC-V login

Things that helped:

Walkthrough for emulating RISC-V Debian on WSL2

Requirements:

These instructions will also work inside of a Ubuntu Docker container, if you are on another system.

Set up QEMU for RISC-V emulation

First we need to prepare our toolchain, and configure QEMU to run with RISC-V emulation.

## Setup build environment
# install base toolchain
sudo apt update
sudo apt install git wget build-essential ninja-build  python3-setuptools
# need some additional build libraries for QEMU
sudo apt install libglib2.0-dev libpixman-1-dev 

## Build QEMU
# clone it
cd ~
git clone https://github.com/qemu/qemu
# make & install QEMU for RISC-V
cd qemu
mkdir build
cd build
../configure --target-list=riscv64-softmmu
make  -j3
sudo make install

Download and run Debian

Debian needs a special kernel for QEMU

## Download RISC-V compatible kernel. 
# Option 1: Install if available on stable
sudo apt install u-boot-qemu

# Option 2: **IF** the package is not found, manually download & install package. As it is on testing, not stable. 
# https://packages.debian.org/bullseye/u-boot-qemu
cd ~
wget http://ftp.us.debian.org/debian/pool/main/u/u-boot/u-boot-qemu_2021.01+dfsg-2_all.deb
sudo dpkg -i ./u-boot-qemu_2021.01+dfsg-2_all.deb

Now that everything is set up, download a pre-built debian distro, extract, and run it.

# Will need unzip
sudo apt install unzip

# download images from https://people.debian.org/~gio/dqib/
cd ~
wget https://gitlab.com/api/v4/projects/giomasce%2Fdqib/jobs/artifacts/master/download?job=convert_riscv64-virt -O artifacts.zip
unzip artifacts.zip
cd artifacts

# Start the VM
# -smp number of processors
# -m for RAM
# -netdev hostfwd 2222 will port forward for SSH
qemu-system-riscv64 \
  -nographic \
  -machine virt \
  -cpu rv64 \
  -smp 4 \
  -m 4G \
  -kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf \
  -device virtio-blk-device,drive=hd \
  -drive file=image.qcow2,if=none,id=hd \
  -device virtio-net-device,netdev=net \
  -netdev user,id=net,hostfwd=tcp::2222-:22 \
  -object rng-random,filename=/dev/urandom,id=rng \
  -device virtio-rng-device,rng=rng \
  -append "root=LABEL=rootfs console=ttyS0"

User/pass: root/root

Debian RISC-V boot

Recommended:
The QEMU terminal window is not great. From a new terminal, SSH in to get a better experience using the forwarded port.

ssh root@localhost -p 2222  #password root

See it in action

Now that your Debian image is up and running you can use it.
I was able to successfully apt install a number of packages such as golang, htop, screenfetch.

# show CPU info
lscpu
# show distro information
apt install screenfetch
screenfetch

Debian CPU info & screenfetch

Additional BIOS

If you want to play further, you can optionally install an additional package that has a generic, and a SiFive BIOS.

# RISC-V firmware
# Additional firmwares will be available under /usr/lib/riscv64-linux-gnu/opensbi/
sudo apt install opensbi


# can now use the bios flag on QEMU
qemu-system-riscv64 \
  -bios /usr/lib/riscv64-linux-gnu/opensbi/qemu/virt/fw_jump.elf \
  ... \

Alternative: Run Fedora

You won’t need to install the additional Kernel that was required for Debian. Once you have QEMU configured for RISC-V you can just download run the latest RISC-V image from https://dl.fedoraproject.org/pub/alt/risc-v/repo/virt-builder-images/images/

# Download images from https://dl.fedoraproject.org/pub/alt/risc-v/repo/virt-builder-images/images/
wget https://dl.fedoraproject.org/pub/alt/risc-v/repo/virt-builder-images/images/Fedora-Minimal-Rawhide-20200108.n.0-fw_payload-uboot-qemu-virt-smode.elf
wget https://dl.fedoraproject.org/pub/alt/risc-v/repo/virt-builder-images/images/Fedora-Minimal-Rawhide-20200108.n.0-sda.raw.xz
unxz Fedora-Minimal-Rawhide-20200108.n.0-sda.raw.xz

# Boot the image. Forward port 10000 for SSH
# user: riscv
# pass: fedora_rocks!
qemu-system-riscv64 \
  -nographic \
  -machine virt \
  -smp 2 \
  -m 2G \
  -kernel Fedora-Minimal-Rawhide-*-fw_payload-uboot-qemu-virt-smode.elf \
  -bios none \
  -object rng-random,filename=/dev/urandom,id=rng0 \
  -device virtio-rng-device,rng=rng0 \
  -device virtio-blk-device,drive=hd0 \
  -drive file=Fedora-Minimal-Rawhide-20200108.n.0-sda.raw,format=raw,id=hd0 \
  -device virtio-net-device,netdev=usernet \
  -netdev user,id=usernet,hostfwd=tcp::10000-:22

Fedora screenfetch