diff --git a/azurelinuxagent/common/osutil/redhat.py b/azurelinuxagent/common/osutil/redhat.py index 312dd16084..05a4b659df 100644 --- a/azurelinuxagent/common/osutil/redhat.py +++ b/azurelinuxagent/common/osutil/redhat.py @@ -117,11 +117,39 @@ def set_hostname(self, hostname): logger.warn("[{0}] failed, attempting fallback".format(' '.join(hostnamectl_cmd))) DefaultOSUtil.set_hostname(self, hostname) + def get_nm_controlled(self): + ifname = self.get_if_name() + filepath = "/etc/sysconfig/network-scripts/ifcfg-{0}".format(ifname) + nm_controlled_cmd = ['grep', 'NM_CONTROLLED=', filepath] + try: + result = shellutil.run_command(nm_controlled_cmd, log_error=False, encode_output=False).rstrip() + + if result and len(result.split('=')) > 1: + # Remove trailing white space and ' or " characters + value = result.split('=')[1].replace("'", '').replace('"', '').rstrip() + if value == "n" or value == "no": + return False + except shellutil.CommandError as e: + # Command might fail because NM_CONTROLLED value is not in interface config file (exit code 1). + # Log warning for any other exit code. + # NM_CONTROLLED=y by default if not specified. + if e.returncode != 1: + logger.warn("[{0}] failed: {1}.\nAgent will continue to publish hostname without NetworkManager restart".format(' '.join(nm_controlled_cmd), e)) + except Exception as e: + logger.warn("Unexpected error while retrieving value of NM_CONTROLLED in {0}: {1}.\nAgent will continue to publish hostname without NetworkManager restart".format(filepath, e)) + + return True + def publish_hostname(self, hostname): """ - Restart NetworkManager first before publishing hostname + Restart NetworkManager first before publishing hostname, only if the network interface is not controlled by the + NetworkManager service (as determined by NM_CONTROLLED=n in the interface configuration). If the NetworkManager + service is restarted before the agent publishes the hostname, and NM_controlled=y, a race condition may happen + between the NetworkManager service and the Guest Agent making changes to the network interface configuration + simultaneously. """ - shellutil.run("service NetworkManager restart") + if not self.get_nm_controlled(): + shellutil.run("service NetworkManager restart") super(RedhatOSUtil, self).publish_hostname(hostname) def register_agent_service(self):