[英]Controlling VirtualBox from commandline with python
我们正在使用python virtualbox API来控制virtualbox。 为此,我们使用了“ pyvb”包(如python API文档中所述)。
al=pyvb.vb.VB()
m=pyvb.vm.vbVM()
al.startVM(m)
我们已经使用python解释器执行了。 没有显示错误,但virtualbox没有启动。 您能否告诉我们什么地方可能出了问题(已导入所有必要的模块和软件包)
我发现可以使用以下功能来查找虚拟机是否正在运行,将虚拟机还原到特定快照并按名称启动虚拟机。
from subprocess import Popen, PIPE
def running_vms():
"""
Return list of running vms
"""
f = Popen(r'vboxmanage --nologo list runningvms', stdout=PIPE).stdout
data = [ eachLine.strip() for eachLine in f ]
return data
def restore_vm(name='', snapshot=''):
"""
Restore VM to specific snapshot uuid
name = VM Name
snapshot = uuid of snapshot (uuid can be found in the xml file of your machines folder)
"""
command = r'vboxmanage --nologo snapshot %s restore %s' % (name,snapshot)
f = Popen(command, stdout=PIPE).stdout
data = [ eachLine.strip() for eachLine in f ]
return data
def launch_vm(name=''):
"""
Launch VM
name = VM Name
"""
command = r'vboxmanage --nologo startvm %s ' % name
f = Popen(command, stdout=PIPE).stdout
data = [ eachLine.strip() for eachLine in f ]
return data
引用的代码似乎没有指定要运行的VM。 您不应该先执行getVM
调用,然后在startVM
调用中使用生成的VM实例吗? 例如:
al=pyvb.vb.VB()
m=al.getVM(guid_of_vm)
al.startVM(m)
...将启动使用给定GUID标识的VM(所有VirtualBox VM在创建时都分配了GUID)。 您可以从VM的XML文件中获取GUID。 如果您需要在运行时发现虚拟机,可以使用方便的listVMS
调用:
al=pyvb.vb.VB()
l=al.listVMS()
# choose a VM from the list, assign to 'm'
al.startVM(m)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.