virsh attach/detach 的延时问题:重试不是灵药,可能是毒药

virsh detach 后 domblklist 有短暂延时,detach 成功后立即 detach 相同卷也能成功,两次重复 detach 后再 attach 会失败。根因是 libvirt 不是实时状态,有一定延时。 现象 已测试:连续 attach、detach、attach、detach… 反复是没有问题的。 有问题的: detach 后,virsh domblklist 是没有卸载的,要过一两秒,有短暂延时; detach 成功后,立即 detach 相同的卷,居然也可以成功。预期应该是会报错提示没有对应的卷; 两次重复的 detach 后,再 attach,attach 命令成功,但实际没有把卷挂载上。 根因 我们挺多奇怪的问题,可能都是类似的原因造成的。 libvirt 和 qemu: libvirt 不是实时状态,有一定延时; 然后一些重复操作,看上去是幂等的,但是它对后续操作有影响(其实也是 libvirt 的 bug)。 比如之前有过 domblk 和 qmp 查询卷不一样的情况,造成后续丢数据等。 设计思考 做重试之前,要充分测试。 是否达到了重试条件(状态、时间); 这个动作是否幂等; 对后续动作是否有影响。 重试不是灵药,可能是毒药。 小结 virsh detach 后 domblklist 有短暂延时,要过一两秒; detach 成功后立即 detach 相同卷也能成功,不符合预期; 两次重复 detach 后再 attach,attach 成功但实际没挂载上; 根因是 libvirt 不是实时状态,有一定延时; 做重试之前要充分测试,重试不是灵药,可能是毒药。

February 1, 2026 · 1 min · 73 words · 文叨叨

libvirt CPU feature 的 disable 和 forbid 有什么区别?

libvirt CPU feature 的 policy 有 force、require、optional、disable、forbid 五种。disable 是虚拟 CPU 不支持该特性,forbid 是宿主机支持该特性时创建会失败。 现象 在 libvirt XML 中配置 CPU feature: <cpu> <model>EPYC</model> <feature name="pclmuldq" policy="disable"/> <feature name="aes" policy="disable"/> <feature name="sha-ni" policy="disable"/> <feature name="monitor" policy="disable"/> <feature name="svm" policy="disable"/> <feature name="npt" policy="disable"/> 【物理机支撑 npt 的】 <topology cores="1" sockets="1" threads="1"/> </cpu> 物理 CPU 是有 npt 的,virsh create 的时候就报错,而且这是 disable 也报错。 足以说明,不是 CPU 问题,而是 libvirt 不支持问题: 2025-06-24 12:50:13,539 WARNING -140207347541760- cmd [virsh create /data/vmxml/vm-example.xml] on [local] failed: ret[1] out[] err[error: Failed to create domain from /data/vmxml/vm-example.xml error: unsupported configuration: unknown CPU feature: npt]. exec_time is [0.089]s 排查 在 commandline 中禁用 npt 是可以创建虚拟机的 <qemu:commandline> <qemu:arg value="-cpu"/> <qemu:arg value="EPYC,vendor=AuthenticAMD,model_id=Example CPU 7380 32-core Processor,-x2apic,-hypervisor,-topoext,-npt,-nrip-save,-monitor,-svm"/> </qemu:commandline> 说明什么? ...

January 31, 2026 · 2 min · 267 words · 文叨叨

qemu 为什么只支持一个 IDE controller?

qemu 只支持一个 IDE controller,一个 IDE controller 只支持 4 个设备(hda 到 hdd)。定义两个 IDE controller 或使用 hde 都会报错。 现象 在 libvirt XML 中定义两个 IDE controller: <controller type='ide' index='0'/> <controller type='ide' index='1'/> 直接会报如下错误: error: Failed to create domain from vm-example.xml error: unsupported configuration: Only a single IDE controller is supported for this machine type 排查 默认有一个 IDE controller 默认有一个 IDE controller。 所以不定义 <controller type='ide' index='0'/>,也是有一个 IDE controller 的。 直接定义 IDE disk 的情况 <disk type="network" device="cdrom"> <driver name="qemu" type="raw"></driver> <source protocol="http" name="/yy/img-example.iso"> <host name="qa1b01" port="8021"></host> </source> <target dev="hdd" bus="ide"></target> 【注意 hdd】 <readonly></readonly> <shareable></shareable> </disk> 直接定义了 IDE 的 disk,没有定义 IDE 的 controller。 ...

January 24, 2026 · 1 min · 174 words · 文叨叨