本文部分转载、整理自openEuler社区、qemu.wiki

原文章链接接

通过 QEMU 仿真 RISC-V 环境并启动 openEuler RISC-V 系统

installation (openeuler.org)

Hosts/Linux - QEMU

安装QEMU

系统环境

Ubuntu 22.04.4 LTS ( WSL2 )

安装支持 RISC-V 架构的 QEMU 模拟器

目前大多数发行版都自带qemu-riscv64软件包,但由于自带的包常常过旧,我们采用手动安装。

安装之前

查看官方 wiki 获取帮助,安装必要的软件包 Hosts/Linux - QEMU

Required additional packages

  • git (30 MiB), version manager
  • glib2.0-dev (9 MiB), this automatically includes zlib1g-dev
  • libfdt-devel

For Ubuntu LTS (and maybe other Debian based distributions), all required additional packages can be installed like this:

1
sudo apt-get install git libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev ninja-build

For Ubuntu (and maybe other Debian based distributions), most of the recommended additional packages for maximum code coverage can be installed like this:

1
2
3
4
5
6
7
8
sudo apt-get install git-email
sudo apt-get install libaio-dev libbluetooth-dev libcapstone-dev libbrlapi-dev libbz2-dev
sudo apt-get install libcap-ng-dev libcurl4-gnutls-dev libgtk-3-dev
sudo apt-get install libibverbs-dev libjpeg8-dev libncurses5-dev libnuma-dev
sudo apt-get install librbd-dev librdmacm-dev
sudo apt-get install libsasl2-dev libsdl2-dev libseccomp-dev libsnappy-dev libssh-dev
sudo apt-get install libvde-dev libvdeplug-dev libvte-2.91-dev libxen-dev liblzo2-dev
sudo apt-get install valgrind xfslibs-dev

手动编译安装

这里作者选择qemu-9.1.0

查看qemu版本: Index of / (qemu.org)

1
2
3
4
5
6
7
8
9
wget https://download.qemu.org/qemu-9.1.0.tar.xz
tar -xvf qemu-9.1.0.tar.xz
cd qemu-9.1.0
mkdir res
cd res
sudo apt install libspice-protocol-dev libepoxy-dev libgtk-3-dev libspice-server-dev build-essential autoconf automake autotools-dev pkg-config bc curl gawk git bison flex texinfo gperf libtool patchutils mingw-w64 libmpc-dev libmpfr-dev libgmp-dev libexpat-dev libfdt-dev zlib1g-dev libglib2.0-dev libpixman-1-dev libncurses5-dev libncursesw5-dev meson libvirglrenderer-dev libsdl2-dev -y
../configure --target-list=riscv64-softmmu,riscv64-linux-user --prefix=/usr/local/bin/qemu-riscv64 --enable-slirp
make -j$(nproc)
sudo make install

上述指令会将 QEMU 安装到 /usr/local/bin/qemu-riscv64。将 /usr/local/bin/qemu-riscv64/bin 添加至 $PATH 即可使用。

1
PATH=$PATH:/usr/local/bin/qemu-riscv64/bin

检查版本

1
qemu-riscv64 --version

如果看到版本信息则安装成功

1
2
qemu-riscv64 version 9.1.0
Copyright (c) 2003-2024 Fabrice Bellard and the QEMU Project developers

然后安装

1
sudo apt-get install qemu-user-static

openEuler RISC-V 磁盘镜像

镜像地址

https://repo.openeuler.org/openEuler-24.03-LTS/virtual_machine_img/riscv64/

  • 标准EFI固件

RISCV_VIRT_CODE.fd

RISCV_VIRT_VARS.fd

要支持 UEFI,需要 QEMU 版本 8.1 或更高版本。

  • openEuler-24.03-LTS ISO文件

openEuler-24.03-LTS-riscv64.qcow2.xz

  • 校验文件

openEuler-24.03-LTS-riscv64.qcow2.xz.sha256sum

  • 启动脚本

start_vm.sh

下载

1
wget URL (上述文件)

校验文件

1
sha256sum -c openEuler-24.03-LTS-riscv64.qcow2.xz.sha256sum

启动 openEuler RISC-V 虚拟机

启动虚拟机

  1. 确认当前目录内包含 EFI 固件,磁盘映像压缩包,以及启动脚本。

  2. 解压映像压缩包

    1
    xz -dk openEuler-24.03-LTS-riscv64.qcow2.xz
  3. 调整启动参数

  4. 执行启动脚本 $ bash start_vm.sh

手动安装 libslirp库(可选)

可能会遇到以下报错

1
qemu-system-riscv64: error while loading shared libraries: libslirp.so.0: cannot open shared object file: No such file or directory

在此情况下需要手动安装libslirp

1
2
3
4
5
6
7
wget https://gitlab.freedesktop.org/slirp/libslirp/-/archive/v4.7.0/libslirp-v4.7.0.tar.bz2
tar -xjf libslirp-v4.7.0.tar.bz2
cd libslirp-v4.7.0
mkdir build && cd build
meson setup --prefix=/usr --buildtype=release ..
ninja
sudo ninja install

修改启动脚本(可选)

可能会收到以下提示

1
qemu-system-riscv64: -drive file=openEuler-24.03-LTS-riscv64.qcow2,format=qcow2,id=hd0: Drive 'hd0' is already in use because it has been automatically connected to another device (did you need 'if=none' in the drive options?)

编辑启动脚本

1
nano start_vm.sh

-drive file 修改为

1
-drive file="$drive",format=qcow2,if=none,id=hd0

登录虚拟机

成功启动后进行登录

  • 用户名: root
  • 默认密码: openEuler12#$