Skip to content

Commit

Permalink
Merge pull request #3935 from TasmiyaNalatwad/obtain_ip_addr
Browse files Browse the repository at this point in the history
Obtaing the ip address of the guest and update address cache
  • Loading branch information
lmr authored Aug 11, 2024
2 parents 96726f4 + 9537745 commit 8ed80f8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
19 changes: 19 additions & 0 deletions virttest/utils_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
utils_misc,
utils_package,
utils_selinux,
virsh,
)
from virttest.remote import RemoteRunner
from virttest.staging import service, utils_memory
Expand Down Expand Up @@ -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
9 changes: 5 additions & 4 deletions virttest/virt_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,10 +935,11 @@ def _get_address():

ipaddr = utils_misc.wait_for(_get_address, timeout, step=interval)
if not ipaddr:
# Read guest address via serial console and update VM address
# cache to avoid get out-dated address.
utils_net.update_mac_ip_address(self, timeout)
ipaddr = self.get_address(nic_index, ip_version)
# obatining ip address from virsh-net-dhcp-leases command
mac = self.get_mac_address(nic_index).lower()
ipaddr = utils_net.obtain_guest_ip_from_dhcp_leases(mac)
# updating cache with the latest ip address value
self.address_cache[mac] = ipaddr
msg = "Found/Verified IP %s for VM %s NIC %s" % (ipaddr, self.name, nic_index)
LOG.debug(msg)
return ipaddr
Expand Down

0 comments on commit 8ed80f8

Please sign in to comment.