RISC-V Qemu setup

From Gentoo Wiki
Jump to:navigation Jump to:search

Virtual Machine

This articles describes how to running a ARCH=riscv Gentoo Linux system on a QEMU virtual machine(VM).


Install QEMU virtual machine. We need to enable QEMU_USER_TARGETS and QEMU_SOFTMMU_TARGETS. To do this, we can simply edit the variables globally in /etc/portage/make.conf, i.e.:

FILE /etc/portage/make.conf
QEMU_SOFTMMU_TARGETS="riscv32 riscv64"
QEMU_USER_TARGETS="riscv32 riscv64"
root #emerge --ask app-emulation/qemu


Install U-boot firmware which is used by qemu when bringing up a RISC-V guest virtual machine

root #eselect repository enable riscv
root #emaint sync -r riscv
root #emerge --ask sys-firmware/u-boot-bin


root #emerge --ask sys-block/gparted sys-fs/e2fsprogs

Prepare disk partition layout and also mount to temporarily directories

FILE do_prepare.sh


# 1G Size in sectors
# Size in GB
# Num of sectors, 1sector=512bytes

# Resev 1GB for boot

${DRY} mkdir -p /var/lib/libvirt/images
echo "==> Create image disk.."
${DRY} dd if=/dev/zero of=${IMG} bs=1G count=$((DISK_SZ))

echo "==> Partition disk.."
${DRY} sgdisk -Z ${IMG}
${DRY} sgdisk -n 1:${DISK_RESV}:${BOOT_END} ${IMG}
${DRY} sgdisk -n 2:${ROOT_START}:${ROOT_END} ${IMG}

${DRY} sgdisk -p ${IMG}

echo "==> Setup losetup disk.."
BOOT_DEV=$(losetup -f)
${DRY} losetup --offset=$((DISK_RESV * 512)) --sizelimit=$(((ROOT_START - DISK_RESV) * 512)) ${BOOT_DEV} ${IMG}

ROOT_DEV=$(losetup -f)
${DRY} losetup --offset=$((ROOT_START * 512)) --sizelimit=$(((ROOT_END - BOOT_END) * 512)) ${ROOT_DEV} ${IMG}

echo "==> Format disk.."
sleep 5
${DRY}  mkfs.ext4 ${BOOT_DEV}

sleep 15
${DRY} partprobe ${IMG}

${DRY} mkfs.ext4 -U ${UUID} ${ROOT_DEV}

echo "==> Mount disk.."
${DRY} mkdir -p /tmp/gentoo/{root,boot}

${DRY} mount ${BOOT_DEV} /tmp/gentoo/boot
${DRY} mount ${ROOT_DEV} /tmp/gentoo/root
root #export UUID="$(uuidgen)"

Download the script for your convenience

root #sh do_prepare.sh ${UUID}

Download riscv stage3 image, and unpack it

root #tar Jxf stage3-rv64_lp64d-systemd-20220826T170537Z.tar.xz -C /tmp/gentoo/root

Download and use prebuilt kernel/ramdisk/modules

root #tar Jxf boot_kernel.tar.xz -C /tmp/gentoo/boot
root #tar Jxf root_modules.tar.xz -C /tmp/gentoo/root

If you need to additionally customize the kernel boot arguments, please adjust "append" parameters

FILE extlinux/extlinux.conf
default Gentoo
label Gentoo
   linux /gentoo/6.0.0/vmlinuz-6.0.0-rc3-riscv64
   initrd /gentoo/6.0.0/initramfs-6.0.0-rc3-riscv64.img
   append real_root=UUID=d07fccbb-c09d-4b9e-af90-9433251ac3c7 ipv6.disable=1

Fix the rootfs's UUID in extlinux/extlinux.conf

root #sed -i -e "s@UUID=.* @UUID=${UUID} @g" /tmp/gentoo/boot/extlinux/extlinux.conf

Delete root password, this will enable you to login without password

root #sed -i -e "s@root:x:@root::@g" /tmp/gentoo/root/etc/passwd

Unmount all directories

root #umount /tmp/gentoo/{root,boot}

Release all loop devices

root #losetup -D


Install the libvirt tool

root #emerge --ask app-emulation/libvirt

Enable and start libvirtd

root #systemctl enable --now libvirtd

Example of Libvirt VM configuration

FILE riscv64_vm.xml
<domain type='qemu'>
  <memory unit='KiB'>4194304</memory>
  <currentMemory unit='KiB'>4194304</currentMemory>
  <vcpu placement='static'>8</vcpu>
    <type arch='riscv64' machine='virt'>hvm</type>
    <boot dev='hd'/>
  <clock offset='utc'/>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw' queues='4'/>
      <source file='/var/lib/libvirt/images/riscv64_vm.img'/>
      <target dev='sda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    <controller type='usb' index='0' model='qemu-xhci' ports='15'>
      <address type='pci' domain='0x0000' bus='0x02' slot='0x00' function='0x0'/>
    <controller type='pci' index='0' model='pcie-root'/>
    <controller type='pci' index='1' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='1' port='0x8'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0' multifunction='on'/>
    <controller type='pci' index='2' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='2' port='0x9'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    <controller type='pci' index='3' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='3' port='0xa'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    <controller type='pci' index='4' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='4' port='0xb'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x3'/>
    <controller type='pci' index='5' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='5' port='0xc'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x4'/>
    <controller type='pci' index='6' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='6' port='0xd'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x5'/>
    <controller type='pci' index='7' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='7' port='0xe'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x6'/>
    <controller type='pci' index='8' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='8' port='0xf'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x7'/>
    <controller type='pci' index='9' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='9' port='0x18'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
    <interface type='bridge'>
      <mac address='52:54:00:12:34:56'/>
      <source bridge='virbr0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
    <serial type='pty'>
      <target type='system-serial' port='0'>
        <model name='16550a'/>
    <console type='pty'>
      <target type='serial' port='0'/>
    <channel type='unix'>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    <input type='mouse' bus='usb'>
      <address type='usb' bus='0' port='1'/>
    <input type='keyboard' bus='usb'>
      <address type='usb' bus='0' port='2'/>
    <graphics type='vnc' port='-1' autoport='yes' listen=''>
      <listen type='address' address=''/>
    <sound model='ac97'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    <audio id='1' type='none'/>
      <model type='vga' vram='131072' heads='1' primary='yes'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x05' slot='0x00' function='0x0'/>
    <rng model='virtio'>
      <backend model='random'>/dev/urandom</backend>
      <address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0'/>
  <seclabel type='dynamic' model='dac' relabel='yes'/>

Create the riscv guest VM:

root #virsh define riscv64_vm.xml

Start the riscv guest VM:

root #virsh start --console riscv64_vm

Once this is done, you should see a riscv64 VM running, kernel booting, and finally got your familiar Gentoo messages..