以下是在 Ubuntu 18.04 上编译 QEMU 并添加外设支持的步骤:
- 安装必要的依赖库:
sudo apt-get install build-essential git libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev libcap-dev
- 下载 QEMU 源代码:
git clone https://github.com/qemu/qemu.git
cd qemu
- 配置编译选项,指定目标架构为 arm 并启用硬件加速支持和外设支持:
./configure --target-list=arm-softmmu --enable-kvm --enable-virtfs --enable-virtio-blk-data-plane --enable-vhost-user-net \
--enable-vhost-user --enable-modules \
--audio-drv-list=pa,alsa,sdl \
--usb-redir=all \
--with-pkgversion="My QEMU" \
--prefix=/usr/local/
这里使用了一些常用的外设支持选项,具体含义如下:
--enable-virtfs
:启用 VirtFS 支持(共享文件系统);--enable-virtio-blk-data-plane
:启用 VirtIO 块数据平面(提高块设备性能);--enable-vhost-user-net
和--enable-vhost-user
:启用 Vhost User 网络支持;--audio-drv-list=pa,alsa,sdl
:启用 PulseAudio、ALSA 和 SDL 音频驱动;--usb-redir=all
:启用 USB 重定向支持;--with-pkgversion="My QEMU"
:指定编译出的 QEMU 版本号为 “My QEMU”;--prefix=/usr/local/
:指定安装路径为 /usr/local/。
- 编译并安装 QEMU:
make -j$(nproc)
sudo make install
- 验证 QEMU 是否正确安装,同时查看是否支持外设:
qemu-system-arm --version
- 运行带有外设支持的 ARM 虚拟机:
以 Raspberry Pi 3 为例,运行以下命令启动一个带有 VirtIO 块设备和 Vhost User 网络支持的虚拟机:
qemu-system-arm -M raspi3 -kernel kernel.img -append 'root=/dev/vda2 panic=1' \
-drive file=raspbian.img,if=none,id=hd0 \
-device virtio-blk-device,drive=hd0 \
-netdev type=vhost-user,id=mynet1,chardev=char1,vhostforce \
-chardev socket,id=char1,path=/tmp/vhost-user-net \
-device virtio-net-device,netdev=mynet1
这里使用了 -device
和 -netdev
选项来添加 VirtIO 块设备和 Vhost User 网络设备。如果需要使用其他外设,请根据实际情况进行调整。
参考文献:https://wiki.qemu.org/Documentation/Platforms/ARM#Compilation