虽然RISC-V开放架构让我们感到非常兴奋和激动,但要使生态系统与Arm或x86体系结构(从芯片到软件)保持一致,还是需做大量的工作。然而进步是缓慢的,其中一个进步是 Ubuntu 的发行商Canonical 为SiFive HiFive 开发板以及 QEMU 开源模拟器发布了 Ubuntu 64 位 RISC-V (RISCV64) 映像。
具体来说,Canonical 发布了用于HiFive Unleashed和 QEMU的Ubuntu 20.04.2 LTS 镜像以及用于 HiFive Unleashed、HiFive Unmatched和 QEMU的Ubuntu 21.04 镜像。值得注意的是,这些只是服务器映像,还没有像 Raspberry Pi 2/3/4 上的 Ubuntu 21.04 那样的桌面映像。

在 QEMU 中运行RISC-V Linux起码有三年了,但当我尝试使用它时,它是一个基于 Busybox 的最小系统,所以让我们按照Discourse 上提供的说明在Ubuntu 21.04 上再试一次。
我有一台运行 Ubuntu 20.04 的 AMD Ryzen 笔记本电脑。
首先,我打开了一个终端窗口,安装依赖库。
1 |
sudo apt install qemu-system-misc opensbi u-boot-qemu qemu-utils |
其次,为 HiFive Unmatched 下载并提取 Ubuntu 21.04 映像:
1 2 |
wget https://cdimage.ubuntu.com/releases/21.04/release/ubuntu-21.04-preinstalled-server-riscv64+unmatched.img.xz xz -dk ubuntu-21.04-preinstalled-server-riscv64+unmatched.img.xz |
我们现在在你的机器上安装 Ubuntu 21.04 RISCV64:
1 |
qemu-system-riscv64 -machine virt -nographic -m 2048 -smp 1 -bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf -kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf -device virtio-net-device,netdev=eth0 -netdev user,id=eth0 -drive file=ubuntu-21.04-preinstalled-server-riscv64+unmatched.img,format=raw,if=virtio |
但它突然停止了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
OpenSBI v0.8 ____ _____ ____ _____ / __ \ / ____| _ \_ _| | | | |_ __ ___ _ __ | (___ | |_) || | | | | | '_ \ / _ \ '_ \ \___ \| _ < | | | |__| | |_) | __/ | | |____) | |_) || |_ \____/| .__/ \___|_| |_|_____/|____/_____| | | |_| Platform Name : riscv-virtio,qemu Platform Features : timer,mfdeleg Platform HART Count : 1 Boot HART ID : 0 Boot HART ISA : rv64imafdcsu BOOT HART Features : pmp,scounteren,mcounteren BOOT HART PMP Count : 16 Firmware Base : 0x80000000 Firmware Size : 92 KB Runtime SBI Version : 0.2 MIDELEG : 0x0000000000000222 MEDELEG : 0x000000000000b109 PMP0 : 0x0000000080000000-0x000000008001ffff (A) PMP1 : 0x0000000000000000-0xffffffffffffffff (A,R,W,X) U-Boot 2020.10+dfsg-1ubuntu0~20.04.2 (Jan 08 2021 - 13:03:11 +0000) CPU: rv64imafdcsu Model: riscv-virtio,qemu DRAM: 2 GiB In: uart@10000000 Out: uart@10000000 Err: uart@10000000 Net: eth0: virtio-net#0 Hit any key to stop autoboot: 0 Device 0: 1af4 VirtIO Block Device Type: Hard Disk Capacity: 3584.0 MB = 3.5 GB (7340032 x 512) ... is now current device Scanning virtio 0:1... Found /boot/extlinux/extlinux.conf Retrieving file: /boot/extlinux/extlinux.conf 743 bytes read in 5 ms (144.5 KiB/s) U-Boot menu 1: Ubuntu 21.04 5.11.0-1007-generic 2: Ubuntu 21.04 5.11.0-1007-generic (rescue target) Enter choice: 1 1: Ubuntu 21.04 5.11.0-1007-generic Retrieving file: /boot/initrd.img-5.11.0-1007-generic 182268553 bytes read in 269 ms (646.2 MiB/s) Retrieving file: /boot/vmlinuz-5.11.0-1007-generic 27355136 bytes read in 50 ms (521.8 MiB/s) append: root=LABEL=cloudimg-rootfs ro earlycon Unhandled exception: Load access fault EPC: 00000000fffb6b26 RA: 00000000fff74b14 TVAL: 0000000000000000 EPC: 0000000080249b26 RA: 0000000080207b14 reloc adjusted ### ERROR ### Please RESET the board ### |
这是因为 Ubuntu 21.04 Hirsute 目前无法使用 Ubuntu 20.04 中提供的 u-boot-qemu 版本启动,仅支持 Ubuntu 21.04 中的版本。所以让我们试试 Ubuntu 20.04 RISC-V 镜像:
1 2 3 |
wget https://cdimage.ubuntu.com/releases/20.04.2/release/ubuntu-20.04.2-preinstalled-server-riscv64.img.xz xz -dk ubuntu-20.04.2-preinstalled-server-riscv64.img.xz qemu-system-riscv64 -machine virt -nographic -m 2048 -smp 2 -bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf -kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf -device virtio-net-device,netdev=eth0 -netdev user,id=eth0 -drive file=ubuntu-20.04.2-preinstalled-server-riscv64.img,format=raw,if=virtio |
很好!几分钟后,我们可以使用“ubuntu”用户名和密码访问命令行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
OpenSBI v0.8 ____ _____ ____ _____ / __ \ / ____| _ \_ _| | | | |_ __ ___ _ __ | (___ | |_) || | | | | | '_ \ / _ \ '_ \ \___ \| _ < | | | |__| | |_) | __/ | | |____) | |_) || |_ \____/| .__/ \___|_| |_|_____/|____/_____| | | |_| Platform Name : riscv-virtio,qemu Platform Features : timer,mfdeleg Platform HART Count : 2 Boot HART ID : 1 Boot HART ISA : rv64imafdcsu BOOT HART Features : pmp,scounteren,mcounteren BOOT HART PMP Count : 16 Firmware Base : 0x80000000 Firmware Size : 100 KB Runtime SBI Version : 0.2 MIDELEG : 0x0000000000000222 MEDELEG : 0x000000000000b109 PMP0 : 0x0000000080000000-0x000000008001ffff (A) PMP1 : 0x0000000000000000-0xffffffffffffffff (A,R,W,X) U-Boot 2020.10+dfsg-1ubuntu0~20.04.2 (Jan 08 2021 - 13:03:11 +0000) CPU: rv64imafdcsu Model: riscv-virtio,qemu DRAM: 2 GiB In: uart@10000000 Out: uart@10000000 Err: uart@10000000 Net: eth0: virtio-net#0 Hit any key to stop autoboot: 0 Device 0: 1af4 VirtIO Block Device Type: Hard Disk Capacity: 3584.0 MB = 3.5 GB (7340032 x 512) ... is now current device Scanning virtio 0:1... Found /boot/extlinux/extlinux.conf Retrieving file: /boot/extlinux/extlinux.conf 631 bytes read in 2 ms (307.6 KiB/s) U-Boot menu 1: Ubuntu 20.04.2 LTS 5.8.0-14-generic 2: Ubuntu 20.04.2 LTS 5.8.0-14-generic (rescue target) Enter choice: 1 1: Ubuntu 20.04.2 LTS 5.8.0-14-generic Retrieving file: /boot/initrd.img-5.8.0-14-generic 160037936 bytes read in 250 ms (610.5 MiB/s) Retrieving file: /boot/vmlinuz-5.8.0-14-generic 20094612 bytes read in 47 ms (407.7 MiB/s) append: root=LABEL=cloudimg-rootfs ro earlycon Moving Image from 0x84000000 to 0x80200000, end=81622f80 ## Flattened Device Tree blob at ff74bea0 Booting using the fdt blob at 0xff74bea0 Using Device Tree in place at 00000000ff74bea0, end 00000000ff74fe1d Starting kernel ... ... -----END SSH HOST KEY KEYS----- [ 263.760272] cloud-init[665]: Cloud-init v. 20.4.1-0ubuntu1~20.04.1 running 'modules:final' at Mon, 28 Jun 2021 04:36:41 +0000. Up 257.45 seconds. [ 263.773658] cloud-init[665]: ci-info: no authorized SSH keys fingerprints found for user ubuntu. [ 263.782580] cloud-init[665]: Cloud-init v. 20.4.1-0ubuntu1~20.04.1 finished at Mon, 28 Jun 2021 04:36:47 +0000. Datasource DataSourceNoCloud [seed=/var/lib/cloud/seed/nocloud-net][dsmode=net]. Up 263.41 seconds Ubuntu 20.04.2 LTS ubuntu ttyS0 ubuntu login: ubuntu Password: You are required to change your password immediately (administrator enforced) Changing password for ubuntu. Current password: New password: Retype new password: Bad: new password is too simple New password: Retype new password: Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.8.0-14-generic riscv64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Mon Jun 28 04:37:40 UTC 2021 System load: 1.08 Usage of /: 76.8% of 3.22GB Memory usage: 8% Swap usage: 0% Processes: 90 Users logged in: 0 IPv4 address for eth0: 10.0.2.15 IPv6 address for eth0: fec0::5054:ff:fe12:3456 0 updates can be installed immediately. 0 of these updates are security updates. The list of available updates is more than a week old. To check for new updates run: sudo apt update The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details. ubuntu@ubuntu:~$ |
我们确实是在双核 64 位 RISC-V 系统中,因为我在命令行中使用了“smp 2”选项:
1 2 3 4 5 6 7 8 9 10 |
ubuntu@ubuntu:~$ cat /proc/cpuinfo processor : 0 hart : 1 isa : rv64imafdcsu mmu : sv48 processor : 1 hart : 0 isa : rv64imafdcsu mmu : sv48 |
我们已经启动以太网并正常运行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
ip address 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0 valid_lft 79022sec preferred_lft 79022sec inet6 fec0::5054:ff:fe12:3456/64 scope site dynamic mngtmpaddr noprefixroute valid_lft 86299sec preferred_lft 14299sec inet6 fe80::5054:ff:fe12:3456/64 scope link valid_lft forever preferred_lft forever |
所以我可以通过 SSH 连接到我的主机,但我无法通过 SSH 连接到 QEMU 会话。所以我关闭了 Ubuntu RISC-V 实例,添加了一个 tcp 重定向以在端口 2222 上启用 SSH,并重新启动 QEMU。
1 |
qemu-system-riscv64 -machine virt -nographic -m 2048 -smp 1 -bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf -kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf -device virtio-net-device,netdev=eth0 -netdev user,id=eth0,hostfwd=tcp::2222-:22 -drive file=ubuntu-20.04.2-preinstalled-server-riscv64.img,format=raw,if=virtio |
最后,我可以通过 SSH 登录 QEMU,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
jaufranc@cnx-laptop-4:~/edev/sandbox/ubuntu-risc-v$ ssh -p 2222 ubuntu@localhost The authenticity of host '[localhost]:2222 ([127.0.0.1]:2222)' can't be established. ECDSA key fingerprint is SHA256:XvWsfVDD4C4qaH0g1VU1mY47+tcGx6E0GRVjX5mORvM. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '[localhost]:2222' (ECDSA) to the list of known hosts. ubuntu@localhost's password: Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.8.0-14-generic riscv64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Mon Jun 28 07:08:20 UTC 2021 System load: 0.18 Usage of /: 78.3% of 3.22GB Memory usage: 8% Swap usage: 0% Processes: 81 Users logged in: 1 IPv4 address for eth0: 10.0.2.15 IPv6 address for eth0: fec0::5054:ff:fe12:3456 0 updates can be installed immediately. 0 of these updates are security updates. The list of available updates is more than a week old. To check for new updates run: sudo apt update Last login: Mon Jun 28 07:05:11 2021 ubuntu@ubuntu:~$ |
因此,可以轻松地在 QEMU 和主机之间任意传输文件。

文章翻译者:Nicholas,技术支持工程师、瑞科慧联(RAK)高级工程师,深耕嵌入式开发技术、物联网行业多年,拥有丰富的行业经验和新颖独到的眼光!