From 745fd8df21b9f70a3cce6382122a352822934151 Mon Sep 17 00:00:00 2001 From: Yanqiu Zhang Date: Wed, 28 Aug 2024 07:17:55 -0400 Subject: [PATCH] utils_net: also check ip for host up nic When create linux bridge or macvtap for guest interface to use, there maybe many UP state host nic, but only the ones with ip can work. Signed-off-by: Yanqiu Zhang --- virttest/utils_net.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/virttest/utils_net.py b/virttest/utils_net.py index 1d380366ec..71a9da898f 100644 --- a/virttest/utils_net.py +++ b/virttest/utils_net.py @@ -1439,11 +1439,24 @@ def get_net_if(runner=local_runner, state=".*", qdisc=".*", optional=".*"): # As the runner converts stdout to unicode on Python2, # it has to be converted to string for struct.pack(). result = str(runner(cmd)) - return re.findall( + ifaces_up = re.findall( r"^\d+: (\S+?)[@:].*%s.*%s.*state %s.*$" % (optional, qdisc, state), result, re.MULTILINE, ) + LOG.debug("The ifaces up on host: %s" % ifaces_up) + ifaces_with_ip = [] + for iface in ifaces_up: + cmd = "ip -c=never addr show %s" % iface + ret = runner(cmd) + ipv4_addr = re.findall("inet (.+?)/..?", ret, re.MULTILINE) + if ipv4_addr: + ifaces_with_ip.append(iface) + LOG.debug("The ifaces up with ip on host: %s" % ifaces_with_ip) + if not ifaces_with_ip: + LOG.error("No up ifaces with ip on host!") + else: + return ifaces_with_ip def get_sorted_net_if():