Arch 搭建 qemu riscv64 虚拟机

Arch 搭建 qemu riscv64 虚拟机

由于 u-boot 已经存在与 AUR 中,所以这里直接采用 Arch 对虚拟机进行搭建,或者我们可以将需要的环境拷贝到任意系统中运行也可以

这里我采用 docker 的方式进行运行,已经搭载好环境的 docker 放在了 docker hub 上,可以自行下载:

arch-qemu-system-riscv64 docker

也可以通过命令拉取

docker pull ppqppl/arch-qemu-system-riscv64:v1.0

Arch 软件包与内核更新

直接使用以下命令即可

pacman -Syy
pacman -Syu
pacman -Syyu

这里注意,国内网络连接 Archlinux 镜像源较慢,需要耐心等待,这里没有进度条

Arch 换源

成功更新软件包与内核后,进行换源,便于我们后面对软件包的下载

先安装 vim

pacman -S vim

换源

# 打开源列表文件
vim etc/pacman.d/mirrorlist
# 添加以下内容
#清华源
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch
#阿里源
Server = http://mirrors.aliyun.com/archlinux/$repo/os/$arch
#中科大源
Server = https://mirrors.ustc.edu.cn/archlinux/$repo/os/$arch
# 保存退出
:wq
# 更新源缓存
pacman -Syyu

或者使用以下命令一键换源(docker 用户第一次无法使用,需要先安装对应软件包,过程很慢,不推荐)

# 查看能用的源镜像排名,并选择速度较快的一个或多个源
pacman-mirrors -i -c China -m rank
# 查看选择的源
cat /etc/pacman.d/mirrorlist
# 更新源
pacman -Syyu

qemu 环境准备

docker 用户注意,由于 docker 默认是使用 root 登录,使用 root 用户编译会报错如下:

==> ERROR: Running makepkg as root is not allowed as it can cause permanent,
catastrophic damage to your system.

添加用户

由于 docker 下的 ArchLinux 除了 root 外没有其他用户,这里我们需要添加一个新用户

# 先修改 root 用户密码
passwd
输入密码
# 创建一个非 root 用户
useradd ppqppl
passwd ppqppl
输入密码
# 给新用户添加管理员权限
# 打开 sudoers 文件
vim /etc/sudoers
# 在 root ALL=(ALL:ALL) ALL 下添加
ppqppl ALL=(ALL) ALL
# 保存退出
:wq

image
注意,下面所有的编译都需要使用新创建的用户进行编译安装相关环境,

# 使用新用户登录
su ppqppl
# 退出当前用户登录命令
exit

环境安装

这里使用 AUR 中的包

sudo pacman -S libfakekey polkit-qt5 qca qt5-x11extras sudo base-devel git
# 安装 libfprint-1
git clone https://aur.archlinux.org/libfprint-1.git
cd libfprint-1
sudo makepkg -si
# 安装 fingerprint-gui
git clone https://aur.archlinux.org/fingerprint-gui.git
cd fingerprint-gui
sudo makepkg -si

安装 AUR helper

git clone https://aur.archlinux.org/yay.git
cd yay
sudo makepkg -si

qemu 安装

qemu 安装

pacman -S qemu-base qemu-emulators-full

安装 qemu-riscv64 镜像启动引导,这里采用开源的 u-boot (编译依旧需要使用新建用户完成)

su ppqppl
git clone --recursive https://aur.archlinux.org/u-boot-qemu-bin.git
cd u-boot-qemu-bin
sudo makepkg -si
exit

到此我们就安装好了 qemu,使用以下命令测试

# 输入以下命令,按 tab 补全可以看到是否安装成功
qemu-

显示有以下命令说明安装成功

image

debian qemu 环境搭建

以下命令在 root 用户下完成

镜像创建

这里使用 debian 官方提供的 riscv 版本

wget "https://gitlab.com/api/v4/projects/giomasce%2Fdqib/jobs/artifacts/master/download?job=convert_riscv64-virt" -O debian-rv64.zip
unzip debian-rv64.zip

创建镜像环境

mkdir image
cd image
cp /usr/share/u-boot-qemu-bin/qemu-riscv64_smode/uboot.elf .
cp /usr/share/qemu/opensbi-riscv64-generic-fw_dynamic.bin .
cd /home/dqib_riscv64-virt(debian镜像文件夹路径)
cp * /home/image
cd /home/image

这样我们的镜像就已经搭建好了

qemu-system-riscv64 启动脚本设置

这里我们使用如下脚本

# 创建脚本
cd /home/image
touch start-qemu-system-riscv64.sh
# 编辑脚本
vim start-qemu-system-riscv64.sh
# 脚本内容如下
qemu-system-riscv64 \
	-machine virt \
	-cpu rv64 \
	-m 8G \
	-device virtio-blk-device,drive=hd \
	-drive file=image.qcow2,if=none,id=hd \
	-device virtio-net-device,netdev=net \
	-netdev user,id=net,hostfwd=tcp::62222-:22 \
	-bios ./opensbi-riscv64-generic-fw_dynamic.bin \
	-kernel ./uboot.elf \
	-object rng-random,filename=/dev/urandom,id=rng \
	-device virtio-rng-device,rng=rng \
	-append "root=LABEL=rootfs console=ttyS0" \
	-nographic
# 保存退出
:wq

这样我们所有的环境与镜像都已经准备好了

运行基于 debian-riscv 的 qemu 虚拟机

直接运行启动脚本即可

cd /home/image
bash start-qemu-system-riscv64.sh

运行效果如下:

image

posted @ 2024-04-20 11:48  ppqppl  阅读(21)  评论(0编辑  收藏  举报