From 19c38cb38fc778ab94154e38435402aac3bf53d4 Mon Sep 17 00:00:00 2001 From: Simon de Vlieger Date: Mon, 30 Jun 2025 11:11:04 +0200 Subject: [PATCH] stages/ovf: allow setting mac address for vbox The MAC address will need to be the same as the one being used by the Vagrant stage *if* it's being used. This leaves it up to images to generate the correct MAC address and set it in both places. When the OVF stage is used separately it's still fine to use a random MAC address. Signed-off-by: Simon de Vlieger --- stages/org.osbuild.ovf | 13 +++++++++++-- stages/org.osbuild.ovf.meta.json | 3 +++ stages/test/test_ovf.py | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/stages/org.osbuild.ovf b/stages/org.osbuild.ovf index 0b5d18e6..e0bcb830 100755 --- a/stages/org.osbuild.ovf +++ b/stages/org.osbuild.ovf @@ -127,12 +127,21 @@ OVF_TEMPLATE = """ """ -def vbox_mac_address(): +def vbox_mac_address(options): + # If we have an explicit mac address we use that. + mac_address = options.get("virtualbox", {}).get("mac_address") + + if mac_address: + return mac_address + + # Otherwise we generate one according to how VirtualBox usually does this: # https://github.com/mirror/vbox/blob/b9657cd5351cf17432b664009cc25bb480dc64c1/src/VBox/Main/src-server/HostImpl.cpp#L3267 # VirtualBox-6.1.12 src/VBox/NetworkServices/Dhcpd/Config.cpp line 276 mac_address = "080027" + for _ in range(0, 3): mac_address += "".join(random.sample("0123456789abcdef", 2)) + return mac_address @@ -158,7 +167,7 @@ def write_template(vmdk, options): vbox_machine_uuid=str(uuid.uuid4()), vbox_disk_uuid=str(uuid.uuid4()), vbox_os_type=options.get("virtualbox", {}).get("os_type", "OtherLinux_64"), - vbox_mac_address=vbox_mac_address(), + vbox_mac_address=vbox_mac_address(options), image_name=basename, ) diff --git a/stages/org.osbuild.ovf.meta.json b/stages/org.osbuild.ovf.meta.json index 71be3916..6799a06f 100644 --- a/stages/org.osbuild.ovf.meta.json +++ b/stages/org.osbuild.ovf.meta.json @@ -38,6 +38,9 @@ "os_type": { "type": "string", "default": "OtherLinux_64" + }, + "mac_address": { + "type": "string" } } } diff --git a/stages/test/test_ovf.py b/stages/test/test_ovf.py index 8e372548..a36f37d2 100644 --- a/stages/test/test_ovf.py +++ b/stages/test/test_ovf.py @@ -43,6 +43,7 @@ def test_schema_validation_ovf(stage_schema, test_data, expected_err): # Replacements ({"vmware": {"os_type": "my-vmware-os-type"}}, ["my-vmware-os-type"],), ({"virtualbox": {"os_type": "my-vbox-os-type"}}, ["my-vbox-os-type"],), + ({"virtualbox": {"mac_address": "my-mac-address"}}, ["my-mac-address"],), ]) def test_ovf_default_template(tmp_path, stage_module, test_opts, expected_substrings): faked_vmdk_path = tmp_path / "some-image.vmdk"