[QEMU]虚拟 CPU 热插拔

使用 QMP 和 的 vCPU 热插拔(和热拔出)的完整示例。device_add
device_del
vCPU hotplug
按如下方式启动 QEMU(请注意,“maxcpus”是必需的,以允许 vCPU 热插拔):
$ qemu-system-x86_64 -display none -no-user-config -m 2048 \ -nodefaults -monitor stdio -machine pc,accel=kvm,usb=off \ -smp 1,maxcpus=2 -cpu IvyBridge-IBRS \ -qmp unix:/tmp/qmp-sock,server=on,wait=off
运行 'qmp-shell'(位于源代码树中,在:“scripts/qmp/”下)以连接到刚刚启动的 QEMU:
$> ./qmp-shell -p -v /tmp/qmp-sock [...] (QEMU)
找出哪些CPU类型可以插入,以及哪些插槽:
(QEMU) query-hotpluggable-cpus{ "execute": "query-hotpluggable-cpus", "arguments": {}}{ "return": [ { "type": "IvyBridge-IBRS-x86_64-cpu", "vcpus-count": 1, "props": { "socket-id": 1, "core-id": 0, "thread-id": 0 } }, { "qom-path": "/machine/unattached/device[0]", "type": "IvyBridge-IBRS-x86_64-cpu", "vcpus-count": 1, "props": { "socket-id": 0, "core-id": 0, "thread-id": 0 } } ]}(QEMU)
该命令为存在的 CPU(包含“qom-path”成员)或可能已热插拔(没有“qom-path”成员)返回一个对象。从其在步骤(3)中的输出中,我们可以看到插槽0中存在,而将CPU热插拔到插槽1中需要将列出的属性传递给QMP:
query-hotpluggable-cpus
IvyBridge-IBRS-x86_64-cpu
device_add
(QEMU) device_add id=cpu-2 driver=IvyBridge-IBRS-x86_64-cpu socket-id=1 core-id=0 thread-id=0{ "execute": "device_add", "arguments": { "socket-id": 1, "driver": "IvyBridge-IBRS-x86_64-cpu", "id": "cpu-2", "core-id": 0, "thread-id": 0 }}{ "return": {}}(QEMU)
(可选)运行 QMP 以获取有关 vCPU 的一些详细信息:
query-cpus-fast
(QEMU) query-cpus-fast{ "execute": "query-cpus-fast", "arguments": {}}{ "return": [ { "qom-path": "/machine/unattached/device[0]", "target": "x86_64", "thread-id": 11534, "cpu-index": 0, "props": { "socket-id": 0, "core-id": 0, "thread-id": 0 }, "arch": "x86" }, { "qom-path": "/machine/peripheral/cpu-2", "target": "x86_64", "thread-id": 12106, "cpu-index": 1, "props": { "socket-id": 1, "core-id": 0, "thread-id": 0 }, "arch": "x86" } ]}(QEMU)
vCPU 热拔出
从“qmp-shell”中,调用 QMP 命令:device_del
(QEMU) device_del id=cpu-2{ "execute": "device_del", "arguments": { "id": "cpu-2" }}{ "return": {}}(QEMU)
注意
vCPU热拔卡需要客户合作;所以上面的命令不能保证vCPU删除 - 这是一个“拔掉插头的请求”。此时,来宾将获得系统控制中断 (SCI),并调用受影响的 vCPU 设备的 ACPI 处理程序。然后,来宾内核将使 vCPU 脱机,并告诉 QEMU 将其拔出。device_del