如何使用 Python 将 VirtualBox 机器导出到 OVA 设备?

How can I export a VirtualBox machine to OVA appliance using Python?

我需要创建(导出)虚拟机 (VirtualBox) 到 OVA/OVF 设备。

我尝试像这样使用 IMachine.export_to() 方法(通过 pyvbox 包装器):

import virtualbox
from virtualbox.library import ExportOptions


vbox = virtualbox.VirtualBox()
vm = vbox.find_machine(VM_NAME)

appliance = vbox.create_appliance()
p = appliance.write('ovf-2.0',
                    [ExportOptions.create_manifest],
                    '~/tmp/test5.ovf')
desc = slredmine.export_to(appliance, '~/tmp/test5.ovf')

上面的代码不符合我的要求:没有创建 ova/ovf。

更新

指令顺序错误。请参阅下面写的我的回答。

根据 pyvbox 文档,它只能导出为 OVF 格式,但这并不重要,具体取决于您要使用它做什么。

文档引用:

As with importing, first call IVirtualBox.create_appliance() to create an empty IAppliance object.

For each machine you would like to export, call IMachine.export_to() with the IAppliance object you just created. Each such call creates one instance of IVirtualSystemDescription inside the appliance.

If desired, call IVirtualSystemDescription.set_final_values() for each virtual system (machine) to override the suggestions made by the IMachine.export_to() routine.

Finally, call write() with a path specification to have the OVF file written.

如果遇到困难,请随时分享您的代码。

已解决

import virtualbox
from virtualbox.library import ExportOptions

VM_NAME = 'foovmname'    

vbox = virtualbox.VirtualBox()
vm = vbox.find_machine(VM_NAME)

appliance = vbox.create_appliance()
desc = slredmine.export_to(appliance, VM_NAME)
p = appliance.write('ovf-2.0',
                    [ExportOptions.create_manifest],
                    '~/tmp/test5.ovf')