QEMU 源码编译

QEMU 源码编译部署,基于 Ubuntu Desktop 22.04.4 amd64 进行编译构建。

安装依赖

安装依赖应用。

1
2
sudo apt update
sudo apt install -y flex bison iasl libpixman-1-dev libslirp-dev liburing-dev libnfs-dev libseccomp-dev libxkbcommon-dev libpulse-dev libalsa-ocaml-dev libjack-dev libsndio-dev libspice-protocol-dev libspice-server-dev libiscsi-dev libzstd-dev libvirglrenderer-dev libcurl-ocaml-dev libudev-dev libncurses-dev libsdl2-dev libssh-dev libepoxy-dev libgnutls28-dev libgcrypt20-dev libsdl2-image-dev libpng-dev libjpeg-dev libcacard-dev re2c bridge-utils

安装 Python 模块。

1
sudo python3 -m pip install --upgrade sphinx sphinx_rtd_theme

安装 ninja 编译工具。

1
2
3
4
git clone https://github.com/ninja-build/ninja
cd ninja/
./configure.py --bootstrap
sudo cp ninja /usr/local/bin/

如果编译 QEMU 时 Ninja 打印以下错误,则将 Ninja 更换为较旧版本。

1
ninja: error: build.ninja:4038: multiple rules generate tests/qtest/libqos

更换为 1.11.1 版本 Ninja 应用。

1
2
3
4
5
6
sudo rm -rf /usr/local/bin/ninja
wget https://github.com/ninja-build/ninja/archive/refs/tags/v1.11.1.zip
unzip v1.11.1.zip
cd ninja-1.11.1
./configure.py --bootstrap
sudo cp ninja /usr/local/bin/

编译源码

下载所需版本的源码,进行编译。

1
2
3
4
5
6
wget https://download.qemu.org/qemu-9.0.0-rc0.tar.xz
tar xvJf qemu-9.0.0-rc0.tar.xz
cd qemu-9.0.0-rc0
./configure --prefix=/
make
sudo make install

或者通过 Git 拉取源码进行编译。

1
2
3
4
5
6
7
git clone https://gitlab.com/qemu-project/qemu.git
cd qemu
git submodule init
git submodule update --recursive
./configure --prefix=/
make
sudo make install

QEMU 默认安装在 /usr/local/bin 目录下,通过 --prefix 参数可以指定安装目录。

配置网络

编辑 QEMU 虚拟机网络启动配置文件。

1
sudo gedit /etc/qemu-ifup

配置内容如下。

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
#! /bin/sh
# Script to bring a network (tap) device for qemu up.
# The idea is to add the tap device to the same bridge
# as we have default routing to.

# in order to be able to find brctl
PATH=$PATH:/sbin:/usr/sbin
ip=$(which ip)

if [ -n "$ip" ]; then
ip link set "$1" up
else
brctl=$(which brctl)
if [ ! "$ip" -o ! "$brctl" ]; then
echo "W: $0: not doing any bridge processing: neither ip nor brctl utility not found" >&2
exit 0
fi
ifconfig "$1" 0.0.0.0 up
fi

switch=$(ip route ls | \
awk '/^default / {
for(i=0;i<NF;i++) { if ($i == "dev") { print $(i+1); next; } }
}'
)

# only add the interface to default-route bridge if we
# have such interface (with default route) and if that
# interface is actually a bridge.
# It is possible to have several default routes too
for br in $switch; do
if [ -d /sys/class/net/$br/bridge/. ]; then
if [ -n "$ip" ]; then
ip link set "$1" master "$br"
else
brctl addif $br "$1"
fi
exit # exit with status of the previous command
fi
done

echo "W: $0: no bridge for guest interface found" >&2

增加执行权限。

1
sudo chmod a+x /etc/qemu-ifup

参考链接

Download QEMU - QEMU