返回首页

QEMU安装使用手册

注:CentOS/Rocky/Alma 8/9,使用systemd;Mac mini测试通过。

一、安装qemu

1、安装依赖

dnf install flex libslirp-devel libcap-ng-devel libattr-devel glib2-devel pixman-devel zlib-devel libcap-devel libattr-devel gcc gcc-c++ make git flex bison bzip2 pam-devel gnutls-devel perl -y
#Mac mini
brew install glib pixman libffi gettext pkgconf

2、下载、编译

wget https://download.qemu.org/qemu-9.2.2.tar.xz
tar xvf qemu-9.2.2.tar.xz
cd qemu-9.2.2
./configure --prefix=/usr/local/kvm --interp-prefix=/usr/local/kvm/gnemul --enable-kvm --enable-modules --enable-vnc --enable-virtfs --enable-auth-pam --target-list=x86_64-softmmu
#Mac mini
./configure --prefix=/usr/local/kvm --interp-prefix=/usr/local/kvm/gnemul --enable-modules --enable-vnc --enable-virtfs --enable-auth-pam --target-list=x86_64-softmmu
make -j5
make install
ln -sf /usr/local/kvm/bin/* /usr/local/bin/

二、创建、启动虚拟机

mkdir /usr/local/kvm/img
qemu-img create -f qcow2 /usr/local/kvm/img/win7.qcow2 20G
qemu-system-x86_64 -m 2048 -enable-kvm -machine usb=on -device usb-tablet -vnc :1 /usr/local/kvm/img/win7.qcow2 -cdrom /usr/local/os/win7.iso -boot d -device intel-hda -vga virtio -device virtio-net-pci,netdev=net0 -netdev user,id=net0,hostfwd=tcp::2222-:22,smb=/usr/local/share -nodefaults -smp 2,sockets=1
#-machine usb=on -device usb-tablet #解决vnc连接时鼠标不同步问题
#-vnc :1 #以主机的5901端口vnc连接虚拟机
#-device intel-hda或-device ich9-intel-hda #默认声卡,有的设备需要再加一个参数:-device hda-duplex
#-device virtio-net-pci,netdev=net0 #设置网卡为virtio,也可以设置为rtl8139
#-netdev user,id=net0,hostfwd=tcp::2222-:22 #将虚拟机的22端口映射到主机2222端口上
#smb=/data/share #qemu内建的samba服务,将主机的/data/share目录设置为共享之后,虚拟机可以通过\\10.0.2.4\qemu来访问(完全共享)
#-nodefaults #不创建默认设备,如:软盘、光驱等,使用此选项,必须加-vga选项,不然会提示没有显示设备
#-smp 4,sockets=1,cores=2 #设置cpu核心数,如:1个物理CPU(双核),4个逻辑CPU
#-boot d #安装时使用,即D盘(光驱)启动

注:qemu旧版中有qemu-kvm模块存在,新版中需要使用kvm特性时候,在qemu-system-x86_64命令中使用--enable-kvm即可。

*使用virtio

wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.262-2/virtio-win.iso

*压缩qcow2

1、客户机空间清零

Windows系统:使用sdelete(下载:https://docs.microsoft.com/zh-cn/sysinternals/downloads/sdelete)

sdelete.exe -z c:

Linux系统:

dd if=/dev/zero of=zero
rm -f zero

2、压缩qcow2

qemu-img convert -c -f qcow2 -O qcow2 -c old.qcow2 new.qcow2

三、安装libvirt

1、安装依赖

dnf install gnutls-devel libnl-devel libpciaccess-devel yajl-devel device-mapper-devel -y

2、下载、编译

wget https://download.libvirt.org/libvirt-10.8.0.tar.xz
tar xvf libvirt-10.8.0.tar.xz
cd libvirt-10.8.0
mkdir build
cd build
../configure --prefix=/usr/local/kvm --without-vmware --without-vbox --without-openvz --without-esx --without-lxc --with-yajl --with-qemu
#去掉vmware/vbox/openvz/esx/lxc支持,只支持qemu
make -j5
make install
ln -sf /usr/local/kvm/lib/pkgconfig/* /usr/lib64/pkgconfig/

3、配置

ln -sf /usr/local/kvm/bin/* /usr/local/bin/
ln -sf /usr/local/kvm/sbin/* /usr/local/bin/
cp src/libvirtd.service /lib/systemd/system/
cp src/libvirtd.socket /lib/systemd/system/
cp src/virtlogd.service /lib/systemd/system/
cp src/virtlogd.socket /lib/systemd/system/
cp src/virtlockd.service /lib/systemd/system/
cp src/virtlockd.socket /lib/systemd/system/
systemctl start libvirtd
systemctl enable libvirtd

四、安装libvirt-php

1、下载、编译

wget https://download.libvirt.org/php/libvirt-php-0.5.8.tar.xz
tar xvf libvirt-php-0.5.8.tar.xz
cd libvirt-php-0.5.8
./configure --with-php-config=/usr/local/php/bin/php-config --with-php-extensiondir=/usr/local/php/lib/php/extensions/no-debug-non-zts-20190902 --with-php-confdir=/usr/local/php/lib/php/extensions/
make -j5
make install

2、重启php,查看是否安装成功

systemctl restart php-fpm
php -m|grep -i libvirt

返回首页

© 2016-2025 清风的个人笔记