13899cd0eSRuoqing He# How to build and test Cloud Hypervisor on riscv64 23899cd0eSRuoqing He 33899cd0eSRuoqing HeThis document introduces how to build and test Cloud Hypervisor on `riscv64`. 43899cd0eSRuoqing HeAll instructions here are tested with Ubuntu 24.04.2 as the host OS. 53899cd0eSRuoqing He 63899cd0eSRuoqing He## Hardware requirements 73899cd0eSRuoqing He 83899cd0eSRuoqing He- riscv64 servers (recommended) or development boards equipped with the AIA 93899cd0eSRuoqing He(Advance Interrupt Architecture) interrupt controller. 103899cd0eSRuoqing He 113899cd0eSRuoqing He## Getting started 123899cd0eSRuoqing He 133899cd0eSRuoqing HeWe create a folder to build and run Cloud Hypervisor at `$HOME/cloud-hypervisor` 143899cd0eSRuoqing He 153899cd0eSRuoqing He```console 163899cd0eSRuoqing Heexport CLOUDH=$HOME/cloud-hypervisor 173899cd0eSRuoqing Hemkdir $CLOUDH 183899cd0eSRuoqing He``` 193899cd0eSRuoqing He 203899cd0eSRuoqing He## Prerequisites 213899cd0eSRuoqing He 223899cd0eSRuoqing HeYou need to install some prerequisite packages to build and test Cloud Hypervisor. 233899cd0eSRuoqing He 243899cd0eSRuoqing He### Tools 253899cd0eSRuoqing He 263899cd0eSRuoqing He```console 273899cd0eSRuoqing He# Install rust tool chain 283899cd0eSRuoqing Hecurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh 293899cd0eSRuoqing He# Install the tools used for building guest kernel, EDK2 and converting guest disk 303899cd0eSRuoqing Hesudo apt-get update 313899cd0eSRuoqing Hesudo apt-get install git build-essential m4 bison flex uuid-dev qemu-utils 323899cd0eSRuoqing He``` 333899cd0eSRuoqing He 343899cd0eSRuoqing He### Building Cloud Hypervisor 353899cd0eSRuoqing He 363899cd0eSRuoqing He```console 373899cd0eSRuoqing Hepushd $CLOUDH 383899cd0eSRuoqing Hegit clone https://github.com/cloud-hypervisor/cloud-hypervisor.git 393899cd0eSRuoqing Hecd cloud-hypervisor 403899cd0eSRuoqing Hecargo build 413899cd0eSRuoqing Hepopd 423899cd0eSRuoqing He``` 433899cd0eSRuoqing He 443899cd0eSRuoqing He### Disk image 453899cd0eSRuoqing He 463899cd0eSRuoqing HeDownload the Ubuntu cloud image and convert the image type. 473899cd0eSRuoqing He 483899cd0eSRuoqing He```console 493899cd0eSRuoqing Hepushd $CLOUDH 503899cd0eSRuoqing Hewget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-riscv64.img 513899cd0eSRuoqing Heqemu-img convert -p -f qcow2 -O raw jammy-server-cloudimg-riscv64.img jammy-server-cloudimg-riscv64.raw 523899cd0eSRuoqing Hepopd 533899cd0eSRuoqing He``` 543899cd0eSRuoqing He 553899cd0eSRuoqing He## Direct-kernel booting 563899cd0eSRuoqing He 573899cd0eSRuoqing He### Building kernel 583899cd0eSRuoqing He 593899cd0eSRuoqing He```console 603899cd0eSRuoqing Hepushd $CLOUDH 613899cd0eSRuoqing Hegit clone --depth 1 "https://github.com/cloud-hypervisor/linux.git" -b ch-6.12.8 623899cd0eSRuoqing Hecd linux 633899cd0eSRuoqing Hemake ch_defconfig 643899cd0eSRuoqing Hemake -j `nproc` 653899cd0eSRuoqing Hepopd 663899cd0eSRuoqing He``` 673899cd0eSRuoqing He 683899cd0eSRuoqing He### Booting the guest VM 693899cd0eSRuoqing He 703899cd0eSRuoqing He```console 713899cd0eSRuoqing Hepushd $CLOUDH 723899cd0eSRuoqing Hesudo $CLOUDH/cloud-hypervisor/target/debug/cloud-hypervisor \ 733899cd0eSRuoqing He --kernel $CLOUDH/linux/arch/riscv64/boot/Image \ 743899cd0eSRuoqing He --disk path=jammy-server-cloudimg-riscv64.raw \ 753899cd0eSRuoqing He --cmdline "console=hvc0 root=/dev/vda rw" \ 763899cd0eSRuoqing He --cpus boot=1 \ 773899cd0eSRuoqing He --memory size=1024M \ 783899cd0eSRuoqing He --seccomp false \ 793899cd0eSRuoqing He --log-file boot.log -vv 803899cd0eSRuoqing Hepopd 813899cd0eSRuoqing He``` 823899cd0eSRuoqing He 83*40055595Sabm-77## Virtualized Development Setup 84*40055595Sabm-77 85*40055595Sabm-77Since there are few RISC-V development boards on the market and not 86*40055595Sabm-77many details about the AIA interrupt controller featured in product listings, 87*40055595Sabm-77QEMU is a popular and viable choice for creating a RISC-V development environment. 88*40055595Sabm-77Below are the steps used to create a QEMU virtual machine that can be used for 89*40055595Sabm-77cloud-hypervisor RISC-V development: 90*40055595Sabm-77 91*40055595Sabm-77### Install Dependencies 92*40055595Sabm-77 93*40055595Sabm-77```console 94*40055595Sabm-77sudo apt update 95*40055595Sabm-77sudo apt install opensbi qemu-system-misc u-boot-qemu 96*40055595Sabm-77``` 97*40055595Sabm-77 98*40055595Sabm-77### Download and Build QEMU (>=v9.2.0) 99*40055595Sabm-77 100*40055595Sabm-77Older versions of QEMU may not have support for the AIA 101*40055595Sabm-77interrupt controller. 102*40055595Sabm-77 103*40055595Sabm-77```console 104*40055595Sabm-77wget https://download.qemu.org/qemu-10.0.0.tar.xz 105*40055595Sabm-77tar xvJf qemu-10.0.0.tar.xz 106*40055595Sabm-77cd qemu-10.0.0 107*40055595Sabm-77./configure --target-list=riscv64-softmmu 108*40055595Sabm-77make -j $(nproc) 109*40055595Sabm-77sudo make install 110*40055595Sabm-77``` 111*40055595Sabm-77 112*40055595Sabm-77### Download Ubuntu Server Image 113*40055595Sabm-77 114*40055595Sabm-77At the time of writing, the best results have been seen with 115*40055595Sabm-77the Ubuntu 24.10 (Oracular) server image. Ex: 116*40055595Sabm-77 117*40055595Sabm-77```console 118*40055595Sabm-77wget https://cdimage.ubuntu.com/releases/oracular/release/ubuntu-24.10-preinstalled-server-riscv64.img.xz 119*40055595Sabm-77xz -dk ubuntu-24.10-preinstalled-server-riscv64.img.xz 120*40055595Sabm-77``` 121*40055595Sabm-77 122*40055595Sabm-77### (Optional) Resize Disk 123*40055595Sabm-77 124*40055595Sabm-77If you would like a larger disk, you can resize it now. 125*40055595Sabm-77 126*40055595Sabm-77```console 127*40055595Sabm-77qemu-img resize -f raw <ubuntu-image> +5G 128*40055595Sabm-77``` 129*40055595Sabm-77 130*40055595Sabm-77### Boot VM 131*40055595Sabm-77 132*40055595Sabm-77Note the inclusion of the AIA interrupt controller in the 133*40055595Sabm-77invocation. 134*40055595Sabm-77 135*40055595Sabm-77```console 136*40055595Sabm-77qemu-system-riscv64 \ 137*40055595Sabm-77 -machine virt,aia=aplic-imsic \ 138*40055595Sabm-77 -nographic -m 1G -smp 8 \ 139*40055595Sabm-77 -kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf \ 140*40055595Sabm-77 -device virtio-rng-pci \ 141*40055595Sabm-77 -device virtio-net-device,netdev=eth0 -netdev user,id=eth0 \ 142*40055595Sabm-77 -drive file=<ubuntu-image>,format=raw,if=virtio 143*40055595Sabm-77``` 144*40055595Sabm-77 145*40055595Sabm-77### Install KVM Kernel Module Within VM 146*40055595Sabm-77KVM is not enabled within the VM by default, so we must enable 147*40055595Sabm-77it manually. 148*40055595Sabm-77 149*40055595Sabm-77```console 150*40055595Sabm-77sudo modprobe kvm 151*40055595Sabm-77``` 152*40055595Sabm-77 153*40055595Sabm-77From this point, you can continue with the above steps from the beginning. 154*40055595Sabm-77 155*40055595Sabm-77### Sources 156*40055595Sabm-77 157*40055595Sabm-77https://risc-v-getting-started-guide.readthedocs.io/en/latest/linux-qemu.html 158*40055595Sabm-77 159*40055595Sabm-77https://canonical-ubuntu-boards.readthedocs-hosted.com/en/latest/how-to/qemu-riscv/#using-the-live-server-image 160*40055595Sabm-77 161*40055595Sabm-77https://www.qemu.org/docs/master/specs/riscv-aia.html 162*40055595Sabm-77 1633899cd0eSRuoqing He## Known limitations 1643899cd0eSRuoqing He 1653899cd0eSRuoqing He- Direct kernel boot only 1663899cd0eSRuoqing He- `64-bit Linux` guest OS only 1673899cd0eSRuoqing He- For more details, see 1683899cd0eSRuoqing He [here](https://github.com/cloud-hypervisor/cloud-hypervisor/issues/6978). 169