qemu-guest-agent windows下的监控开发

       windows下的qemu-guest-agent(简称qga)的名字叫做qemu-guest-agent-win32,目前最新版的版本号是qemu-guest-agent-win32-0.12.1.2-2.355.el6_4.9.x86_64,下载下来是一个rpm包,在centos6上安装后就会出现qemu-ga.exe和一个readme。可以看出windows下的qga是根据el6(rhel6/centos6)上的qemu-guest-agent编译出来的,而qemu-guest-agent的源码是包含在qemu的源码中的,所以我们只需要下载qemu-kvm.el6版本的源码就可以。qemu-kvm-0.12.1.2-2.355.el6_4.9.x86_64是centos6.4上的qemu-kvm版本,为什么不用centos6.5的qemu-kvm呢,那是因为6.4上的src.rpm包里的specfile是直接可以打包出qemu-guest-agent-win32.rpm的,而6.5的src.rpm包里的specfile是没有这一项的,而且6.5对windows版本仅更新了settime和gettime这两个功能,所以如果只是想快速添加自己的功能,可以直接下centos6.4的qemu-kvm。

1、下载源码
        从centos6.4的源下载qemu-kvm-0.12.1.2-2.355.0.1.el6_4.9.x86_64.src.rpm,安装src.rpm包,源码和补丁解压到$HOME/rpmbuild/SOURCES,此时的源码还不是真正的源码,因为qga的代码是作为补丁在patch文件中的,到SPECS文件夹中rpmbuild –bp qemu-kvm.spec将补丁和tar包的代码全部解压到BUILD文件夹中,现在才是真正的源代码,可以找到qga文件夹,其中就是qga的代码了,有win32的就是windows下的代码。
2、创建打包环境
        为了更方便的修改代码和打包,将刚才解压到BUILD中的代码打包成tar.gz包,更换掉SOURCES中的包,删除SOURCES中的所有patch文件,修改SPECS中specfile将文件中的Patch*和%patch* -p格式的全部删掉或者注释掉。现在打包就可以直接打包修改过的源代码了。
3、文件介绍
        与qga-win32有关的文件有qapi-schema-guest.json,qemu-ga.c,qga/*-win32。qagi-schema-guest.json中定义功能名称,输入输出数据类型,新加的功能函数的具体实现如果是加在commands-win32.c中时则也要在commands-posix.c中加入,linux和windows两边功能要一致不然编译会报错,即使linux函数没有实现具体功能,commands.c中是两边通用功能。commands-win32.c中就是windows下具体的功能函数,命名为qmp_*。qemu-ga.c中有windows下服务的创建,有默认virtio路径,可以在这里修改。

4、代码的修改
        举例:现在我们加入windows下的文件创建功能。
qapi-schema-guest.json中添加功能名称:

  1. ##  
  2. # @guest-file-create:  
  3. #  
  4. # create a file in the guest and retrieve a sign for it  
  5. #  
  6. # @filepath: Full path to the file in the guest to create.  
  7. #  
  8. # Returns: 0 on success,-1 on failure.  
  9. #  
  10. # Since: 0.0.1  
  11. ##  
  12. 'command''guest-file-create',  
  13.   'data':    { 'path''str' },  
  14.   'returns''int' }  
commands-win32.c中添加功能函数:
  1. int64_t qmp_guest_file_create(const char *path, Error **err)  
  2. {  
  3.     const char *mode;  
  4.     mode = "wb";  
  5.     FILE *fh;  
  6.   
  7.     slog("guest-file-create called, filepath: %s", path);  
  8.     fh = fopen(path, mode);  
  9.     if (NULL == fh) {  
  10.         slog("error on open %s", path);  
  11.         error_set(err, QERR_QGA_COMMAND_FAILED, "fopen() failed");  
  12.         return -1;  
  13.     }  
  14.     fclose(fh);  
  15.     return 0;  
  16.   
  17. }  
commands-posix.c中添加对应函数,可利用现有error不添加功能。
  1. int64_t qmp_guest_file_create(const char *path, Error **err)  
  2. {  
  3.     error_set(err, QERR_UNSUPPORTED);  
  4.     return -1;  

5、功能的验证
修改代码后打包替换掉SOURCES中的tar.gz包,会生成新的rpm包或者直接在BUILD中qemu-kvm-win32-build下拷贝qemu-ga.exe。

利用libvirt创建虚拟机win7,在XML文件device中加入

  1. <channel type='unix'>  
  2.    <source mode='bind' path='/var/lib/libvirt/qemu/test.org.qemu.ga.0'/>  
  3.    <target type='virtio' name='org.qemu.ga.0'/>  
  4.  </channel>  
path为宿主机上的unix domain socket文件路径,会在虚拟机创建时生成,name为虚拟机内部生成的serial设备名字。qemu-guest-agent默认的名字是org.qemu.guest_agent.0,由于我修改的vdsm上该名字已经被占用,所以自己新加一个serial。
将qemu-ga.exe放入win7中,下载需要的dll放在同一个路径下,使用cmd运行qemu-ga,如果既没有修改服务,也没有使用默认设备,可以使用-p来指定自己的serial设备名:
  1. qemu-ga.exe –p \\.\Global\org.qemu.ga.0  
在宿主机端使用socat来快速连接:
  1. socat \var\lib\libvirt\qemu\test.org.qemu.ga.0 –  
连上后就可以收发命令了
  1. {“execute”:”guest-file-create”,”arguments”:{“path”:”c:\\test.txt”}}  
如果成功会收到return 0的返回,并且c:\test.txt创建成功。qemu-ga由于是c语言写的,linux下实现的很多功能可以直接拷贝到windows下使用,修改起来还是很方便的。

6、已有功能

目前qga最新版本为1.5.50linux已经实现下面的所有功能,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。

7、参考

exe(rpm)下载路径:http://linuxsoft.cern.ch/cern/slc64/x86_64/yum/updates/repoview/qemu-guest-agent-win32.html

源码下载路径:http://nl.mirror.eurid.eu/centos-vault/6.4/updates/Source/SPackages/

参考文献1:http://wiki.qemu.org/Features/QAPI/GuestAgent
参考文献2:http://blog.csdn.net/hbsong75/article/details/9465683
参考文献3:http://www.cnblogs.com/biangbiang/p/3222458.html
参考:http://blog.csdn.net/hbsong75/article/details/9465683
参考:http://www.csdn123.com/html/itweb/20130729/27101_27081_27076.htm

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值