Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to interact with qemu virtual machine based on QMP

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to interact with the qemu virtual machine based on QMP". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

Introduction to ▪ QMP

▪ QMP syntax

▪ uses qemu alone to enable QMP

▪ starts qemu through libvirt and enables QMP

▪ qemu-guest-agent (qemu-ga)

▪ official reference documentation

QMP introduction

Qemu provides an external socket interface, called qemu monitor, through which you can manage the entire lifecycle of a virtual machine instance with the following functions

▷ status view, change

▷ device view, change

▷ performance review, limitation

▷ online migration

▷ data backup

▷ accesses internal operating system

The protocol that passes the interaction through the socket interface is qmp, the full name is qemu monitor protocol, which is based on the json format.

Before you go any further, you need to understand the difference between qemu, kvm, and libvirt (because many children's shoes have a confused understanding of the three)

▷ qemu: virtual machine emulator. Cpu, memory, disk, motherboard, network card and other devices are simulated by software.

▷ kvm: a high-performance cpu simulator. Due to the poor performance of cpu simulated by software, kvm appears, which is a cpu simulator which is close to native performance through the support of hardware and kernel, which can be understood as the cpu task in the virtual machine is directly handed over to the physical machine cpu to complete.

▷ libvirt: virtual machine management platform. Can manage qemu, lxc, esx and other virtualization software, and write xml to configure and manage virtual machines, storage, network, etc.

Only the core functions are described above, and some advanced functions and overlapping functions are not described here, otherwise they are easy to be confused.

QMP Syntax # directives without parameters {"execute": "XXX"} # instructions with parameters {"execute": "XXX", "arguments": {...}} use qemu alone to enable QMP

Start the qemu virtual machine

# qemu monitor uses tcp mode, listening on 127.0.0.1, port 4444/usr/libexec/qemu-kvm-qmp tcp:127.0.0.1:4444,server,nowait# qemu monitor and unix socket,socket file generated in / opt/qmp.socket/usr/libexec/qemu-kvm-qmp unix:/opt/qmp.socket,server,nowait

Connect qemu monitor

# tcp can connect through telnet The method is as follows: telnet 127.0.0.1 4444Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is'^'. {"QMP": {"version": {"qemu": {"micro": 0, "minor": 12, "major": 2}, "package": "qemu-kvm-ev-2.12.0-18.el7_6.1.1"} "capabilities": []}} # unix socket can be connected through nc-U The method is as follows: nc-U qmp.socket {"QMP": {"version": {"micro": 0, "minor": 12, "major": 2}, "package": "qemu-kvm-ev-2.12.0-18.el7_6.1.1"}, "capabilities": []}}

After executing the command above, you will not quit but continue to wait for input, but it cannot be used at this time. Then, you need to enter a qmp instruction.

{"execute": "qmp_capabilities"}

The screen will output the following, indicating that you have entered "command" mode from "capabilities negotiation mode"

{"return": {}}

Next, you can execute qmp instructions. There are a lot of qmp instructions. Due to the limited space, here are only a few examples (please refer to the official documentation for more information, and the URL is attached at the end of this article).

# check which qmp instructions {"execute": "query-commands"} # virtual machine status {"execute": "query-status"} # virtual machine pause {"execute": "stop"} # disk view {"execute": "query-block"} # disk online insert {"execute": "blockdev-add", "arguments": {"driver": "qcow2" "node-name": "drive-virtio-disk1", "file": {"driver": "file", "filename": "/ opt/data.qcow2"} {"execute": "device_add", "arguments": {"driver": "virtio-blk-pci", "drive": "drive-virtio-disk1"} # disk full backup {"execute": "drive-backup" "arguments": {"device": "drive-virtio-disk0", "sync": "full", "target": "/ opt/backuptest/fullbackup.img"}}

In addition to using telnet and nc to connect from the outside, you can enter instructions directly into an interactive cli interface when qemu starts, but at this time you enter hmp (human monitor protocol) instead of qmp. Hmp simplifies the use of qmp, but it is still converted to qmp at the bottom. The configuration method is as follows.

/ usr/libexec/qemu-kvm-qmp tcp:127.0.0.1:4444,server,nowait-monitor stdio

The interactive interface appears, type help, and you can see all the commands supported by hmp

(qemu) help

You do not need to enter {"execute": "qmp_capabilities"} like qmp to use hmp

Here are a few examples

# enter info enter directly, you can see the instructions of all query classes using (qemu) info# View Block device (qemu) info block# online add disk (qemu) drive_add 0 file=/opt/data.qcow2,format=qcow2,id=drive-virtio-disk1,if=none (qemu) device_add virtio-blk-pci,scsi=off,drive=drive-virtio-disk1 launch qemu through libvirt, enable QMP

There are two ways:

1. Without any additional configuration in xml, QMP is enabled by default, but QMP enabled by this method can only input QMP instructions through libvirt APIs (such as virsh command or libvirt api), not through telnet, nc and so on, because QMP enabled by default will only generate unix socket (located in / var/lib/libvirt/qemu/domain-xx-DOMAIN/monitor.sock), and the socket is always occupied by libvirtd connection. At this point, you can see the qemu process parameters through the ps aux command, which is a little different from the previous one, not-qmp, but as follows

-chardev socket,id=charmonitor,fd=36,server,nowait\-mon chardev=charmonitor,id=monitor,mode=control

The qemu command parameter supports two ways to configure qmp, namely-qmp and-mon

Here is a simple demonstration through virsh.

Virsh qemu-monitor-command DOMAIN-pretty'{"execute": "query-block"}'

-- pretty is used to format the output of json with newline indentation, rather than printing on a line without the need to execute {"execute": "qmp_capabilities"} before executing other instructions.

two。 Add 2 additional sections to the xml. Note that the first line of the xml below requires the addition of a xmlns:qemu and an additional qemu:command.

......

Then start qemu (such as virsh start xxx) through libvirt, and create two qmp channels, one is created by default by libvirt, you can still use the libvirt interface to execute QMP instructions, and the other is a custom qmp, which can be used through the nc mentioned above

Nc-U / tmp/qmp-sock

Libvirt also supports hmp:

Virsh qemu-monitor-command DOMAIN-hmp 'info block'qemu-guest-agent (qemu-ga)

Through qmp, you can also RPC the operating system in the virtual machine. The principle is as follows:

1. First configure the channel segment in xml, and then start the virtual machine, which will generate a unix socket on the host and a character device in the vm. The resulting unix socket and character device can be understood as both ends of a channel tunnel.

two。 The qemu-guest-agent daemon is started in the virtual machine, which listens for character devices

3. Then the RPC instructions supported by qemu-guest-agent in the virtual machine can be sent to the virtual machine through channel on the host. After receiving data from the character device, the qemu-guest-agent in the virtual machine executes instructions, such as reading and writing files, changing passwords, and so on.

To use qemu-guest-agent, you need to meet the following conditions

1. Configure channel in xml. Example:

......

Note that path can be customized, but name needs to keep org.qemu.guest_agent.0, because this will affect the file name of the character device in the virtual machine, and the qemu-guest-agent service in the virtual machine reads the character device corresponding to org.qemu.guest_agent.0 by default. If you change name, then the configuration file of qemu-guest-agent should also be changed to the path corresponding to name.

two。 The operating system kernel in the virtual machine needs to be supported (both linux and windows)

3. Services in the virtual machine to install and start qemu-ga (for example, centos can yum install qemu-ga & & systemctl start qemu-guest-agent,windows by importing the iso of virtio-win, which contains the qemu-ga program)

After the above configuration is completed, RPC operation can be performed on the host.

# Test whether the qemu-guest-agent in the virtual machine is available with virsh qemu-agent-command DOMAIN-- pretty'{"execute": "guest-ping"}'# View the supported qemu-guest-agent directive virsh qemu-agent-command DOMAIN-- pretty'{"execute": "guest-info"}'# to get the network card information virsh qemu-agent-command DOMAIN-- pretty'{"execute": "guest-network-get-interfaces"}'# execute the command This is asynchronous, and the first step will return a pid, assuming 797 In the second step, you need to bring this pidvirsh qemu-agent-command DOMAIN-- pretty'{"execute": "guest-exec", "arguments": {"path": "ip", "arg": ["addr", "list"], "capture-output": true} 'virsh qemu-agent-command DOMAIN-- pretty' {"execute": "guest-exec-status", "arguments": {"pid": 797}'

Qemu-guest-agent does not support hmp calls

The BLACKLIST_RPC parameter in the / etc/sysconfig/qemu-ga content in the virtual machine can configure which instructions are prohibited

The official reference document # qemu https://qemu.weilnetz.de/doc/qemu-doc.html# qmp https://qemu.weilnetz.de/doc/qemu-qmp-ref.html# qemu-guest-agent https://qemu.weilnetz.de/doc/qemu-ga-ref.html" how to interact with qemu virtual machines based on QMP "ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report