Booting Riscv linux on Qemu

The linux kernel on its own is not of any use, we need applications that leverage the power of the kernel. For this we must create a filesystem which contains these applications . Busybox is a software that provides minimalistic software utilities to create a file system.

Requirement

We need following softwares to successfully boot riscv linux on Qemu.

  • Qemu
  • Riscv compiler tool chain
  • Linux kernel
  • busybox

Qemu

cd workspace
git clone https://github.com/qemu/qemu.git
cd qemu
./configure --target-list=riscv64-softmmu
make -j $(nproc)
sudo make install

Linux

cd workspace
git clone https://github.com/torvalds/linux
cd linux
make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- defconfig

Busybox file system

cd workspace
git clone https://git.busybox.net/busybox
dd if=/dev/zero of=rootfs.img bs=1M count=1024
mkfs.ext4 rootfs.img
mkdir rootfs
sudo mount rootfs.img mnt
cd busybox
sudo make CROSS_COMPILE=riscv64-linux-gnu- \
   CONFIG_PREFIX=../mnt \
   LDFLAGS=--static \
   install 
cd ..
sudo mkdir -p mnt/etc mnt/fstab mnt/etc/init.d 

Run Qemu

qemu-system-riscv64 -nographic \
   -machine virt \
   -kernel ./kernel/arch/riscv/boot/Image \
   -append "root=/dev/vda ro console=ttyS0" \
   -drive file=rootfs.img,format=raw,id=hd0 \
   -device virtio-blk-device,drive=hd0
   
   

Leave a Comment