From d84eed9823ab3f28c31c257cfed42a309f2cafd1 Mon Sep 17 00:00:00 2001 From: Tasmiya Nalatwad Date: Wed, 18 Sep 2024 16:21:53 +0530 Subject: [PATCH] Obtaing the ip address of the guest and update address cache Few of the times it is seen that the ip address is not being fetch and the avocado runs were failed with error "ERROR: Failures occurred while postprocess:\n\n: Guest virt-tests-vm1 dmesg verification failed: Login timeout expired (output: 'exceeded 240 s timeout, last failure: No ipv4 DHCP lease for MAC aa:bb:cc:dd:ee:ff') " To handle this error the patch has been sent. The patch helps in obtaining ip address of the guest using "virsh-net-dhcp-leases default" command. If the guest mac address is found in the command output, the mac ipv4 address is obatined and updated in the address.cache Signed-off-by: Tasmiya Nalatwad --- virttest/libvirt_vm.py | 10 ++++++++++ virttest/utils_net.py | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/virttest/libvirt_vm.py b/virttest/libvirt_vm.py index 825674e2f7..c1be15e033 100644 --- a/virttest/libvirt_vm.py +++ b/virttest/libvirt_vm.py @@ -35,6 +35,7 @@ virsh, virt_vm, xml_utils, + utils_net, ) # Using as lower capital is not the best way to do, but this is just a @@ -378,6 +379,15 @@ def backup_xml(self, active=False): LOG.error("Failed to backup xml file:\n%s", detail) return "" + def _get_address(self, index=0, ip_version="ipv4", session=None, timeout=60.0): + try: + return super()._get_address(index, ip_version, session, timeout) + except virt_vm.VMIPAddressMissingError: + mac = self.get_mac_address(index).lower() + ipaddr = utils_net.obtain_guest_ip_from_dhcp_leases(mac) + self.address_cache[mac] = ipaddr + return ipaddr + def clone( self, name=None, diff --git a/virttest/utils_net.py b/virttest/utils_net.py index 875d537fd3..446394df74 100644 --- a/virttest/utils_net.py +++ b/virttest/utils_net.py @@ -35,6 +35,7 @@ utils_misc, utils_package, utils_selinux, + virsh, ) from virttest.remote import RemoteRunner from virttest.staging import service, utils_memory @@ -4889,3 +4890,21 @@ def check_class_rules(ifname, rule_id, bandwidth, expect_none=False): stacktrace.log_exc_info(sys.exc_info()) return False return True + + +def obtain_guest_ip_from_dhcp_leases(mac): + """ + Obtaining the guest ip address from virsh-net-dhcp-leases command + :param: Mac address of the guest + :return: return ip-address if found for given mac in the + virsh-net-dhcp-leases default table, else return None + """ + output = virsh.net_dhcp_leases("default") + lines = output.stdout.splitlines() + for line in lines: + if mac in line: + parts = line.split() + for part in parts: + if "/" in part: + return part.split("/")[0] + return None