db4*_*b42 11
[注意:自动化不使用任何虚拟化API.来自我的博文.]
步骤1:
默认情况下,qemu使用SDL显示VGA输出.所以,第一步是通过stdio与qemu进行这种交互.Qemu为此提供了一个选项.
来自qemu docs:
-nographic 通常,QEMU使用SDL显示VGA输出.使用此选项,您可以完全禁用图形输出,以便QEMU是一个简单的命令行应用程序.模拟的串行端口在控制台上重定向.因此,您仍然可以使用QEMU通过串行控制台调试Linux内核.
所以,你所要做的就是用-nographic调用qemu.
qemu -nographic -hda guest.disk
第2步:
现在您可以通过命令行与guest虚拟机(或qemu进程)进行交互,您必须自动执行此交互.在python中执行此操作的显而易见的方法是使用子进程模块启动qemu进程(使用-nographic),然后与该进程进行通信.但令我惊讶的是,这对我来说并不合适.所以,我寻找其他方式.
后来,我发现这种工作最棒的工具是Expect.它是用tcl编写的交互式应用程序的自动化工具.
本指南应该可以帮助您开始使用Expect.以下是使用Expect使用qemu运行guest虚拟机的脚本.
#!/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”
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6297 次 |
| 最近记录: |