0

我们使用 python virtualbox API 来控制 virtualbox。为此,我们正在使用“pyvb”包(如 python API 文档中给出的)。

al=pyvb.vb.VB()
m=pyvb.vm.vbVM()
al.startVM(m)

我们已经使用 python 解释器执行了。没有显示错误,但 virtualbox 没有启动。您能否告诉我们可能出了什么问题(所有必要的模块和包都已导入)

4

2 回答 2

3

我发现我可以使用以下函数来查找 VM 是否正在运行,将 VM 还原到特定快照,并按名称启动 VM。

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
于 2010-02-22T19:05:25.053 回答
0

引用的代码似乎没有指定要运行的 VM。你不应该getVM打电话,然后在你的电话中使用生成的虚拟机实例startVM吗?例如:

al=pyvb.vb.VB()
m=al.getVM(guid_of_vm)
al.startVM(m)

...将启动使用给定 GUID 标识的 VM(所有 VirtualBox VM 在创建时都分配了一个 GUID)。您可以从 VM 的 XML 文件中获取 GUID。如果您需要在运行时发现 VM,可以使用方便的listVMS调用:

al=pyvb.vb.VB()
l=al.listVMS()
# choose a VM from the list, assign to 'm'
al.startVM(m)
于 2010-02-22T18:22:07.293 回答