上一期由于篇幅所限,还有一些关键问题未能展开说明。为了让整体内容更加完整、逻辑更加清晰,本期我们将延续上一次的话题,继续深入探讨 KVM 虚拟化实战中的几个关键细节与注意事项。
相关内容可参考往期文章链接:KVM 虚拟化深度解析:Linux 最强内核级虚拟机(一)
使用如下命令创建了 Windows 虚拟机,分配了 16 个 vCPU,但是系统中只显示 2 个 vCPU。
virt-install --accelerate \--name windows10 \--memory 16384 \--vcpus 16 \--controller type=scsi,model=virtio-scsi \--disk path=/home/sdb1/KVM/VMs/windows10.qcow2,size=200,sparse=true,cache=none,bus=ide,format=qcow2 \--cdrom=/home/sdb1/KVM/ISOs/zh-cn_windows_10_business_editions_version_22h2_updated_feb_2025_x64_dvd_016e01fc.iso \--graphics vnc,listen=0.0.0.0,port=5901,password=Tmp4950Pa20 \--network network=default \--os-variant=win10
--vcpus 16 不指定拓扑时,KVM 默认把 16 个 vCPU 以 16 个独立插槽(socket) 的方式呈现给虚拟机:
默认拓扑:16 socket × 1 core × 1 thread = 16 vCPU而 Windows 10 的版本对物理插槽有限制:
挂载的镜像是 Business Editions,包含专业版,最多识别 2 个物理插槽,所以只用到了 2 个 vCPU。
Linux 无授权锁插槽,只受硬件与内核配置限制。
重新定义 CPU 拓扑,把 16 个 vCPU 放在 1 个或 2 个插槽里:
### 方案一:1个插槽,16个核心(推荐)virt-install \ --vcpus 16,sockets=1,cores=16,threads=1 \ ...其他参数不变...### 方案二:2个插槽,每个8核(如果需要模拟多路)virt-install \ --vcpus 16,sockets=2,cores=8,threads=1 \ ...其他参数不变...### 方案三:1个插槽,4核超线程(模拟常见桌面CPU)virt-install \ --vcpus 16,sockets=1,cores=8,threads=2 \ ...其他参数不变...如果虚拟机已经创建,直接修改 XML,不需要重新安装,直接编辑现有虚拟机配置:
virsh edit windows10找到 CPU 部分,修改为:
<vcpu placement="static">16</vcpu><cpu mode="host-passthrough"> <topology sockets="1" cores="8" threads="2"/></cpu>关于 NUMA 的建议
如果宿主机有两个 NUMA 节点,跨 NUMA 访问内存会有额外延迟。如果想进一步优化性能,可以把虚拟机的 CPU 和内存都绑定在同一个 NUMA 节点上:
<vcpu placement="static">16</vcpu><cpu mode="host-passthrough"> <topology sockets="1" cores="8" threads="2"/></cpu><numatune> <memory mode="strict" nodeset="0"/></numatune>这样虚拟机的内存全部从 NUMA node0 分配,避免跨节点访问,延迟更低。不过前提是 node0 有足够的空闲内存(本次实验中有 16GB 分配给 Windows,node0 需要有足够空间)。
修改完后重启虚拟机生效:
virsh shutdown windows10virsh start windows10NUMA 是什么?
NUMA = Non-Uniform Memory Access,非统一内存访问架构,简单来说:

用命令行创建虚拟机时,不知道你有没有关注 --os-variant 这个参数,它后面对应的是要安装的虚拟机操作系统名称,必须完全精确,不能写错、不能简写,我们如何知道安装某一个系统时应该写哪个名字?当然是可以查询的了^_^
不同的 KVM 版本支持的系统版本会有不同,以下输出做了大量删减,仅供参考。
[root@ovirt-node02 ~]# osinfo-query os Short ID | Name | Version | ID ----------------------+----------------------------------------------------+----------+---------------------------------------- android-x86-8.1 | Android-x86 8.1 | 8.1 | http://android-x86.org/android-x86/8.1 android-x86-9.0 | Android-x86 9.0 | 9.0 | http://android-x86.org/android-x86/9.0 archlinux | Arch Linux | | http://archlinux.org/archlinux/rolling asianux-unknown | Asianux unknown | unknown | http://asianux.com/asianux/unknown asianux4.6 | Asianux Server 4 SP6 | | http://asianux.com/asianux/4.6 asianux4.7 | Asianux Server 4 SP7 | | http://asianux.com/asianux/4.7 asianux7.0 | Asianux Server 7 | 7.0 | http://asianux.com/asianux/7.0 asianux7.1 | Asianux Server 7 SP1 | 7.1 | http://asianux.com/asianux/7.1 asianux7.2 | Asianux Server 7 SP2 | 7.2 | http://asianux.com/asianux/7.2 asianux7.3 | Asianux Server 7 SP3 | 7.3 | http://asianux.com/asianux/7.3 asianux8.0 | Asianux Server 8 | 8.0 | http://asianux.com/asianux/8.0 centos6.10 | CentOS 6.10 | 6.10 | http://centos.org/centos/6.10 centos6.2 | CentOS 6.2 | 6.2 | http://centos.org/centos/6.2 centos6.3 | CentOS 6.3 | 6.3 | http://centos.org/centos/6.3 centos6.4 | CentOS 6.4 | 6.4 | http://centos.org/centos/6.4 centos6.5 | CentOS 6.5 | 6.5 | http://centos.org/centos/6.5 centos6.6 | CentOS 6.6 | 6.6 | http://centos.org/centos/6.6 centos6.7 | CentOS 6.7 | 6.7 | http://centos.org/centos/6.7 centos6.8 | CentOS 6.8 | 6.8 | http://centos.org/centos/6.8 centos6.9 | CentOS 6.9 | 6.9 | http://centos.org/centos/6.9 centos7.0 | CentOS 7 | 7 | http://centos.org/centos/7.0 centos8 | CentOS 8 | 8 | http://centos.org/centos/8 debian1.1 | Debian GNU/Linux 1.1 | 1.1 | http://debian.org/debian/1.1 debian1.2 | Debian GNU/Linux 1.2 | 1.2 | http://debian.org/debian/1.2 debian1.3 | Debian GNU/Linux 1.3 | 1.3 | http://debian.org/debian/1.3 debian10 | Debian 10 | 10 | http://debian.org/debian/10 debian11 | Debian 11 | 11 | http://debian.org/debian/11 debian2.0 | Debian GNU/Linux 2.0 | 2.0 | http://debian.org/debian/2.0 debian2.1 | Debian GNU/Linux 2.1 | 2.1 | http://debian.org/debian/2.1 debian2.2 | Debian GNU/Linux 2.2 | 2.2 | http://debian.org/debian/2.2 debian3 | Debian GNU/Linux 3.0 | 3.0 | http://debian.org/debian/3 debian3.1 | Debian GNU/Linux 3.1 | 3.1 | http://debian.org/debian/3.1 debian4 | Debian GNU/Linux 4.0 | 4.0 | http://debian.org/debian/4 debian5 | Debian GNU/Linux 5.0 | 5.0 | http://debian.org/debian/5 debian6 | Debian 6.0 | 6.0 | http://debian.org/debian/6 debian7 | Debian 7 | 7 | http://debian.org/debian/7 debian8 | Debian 8 | 8 | http://debian.org/debian/8 debian9 | Debian 9 | 9 | http://debian.org/debian/9 ol7.4 | Oracle Linux 7.4 | 7.4 | http://oracle.com/ol/7.4 ol7.5 | Oracle Linux 7.5 | 7.5 | http://oracle.com/ol/7.5 ol7.6 | Oracle Linux 7.6 | 7.6 | http://oracle.com/ol/7.6 ol7.7 | Oracle Linux 7.7 | 7.7 | http://oracle.com/ol/7.7 ol7.8 | Oracle Linux 7.8 | 7.8 | http://oracle.com/ol/7.8 ol7.9 | Oracle Linux 7.9 | 7.9 | http://oracle.com/ol/7.9 ol8.0 | Oracle Linux 8.0 | 8.0 | http://oracle.com/ol/8.0 ol8.1 | Oracle Linux 8.1 | 8.1 | http://oracle.com/ol/8.1 ol8.2 | Oracle Linux 8.2 | 8.2 | http://oracle.com/ol/8.2 ol8.3 | Oracle Linux 8.3 | 8.3 | http://oracle.com/ol/8.3 ol8.4 | Oracle Linux 8.4 | 8.4 | http://oracle.com/ol/8.4 ol8.5 | Oracle Linux 8.5 | 8.5 | http://oracle.com/ol/8.5 openbsd6.8 | OpenBSD 6.8 | 6.8 | http://openbsd.org/openbsd/6.8 openbsd6.9 | OpenBSD 6.9 | 6.9 | http://openbsd.org/openbsd/6.9 opensolaris2009.06 | OpenSolaris 2009.06 | 2009.06 | http://sun.com/opensolaris/2009.06 opensuse-factory | openSUSE | factory | http://opensuse.org/opensuse/factory opensuse-unknown | openSUSE | unknown | http://opensuse.org/opensuse/unknown opensuse10.2 | openSUSE 10.2 | 10.2 | http://opensuse.org/opensuse/10.2 opensuse10.3 | openSUSE 10.3 | 10.3 | http://opensuse.org/opensuse/10.3 opensuse11.0 | openSUSE 11.0 | 11.0 | http://opensuse.org/opensuse/11.0 opensuse11.1 | openSUSE 11.1 | 11.1 | http://opensuse.org/opensuse/11.1 opensuse11.2 | openSUSE 11.2 | 11.2 | http://opensuse.org/opensuse/11.2 opensuse11.3 | openSUSE 11.3 | 11.3 | http://opensuse.org/opensuse/11.3 opensuse11.4 | openSUSE 11.4 | 11.4 | http://opensuse.org/opensuse/11.4 opensuse12.1 | openSUSE 12.1 | 12.1 | http://opensuse.org/opensuse/12.1 opensuse12.2 | openSUSE 12.2 | 12.2 | http://opensuse.org/opensuse/12.2 opensuse12.3 | openSUSE 12.3 | 12.3 | http://opensuse.org/opensuse/12.3 opensuse13.1 | openSUSE 13.1 | 13.1 | http://opensuse.org/opensuse/13.1 opensuse13.2 | openSUSE 13.2 | 13.2 | http://opensuse.org/opensuse/13.2 opensuse15.0 | openSUSE Leap 15.0 | 15.0 | http://opensuse.org/opensuse/15.0 opensuse15.1 | openSUSE Leap 15.1 | 15.1 | http://opensuse.org/opensuse/15.1 opensuse15.2 | openSUSE Leap 15.2 | 15.2 | http://opensuse.org/opensuse/15.2 opensuse15.3 | openSUSE Leap 15.3 | 15.3 | http://opensuse.org/opensuse/15.3 opensuse42.1 | openSUSE Leap 42.1 | 42.1 | http://opensuse.org/opensuse/42.1 opensuse42.2 | openSUSE Leap 42.2 | 42.2 | http://opensuse.org/opensuse/42.2 opensuse42.3 | openSUSE Leap 42.3 | 42.3 | http://opensuse.org/opensuse/42.3 popos17.10 | Pop!_OS 17.10 | 17.10 | http://system76.com/popos/17.10 popos18.04 | Pop!_OS 18.04 | 18.04 | http://system76.com/popos/18.04 popos18.10 | Pop!_OS 18.10 | 18.10 | http://system76.com/popos/18.10 popos19.04 | Pop!_OS 19.04 | 19.04 | http://system76.com/popos/19.04 popos19.10 | Pop!_OS 19.10 | 19.10 | http://system76.com/popos/19.10 popos20.04 | Pop!_OS 20.04 | 20.04 | http://system76.com/popos/20.04 popos20.10 | Pop!_OS 20.10 | 20.10 | http://system76.com/popos/20.10 pureos8 | PureOS | 8 | http://pureos.net/pureos/8 rhel-atomic-7.0 | Red Hat Enterprise Linux Atomic Host 7.0 | 7.0 | http://redhat.com/rhel-atomic/7.0 rhel-atomic-7.1 | Red Hat Enterprise Linux Atomic Host 7.1 | 7.1 | http://redhat.com/rhel-atomic/7.1 rhel-atomic-7.2 | Red Hat Enterprise Linux Atomic Host 7.2 | 7.2 | http://redhat.com/rhel-atomic/7.2 rhel-atomic-7.3 | Red Hat Enterprise Linux Atomic Host 7.3 | 7.3 | http://redhat.com/rhel-atomic/7.3 rhel-atomic-7.4 | Red Hat Enterprise Linux Atomic Host 7.4 | 7.4 | http://redhat.com/rhel-atomic/7.4 rhel6.1 | Red Hat Enterprise Linux 6.1 | 6.1 | http://redhat.com/rhel/6.1 rhel6.10 | Red Hat Enterprise Linux 6.10 | 6.10 | http://redhat.com/rhel/6.10 rhel6.2 | Red Hat Enterprise Linux 6.2 | 6.2 | http://redhat.com/rhel/6.2 rhel6.3 | Red Hat Enterprise Linux 6.3 | 6.3 | http://redhat.com/rhel/6.3 rhel6.4 | Red Hat Enterprise Linux 6.4 | 6.4 | http://redhat.com/rhel/6.4 rhel6.5 | Red Hat Enterprise Linux 6.5 | 6.5 | http://redhat.com/rhel/6.5 rhel6.6 | Red Hat Enterprise Linux 6.6 | 6.6 | http://redhat.com/rhel/6.6 rhel6.7 | Red Hat Enterprise Linux 6.7 | 6.7 | http://redhat.com/rhel/6.7 rhel6.8 | Red Hat Enterprise Linux 6.8 | 6.8 | http://redhat.com/rhel/6.8 rhel6.9 | Red Hat Enterprise Linux 6.9 | 6.9 | http://redhat.com/rhel/6.9 rhel7.0 | Red Hat Enterprise Linux 7.0 | 7.0 | http://redhat.com/rhel/7.0 rhel7.1 | Red Hat Enterprise Linux 7.1 | 7.1 | http://redhat.com/rhel/7.1 rhel7.2 | Red Hat Enterprise Linux 7.2 | 7.2 | http://redhat.com/rhel/7.2 rhel7.3 | Red Hat Enterprise Linux 7.3 | 7.3 | http://redhat.com/rhel/7.3 rhel7.4 | Red Hat Enterprise Linux 7.4 | 7.4 | http://redhat.com/rhel/7.4 rhel7.5 | Red Hat Enterprise Linux 7.5 | 7.5 | http://redhat.com/rhel/7.5 rhel7.6 | Red Hat Enterprise Linux 7.6 | 7.6 | http://redhat.com/rhel/7.6 rhel7.7 | Red Hat Enterprise Linux 7.7 | 7.7 | http://redhat.com/rhel/7.7 rhel7.8 | Red Hat Enterprise Linux 7.8 | 7.8 | http://redhat.com/rhel/7.8 rhel7.9 | Red Hat Enterprise Linux 7.9 | 7.9 | http://redhat.com/rhel/7.9 rhel8.0 | Red Hat Enterprise Linux 8.0 | 8.0 | http://redhat.com/rhel/8.0 rhel8.1 | Red Hat Enterprise Linux 8.1 | 8.1 | http://redhat.com/rhel/8.1 rhel8.2 | Red Hat Enterprise Linux 8.2 | 8.2 | http://redhat.com/rhel/8.2 rhel8.3 | Red Hat Enterprise Linux 8.3 | 8.3 | http://redhat.com/rhel/8.3 rhel8.4 | Red Hat Enterprise Linux 8.4 | 8.4 | http://redhat.com/rhel/8.4 rhel8.5 | Red Hat Enterprise Linux 8.5 | 8.5 | http://redhat.com/rhel/8.5 rhel9.0 | Red Hat Enterprise Linux 9.0 | 9.0 | http://redhat.com/rhel/9.0 rhl6.0 | Red Hat Linux 6.0 | 6.0 | http://redhat.com/rhl/6.0 rhl6.1 | Red Hat Linux 6.1 | 6.1 | http://redhat.com/rhl/6.1 rhl6.2 | Red Hat Linux 6.2 | 6.2 | http://redhat.com/rhl/6.2 rhl7 | Red Hat Linux 7 | 7 | http://redhat.com/rhl/7 rhl7.1 | Red Hat Linux 7.1 | 7.1 | http://redhat.com/rhl/7.1 rhl7.2 | Red Hat Linux 7.2 | 7.2 | http://redhat.com/rhl/7.2 rhl7.3 | Red Hat Linux 7.3 | 7.3 | http://redhat.com/rhl/7.3 rhl8.0 | Red Hat Linux 8.0 | 8.0 | http://redhat.com/rhl/8.0 rhl9 | Red Hat Linux 9 | 9 | http://redhat.com/rhl/9 rocky8.4 | Rocky Linux 8.4 | 8.4 | http://rockylinux.org/rocky/8.4 rocky8.5 | Rocky Linux 8.5 | 8.5 | http://rockylinux.org/rocky/8.5 rocky8.6 | Rocky Linux 8.6 | 8.6 | http://rockylinux.org/rocky/8.6 rocky9.0 | Rocky Linux 9.0 | 9.0 | http://rockylinux.org/rocky/9.0 sled12sp1 | SUSE Linux Enterprise Desktop 12 SP1 | 12.1 | http://suse.com/sled/12.1 sled12sp2 | SUSE Linux Enterprise Desktop 12 SP2 | 12.2 | http://suse.com/sled/12.2 sled12sp3 | SUSE Linux Enterprise Desktop 12 SP3 | 12.3 | http://suse.com/sled/12.3 sled12sp4 | SUSE Linux Enterprise Desktop 12 SP4 | 12.4 | http://suse.com/sled/12.4 sled12sp5 | SUSE Linux Enterprise Desktop 12 SP5 | 12.5 | http://suse.com/sled/12.5 sles12 | SUSE Linux Enterprise Server 12 | 12 | http://suse.com/sles/12 sles12sp1 | SUSE Linux Enterprise Server 12 SP1 | 12.1 | http://suse.com/sles/12.1 sles12sp2 | SUSE Linux Enterprise Server 12 SP2 | 12.2 | http://suse.com/sles/12.2 sles12sp3 | SUSE Linux Enterprise Server 12 SP3 | 12.3 | http://suse.com/sles/12.3 sles12sp4 | SUSE Linux Enterprise Server 12 SP4 | 12.4 | http://suse.com/sles/12.4 sles12sp5 | SUSE Linux Enterprise Server 12 SP5 | 12.5 | http://suse.com/sles/12.5 sles9 | SUSE Linux Enterprise Server 9 | 9 | http://suse.com/sles/9 solaris10 | Solaris 10 | 10 | http://sun.com/solaris/10 solaris11 | Oracle Solaris 11 | 11 | http://oracle.com/solaris/11 solaris9 | Solaris 9 | 9 | http://sun.com/solaris/9 trisquel9 | Trisquel | 9 | http://trisquel.info/trisquel/9 ubuntu20.04 | Ubuntu 20.04 LTS | 20.04 | http://ubuntu.com/ubuntu/20.04 ubuntu20.10 | Ubuntu 20.10 | 20.10 | http://ubuntu.com/ubuntu/20.10 ubuntu21.04 | Ubuntu 21.04 | 21.04 | http://ubuntu.com/ubuntu/21.04 ubuntu21.10 | Ubuntu 21.10 | 21.10 | http://ubuntu.com/ubuntu/21.10 win1.0 | Microsoft Windows 1.0 | 1.0 | http://microsoft.com/win/1.0 win10 | Microsoft Windows 10 | 10.0 | http://microsoft.com/win/10 win2.0 | Microsoft Windows 2.0 | 2.0 | http://microsoft.com/win/2.0 win2.1 | Microsoft Windows 2.1 | 2.1 | http://microsoft.com/win/2.1 win2k | Microsoft Windows 2000 | 5.0 | http://microsoft.com/win/2k win2k12 | Microsoft Windows Server 2012 | 6.3 | http://microsoft.com/win/2k12 win2k12r2 | Microsoft Windows Server 2012 R2 | 6.3 | http://microsoft.com/win/2k12r2 win2k16 | Microsoft Windows Server 2016 | 10.0 | http://microsoft.com/win/2k16 win2k19 | Microsoft Windows Server 2019 | 10.0 | http://microsoft.com/win/2k19 win2k22 | Microsoft Windows Server 2022 | 10.0 | http://microsoft.com/win/2k22 win2k3 | Microsoft Windows Server 2003 | 5.2 | http://microsoft.com/win/2k3 win2k3r2 | Microsoft Windows Server 2003 R2 | 5.2 | http://microsoft.com/win/2k3r2 win2k8 | Microsoft Windows Server 2008 | 6.0 | http://microsoft.com/win/2k8 win2k8r2 | Microsoft Windows Server 2008 R2 | 6.1 | http://microsoft.com/win/2k8r2 win3.1 | Microsoft Windows 3.1 | 3.1 | http://microsoft.com/win/3.1 win7 | Microsoft Windows 7 | 6.1 | http://microsoft.com/win/7 win8 | Microsoft Windows 8 | 6.2 | http://microsoft.com/win/8 win8.1 | Microsoft Windows 8.1 | 6.3 | http://microsoft.com/win/8.1 win95 | Microsoft Windows 95 | 4.0 | http://microsoft.com/win/95 win98 | Microsoft Windows 98 | 4.1 | http://microsoft.com/win/98 winme | Microsoft Windows Millennium Edition | 4.9 | http://microsoft.com/win/me winnt3.1 | Microsoft Windows NT Server 3.1 | 3.1 | http://microsoft.com/winnt/3.1 winnt3.5 | Microsoft Windows NT Server 3.5 | 3.5 | http://microsoft.com/winnt/3.5 winnt3.51 | Microsoft Windows NT Server 3.51 | 3.51 | http://microsoft.com/winnt/3.51 winnt4.0 | Microsoft Windows NT Server 4.0 | 4.0 | http://microsoft.com/winnt/4.0 winvista | Microsoft Windows Vista | 6.0 | http://microsoft.com/win/vista winxp | Microsoft Windows XP | 5.1 | http://microsoft.com/win/xp你是否好奇,全是命令行的执行方式,如何完成操作系统安装过程中需要图形化操作的内容?这个秘密就存在于以下选项中:
--graphics vnc,listen=0.0.0.0,port=5901,password=Tmp4950Pa20--graphics 设置虚拟机的图形显示方式vnc 图形输出使用 VNC 协议listen=0.0.0.0 VNC 监听所有网卡 IPport=5901 VNC 连接端口号,设置不同的端口号就可以管理不同的虚拟机password=Tmp4950Pa20 VNC 连接密码下载一个 VNC 客户端,使用上面的连接信息,即可在图形界面环境中完成操作系统的安装。

有时候使用 virsh shutdown myvm 过于温柔无法关闭虚拟机,可以通过 virsh destroy myvm 进行强制断电。
虚拟机默认使用 DHCP 的方式获得 IP,可以通过如下命令查询:
[root@appt02 ~]# virsh net-dhcp-leases default
Expiry Time MAC address Protocol IP address Hostname Client ID or DUID
-------------------------------------------------------------------------------------------------------------------
2026-04-27 22:44:00 52:54:00:25:a1:ef ipv4 192.168.122.39/24 DESKTOP-82TEH0H 01:52:54:00:25:a1:ef
2026-04-27 22:27:27 52:54:00:88:12:7c ipv4 192.168.122.210/24 WIN-BVHD77ISGU6 01:52:54:00:88:12:7c使用 virsh undefine myvm 时仅删除了相关的定义文件,对应的磁盘文件还在占用空间。查询virsh的帮助手册时会发现有一个选项 --remove-all-storage 可以达成我们的目标,而如果你真的使用 virsh undefine --remove-all-storage myvm 想一并删除磁盘文件时,会发现它连系统镜像 ISO文件也一并删除了,这通常不是我们需要的结果(保留 ISO 文件方便下次使用)。
标准删除虚拟机的流程,可以这样操作:
virsh shutdown myvm # 正常关机virsh destroy myvm # 强制关机 virsh list --allvirsh undefine myvmrm /path/to/xxx.qcow2在实际部署和使用 KVM 的过程中,很多问题往往出在一些容易被忽视的配置细节或经验盲区,例如性能调优、网络配置、存储选择以及常见故障的排查思路等。这些内容看似零散,却直接影响到虚拟化环境的稳定性与使用体验。
祝你用的愉快~
💬 欢迎在评论区留下你的真知灼见~
