qemu-guest-agent简介
经常使用vmWare的同学都知道有vmware-tools这个工具,这个安装在vm内部的工具,可以实现宿主机与虚拟机的通讯,大大增强了虚拟机的性能与功能,
如vmware现在的Unity mode下可以让应用程序无缝地与宿主机交互,更不用提直接复制粘帖文件及内容的小功能了。
对于KVM而言,其实也有一款这样的工具叫做 Qemu Guest Agent(以下称qga).
原理分析:
qga是一个运行在虚拟机内部的普通应用程序(可执行文件名称默认为qemu-ga,服务名称默认为qemu-guest-agent),其目的是实现一种宿主机和虚拟机进行交互的方式,这种方式不依赖于网络,而是依赖于virtio-serial(默认首选方式)或者isa-serial,而QEMU则提供了串口设备的模拟及数据交换的通道,最终呈现出来的是一个串口设备(虚拟机内部)和一个unix socket文件(宿主机上)。
qga通过读写串口设备与宿主机上的socket通道进行交互,宿主机上可以使用普通的unix socket读写方式对socket文件进行读写,最终实现与qga的交互,交互的协议与qmp(QEMU Monitor Protocol)相同(简单来说就是使用JSON格式进行数据交换),串口设备的速率通常都较低,所以比较适合小数据量的交换。
QEMU virtio串口设备模拟参数:

/usr/bin/kvm(QEMU) \
……\
-device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 \
-device isa-serial,chardev=charserial1,id=serial1 \
-chardev socket,id=charchannel0,path=/var/lib/libvirt/qemu/test.agent,server,nowait \
-device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,\
name=com.163.spice.0

通过上面的参数就可以在宿主机上生成一个unix socket文件,路径为:/var/lib/libvirt/qemu/test.agent,同时在虚拟机内部生成一个serial设备,名字为com.163.spice.0,设备路径为:/dev/vport0p1,映射出来的可读性比较好的路径为:/dev/virtio-ports/com.163.spice.0,可以在运行qga的时候通过-p参数指定读写这个设备。
也可以通的XML文件来配置这个串口设备:
<channel type='unix'>
<source mode='bind' path='/var/lib/libvirt/qemu/test.agent'/>
<target type='virtio' name='com.163.spice.0'/>
</channel>
注意: libvirt-qemu:kvm用户要有权限读写'/var/lib/libvirt/qemu/test.agent'
已有功能
目前qga最新版本为1.5.50,linux已经实现下面的所有功能,windows仅支持加*的那些功能:

Ø guest-sync-delimited*:宿主机发送一个int数字给qga,qga返回这个数字,并且在后续返回字符串响应中加入ascii码为0xff的字符,其作用是检查宿主机与qga通信的同步状态,主要用在宿主机上多客户端与qga通信的情况下客户端间切换过程的状态同步检查,比如有两个客户端A、B,qga发送给A的响应,由于A已经退出,目前B连接到qga的socket,所以这个响应可能被B收到,如果B连接到socket之后,立即发送该请求给qga,响应中加入了这个同步码就能区分是A的响应还是B的响应;在qga返回宿主机客户端发送的int数字之前,qga返回的所有响应都要忽略; Ø guest-sync*:与上面相同,只是不在响应中加入0xff字符; Ø guest-ping*:Ping the guest agent, a non-error return implies success; Ø guest-get-time*:获取虚拟机时间(返回值为相对于1970-01-01 in UTC,Time in nanoseconds.); Ø guest-set-time*:设置虚拟机时间(输入为相对于1970-01-01 in UTC,Time in nanoseconds.); Ø guest-info*:返回qga支持的所有命令; Ø guest-shutdown*:关闭虚拟机(支持halt、powerdown、reboot,默认动作为powerdown); Ø guest-file-open:打开虚拟机内的某个文件(返回文件句柄); Ø guest-file-close:关闭打开的虚拟机内的文件; Ø guest-file-read:根据文件句柄读取虚拟机内的文件内容(返回base64格式的文件内容); Ø guest-file-write:根据文件句柄写入文件内容到虚拟机内的文件; Ø guest-file-seek:Seek to a position in the file, as with fseek(), and return the current file position afterward. Also encapsulates ftell()'s functionality, just Set offset=0, whence=SEEK_CUR; Ø guest-file-flush:Write file changes bufferred in userspace to disk/kernel buffers; Ø guest-fsfreeze-status:Get guest fsfreeze state. error state indicates; Ø guest-fsfreeze-freeze:Sync and freeze all freezable, local guest filesystems; Ø guest-fsfreeze-thaw:Unfreeze all frozen guest filesystems; Ø guest-fstrim:Discard (or "trim") blocks which are not in use by the filesystem; Ø guest-suspend-disk*:Suspend guest to disk; Ø guest-suspend-ram*:Suspend guest to ram; Ø guest-suspend-hybrid:Save guest state to disk and suspend to ram(This command requires the pm-utils package to be installed in the guest.); Ø guest-network-get-interfaces:Get list of guest IP addresses, MAC addresses and netmasks; Ø guest-get-vcpus:Retrieve the list of the guest's logical processors; guest-set-vcpus:Attempt to reconfigure (currently: enable/disable) logical processors inside the guest。

功能扩展方式
qga功能扩展十分方便,只需要在qapi-schema.json文件中定义好功能名称、输入输出数据类型,然后在commands-posix.c里面增加对应的功能函数即可,下面的补丁即在qga中增加一个通过statvfs获取虚拟机磁盘空间信息的功能:

diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index e199738..2f42a2f 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -21,6 +21,7 @@
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
+#include <sys/statvfs.h>
#include <inttypes.h>
#include "qga/guest-agent-core.h"
#include "qga-qmp-commands.h"
@@ -1467,6 +1468,36 @@ void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
}
#endif +GuestFileSystemStatistics *qmp_guest_get_statvfs(const char *path, Error **errp)
+{
+ int ret;
+ GuestFileSystemStatistics *fs_stat;
+ struct statvfs *buf;
+ buf = g_malloc0(sizeof(struct statvfs));
+
+
+ ret = statvfs(path, buf);
+ if (ret < 0) {
+ error_setg_errno(errp, errno, "Failed to get statvfs");
+ return NULL;
+ }
+
+ fs_stat = g_malloc0(sizeof(GuestFileSystemStatistics));
+ fs_stat->f_bsize = buf->f_bsize;
+ fs_stat->f_frsize = buf->f_frsize;
+ fs_stat->f_blocks = buf->f_blocks;
+ fs_stat->f_bfree = buf->f_bfree;
+ fs_stat->f_bavail = buf->f_bavail;
+ fs_stat->f_files = buf->f_files;
+ fs_stat->f_ffree = buf->f_ffree;
+ fs_stat->f_favail = buf->f_favail;
+ fs_stat->f_fsid = buf->f_fsid;
+ fs_stat->f_flag = buf->f_flag;
+ fs_stat->f_namemax = buf->f_namemax;
+
+ return fs_stat;
+}
+
/* register init/cleanup routines for stateful command groups */
void ga_command_state_init(GAState *s, GACommandState *cs)
{
diff --git a/qga/qapi-schema.json b/qga/qapi-schema.json
index 7155b7a..a071c3f 100644
--- a/qga/qapi-schema.json
+++ b/qga/qapi-schema.json
@@ -638,3 +638,52 @@
{ 'command': 'guest-set-vcpus',
'data': {'vcpus': ['GuestLogicalProcessor'] },
'returns': 'int' }
+
+##
+# @GuestFileSystemStatistics:
+#
+# Information about guest file system statistics.
+#
+# @f_bsize: file system block size.
+#
+# @f_frsize: fragment size.
+#
+# @f_blocks: size of fs in f_frsize units.
+#
+# @f_bfree: free blocks.
+#
+# @f_bavail: free blocks for non-root.
+#
+# @f_files: inodes.
+#
+# @f_ffree: free inodes.
+#
+# @f_favail: free inodes for non-root.
+#
+# @f_fsid: file system id.
+#
+# @f_flag: mount flags
+#
+# @f_namemax: maximum filename length.
+#
+# Since 1.5.10(NetEase)
+##
+{ 'type': 'GuestFileSystemStatistics',
+ 'data': { 'f_bsize': 'int', 'f_frsize': 'int', 'f_blocks': 'int',
+ 'f_bfree': 'int', 'f_bavail': 'int', 'f_files': 'int',
+ 'f_ffree': 'int', 'f_favail': 'int', 'f_fsid': 'int',
+ 'f_flag': 'int', 'f_namemax': 'int'} }
+
+##
+# @guest-get-statvfs:
+#
+# Get the information about guest file system statistics by statvfs.
+#
+# Returns: @GuestFileSystemStatistics.
+#
+# Since 1.5.10(NetEase)
+##
+{ 'command': 'guest-get-statvfs',
+ 'data': { 'path': 'str' },
+ 'returns': 'GuestFileSystemStatistics' }
+

中间复杂的类型定义代码,以及头文件包含关系处理都由一个python脚本在编译的时候动态生成出来,这对开发人员来说是非常方便的,开发人员在扩展功能的时候只需要关注输入、输出的数据类型,以及功能的函数内容即可。
写到这里,有经验的开发人员已经知道如何用qga开发监控平台,下面是网友的实现思路,供参考:
qemu-guest-agent简介的更多相关文章
- QEMU Guest Agent
QEMU Guest Agent It is a daemon program running inside the domain which is supposed to help manageme ...
- 利用Qemu Guest Agent (Qemu-ga) 实现 Openstack 监控平台
经常使用vmWare的同学都知道有vmware-tools这个工具,这个安装在vm内部的工具,可以实现宿主机与虚拟机的通讯,大大增强了虚拟机的性能与功能, 如vmware现在的Unity mode下可 ...
- java agent简介
java agent简介 主要就是两种,一种的方法是premain,一种是agentmain.这两种的区别是: premain是在jvm启动的时候类加载到虚拟机之前执行的 agentmain是可以在j ...
- Salesforce Live Agent 简介
Live Agent Salesforce 内置了即时聊天功能--Live Agent,可以用来作为客服工具. 本文简单介绍了使用 Live Agent 的步骤,主要包含: 启用 Live Agent ...
- Qemu事件处理机制简介
Qmeu 采用了基于事件驱动的架构,所有的事件都在一个事件循环(event loop)中被处理,系统中默认的事件循环是在main-loop.c 中的主循环(main loop).我们也可以使用 –ob ...
- 11gR2 Agent 简介
目的:本文会对oracle 11gR2 集群件(Grid Infrastructure,以下简称GI) 新特性 agent进行介绍,包括 agent的功能,常见的agent介绍,以及基本的诊断方法. ...
- KVM 介绍(7):使用 libvirt 做 QEMU/KVM 快照和 Nova 实例的快照 (Nova Instances Snapshot Libvirt)
学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I/O PCI/PCIe设备直接分 ...
- 使用QEMU创建虚拟机
下载安装: wget http://wiki.qemu-project.org/download/qemu-2.0.0.tar.bz2 tar xjvf qemu- ./configure --ena ...
- KVM(七)使用 libvirt 做 QEMU/KVM 快照和 Nova 实例的快照
本文将梳理 QEMU/KVM 快照相关的知识,以及在 OpenStack Nova 中使用 libvirt 来对 QEMU/KVM 虚机做快照的过程. 1. QEMU/KVM 快照 1.1 概念 QE ...
随机推荐
- bzoj 2878: [Noi2012]迷失游乐园【树上期望dp+基环树】
参考:https://blog.csdn.net/shiyukun1998/article/details/44684947 先看对于树的情况 设d[u]为点u向儿子走的期望长度和,du[u]为u点的 ...
- hexo简易脚本
!/bin/bash 检查是否为master分支.目录是否正确 function git-branch-name { git symbolic-ref --short -q HEAD } functi ...
- 【Linux】小米路由开启SSH访问权限
一.验证小米路由ROM是否为开发版 1. 登录小米路由Web管理页面,检查ROM版本是否为开发版(若为开发版直接跳至第二步,若为稳定版继续本步骤). 2. 进入小米路由器官网(http://www1 ...
- 图片预览------photoswipe 使用
photoswipe 使用 预览图片后,需要点击关闭按钮才能关闭,点击图片事件效果是放大图片,和微信的效果不一致,最后改用微信预览图片的接口了,但是例子可以用,记录一下!! http://www.cn ...
- 分享几篇有关react-native的博客
http://segmentfault.com/a/1190000002646155 http://facebook.github.io/react-native/docs/native-module ...
- 列表框、分组列表框、标签(label)、分组框(fieldset)、框架(frameset)
列表框(select) 下拉列表,用户可以从一些可选项中选择. 示例:简单的下拉列表 <select name="country"> <option value= ...
- 漫谈未来的HDFS
前面我们提到的HDFS,了解了HDFS的特性和架构.HDFS能够存储TB甚至PB规模的数据是有前提的,首先数据要以大文件为主,其次NameNode的内存要足够大.对HDFS有所了解的同学肯定都知道,N ...
- Python之绘图和可视化
Python之绘图和可视化 1. 启用matplotlib 最常用的Pylab模式的IPython(IPython --pylab) 2. matplotlib的图像都位于Figure对象中. 可以使 ...
- Selenium IDE的第一个测试用例——路漫长。。。
一周时间过去了,断断续续学习selenium也有几个小时了:今天细想一下学习效率不高的原因在哪,总结出以下几点: 1.求“进”心切——总想一步到位,搭建好环境,开始动手写用例. 2.学习深度不够——同 ...
- NoSQL与关系数据库
关系型数据库:完全支持关系代数理论作为基础:有较大的数据规模:固定的数据库模式:查询效率快:强一致性:数据完整性较易实现:扩展性一般:可用性好. NoSQL:部分支持关系代数理论作为基础:有超大数据规 ...