Skip to content

Setting up Ubuntu Disk Image for RISC‐V

neeluk7 edited this page Sep 20, 2023 · 1 revision

Tyche runs an unmodified Linux Kernel. However, it can sometimes be useful to modify Linux for debugging purposes. At the same time, we would like to benefit from having a full distribution, e.g., Ubuntu, for convenience. This page describes how to create an Ubuntu disk image for RISC-V with a custom kernel.

Download the Disk Image for RISC-V

Download the Ubuntu Disk Image from the website, e.g., Ubuntu. Download the prebuilt image for QEMU for the RISC-V architecture (The SiFive unmatched prebuilt image is also compatible) or make sure you perform the installation process. Here's a guide for running the installer.

If you wish to use another distribution, refer the documentation.

The following steps have been tested for the Ubuntu 22.04 (Jammy Jellyfish) distribution which provides improved RISC-V support and is LTS (long term support).

Determine the root partition of the disk.

There are several ways to find the root partition - using tools like parted, fdisk, etc.

Using fdisk:

First mount the disk image as a loop device and then run fdisk. Replace "disk-image.img" with your disk image path.

sudo losetup -P /dev/loop0 disk-image.img
sudo fdisk -l /dev/loop0

You can further use mount to examine the partitions. The linux file system should be in one of the paritions e.g. /dev/loop0p1 implies that the 1 is the root partition number. Now you can detach the loop device.

sudo losetup -d /dev/loop0

Setting a password for logging into Ubuntu.

You can use the libguestfs-tools package to do this. First install it and then set the root password (to "pass" for instance).

sudo apt-get update
sudp apt-get install libguestfs-tools
virt-customize -a disk-image.img --root-password password:pass

If there are any errors on running the virt-customize command, make sure that /boot/vmlinuz* is readable.

sudo chmod +r /boot/vmlinuz*

Use the following command to get more logs if the error persists:

export LIBGUESTFS_DEBUG=1 LIBGUESTFS_TRACE=1

Run the disk image with a custom kernel built.

The RISC-V Ubuntu disk image doesn't seem to support the SD interface. So we need to use the VIRTIO interface to connect the disk with the guest OS. Make sure that the CONFIG_VIRTIO_BLK option is enabled in the linux config so linux loads the virtio driver.

Here's the corresponding QEMU command. Remember to set the appropriate paths for the firmware, disk image, linux image, and set the root option based on the result from the earlier step for finding the root partition. Here, since the root partition number was found to be 1, root=vda1.

qemu-system-riscv64 \
    -cpu rv64,h=true -M virt -m 4G -nographic \
    -drive "file=disk-image.img,format=raw,if=virtio" \
    -bios fw_jump.bin \
    -kernel Image -append "root=/dev/vda1 rw console=ttyS0 earlycon=sbi" \

Other

Here's a guide which describes how to build a root fs using buildroot and also how to run a Fedora distribution image.

All done!

Clone this wiki locally