2019-12-17 16:40:10 +01:00
|
|
|
#!/usr/bin/env python3
|
2014-10-09 16:02:46 -07:00
|
|
|
#
|
2018-12-13 11:43:32 -08:00
|
|
|
# Copyright 2014-2017 Cumulus Networks, Inc. All rights reserved.
|
2014-10-09 16:02:46 -07:00
|
|
|
# Author: Roopa Prabhu, roopa@cumulusnetworks.com
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
2019-12-17 01:04:54 +01:00
|
|
|
import errno
|
2014-10-09 16:02:46 -07:00
|
|
|
|
2018-12-13 11:43:32 -08:00
|
|
|
try:
|
|
|
|
from ifupdown2.ifupdown.utils import utils
|
|
|
|
from ifupdown2.ifupdownaddons.utilsbase import *
|
2019-12-17 17:25:32 +01:00
|
|
|
except (ImportError, ModuleNotFoundError):
|
2018-12-13 11:43:32 -08:00
|
|
|
from ifupdown.utils import utils
|
|
|
|
from ifupdownaddons.utilsbase import *
|
|
|
|
|
2014-10-09 16:02:46 -07:00
|
|
|
|
|
|
|
class dhclient(utilsBase):
|
|
|
|
""" This class contains helper methods to interact with the dhclient
|
|
|
|
utility """
|
|
|
|
|
|
|
|
def _pid_exists(self, pidfilename):
|
|
|
|
if os.path.exists(pidfilename):
|
2019-12-17 01:04:54 +01:00
|
|
|
try:
|
|
|
|
return os.readlink(
|
|
|
|
"/proc/%s/exe" % self.read_file_oneline(pidfilename)
|
|
|
|
).endswith("dhclient")
|
|
|
|
except OSError as e:
|
|
|
|
try:
|
|
|
|
if e.errno == errno.EACCES:
|
|
|
|
return os.path.exists("/proc/%s" % self.read_file_oneline(pidfilename))
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
except:
|
2014-10-09 16:02:46 -07:00
|
|
|
return False
|
2019-12-17 01:04:54 +01:00
|
|
|
return False
|
2014-10-09 16:02:46 -07:00
|
|
|
|
|
|
|
def is_running(self, ifacename):
|
|
|
|
return self._pid_exists('/run/dhclient.%s.pid' %ifacename)
|
|
|
|
|
|
|
|
def is_running6(self, ifacename):
|
|
|
|
return self._pid_exists('/run/dhclient6.%s.pid' %ifacename)
|
|
|
|
|
2016-04-05 16:11:41 -07:00
|
|
|
def _run_dhclient_cmd(self, cmd, cmd_prefix=None):
|
|
|
|
if not cmd_prefix:
|
|
|
|
cmd_aslist = []
|
|
|
|
else:
|
|
|
|
cmd_aslist = cmd_prefix.split()
|
|
|
|
if cmd_aslist:
|
|
|
|
cmd_aslist.extend(cmd)
|
|
|
|
else:
|
|
|
|
cmd_aslist = cmd
|
2016-05-13 19:52:57 +02:00
|
|
|
utils.exec_commandl(cmd_aslist, stdout=None, stderr=None)
|
2016-04-05 16:11:41 -07:00
|
|
|
|
|
|
|
def stop(self, ifacename, cmd_prefix=None):
|
2014-10-09 16:02:46 -07:00
|
|
|
if os.path.exists('/sbin/dhclient3'):
|
|
|
|
cmd = ['/sbin/dhclient3', '-x', '-pf',
|
|
|
|
'/run/dhclient.%s.pid' %ifacename, '-lf',
|
|
|
|
'/var/lib/dhcp3/dhclient.%s.leases' %ifacename,
|
|
|
|
'%s' %ifacename]
|
|
|
|
else:
|
|
|
|
cmd = ['/sbin/dhclient', '-x', '-pf',
|
|
|
|
'/run/dhclient.%s.pid' %ifacename,
|
|
|
|
'-lf', '/var/lib/dhcp/dhclient.%s.leases' %ifacename,
|
|
|
|
'%s' %ifacename]
|
2016-04-05 16:11:41 -07:00
|
|
|
self._run_dhclient_cmd(cmd, cmd_prefix)
|
2014-10-09 16:02:46 -07:00
|
|
|
|
2016-04-05 16:11:41 -07:00
|
|
|
def start(self, ifacename, wait=True, cmd_prefix=None):
|
2014-10-09 16:02:46 -07:00
|
|
|
if os.path.exists('/sbin/dhclient3'):
|
|
|
|
cmd = ['/sbin/dhclient3', '-pf',
|
|
|
|
'/run/dhclient.%s.pid' %ifacename,
|
|
|
|
'-lf', '/var/lib/dhcp3/dhclient.%s.leases' %ifacename,
|
|
|
|
'%s' %ifacename]
|
|
|
|
else:
|
2016-04-05 16:11:41 -07:00
|
|
|
cmd = ['/sbin/dhclient', '-pf',
|
|
|
|
'/run/dhclient.%s.pid' %ifacename, '-lf',
|
|
|
|
'/var/lib/dhcp/dhclient.%s.leases' %ifacename,
|
2014-10-09 16:02:46 -07:00
|
|
|
'%s' %ifacename]
|
2016-02-03 16:38:18 -08:00
|
|
|
if not wait:
|
ifupdown2: Add nowait attribute for dhcp addon
Ticket: None
Reviewed By: CCR-4058
Testing Done: ifup'd interface with both nowait=0 and nowait=1 and not specified
at all.
The Mellanox platform, as well as some others probably, has two management
interfaces: eth0 and eth1. The customer may plug a cable into either one of
these interfaces, and very rarely both of them. If only one cable is plugged in
and we don't know which one, then /etc/network/interfaces must be configured
by default to automatically bring up both interfaces using DHCP. But when an
interface does not have link, it stalls the boot process for 60 seconds while
dhclient times out.
This patch changes the default dhclient behavior to not wait for DHCP to
complete, by using the "-nw" option when calling dhclient. This means that
dhclient will immediately return and DHCP will complete in the background.
A module attribute has been added for the DHCP addon called "nowait", which
defaults to 1. If this attribute is set to 0, then dhclient will revert to its
previous behavior and delay up to a minute while DHCP completes. This attribute
can be specified in a policy file, e.g. /etc/network/ifupdown2/policy.d/dhcp.json,
with contents such as:
{
"dhcp" :
{
"nowait" : 0
}
}
2016-02-01 16:42:14 -08:00
|
|
|
cmd.append('-nw')
|
2016-04-05 16:11:41 -07:00
|
|
|
self._run_dhclient_cmd(cmd, cmd_prefix)
|
2014-10-09 16:02:46 -07:00
|
|
|
|
2016-04-05 16:11:41 -07:00
|
|
|
def release(self, ifacename, cmd_prefix=None):
|
2014-10-09 16:02:46 -07:00
|
|
|
if os.path.exists('/sbin/dhclient3'):
|
|
|
|
cmd = ['/sbin/dhclient3', '-r', '-pf',
|
|
|
|
'/run/dhclient.%s.pid' %ifacename, '-lf',
|
|
|
|
'/var/lib/dhcp3/dhclient.%s.leases' %ifacename,
|
|
|
|
'%s' %ifacename]
|
|
|
|
else:
|
|
|
|
cmd = ['/sbin/dhclient', '-r', '-pf',
|
|
|
|
'/run/dhclient.%s.pid' %ifacename,
|
|
|
|
'-lf', '/var/lib/dhcp/dhclient.%s.leases' %ifacename,
|
|
|
|
'%s' %ifacename]
|
2016-04-05 16:11:41 -07:00
|
|
|
self._run_dhclient_cmd(cmd, cmd_prefix)
|
2014-10-09 16:02:46 -07:00
|
|
|
|
2019-07-06 15:14:38 -07:00
|
|
|
def start6(self, ifacename, wait=True, cmd_prefix=None, duid=None):
|
2016-04-05 16:11:41 -07:00
|
|
|
cmd = ['/sbin/dhclient', '-6', '-pf',
|
2014-10-09 16:02:46 -07:00
|
|
|
'/run/dhclient6.%s.pid' %ifacename, '-lf',
|
2016-11-15 18:33:07 +01:00
|
|
|
'/var/lib/dhcp/dhclient6.%s.leases' % ifacename,
|
ifupdown2: Add nowait attribute for dhcp addon
Ticket: None
Reviewed By: CCR-4058
Testing Done: ifup'd interface with both nowait=0 and nowait=1 and not specified
at all.
The Mellanox platform, as well as some others probably, has two management
interfaces: eth0 and eth1. The customer may plug a cable into either one of
these interfaces, and very rarely both of them. If only one cable is plugged in
and we don't know which one, then /etc/network/interfaces must be configured
by default to automatically bring up both interfaces using DHCP. But when an
interface does not have link, it stalls the boot process for 60 seconds while
dhclient times out.
This patch changes the default dhclient behavior to not wait for DHCP to
complete, by using the "-nw" option when calling dhclient. This means that
dhclient will immediately return and DHCP will complete in the background.
A module attribute has been added for the DHCP addon called "nowait", which
defaults to 1. If this attribute is set to 0, then dhclient will revert to its
previous behavior and delay up to a minute while DHCP completes. This attribute
can be specified in a policy file, e.g. /etc/network/ifupdown2/policy.d/dhcp.json,
with contents such as:
{
"dhcp" :
{
"nowait" : 0
}
}
2016-02-01 16:42:14 -08:00
|
|
|
'%s' %ifacename]
|
2016-02-03 16:38:18 -08:00
|
|
|
if not wait:
|
ifupdown2: Add nowait attribute for dhcp addon
Ticket: None
Reviewed By: CCR-4058
Testing Done: ifup'd interface with both nowait=0 and nowait=1 and not specified
at all.
The Mellanox platform, as well as some others probably, has two management
interfaces: eth0 and eth1. The customer may plug a cable into either one of
these interfaces, and very rarely both of them. If only one cable is plugged in
and we don't know which one, then /etc/network/interfaces must be configured
by default to automatically bring up both interfaces using DHCP. But when an
interface does not have link, it stalls the boot process for 60 seconds while
dhclient times out.
This patch changes the default dhclient behavior to not wait for DHCP to
complete, by using the "-nw" option when calling dhclient. This means that
dhclient will immediately return and DHCP will complete in the background.
A module attribute has been added for the DHCP addon called "nowait", which
defaults to 1. If this attribute is set to 0, then dhclient will revert to its
previous behavior and delay up to a minute while DHCP completes. This attribute
can be specified in a policy file, e.g. /etc/network/ifupdown2/policy.d/dhcp.json,
with contents such as:
{
"dhcp" :
{
"nowait" : 0
}
}
2016-02-01 16:42:14 -08:00
|
|
|
cmd.append('-nw')
|
2019-07-06 15:14:38 -07:00
|
|
|
if duid is not None:
|
|
|
|
cmd.append('-D')
|
|
|
|
cmd.append(duid)
|
2016-04-05 16:11:41 -07:00
|
|
|
self._run_dhclient_cmd(cmd, cmd_prefix)
|
2014-10-09 16:02:46 -07:00
|
|
|
|
2019-07-06 15:14:38 -07:00
|
|
|
def stop6(self, ifacename, cmd_prefix=None, duid=None):
|
2016-04-05 16:11:41 -07:00
|
|
|
cmd = ['/sbin/dhclient', '-6', '-x', '-pf',
|
2016-11-15 18:33:07 +01:00
|
|
|
'/run/dhclient6.%s.pid' % ifacename, '-lf',
|
|
|
|
'/var/lib/dhcp/dhclient6.%s.leases' % ifacename,
|
2016-04-05 16:11:41 -07:00
|
|
|
'%s' %ifacename]
|
2019-07-06 15:14:38 -07:00
|
|
|
if duid is not None:
|
|
|
|
cmd.append('-D')
|
|
|
|
cmd.append(duid)
|
2016-04-05 16:11:41 -07:00
|
|
|
self._run_dhclient_cmd(cmd, cmd_prefix)
|
2014-10-09 16:02:46 -07:00
|
|
|
|
2019-07-06 15:14:38 -07:00
|
|
|
def release6(self, ifacename, cmd_prefix=None, duid=None):
|
2016-04-05 16:11:41 -07:00
|
|
|
cmd = ['/sbin/dhclient', '-6', '-r', '-pf',
|
2016-07-15 14:21:23 -07:00
|
|
|
'/run/dhclient6.%s.pid' %ifacename,
|
2016-12-02 00:39:35 +01:00
|
|
|
'-lf', '/var/lib/dhcp/dhclient6.%s.leases' % ifacename,
|
2016-04-05 16:11:41 -07:00
|
|
|
'%s' %ifacename]
|
2019-07-06 15:14:38 -07:00
|
|
|
if duid is not None:
|
|
|
|
cmd.append('-D')
|
|
|
|
cmd.append(duid)
|
2016-04-05 16:11:41 -07:00
|
|
|
self._run_dhclient_cmd(cmd, cmd_prefix)
|