qemu 访客自动化

发布于 2024-09-07 13:25:28 字数 99 浏览 4 评论 0原文

我找不到任何文档说明存在可用于在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

眼中杀气 2024-09-14 13:25:29

据我所知,与客人沟通的唯一方式是通过网桥。

As far as I know, the only way to communicate to the guest is through the network bridge.

本宫微胖 2024-09-14 13:25:29

PyQemu 理论上可以做到这一点。我过去使用过它,尽管它现在看起来像是一个过时的项目。它提供了一个Python代理(相当于VMWare来宾工具)在来宾上运行,通过串行端口与主机通信。您可以获取在虚拟机上下文中运行的 python 模块的代理,并且与它们的任何通信都通过串行端口进行编组。在以下示例中,AutoIt 用于自动化记事本:

machine = PyQemu.GetProxy("win2k")

# Wrap the machine object in another proxy representing the 'os'
# module running inside the VM.
os = PyQemu.vm.Module(machine,"os")

# NOTE: This is running on the VM!
os.system("notepad")

# Get an IDispatch object representing the autoit ActiveX control
autoit = PyQemu.vm.Dispatch(machine,"AutoItX3.Control")

# See if a window is active on the VM
state = autoit.WinActive("Untitled -")

警告:由于使用串行端口,它的速度远远不够快(无论串行速度设置如何),因此最好通过其他方式传输任何批量数据,例如虚拟 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:

machine = PyQemu.GetProxy("win2k")

# Wrap the machine object in another proxy representing the 'os'
# module running inside the VM.
os = PyQemu.vm.Module(machine,"os")

# NOTE: This is running on the VM!
os.system("notepad")

# Get an IDispatch object representing the autoit ActiveX control
autoit = PyQemu.vm.Dispatch(machine,"AutoItX3.Control")

# See if a window is active on the VM
state = autoit.WinActive("Untitled -")

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.

失退 2024-09-14 13:25:29

您可以创建从访客到主机的反向 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.

謌踐踏愛綪 2024-09-14 13:25:29

如果您在来宾中运行 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.

小草泠泠 2024-09-14 13:25:28

[注意:无需使用任何虚拟化 API 即可实现自动化。来自我的博客文章。]

步骤1:

默认情况下,QEMU 使用SDL 显示VGA 输出。因此,第一步是通过标准 I/O 与 QEMU 进行交互。 QEMU 为此提供了一个选项。

来自 QEMU 文档:

-ngraphic 通常,QEMU 使用 SDL 来显示 VGA 输出。使用此选项,您可以完全禁用图形输出,以便 QEMU
一个简单的命令行应用程序。仿真的串口是
在控制台上重定向。因此,仍然可以使用QEMU来调试
带有串行控制台的 Linux 内核。

因此,您所要做的就是使用 -ngraphic 调用 QEMU。

qemu -nographic -hda guest.disk

第 2 步:

现在您可以通过命令行与来宾(或 QEMU 进程)进行交互,您必须自动执行此交互。在 python 中执行此操作的明显方法是使用 子进程模块,然后与该进程通信。但令我惊讶的是,这对我来说不起作用。所以,我寻找其他方法。

后来,我发现这种工作最棒的工具是 Expect。它是用 TCL 编写的交互式应用程序的自动化工具。

本指南应该可以帮助您开始使用 Expect。以下是使用 Expect 运行 QEMU 来宾的脚本。

#!/usr/bin/expect -f

#starts guest vm, run benchmarks, poweroff
set timeout -1

#Assign a variable to the log file
set log     [lindex $argv 0]

#Start the guest VM
spawn qemu -nographic -hda guest.disk

#Login process
expect "login: "
#Enter username
send "user\r"

#Enter Password
expect "Password: "
send "user\r"

#Do whatever you want to do with in the guest VM. ( Run a process and write result to log )

#poweroff the Guest VM
expect "# "
send "shutdown -h now\r"

[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:

-nographic Normally, QEMU uses SDL to display the VGA output. With this option, you can totally disable graphical output so that QEMU is
a simple command line application. The emulated serial port is
redirected on the console. Therefore, you can still use QEMU to debug
a Linux kernel with a serial console.

So, all you have to do is invoke QEMU with -nographic.

qemu -nographic -hda guest.disk

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.

#!/usr/bin/expect -f

#starts guest vm, run benchmarks, poweroff
set timeout -1

#Assign a variable to the log file
set log     [lindex $argv 0]

#Start the guest VM
spawn qemu -nographic -hda guest.disk

#Login process
expect "login: "
#Enter username
send "user\r"

#Enter Password
expect "Password: "
send "user\r"

#Do whatever you want to do with in the guest VM. ( Run a process and write result to log )

#poweroff the Guest VM
expect "# "
send "shutdown -h now\r"
Oo萌小芽oO 2024-09-14 13:25:28

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.

难如初 2024-09-14 13:25:28

我使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文