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 <supakeen@redhat.com>
This commit is contained in:
Simon de Vlieger 2025-06-30 11:11:04 +02:00 committed by Simon de Vlieger
parent 1cb0f26b09
commit 19c38cb38f
3 changed files with 15 additions and 2 deletions

View file

@ -127,12 +127,21 @@ OVF_TEMPLATE = """<?xml version="1.0"?>
"""
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,
)

View file

@ -38,6 +38,9 @@
"os_type": {
"type": "string",
"default": "OtherLinux_64"
},
"mac_address": {
"type": "string"
}
}
}

View file

@ -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"