qemu monitor

qemu向外界暴露的唯一接口就是 qemu monitor, 通过monitor可以 查询虚拟机信息, 控制虚拟机。传输数据方式是网络套结字。 qemu monitor 支持几种协议类型: qmp 和 hmp ; 通过monitor能够完成查询虚拟机的内部状态,进行设备的热插拔,虚拟机的迁移/备份/快照等功能。
qmp (Qemu Monitor Protocol)是一种基于JSON格式的传输协议,定义了与虚拟机的交互规范,底层支撑了QM的实现;
hmp(human monitor) 是一个方便用户阅读的协议,不必关心他的底层JSON;
一般手工维护大部分都是用 hmp 方式,而做自动化工具维护或者二次定制就需要基于QMP进行封装。

直接使用 qemu 启动时可以通过 tcp 和 unix socket 来指定 qemu monitor 的接口,而通过 virt-manager/libvirtd 启动的虚拟机只能通过 virsh 来进行虚拟机交互。

hmp接口使用

使用-monitor 通过 qmp 方式进行交互:

tcp socket 方式

./qemu-system-x86_64 -m 2048 -hda /home/linux/virt/xxxx.qcow2 -enable-kvm -monitor tcp:localhost:1234,server,nowait

使用telnet连接localhost:1234之后就可以使用qmp的命令和虚拟机交互了

[linux@localhost qemu]$ telnet localhost 1234
Trying ::1...
Connected to localhost.
Escape character is '^]'.
QEMU 2.12.0 monitor - type 'help' for more information
(qemu) help
help
__com.redhat_drive_add id=name,[file=file][,format=f][,media=d]... -- Create a drive similar to -drive if=none.
__com.redhat_qxl_screendump id filename -- save screen from qxl device 'id' into PPM image 'filename'

unix socket 方式

./qemu-system-x86_64 -m 2048 -hda /root/centos6.img -enable-kvm -monitor unix:/tmp/qmp-test,server,nowait

使用nc连接该socket,之后就一样了。

[root@localhost qmp]# nc -U /tmp/qmp-test
QEMU 2.12.0 monitor - type 'help' for more information
(qemu) info
info balloon  -- show balloon information

hmp 的详细命令格式可以在qemu的代码树主目录下面的hmp-commands.hx hmp-commands-info.hx中找到。

qmp接口使用

使用 -qmp 指定通过 json 方式进行交互:

./qemu-system-x86_64 -m 2048 -hda /home/linux/virt/xxxx.qcow2 -enable-kvm -qmp tcp:localhost:1234,server,nowait

使用telnet连接localhost:1234之后就可以使用qmp的命令和虚拟机交互了。但是QMP基于json格式,所以在命令行里手动输入json就不是很方便。

[linux@localhost qemu]$ telnet localhost 1234
Trying ::1...
Connected to localhost.
Escape character is '^]'.
{"QMP": {"version": {"qemu": {"micro": 0, "minor": 12, "major": 2}, "package": "v2.12.0-41-ga481765"}, "capabilities": []}}

其他方式和 hmp 中接口类似,不再赘述。

qemu 源码中提供了对QMP的封装示例scripts/qmp/ 提供了一些 demo

[linux@localhost qemu]$ ls ./scripts/qmp/
__init__.py  qemu-ga-client  qmp  qmp.py  qmp.pyc  qmp-shell  qom-fuse  qom-get  qom-list  qom-set  qom-tree

通过libvirt启动时使用qemu monitor

不能再通过端口直接连接qemu,而是需要通过 virsh -> libvirtd -> qemu 中转数据,可以通过如下方式使用qemu monitor

virsh qemu-monitor-command debian10 --hmp "x/128i 0x98000000012fbf09"

常用用法

查询cpu类型和machine类型

qemu-system-mips64el -cpu ?
qemu-system-mips64el -machine ?

cpu热插拔

通过qemu启动,并且开放qemu monitor端口: -monitor tcp:localhost:1234,server,nowait

# telnet 127.0.0.1 1234
(qemu) info hotpluggable-cpus 
Hotpluggable CPUs:
  type: "Loongson-3A4000-COMP-mips64-cpu"
  vcpus_count: "1"
  CPUInstance Properties:
    node-id: "0"
    core-id: "3"
  type: "Loongson-3A4000-COMP-mips64-cpu"
  vcpus_count: "1"
  CPUInstance Properties:
    node-id: "0"
    core-id: "2"
  type: "Loongson-3A4000-COMP-mips64-cpu"
  vcpus_count: "1"
  qom_path: "/machine/unattached/device[1]"
  CPUInstance Properties:
    node-id: "0"
    core-id: "1"
  type: "Loongson-3A4000-COMP-mips64-cpu"
  vcpus_count: "1"
  qom_path: "/machine/unattached/device[0]"
  CPUInstance Properties:
    node-id: "0"
    core-id: "0"
(qemu) device_add driver=Loongson-3A4000-COMP-mips64-cpu,node-id=0,core-id=3,id=cpu2

官方链接: https://wiki.qemu.org/Features/CPUHotplug#cpu-add_interface

链接

gentoo官网: https://wiki.gentoo.org/wiki/QEMU/OptionsBoot_order
qemu官网: https://www.qemu.org/docs/master/system/invocation.html
suse virsh官网: https://doc.opensuse.org/documentation/leap/virtualization/html/book-virt/cha-qemu-monitor.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值