Skip to content

Commit

Permalink
skip run dhclient if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
LiliDeng committed Jul 13, 2024
1 parent 94e57d9 commit 547a563
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions lisa/tools/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,25 @@ def restart_device(
default_route: str = "",
) -> None:
cmd = f"ip link set dev {nic_name} down;ip link set dev {nic_name} up "
if run_dhclient:
# if no ip address
# firstly kill dhclient if it is running
# then run dhclient to get ip address
cmd += (
f' && (ip addr show {nic_name} | grep "inet ") || '
"(pidof dhclient && kill $(pidof dhclient) && "
f"dhclient -r {nic_name}; dhclient {nic_name})"
)
if default_route:
# need add wait 1 second, for some distro, e.g.
# redhat rhel 7-lvm 7.8.2021051701
# the ip route will be back after nic down and up for a while
cmd += " && sleep 1 "
# if no default route, add it back
cmd += f" && ip route show | grep default || ip route add {default_route}"
dhclient_exist = self.node.execute("which dhclient", shell=True, sudo=True)
if dhclient_exist.exit_code == 0:
if run_dhclient:
# if no ip address
# firstly kill dhclient if it is running
# then run dhclient to get ip address
cmd += (
f' && (ip addr show {nic_name} | grep "inet ") || '
"(pidof dhclient && kill $(pidof dhclient) && "
f"dhclient -r {nic_name}; dhclient {nic_name})"
)
if default_route:
# need add wait 1 second, for some distro, e.g.
# redhat rhel 7-lvm 7.8.2021051701
# the ip route will be back after nic down and up for a while
cmd += " && sleep 1 "
# if no default route, add it back
cmd += " && (ip route show | grep default ||"
cmd += f" ip route add {default_route})"
self.node.execute(
cmd,
shell=True,
Expand Down

0 comments on commit 547a563

Please sign in to comment.