From 5b1af7ba97da0872df666a8da9a2329084223bd3 Mon Sep 17 00:00:00 2001 From: KTokarze Date: Thu, 21 May 2026 01:35:40 -0700 Subject: [PATCH] feat: Add possibility of assigning a MAC address to VM's VF Signed-off-by: Kacper Tokarzewski --- mfd_kvm/hypervisor.py | 9 ++++++++- mfd_kvm/vf_template_with_mac.xml | 6 ++++++ tests/unit/test_mfd_kvm/test_hypervisor.py | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 mfd_kvm/vf_template_with_mac.xml diff --git a/mfd_kvm/hypervisor.py b/mfd_kvm/hypervisor.py index 5acf9fc..e55d688 100644 --- a/mfd_kvm/hypervisor.py +++ b/mfd_kvm/hypervisor.py @@ -764,6 +764,7 @@ def prepare_vf_xml( template_path: Union[Path, str] = Path(__file__).parent / "vf_template.xml", file_to_save: str, pci_address: "PCIAddress", + mac_address: Optional[MACAddress] = None, ) -> Path: """ Create configuration xml of VF for QEMU. @@ -771,6 +772,7 @@ def prepare_vf_xml( :param template_path: Path to template config :param file_to_save: Path to destination directory :param pci_address: PCI Address of VF + :param mac_address: MAC address of VF, if needed to set in config :return: Path to saved configuration """ logger.log(log_levels.MODULE_DEBUG, msg="Creating configuration file for VF.") @@ -778,7 +780,12 @@ def prepare_vf_xml( for key, value in asdict(pci_address).items(): # convert int to hex required for xml data_for_template[key] = hex(value) - file_to_save = self._render_file(file_to_save, data_for_template, template_path) + if mac_address is not None: + data_for_template["mac_address"] = str(mac_address) + template_path_with_mac = Path(__file__).parent / "vf_template_with_mac.xml" + file_to_save = self._render_file(file_to_save, data_for_template, template_path_with_mac) + else: + file_to_save = self._render_file(file_to_save, data_for_template, template_path) return file_to_save def _render_file( diff --git a/mfd_kvm/vf_template_with_mac.xml b/mfd_kvm/vf_template_with_mac.xml new file mode 100644 index 0000000..776bc2c --- /dev/null +++ b/mfd_kvm/vf_template_with_mac.xml @@ -0,0 +1,6 @@ + + + +
+ + \ No newline at end of file diff --git a/tests/unit/test_mfd_kvm/test_hypervisor.py b/tests/unit/test_mfd_kvm/test_hypervisor.py index ef17e37..ec03de4 100644 --- a/tests/unit/test_mfd_kvm/test_hypervisor.py +++ b/tests/unit/test_mfd_kvm/test_hypervisor.py @@ -798,6 +798,21 @@ def test_prepare_vf_xml(self, hv, mocker): hv.prepare_vf_xml(template_path="", file_to_save="", pci_address=PCIAddress(0, 0, 15, 1)) hv._conn.path().write_text.assert_called_once() + def test_prepare_vf_xml_with_mac_address(self, hv, mocker): + template_data = b"
" + mocker.patch("mfd_kvm.hypervisor.open", mocker.mock_open(read_data=template_data)) + hv._conn.path = mocker.Mock() + pci_address = PCIAddress(0, 0, 15, 1) + mac = MACAddress("AA:BB:CC:DD:EE:FF") + + hv.prepare_vf_xml(template_path="", file_to_save="", pci_address=pci_address, mac_address=mac) + + expected_rendered = ( + "" + "
" + ) + hv._conn.path().write_text.assert_called_once_with(expected_rendered) + def test_prepare_pci_controller_xml(self, hv, mocker): template_data = ( b"index='{{index}}' chassis='{{chassis}}' port='{{port}}' bus='{{bus}}' slot='{{slot}}' "