简介
在本教程中,您将学习如何从头编译 qemu 和 libvirt,而不干扰正在 Linux® on IBM® Power Systems™ 服务器上运行的现有 qemu 和 libvirt 守护程序。
在本教程中,您将学习如何:
- 克隆上游 libvirt 和 qemu 代码
- 配置、构建和安装上游 qemu 代码
- 配置、构建和安装上游 libvirt 代码
- 运行 libvirt/qemu 实例
前提条件
基本了解 Linux 并拥有一个正常运行的 Linux 系统是练习本教程中介绍的命令和步骤的前提条件。
在配置和构建 libvirt 和 qemu 代码之前,使用以下脚本在 Linux on Power 系统上安装所需的软件包。
sudo dnf install -y automake gcc make glibc glibc-utils glib2-devel zlib-devel pixman-devel flex bison \
numad numactl-devel numactl-libs numactl \
libtool gnutls-utils gnutls-devel libnl3-devel libxml2-devel \
libtirpc-devel python3-docutils device-mapper-devel libpciaccess-devel \
rpcbind readline-devel rpcgen yajl-devel libxslt-devel bzip2
预估时间
完成配置并运行 libvirt 和 qemu 实例的设置大约需要 1 小时。
步骤
安装 Git 并克隆上游 libvirt 和 qemu 存储库。
sudo dnf install -y git git clone https://github.com/qemu/qemu.git git clone https://github.com/libvirt/libvirt.git
配置并构建 qemu 代码,然后安装 qemu 代码。
make install
命令将在 /usr/local 目录中创建二进制文件。cd qemu mkdir -p build cd build # if you want the x86 target too you can add "x86-softmmu" in the target list ../configure --enable-trace-backend=simple --enable-debug --target-list=ppc64-softmmu --prefix=/usr/local make -j sudo make install
配置并构建 libvirt 代码,然后安装 libvirt 代码。
libvirt_build
中包含守护程序和 qemu 驱动程序的日志及配置文件。cd $HOME mkdir -p libvirt_build cd libvirt mkdir build cd build ../autogen.sh --prefix=$HOME/libvirt_build make -j # make install is required only once to generate the config and log file structure make install
运行完步骤 1、2 和 3 后,您会发现已在 /usr/local/bin 目录中编译并创建了以下 qemu 二进制文件。
$ ls /usr/local/bin ivshmem-client ivshmem-server qemu-edid qemu-ga qemu-img qemu-io qemu-nbd qemu-pr-helper qemu-storage-daemon qemu-system-ppc64
同样,libvirt 二进制文件位于 $HOME/libvirt/build 中。
这些二进制文件实际上存储在另一个目录内,但此目录中有一个名为 ‘run’ 的帮助程序脚本,可以用来运行这些二进制文件。该帮助程序脚本将设置环境变量以运行 libvirt 实用程序。
从 $HOME/libvirt/build 目录中运行以下命令以启动 libvirt 守护程序 (libvirtd)。
sudo ./run src/virtlockd &
(必须在后台运行)sudo ./run src/virtlogd &
(必须在后台运行)sudo ./run src/libvirtd
(我在前台运行的 libvirtd 守护程序,用于查看日志)这些已编译的守护程序的配置文件位于 $HOME/libvirt_build/etc/libvirt 中。
./run 脚本可用于运行这些工具的编译版本。
启动 libvirtd 守护程序后,可以使用
virsh
命令来处理虚拟机 (VM)。以下是我使用已编译的
virsh
的方式:build]$ pwd /home/sthoufee/libvirt/build build]$ build]$ sudo ./run tools/virsh destroy apic_test Domain apic_test destroyed build]$ sudo ./run tools/virsh list --all Id Name State ---------------------------- - apic_test shut off
注意,已编译的
virsh
命令可用于销毁虚拟机,如果查看虚拟机列表,则可以看到其状态为shut off
,以此确认该虚拟机已销毁。
附加信息
您可以通过运行 libvirt ./autogen.sh 来查看配置摘要。通过输出,您可以了解 qemu 驱动程序是否已启用。
-----
configure: Configuration summary
configure: =====================
configure:
configure: Drivers
configure:
configure: QEMU: yes <----------
(...)
-----
如果 qemu 未启用,可能是您的设置中缺少某些软件包。libvirt 无需 qemu 驱动程序也能运行(因为 libvirt 不依赖 qemu),但如果没有该驱动程序,您将无法运行 qemu 虚拟机。/etc/libvirt/qemu.conf 文件也将丢失。
注意,您必须使用已编译的 virsh
命令来处理已编译的 libvirt。使用来自系统安装的 virsh
命令可处理来自系统的 libvirt。
以下是在我的系统中运行的 virt 进程的输出示例。
[build]$ ps axf | grep virt
36770 pts/1 S 0:00 | \_ sudo ./run src/virtlockd
36778 pts/1 S 0:00 | | \_ /home/sthoufee/libvirt/build/src/.libs/lt-virtlockd
36814 pts/1 S 0:00 | \_ sudo ./run src/virtlogd
36816 pts/1 S 0:00 | | \_ /home/sthoufee/libvirt/build/src/.libs/lt-virtlogd
76055 pts/1 S+ 0:00 | \_ sudo ./run src/libvirtd
76063 pts/1 Sl+ 0:00 | \_ /home/sthoufee/libvirt/build/src/.libs/lt-libvirtd
135149 pts/2 S+ 0:00 \_ grep --color=auto virt
192443 ? Ss 0:00 /usr/sbin/virtlockd
192452 ? Ss 0:00 /usr/sbin/virtlogd
192462 ? Ssl 0:02 /usr/sbin/libvirtd
192632 ? S 0:03 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
192633 ? S 0:00 \_ /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
在此进程中,从 ‘/usr/sbin’ 中运行的所有命令都是来自基于系统的 libvirt OS 安装。以 sudo 运行的 virt 进程来自自定义 libvirt。这表明您可以同时运行基于系统的 libvirtd 守护程序和自定义 libvirtd 守护程序。
以下命令列出了在自定义 libvirt 实例上运行的虚拟机。
[build]$ sudo ./run tools/virsh list --all (handles custom libvirt)
Id Name State
----------------------------
- apic_test shut off
[build]$
[build]$
以下命令列出了在基于系统的 libvirt 实例上运行的虚拟机。
[build]$ sudo virsh list --all (handles system libvirt)
Id Name State
----------------------------------------------------
- apic_test shut off
- memhotplug shut off
- vcpupintest shut off
结束语
您将在 /usr/local/bin 目录中拥有本地 qemu 二进制文件,在 $HOME/libvirt/build 目录中(其配置文件在 $HOME/libvirt_build/etc/libvirt 中)拥有本地 libvirt 安装,并且这些二进制文件应与系统中现有的 qemu 和 libvirt 安装并存。通过本教程中介绍的过程,您应该能够在系统中运行两个 libvirt 守护程序 (libvirtd)(一个来自系统,一个从源代码编译而来),并且不会相互干扰。
本文翻译自:Compiling upstream libvirt and qemu from scratch(2020-07-30)