qemu 访客自动化
我找不到任何文档说明存在可用于在 qemu guest 内部实现自动化操作的 API。
例如,我想从主机启动来宾计算机内部的进程。 Libvirt 似乎不包含此类功能。
I've not been able to find any documentation stating the existence of an API that can be used to automate things inside of a qemu guest.
For example, I would like to launch a process inside of the guest machine from the host machine. Libvirt does not appear to contain such functionality.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
据我所知,与客人沟通的唯一方式是通过网桥。
As far as I know, the only way to communicate to the guest is through the network bridge.
PyQemu 理论上可以做到这一点。我过去使用过它,尽管它现在看起来像是一个过时的项目。它提供了一个Python代理(相当于VMWare来宾工具)在来宾上运行,通过串行端口与主机通信。您可以获取在虚拟机上下文中运行的 python 模块的代理,并且与它们的任何通信都通过串行端口进行编组。在以下示例中,AutoIt 用于自动化记事本:
警告:由于使用串行端口,它的速度远远不够快(无论串行速度设置如何),因此最好通过其他方式传输任何批量数据,例如虚拟 FAT 磁盘图像。
PyQemu can theoretically do this. I've used it in the past, although it looks like a stale project now. It provides a python agent (the equivalent of VMWare guest tools) to run on the guest, communicating with the host via serial port. You can get proxies to python modules running in the context of the VM, and any communication with them is marshaled over the serial port. In the following example, AutoIt is being used to automate Notepad:
Caveat: Due to using the serial port it is far from quick (regardless of serial speed settings), so perhaps best to transfer any bulk data by other means, e.g. Virtual FAT disk image.
您可以创建从访客到主机的反向 ssh 隧道,这会将每个请求重定向到特定端口上的主机到访客。这种方式将帮助您从主机控制访客。
You can create a reverse ssh tunnel from guest to host, which will redirect each request to host on specific port to guest. This way will help you to control guest from host.
如果您在来宾中运行 Linux,难道不能使用 ssh/screen 在来宾上启动远程进程吗?
或者,我见过人们编写 python 包装器,使用 popen() 来获取 stdin/stdout 并使用它们来自动执行一些命令(即,当您看到登录提示时,将登录名发送到 QEMU 的 stdin 。
If you're running Linux in the guest, couldn't you just use ssh/screen to launch remote processes on the guest?
Alternatively, I have seen people write python wrappers that use
popen()
to grab stdin/stdout and use those to automate some commands (i.e. when you see the login prompt, send the login name to stdin of QEMU.[注意:无需使用任何虚拟化 API 即可实现自动化。来自我的博客文章。]
步骤1:
默认情况下,QEMU 使用SDL 显示VGA 输出。因此,第一步是通过标准 I/O 与 QEMU 进行交互。 QEMU 为此提供了一个选项。
来自 QEMU 文档:
因此,您所要做的就是使用
-ngraphic
调用 QEMU。第 2 步:
现在您可以通过命令行与来宾(或 QEMU 进程)进行交互,您必须自动执行此交互。在 python 中执行此操作的明显方法是使用 子进程模块,然后与该进程通信。但令我惊讶的是,这对我来说不起作用。所以,我寻找其他方法。
后来,我发现这种工作最棒的工具是 Expect。它是用 TCL 编写的交互式应用程序的自动化工具。
本指南应该可以帮助您开始使用 Expect。以下是使用 Expect 运行 QEMU 来宾的脚本。
[Note: Automation without using any virtualization API. From my blog post.]
Step 1:
By default, QEMU uses SDL to display the VGA output. So, the first step is make this interaction with QEMU through standard I/O. QEMU provides an option for this.
From QEMU docs:
So, all you have to do is invoke QEMU with
-nographic
.Step 2:
Now that you can interact with your guest (or QEMU process) through command line, you have to automate this interaction. The obvious way to do this in python is start the QEMU process (with
-nographic
) with subprocess module and then communicate with that process. But to my surprise, this just didn’t work out for me. So, I looked for some other way.Later, I found out that the most awesome tool for this kind of jobs is Expect. It is an automation tool for interactive applications written in TCL.
This guide should help you in getting started with Expect. Here is the script to run a guest with QEMU using Expect.
QEMU Monitor 可以使用其自己的控制台在有限范围内与来宾系统进行交互。这包括读取寄存器、控制鼠标/键盘以及获取屏幕转储。
有一个 QEMU 监控协议 (QMP),可让您将 JSON 命令传递到来宾系统并从来宾系统读取值。
The QEMU Monitor can interact with guest systems to a limited extent using it's own console. This includes reading registers, controlling the mouse/keyboard, and getting screen dumps.
There is a QEMU Monitor Protocol (QMP) that let's you pass JSON commands to and read values from the guest system.
我使用 python 和 pexpect 来通过串行控制台与生成的虚拟机进行交互。我通常通过这种方式自动化最多 128 个虚拟机的场景,速度相当快。我通常使用 virt-install 来实例化来宾,并使用 pexpect 来使用“virsh 控制台(域名)”来获取每个控制台的“句柄”,这样我就可以发送命令来配置网络、启动工具/实用程序/脚本、监控操作,就简单性而言,这相当不错,并且由于脚本只是发出 shell 命令,因此您不会接触到随版本不同而变化的 API,例如串行控制台将始终存在。有时我直接使用 qemu(最近我正在使用 libvirt 不支持的 QEMU,因为它太新了),在这种情况下,我会让来宾控制台使用 telnet 端口,这样我就可以“telnet localhost portnumber”来创建一个控制台连接而不是“virsh console(域名)”。无论哪种方式,带有 pexpect 模块的用于与虚拟机交互的 python 脚本都很棒。
I use python with pexpect to interact with spawned VMs using their serial consoles. I generally automate scenarios that have up to 128VMs this way, its reasonably swift. I generally use virt-install to instantiate guests, and use "virsh console (domainname)" using pexpect to get a "handle" to each console, so I can send commands to configure networking, startup tools/utilities/scripts, monitor operation, etc. Pretty sweet in terms of simplicity, and since the scripts are just issuing shell commands, you aren't exposed to APIs that change from version to version, e.g. the serial console will always be there. Sometimes I use qemu directly, (lately I am working with a QEMU that libvirt doesn't support since its too new), in that case I will have the guest console use a telnet port so I can "telnet localhost portnumber" to make a console connection instead of "virsh console (domainname)". Either way, python scripts with the pexpect module for interacting with VMs is great.