From a09fd62b110d6407b63743cd6566b8eaca236145 Mon Sep 17 00:00:00 2001 From: Ana Maria Martinez Gomez Date: Mon, 2 Sep 2024 11:26:57 +0200 Subject: [PATCH 1/3] [vbox-export-snapshots] Change appliance name We are using `exported_vm_name` to name the exported `.ova` file but keeping the original VM name (`FLARE-VM.testing`) for the appliance name (the VM name when importing it). Change the VM appliance name to match the `.ova` file name. --- virtualbox/vbox-export-snapshots.py | 1 + 1 file changed, 1 insertion(+) diff --git a/virtualbox/vbox-export-snapshots.py b/virtualbox/vbox-export-snapshots.py index 1abd8cc..140958d 100755 --- a/virtualbox/vbox-export-snapshots.py +++ b/virtualbox/vbox-export-snapshots.py @@ -71,6 +71,7 @@ def change_network_adapters(vm, max_adapters): filename = os.path.join(export_directory, f"{exported_vm_name}.ova") appliance = vbox.create_appliance() sys_description = vm.export_to(appliance, exported_vm_name) + sys_description.set_final_value(DescType.name, exported_vm_name) sys_description.set_final_value(DescType.description, description) progress = appliance.write("ovf-1.0", [], filename) print(f"Exporting {filename} (this will take some time, go for an 🍦!)") From dcbedd34b45ce923860bdef6d7d6d3a6e9327b89 Mon Sep 17 00:00:00 2001 From: Ana Maria Martinez Gomez Date: Mon, 2 Sep 2024 18:14:04 +0200 Subject: [PATCH 2/3] [vbox-export-snapshots] Create manifest Use the `create_manifest` export option that writes the optional manifest file (`.mf`) which is used for integrity checks prior import as done by default in the manual process. --- virtualbox/vbox-export-snapshots.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/virtualbox/vbox-export-snapshots.py b/virtualbox/vbox-export-snapshots.py index 140958d..35c607d 100755 --- a/virtualbox/vbox-export-snapshots.py +++ b/virtualbox/vbox-export-snapshots.py @@ -10,6 +10,7 @@ import virtualbox from virtualbox.library import VirtualSystemDescriptionType as DescType from virtualbox.library import NetworkAttachmentType as NetType +from virtualbox.library import ExportOptions as ExportOps from datetime import datetime # Base name of the exported VMs @@ -73,7 +74,7 @@ def change_network_adapters(vm, max_adapters): sys_description = vm.export_to(appliance, exported_vm_name) sys_description.set_final_value(DescType.name, exported_vm_name) sys_description.set_final_value(DescType.description, description) - progress = appliance.write("ovf-1.0", [], filename) + progress = appliance.write("ovf-1.0", [ExportOps.create_manifest], filename) print(f"Exporting {filename} (this will take some time, go for an 🍦!)") progress.wait_for_completion(-1) From c2dcf89f60d2605fd49dea5e9ece71c9933e228e Mon Sep 17 00:00:00 2001 From: Ana Maria Martinez Gomez Date: Tue, 3 Sep 2024 12:23:54 +0200 Subject: [PATCH 3/3] [vbox-export-snapshots] Refactor code Refactor code for clarify including the session creation. --- virtualbox/vbox-export-snapshots.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/virtualbox/vbox-export-snapshots.py b/virtualbox/vbox-export-snapshots.py index 35c607d..8defebe 100755 --- a/virtualbox/vbox-export-snapshots.py +++ b/virtualbox/vbox-export-snapshots.py @@ -42,6 +42,7 @@ def change_network_adapters(vm, max_adapters): for i in range(max_adapters): adapter = vm.get_network_adapter(i) adapter.attachment_type = NetType.host_only + vm.save_settings() if __name__ == "__main__": @@ -49,20 +50,17 @@ def change_network_adapters(vm, max_adapters): vbox = virtualbox.VirtualBox() vm = vbox.find_machine(VM_NAME) - session = vm.create_session() - vm = session.machine - max_adapters = vbox.system_properties.get_max_network_adapters(vm.chipset_type) for snapshot_name, extension, description in SNAPSHOTS: try: # Restore snapshot - snapshot = vm.find_snapshot(snapshot_name) - progress = vm.restore_snapshot(snapshot) + session = vm.create_session() + snapshot = session.machine.find_snapshot(snapshot_name) + progress = session.machine.restore_snapshot(snapshot) progress.wait_for_completion(-1) - - change_network_adapters(vm, max_adapters) - + change_network_adapters(session.machine, max_adapters) + session.unlock_machine() print(f"Restored '{snapshot_name}' and changed its adapter(s) to host-only") # Export .ova @@ -86,6 +84,4 @@ def change_network_adapters(vm, max_adapters): except Exception as e: print(f"ERROR exporting {snapshot_name}: {e}") - next - session.unlock_machine()