def kill_dhclient(device_name, family=4): for pid in pgrep('dhclient'): try: with open('/proc/%s/cmdline' % pid) as cmdline: args = cmdline.read().strip('\0').split('\0') except IOError as ioe: if ioe.errno == errno.ENOENT: # exited before we read cmdline continue if args[-1] != device_name: # dhclient of another device continue tokens = iter(args) pid_file = '/var/run/dhclient.pid' # Default client pid location running_family = 4 for token in tokens: if token == '-pf': pid_file = next(tokens) elif token == '--no-pid': pid_file = None elif token == '-6': running_family = 6 if running_family != family: continue logging.info('Stopping dhclient -%s before running our own on %s', family, device_name) _kill_and_rm_pid(pid, pid_file) # In order to be able to configure the device with dhclient again. It is # necessary that dhclient does not find it configured with any IP address # (except 0.0.0.0 which is fine, or IPv6 link-local address needed for # DHCPv6). ipwrapper.addrFlush(device_name, family)
def _remove_ip_config(iface, ipv4, ipv6): net_dev = NetDevice(iface, iproute2, ipv4=ipv4, ipv6=ipv6) DynamicSourceRoute.addInterfaceTracking(net_dev) DhcpClient(iface).shutdown() iproute2._removeSourceRoute(net_dev, DynamicSourceRoute) if ipv4.address or ipv6.address: with suppress(ipwrapper.IPRoute2Error): # device does not exist ipwrapper.addrFlush(iface)
def _remove_ip_config(ip_config): iface = ip_config.top_dev ipv4 = ip_config.ipv4 ipv6 = ip_config.ipv6 net_dev = NetDevice(iface, iproute2, ipv4=ipv4, ipv6=ipv6) DynamicSourceRoute.addInterfaceTracking(net_dev) DhcpClient(iface).shutdown() iproute2._removeSourceRoute(net_dev, DynamicSourceRoute) if ipv4.address or ipv6.address: with suppress(ipwrapper.IPRoute2Error): # device does not exist ipwrapper.addrFlush(iface)
def _remove_network_ip_config(ip_config): """Remove IP configuration from Vdsm controlled OVS network""" iface = ip_config.top_dev ipv4 = ip_config.ipv4 ipv6 = ip_config.ipv6 net_dev = NetDevice(iface, iproute2, ipv4=ipv4, ipv6=ipv6) DynamicSourceRoute.addInterfaceTracking(net_dev) DhcpClient(iface).shutdown() iproute2._removeSourceRoute(net_dev, DynamicSourceRoute) if ipv4.address or ipv6.address: with suppress(ipwrapper.IPRoute2Error): # device does not exist ipwrapper.addrFlush(iface)
def kill_dhclient(device_name): for pid in pgrep('dhclient'): try: with open('/proc/%s/cmdline' % pid) as cmdline: args = cmdline.read().strip('\0').split('\0') except IOError as ioe: if ioe.errno == errno.ENOENT: # exited before we read cmdline continue if args[-1] != device_name: # dhclient of another device continue tokens = iter(args) pid_file = '/var/run/dhclient.pid' # Default client pid location for token in tokens: if token == '-pf': pid_file = next(tokens) elif token == '--no-pid': pid_file = None _kill_and_rm_pid(pid, pid_file) # In order to be able to configure the device with dhclient again. It is # necessary that dhclient does not find it configured with any IP address # (except 0.0.0.0 which is fine). ipwrapper.addrFlush(device_name)
def removeIpConfig(self, iface): ipwrapper.addrFlush(iface.name)