静态网站怎么容易做,阿里巴巴 商城网站怎么做,清华紫光做网站,官方网站优化价格简介 
之前介绍过qemu传输文件#xff0c;使用的挂载 / samba方式 #xff1a;Qemu和宿主机不使用外网进行文件传输。 
这是一种方式#xff0c;这里还有另一种方式#xff1a;使用Qemu-Guest-Agent#xff0c;后面简称qga。 
官网介绍#xff1a;https://www.qemu.org/d… 
简介 
之前介绍过qemu传输文件使用的挂载 / samba方式 Qemu和宿主机不使用外网进行文件传输。 
这是一种方式这里还有另一种方式使用Qemu-Guest-Agent后面简称qga。 
官网介绍https://www.qemu.org/docs/master/interop/qemu-ga.html 安装 
这里有一篇参考文章会比我下面介绍的相对复杂一点但也可以完成操作。 
参考文章https://www.easystack.cn/doc/ComputingService/6.1.1/zh-cn/FAQs/DeployQGA.html 
一、Linux的Qemu机器 
直接install命令即可查看上面的参考链接即可。 
二、Windows的Qemu机器 
Windows的Qemu机器需要安装好virtio-serial driver下面详细介绍一下 
首先下载该文件1积分这个文件我已经测试了Windows7-10、Centos7-8、Ubuntu16-24可用Qemu-Guest-Agnet整理包 
这是一个Windows整理后的qga包包含amd64.zip和qemu-ga.zip两个文件请都解压出来。 
1、amd64首先安装证书管理员运行cmd命令pnputil -I -a vioser.inf 
2、qemu-ga将qemu-ga目录放到C:\Program Files下管理员运行cmd命令qemu-ga -s install 
3、然后检查【服务】是否包含以下两个服务如果存在则说明安装成功将两个内容全部设置为自动启动 QEMU Guest Agent QEMU Guest Agent VSS Provider 
4、验证 转为qcow2上传到linux服务器运行以下指令假设该qcow2文件名字为Windows10.qcow2 运行以下内容 
qemu-system-x86_64 -name test -hda Windows10.qcow2 -m 4096M -smp cpus2,sockets1 -enable-kvm -netdev user,idnet0 -device e1000,netdevnet0 -chardev socket,path/tmp/test.sock,serveron,waitoff,idqga0 -device virtio-serial -device virtserialport,chardevqga0,nameorg.qemu.guest_agent.0 -machine usbon -device usb-tablet 
成功开机后有可能QEMU Guest Agent服务并没有成功运行那么此时到【服务】中手动开启如果能手动开启成功则没问题下次就能自动开启了。 
此时使用命令socat - unix-connect:/tmp/test.sock连接上以后输入一些命令测试一下如{execute:guest-info}。 使用 
前面安装没问题以后就可以用代码去连接socket文件并执行内容了。这里写几个demo 
1、获取系统信息 
import asyncio
import jsonclass QEMUGuestAgent(object):endpoint: strdef __init__(self, endpoint):self.endpoint  endpointasync def execute(self, command, timeout2.0):try:reader, writer  await asyncio.open_unix_connection(self.endpoint)writer.write(json.dumps(command).encode())response  await asyncio.wait_for(reader.readline(), timeout)writer.close()return json.loads(response)except ConnectionAbortedError:print(ConnectionAbortedError: QEMU Agent功能未开启)return {}except asyncio.TimeoutError:print(TimeoutError: Agent 未安装或运行)return {}async def get_osinfo(self):查询虚拟机操作系统版本:param command::return:command  {execute: guest-get-osinfo}return await self.execute(command)async def main():if __name__  __main__:qga  QEMUGuestAgent(/tmp/test.sock)rv  await qga.get_osinfo()print(rv)2、上传文件 
import asyncio
import jsonclass QEMUGuestAgent(object):endpoint: strdef __init__(self, endpoint):self.endpoint  endpointasync def execute(self, command, timeout2.0):try:reader, writer  await asyncio.open_unix_connection(self.endpoint)writer.write(json.dumps(command).encode())response  await asyncio.wait_for(reader.readline(), timeout)writer.close()return json.loads(response)except ConnectionAbortedError:print(ConnectionAbortedError: QEMU Agent功能未开启)return {}except asyncio.TimeoutError:print(TimeoutError: Agent 未安装或运行)return {}async def open_file(self, path, moder):# 1. 获取文件句柄command  {execute: guest-file-open,arguments: {path: path,mode: mode}}rv  await self.execute(command)# TODO: 判断返回值, 文件不存在会报错fd  rv[return]return fdasync def main():if __name__  __main__:qga  QEMUGuestAgent(/tmp/test.sock)fd  await qga.open_file(C:\\cccc\\1111.txt, modew) 
其他的比如下载文件、在qemu内执行指令等功能参考https://www.qemu.org/docs/master/interop/qemu-ga-ref.html。