emulationqemuriscv

QEMU stuck while booting ubuntu for RISC-V


I was following the installation instructions of Ubuntu for RISC-V on QEMU from a book called "RISC-V Assembly Language Programming" by "Stephen Smith" and I faced problem while booting the image. I ran this command:

qemu-system-riscv64 -machine virt -cpu rv64 -m 2G \
-device virtio-blk-device,drive=hd \
-drive file=ubunturv.img,if=none,id=hd \
-device virtio-net-device,netdev=net \
-netdev user,id=net,hostfwd=tcp::2222-:22 \
-bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf \
-kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf \
-object rng-random,filename=/dev/urandom,id=rng \
-device virtio-rng-device,rng=rng \
-append "root=LABEL=rootfs console=ttyS0" \
-nographic

And this what was produced:

➜  Downloads qemu-system-riscv64 -machine virt -cpu rv64 -m 2G -device virtio-blk-device,drive=hd -drive file=ubunturv.img,format=raw,if=none,id=hd -device virtio-net-device,netdev=net -netdev user,id=net,hostfwd=tcp::2222-:22 -bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf -kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf -object rng-random,filename=/dev/urandom,id=rng -device virtio-rng-device,rng=rng -append "root=LABEL=rootfs console=ttyS0 earlyprintk=serial,ttyS0,115200 debug" --nographic

OpenSBI v1.3
   ____                    _____ ____ _____
  / __ \                  / ____|  _ \_   _|
 | |  | |_ __   ___ _ __ | (___ | |_) || |
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
 | |__| | |_) |  __/ | | |____) | |_) || |_
  \____/| .__/ \___|_| |_|_____/|___/_____|
        | |
        |_|

Platform Name             : riscv-virtio,qemu
Platform Features         : medeleg
Platform HART Count       : 1
Platform IPI Device       : aclint-mswi
Platform Timer Device     : aclint-mtimer @ 10000000Hz
Platform Console Device   : semihosting
Platform HSM Device       : ---
Platform PMU Device       : ---
Platform Reboot Device    : sifive_test
Platform Shutdown Device  : sifive_test
Platform Suspend Device   : ---
Platform CPPC Device      : ---
Firmware Base             : 0x80000000
Firmware Size             : 194 KB
Firmware RW Offset        : 0x20000
Firmware RW Size          : 66 KB
Firmware Heap Offset      : 0x28000
Firmware Heap Size        : 34 KB (total), 2 KB (reserved), 9 KB (used), 22 KB (free)
Firmware Scratch Size     : 4096 B (total), 760 B (used), 3336 B (free)
Runtime SBI Version       : 1.0

Domain0 Name              : root
Domain0 Boot HART         : 0
Domain0 HARTs             : 0*
Domain0 Region00          : 0x0000000002000000-0x000000000200ffff M: (I,R,W) S/U: ()
Domain0 Region01          : 0x0000000080000000-0x000000008001ffff M: (R,X) S/U: ()
Domain0 Region02          : 0x0000000080020000-0x000000008003ffff M: (R,W) S/U: ()
Domain0 Region03          : 0x0000000000000000-0xffffffffffffffff M: (R,W,X) S/U: (R,W,X)
Domain0 Next Address      : 0x0000000080200000
Domain0 Next Arg1         : 0x0000000082200000
Domain0 Next Mode         : S-mode
Domain0 SysReset          : yes
Domain0 SysSuspend        : yes

Boot HART ID              : 0
Boot HART Domain          : root
Boot HART Priv Version    : v1.10
Boot HART Base ISA        : rv64imafdc
Boot HART ISA Extensions  : time
Boot HART PMP Count       : 16
Boot HART PMP Granularity : 4
Boot HART PMP Address Bits: 54
Boot HART MHPM Count      : 0
Boot HART MIDELEG         : 0x0000000000000222
Boot HART MEDELEG         : 0x000000000000b109


U-Boot 2022.01+dfsg-2ubuntu2.5 (Sep 18 2023 - 03:02:43 +0000)

CPU:   rv64imafdcsu
Model: riscv-virtio,qemu
DRAM:  2 GiB
Flash: 32 MiB
Loading Environment from nowhere... OK
In:    uart@10000000
Out:   uart@10000000
Err:   uart@10000000
Net:   eth0: virtio-net#1
Hit any key to stop autoboot:  0 

Device 0: QEMU VirtIO Block Device
            Type: Hard Disk
            Capacity: 25088.0 MB = 24.5 GB (51380224 x 512)
... is now current device
Scanning virtio 0:1...
libfdt fdt_check_header(): FDT_ERR_BADMAGIC
Scanning disk virtio-blk#0...
Found 7 disks
** Unable to read file ubootefi.var **
Failed to load EFI variables
BootOrder not defined
EFI boot manager: Cannot load any image
Scanning virtio 0:f...
** Unable to read file / **
Failed to load '/'
libfdt fdt_check_header(): FDT_ERR_BADMAGIC
BootOrder not defined
EFI boot manager: Cannot load any image
Found EFI removable media binary efi/boot/bootriscv64.efi
159744 bytes read in 1 ms (152.3 MiB/s)
libfdt fdt_check_header(): FDT_ERR_BADMAGIC
Booting /efi\boot\bootriscv64.efi
error: no suitable video mode found.

After this, a selection appeared and I choosed to boot with ubuntu, not with advanced options:


                                    GNU GRUB  version 2.12

 ┌──────────────────────────────────────────────────────────────────────────────────────────┐
 │*Ubuntu                                                                                   │ 
 │ Advanced options for Ubuntu                                                              │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │
 │                                                                                          │ 
 └──────────────────────────────────────────────────────────────────────────────────────────┘

      Use the ▲ and ▼ keys to select which entry is highlighted.                        
      Press enter to boot the selected OS, `e' to edit the commands before booting or   
      `c' for a command-line.                                                           

Then the boot process got stuck with these messages:

Loading Linux 6.8.0-31-generic ...
Loading initial ramdisk ...
Loading device tree blob...
EFI stub: Booting Linux Kernel...
EFI stub: EFI_RNG_PROTOCOL unavailable
EFI stub: Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path
EFI stub: Using DTB from configuration table
EFI stub: Exiting boot services...

I kept waiting maybe I get a prompt or something but nothing happened for 10 minutes. I thought maybe I'm not supposed to get any prompt back and an ssh server is running, so I ran this command ssh ubuntu@localhost:2222 but it was unsuccessful.


How to reproduce:

The book states that you have to follow these instructions if you work a linux machine (ubuntu).

  1. Install QEMU with sudo apt install qemu-system-riscv64 sudo apt install u-boot-qemu opensbi
  2. Download the Ubuntu Server preinstalled image from https://ubuntu.com/download/risc-v.
  3. From the downloaded Ubuntu image, extract the img file from the xz file with unxz < downloaded_file_name.img.xz > ubunturv.img
  4. Expand the image, so there is some disk space to play around with sudo qemu-img resize ubunturv.img +10G
  5. Now we are ready to run with the rather long qemu command:
qemu-system-riscv64 -machine virt -cpu rv64 -m 2G \
-device virtio-blk-device,drive=hd \
-drive file=ubunturv.img,if=none,id=hd \
-device virtio-net-device,netdev=net \
-netdev user,id=net,hostfwd=tcp::2222-:22 \
-bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf \
-kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf \
-object rng-random,filename=/dev/urandom,id=rng \
-device virtio-rng-device,rng=rng \
-append "root=LABEL=rootfs console=ttyS0" \
-nographic

I really appreciate if you have any clue how to solve this issue.


Solution

  • I was running into the same problem, but then I realized that the download links at https://ubuntu.com/download/risc-v are incorrect for QEMU. The "Ubuntu Server preinstalled image" button points to:

    But that's the image for the SiFive Unmatched board. The download link for QEMU is actually:

    Using the appropriate kernel, I was able to successfully boot into Linux following the instructions:

    qemu-system-riscv64 \
        -machine virt -nographic -m 2048 -smp 4 \
        -kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf \
        -device virtio-net-device,netdev=eth0 -netdev user,id=eth0 \
        -device virtio-rng-pci \
        -drive file=ubuntu-24.04-preinstalled-server-riscv64.img,format=raw,if=virtio
    

    (Note that I am using QEMU 9.0.0.)

    You can see a complete successfull boot here.