From bb58463474e334b8c8d1769101bd3afc48ebfef4 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Mon, 21 Mar 2016 20:42:26 -0500 Subject: Added net.find_fallback_network_device() to find an appropriate device to dhcp on in the event that no network configuration was provided to cloud-init - Devices in /sys/class/net aside from loopback devices are scanned - Each device is tested to determine if it has a carrier using /sys/class/net/DEV/carrier, devices which do are preferred as they are most likely connected to the outside world - Devices which do not have a carrier but which might still be connected due to being in a dormant or down state are used as fallbacks in case no devices are found which have a carrier - A network state dictionary is generated to be passed to render_network_state to write ENI - A systemd link file is generated that will rename the chosen device to eth0 --- cloudinit/net/__init__.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) (limited to 'cloudinit/net') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 3cf99604..800ffe61 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -20,6 +20,8 @@ import errno import glob import os import re +import string +import textwrap from cloudinit import log as logging from cloudinit import util @@ -280,6 +282,87 @@ def parse_net_config(path): return ns +def find_fallback_network_device(): + """Determine which attached net dev is most likely to have a connection and + generate network state to run dhcp on that interface""" + ns = {'interfaces': {}, 'dns': {'search': [], 'nameservers': []}, + 'routes': []} + default_link_file = textwrap.dedent(""" + #cloud-init + [Match] + MACAddress={mac} + + [Link] + Name={name} + """) + + # get list of interfaces that could have connections + invalid_interfaces = set(['lo']) + potential_interfaces = set(os.listdir(SYS_CLASS_NET)) + potential_interfaces = potential_interfaces.difference(invalid_interfaces) + + # sort into interfaces with carrier, interfaces which could have carrier, + # and ignore interfaces that are definitely disconnected + connected = [] + possibly_connected = [] + for interface in potential_interfaces: + sysfs_carrier = os.path.join(SYS_CLASS_NET, interface, 'carrier') + carrier = int(util.load_file(sysfs_carrier).strip()) + if carrier: + connected.append(interface) + continue + # check if nic is dormant or down, as this may make a nick appear to + # not have a carrier even though it could acquire one when brought + # online by dhclient + sysfs_dormant = os.path.join(SYS_CLASS_NET, interface, 'dormant') + dormant = int(util.load_file(sysfs_dormant).strip()) + if dormant: + possibly_connected.append(interface) + continue + sysfs_operstate = os.path.join(SYS_CLASS_NET, interface, 'operstate') + operstate = util.load_file(sysfs_operstate).strip() + if operstate in ['dormant', 'down', 'lowerlayerdown', 'unknown']: + possibly_connected.append(interface) + continue + + # don't bother with interfaces that might not be connected if there are + # some that definitely are + if connected: + potential_interfaces = connected + else: + potential_interfaces = possibly_connected + + # if there are no interfaces, give up + if not potential_interfaces: + return + + # if eth0 exists use it above anything else, otherwise get the interface + # that looks 'first' + if 'eth0' in potential_interfaces: + name = 'eth0' + else: + name = potential_interfaces.sort( + key=lambda x: int(x.strip(string.ascii_letters)))[0] + + sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') + mac = util.load_file(sysfs_mac).strip() + + # generate net config for interface, rename interface to eth0 for backwards + # compatibility, and attempt both dhcp4 and dhcp6 + ns['interfaces']['eth0'] = { + 'mac_address': mac, 'name': 'eth0', 'type': 'physical', + 'mode': 'manual', 'inet': 'inet', + 'subnets': [{'type': 'dhcp4'}, {'type': 'dhcp6'}] + } + + # insert params into link file + link_file = default_link_file.format(name=name, mac=mac) + + syslink_name = "/etc/systemd/network/50-cloud-init-{}.link".format(name) + + return (ns, link_file, syslink_name) + + def render_persistent_net(network_state): ''' Given state, emit udev rules to map mac to ifname -- cgit v1.2.3 From 2aacb06be37e7e8aa84d11ae8c566a26f9df27e4 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 00:33:35 -0500 Subject: Wrap read calls to /sys/class/net/DEV/{carrier, dormant, operstate} in try/except blocks because there are sometimes read errors on the files and this should not cause a stacktrace --- cloudinit/net/__init__.py | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'cloudinit/net') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 800ffe61..e5b45926 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -306,24 +306,34 @@ def find_fallback_network_device(): connected = [] possibly_connected = [] for interface in potential_interfaces: - sysfs_carrier = os.path.join(SYS_CLASS_NET, interface, 'carrier') - carrier = int(util.load_file(sysfs_carrier).strip()) - if carrier: - connected.append(interface) - continue + try: + sysfs_carrier = os.path.join(SYS_CLASS_NET, interface, 'carrier') + carrier = int(util.load_file(sysfs_carrier).strip()) + if carrier: + connected.append(interface) + continue + except OSError: + pass # check if nic is dormant or down, as this may make a nick appear to # not have a carrier even though it could acquire one when brought # online by dhclient - sysfs_dormant = os.path.join(SYS_CLASS_NET, interface, 'dormant') - dormant = int(util.load_file(sysfs_dormant).strip()) - if dormant: - possibly_connected.append(interface) - continue - sysfs_operstate = os.path.join(SYS_CLASS_NET, interface, 'operstate') - operstate = util.load_file(sysfs_operstate).strip() - if operstate in ['dormant', 'down', 'lowerlayerdown', 'unknown']: - possibly_connected.append(interface) - continue + try: + sysfs_dormant = os.path.join(SYS_CLASS_NET, interface, 'dormant') + dormant = int(util.load_file(sysfs_dormant).strip()) + if dormant: + possibly_connected.append(interface) + continue + except OSError: + pass + try: + sysfs_operstate = os.path.join(SYS_CLASS_NET, interface, + 'operstate') + operstate = util.load_file(sysfs_operstate).strip() + if operstate in ['dormant', 'down', 'lowerlayerdown', 'unknown']: + possibly_connected.append(interface) + continue + except OSError: + pass # don't bother with interfaces that might not be connected if there are # some that definitely are -- cgit v1.2.3 From 217d92372ca5a4e994f1e9bc9580363dbba59032 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 00:43:31 -0500 Subject: Fix typo --- cloudinit/net/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'cloudinit/net') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index e5b45926..4641e54d 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -351,8 +351,9 @@ def find_fallback_network_device(): if 'eth0' in potential_interfaces: name = 'eth0' else: - name = potential_interfaces.sort( - key=lambda x: int(x.strip(string.ascii_letters)))[0] + potential_interfaces.sort( + key=lambda x: int(x.strip(string.ascii_letters))) + name = potential_interfaces[0] sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') mac = util.load_file(sysfs_mac).strip() -- cgit v1.2.3 From 7e399773a95e21e4c825dab61847d6abcd2aa511 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 01:17:12 -0500 Subject: For find_fallback_network_device, kwarg rename_to_default specifies whether or not to attempt renaming the network interface to the default interface. Default interface is controleld by net.DEFAULT_PRIMARY_INTERFACE and is currently set to eth0 for legacy reasons. By default cloud-init will not attempt to rename the device as this does not work in some situtations depending on the backing driver of the device. --- cloudinit/net/__init__.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'cloudinit/net') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 4641e54d..e2e50441 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -48,6 +48,8 @@ NET_CONFIG_BRIDGE_OPTIONS = [ "bridge_hello", "bridge_maxage", "bridge_maxwait", "bridge_stp", ] +DEFAULT_PRIMARY_INTERFACE = 'eth0' + def sys_dev_path(devname, path=""): return SYS_CLASS_NET + devname + "/" + path @@ -282,9 +284,10 @@ def parse_net_config(path): return ns -def find_fallback_network_device(): +def find_fallback_network_device(rename_to_default=False): """Determine which attached net dev is most likely to have a connection and generate network state to run dhcp on that interface""" + # by default use eth0 as primary interface ns = {'interfaces': {}, 'dns': {'search': [], 'nameservers': []}, 'routes': []} default_link_file = textwrap.dedent(""" @@ -348,8 +351,8 @@ def find_fallback_network_device(): # if eth0 exists use it above anything else, otherwise get the interface # that looks 'first' - if 'eth0' in potential_interfaces: - name = 'eth0' + if DEFAULT_PRIMARY_INTERFACE in potential_interfaces: + name = DEFAULT_PRIMARY_INTERFACE else: potential_interfaces.sort( key=lambda x: int(x.strip(string.ascii_letters))) @@ -358,18 +361,23 @@ def find_fallback_network_device(): sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') mac = util.load_file(sysfs_mac).strip() + target_name = name + if rename_to_default: + target_name = DEFAULT_PRIMARY_INTERFACE + # generate net config for interface, rename interface to eth0 for backwards # compatibility, and attempt both dhcp4 and dhcp6 - ns['interfaces']['eth0'] = { - 'mac_address': mac, 'name': 'eth0', 'type': 'physical', + ns['interfaces'][target_name] = { + 'mac_address': mac, 'name': target_name, 'type': 'physical', 'mode': 'manual', 'inet': 'inet', 'subnets': [{'type': 'dhcp4'}, {'type': 'dhcp6'}] } # insert params into link file - link_file = default_link_file.format(name=name, mac=mac) + link_file = default_link_file.format(name=target_name, mac=mac) - syslink_name = "/etc/systemd/network/50-cloud-init-{}.link".format(name) + syslink_name = "/etc/systemd/network/50-cloud-init-{}.link".format( + target_name) return (ns, link_file, syslink_name) -- cgit v1.2.3 From 3a7e3d198172f26b7b97707520d06fe5303fadbc Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 01:33:20 -0500 Subject: Got rid of blank lines in net.find_fallback_network_device --- cloudinit/net/__init__.py | 7 ------- 1 file changed, 7 deletions(-) (limited to 'cloudinit/net') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index e2e50441..2596b4f5 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -303,7 +303,6 @@ def find_fallback_network_device(rename_to_default=False): invalid_interfaces = set(['lo']) potential_interfaces = set(os.listdir(SYS_CLASS_NET)) potential_interfaces = potential_interfaces.difference(invalid_interfaces) - # sort into interfaces with carrier, interfaces which could have carrier, # and ignore interfaces that are definitely disconnected connected = [] @@ -344,11 +343,9 @@ def find_fallback_network_device(rename_to_default=False): potential_interfaces = connected else: potential_interfaces = possibly_connected - # if there are no interfaces, give up if not potential_interfaces: return - # if eth0 exists use it above anything else, otherwise get the interface # that looks 'first' if DEFAULT_PRIMARY_INTERFACE in potential_interfaces: @@ -360,11 +357,9 @@ def find_fallback_network_device(rename_to_default=False): sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') mac = util.load_file(sysfs_mac).strip() - target_name = name if rename_to_default: target_name = DEFAULT_PRIMARY_INTERFACE - # generate net config for interface, rename interface to eth0 for backwards # compatibility, and attempt both dhcp4 and dhcp6 ns['interfaces'][target_name] = { @@ -372,10 +367,8 @@ def find_fallback_network_device(rename_to_default=False): 'mode': 'manual', 'inet': 'inet', 'subnets': [{'type': 'dhcp4'}, {'type': 'dhcp6'}] } - # insert params into link file link_file = default_link_file.format(name=target_name, mac=mac) - syslink_name = "/etc/systemd/network/50-cloud-init-{}.link".format( target_name) -- cgit v1.2.3 From e66bcc8b2ea7648c15476cdd43d3753ee6c27ff1 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 01:48:39 -0500 Subject: - Rename find_fallback_network_device to generate_fallback_config - Removed systemd .link file generation, as it is not needed right now - Changed return of generate_fallback_config to be just ns dict - In distros.debian don't attempt to write .link file --- cloudinit/distros/debian.py | 4 +--- cloudinit/net/__init__.py | 23 ++++------------------- 2 files changed, 5 insertions(+), 22 deletions(-) (limited to 'cloudinit/net') diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py index 8e57f70e..0fa47274 100644 --- a/cloudinit/distros/debian.py +++ b/cloudinit/distros/debian.py @@ -85,9 +85,7 @@ class Distro(distros.Distro): def _write_network_fallback(self): # old fallback configuration is obsolete, disable it util.disable_conf_file('/etc/network/interfaces.d/eth0.cfg') - (ns, link_file, syslink_name) = net.find_fallback_network_device() - if link_file is not None: - util.write_file(syslink_name, link_file) + ns = net.generate_fallback_config() if ns is not None: net.render_network_state(network_state=ns, target="/") return [] diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 2596b4f5..48b82a2c 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -21,7 +21,6 @@ import glob import os import re import string -import textwrap from cloudinit import log as logging from cloudinit import util @@ -284,20 +283,12 @@ def parse_net_config(path): return ns -def find_fallback_network_device(rename_to_default=False): +def generate_fallback_config(): """Determine which attached net dev is most likely to have a connection and generate network state to run dhcp on that interface""" # by default use eth0 as primary interface ns = {'interfaces': {}, 'dns': {'search': [], 'nameservers': []}, 'routes': []} - default_link_file = textwrap.dedent(""" - #cloud-init - [Match] - MACAddress={mac} - - [Link] - Name={name} - """) # get list of interfaces that could have connections invalid_interfaces = set(['lo']) @@ -358,21 +349,15 @@ def find_fallback_network_device(rename_to_default=False): sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') mac = util.load_file(sysfs_mac).strip() target_name = name - if rename_to_default: - target_name = DEFAULT_PRIMARY_INTERFACE - # generate net config for interface, rename interface to eth0 for backwards - # compatibility, and attempt both dhcp4 and dhcp6 + + # generate net config for interface ns['interfaces'][target_name] = { 'mac_address': mac, 'name': target_name, 'type': 'physical', 'mode': 'manual', 'inet': 'inet', 'subnets': [{'type': 'dhcp4'}, {'type': 'dhcp6'}] } - # insert params into link file - link_file = default_link_file.format(name=target_name, mac=mac) - syslink_name = "/etc/systemd/network/50-cloud-init-{}.link".format( - target_name) - return (ns, link_file, syslink_name) + return ns def render_persistent_net(network_state): -- cgit v1.2.3 From 7eccb0f0f3693662b3f288e7a74cb5bd6d7814ea Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 02:02:22 -0500 Subject: In generate_fallback_config return full netconfig dict with 'config' and 'version' keys --- cloudinit/distros/debian.py | 5 +++-- cloudinit/net/__init__.py | 11 +++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'cloudinit/net') diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py index 0fa47274..de8c4c6c 100644 --- a/cloudinit/distros/debian.py +++ b/cloudinit/distros/debian.py @@ -85,8 +85,9 @@ class Distro(distros.Distro): def _write_network_fallback(self): # old fallback configuration is obsolete, disable it util.disable_conf_file('/etc/network/interfaces.d/eth0.cfg') - ns = net.generate_fallback_config() - if ns is not None: + nconf = net.generate_fallback_config() + if nconf is not None: + ns = nconf['config'] net.render_network_state(network_state=ns, target="/") return [] diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 48b82a2c..389c2afb 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -287,8 +287,11 @@ def generate_fallback_config(): """Determine which attached net dev is most likely to have a connection and generate network state to run dhcp on that interface""" # by default use eth0 as primary interface - ns = {'interfaces': {}, 'dns': {'search': [], 'nameservers': []}, - 'routes': []} + nconf = {'config': {'interfaces': {}, + 'dns': {'search': [], 'nameservers': []}, 'routes': [] + }, + 'version': 1 + } # get list of interfaces that could have connections invalid_interfaces = set(['lo']) @@ -351,13 +354,13 @@ def generate_fallback_config(): target_name = name # generate net config for interface - ns['interfaces'][target_name] = { + nconf['config']['interfaces'][target_name] = { 'mac_address': mac, 'name': target_name, 'type': 'physical', 'mode': 'manual', 'inet': 'inet', 'subnets': [{'type': 'dhcp4'}, {'type': 'dhcp6'}] } - return ns + return nconf def render_persistent_net(network_state): -- cgit v1.2.3 From 3a3f960d5cfee60766e7de9e1fced537cac72106 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 02:20:06 -0500 Subject: In generate_fallback_config() fix function to sort potential interfaces to work on interfaces with characters between their numbers --- cloudinit/net/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cloudinit/net') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 389c2afb..36f07a02 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -346,7 +346,7 @@ def generate_fallback_config(): name = DEFAULT_PRIMARY_INTERFACE else: potential_interfaces.sort( - key=lambda x: int(x.strip(string.ascii_letters))) + key=lambda x: int(''.join(i for i in x if i in string.digits))) name = potential_interfaces[0] sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') -- cgit v1.2.3 From 9c0a2abc8d2c0e390745ddb163f5eae07b20d61d Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Tue, 22 Mar 2016 03:50:28 -0400 Subject: add code to invoke networking config there is no data source that has a populated network_config() so at this point this doesn't do anything. --- bin/cloud-init | 4 ++++ cloudinit/distros/__init__.py | 2 +- cloudinit/net/__init__.py | 17 +++++++++++++++++ cloudinit/sources/__init__.py | 4 ++++ cloudinit/stages.py | 24 ++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) (limited to 'cloudinit/net') diff --git a/bin/cloud-init b/bin/cloud-init index 63aa765b..8875d2f6 100755 --- a/bin/cloud-init +++ b/bin/cloud-init @@ -263,6 +263,10 @@ def main_init(name, args): return (None, []) else: return (None, ["No instance datasource found."]) + + if args.local: + init.apply_network_config() + # Stage 6 iid = init.instancify() LOG.debug("%s will now be targeting instance id: %s", name, iid) diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 74b484a7..418421b9 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -135,7 +135,7 @@ class Distro(object): return self._bring_up_interfaces(dev_names) return False - def apply_network_config(self, netconfig, bring_up=True): + def apply_network_config(self, netconfig, bring_up=False): # Write it out dev_names = self._write_network_config(netconfig) # Now try to bring them up diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 3cf99604..799cb97e 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -434,4 +434,21 @@ def render_network_state(target, network_state): with open(netrules, 'w+') as f: f.write(render_persistent_net(network_state)) + +def is_disabled_cfg(cfg): + if not cfg or not isinstance(cfg, dict): + return False + return cfg.get('config') == "disabled" + + +def generate_fallback_config(): + # FIXME: add implementation here + return None + + +def read_kernel_cmdline_config(): + # FIXME: add implementation here + return None + + # vi: ts=4 expandtab syntax=python diff --git a/cloudinit/sources/__init__.py b/cloudinit/sources/__init__.py index 28540a7b..c63464b2 100644 --- a/cloudinit/sources/__init__.py +++ b/cloudinit/sources/__init__.py @@ -221,6 +221,10 @@ class DataSource(object): # quickly (local check only) if self.instance_id is still return False + @property + def network_config(self): + return None + def normalize_pubkey_data(pubkey_data): keys = [] diff --git a/cloudinit/stages.py b/cloudinit/stages.py index c230ec0d..8e681e29 100644 --- a/cloudinit/stages.py +++ b/cloudinit/stages.py @@ -43,6 +43,7 @@ from cloudinit import distros from cloudinit import helpers from cloudinit import importer from cloudinit import log as logging +from cloudinit import net from cloudinit import sources from cloudinit import type_utils from cloudinit import util @@ -567,6 +568,29 @@ class Init(object): # Run the handlers self._do_handlers(user_data_msg, c_handlers_list, frequency) + def _find_networking_config(self): + cmdline_cfg = ('cmdline', net.read_kernel_cmdline_config()) + dscfg = ('ds', None) + if self.datasource and hasattr(self.datasource, 'network_config'): + dscfg = ('ds', self.datasource.network_config) + sys_cfg = ('system_cfg', self.cfg.get('network')) + + for loc, ncfg in (cmdline_cfg, dscfg, sys_cfg): + if net.is_disabled_cfg(ncfg): + LOG.debug("network config disabled by %s", loc) + return None + if ncfg: + return ncfg + return net.generate_fallback_config() + + def apply_network_config(self): + netcfg = self._find_networking_config() + if netcfg is None: + LOG.info("network config is disabled") + return + + return self.distro.apply_network_config(netcfg) + class Modules(object): def __init__(self, init, cfg_files=None, reporter=None): -- cgit v1.2.3 From ca00b0f1f8c8a40409328c595d44234bb61c24c4 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Tue, 22 Mar 2016 04:49:34 -0400 Subject: make NoCloud work for seeding network. Tested now with the generated fallback config in an lxc container. Had to change to return a config rather than a network state. Also this makes nocloud look in nocloud-net's seed dir. This way it will read the seed and clame the datasource but not do anything other than apply networking and the init_modules early. It is a change in behavior of the time that boothooks woudl run to do this. May need to change that back. --- cloudinit/net/__init__.py | 20 +++++--------------- cloudinit/sources/DataSourceNoCloud.py | 34 +++++++++++++++++++--------------- cloudinit/stages.py | 1 + 3 files changed, 25 insertions(+), 30 deletions(-) (limited to 'cloudinit/net') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index b45153f4..63fad2fa 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -448,11 +448,7 @@ def generate_fallback_config(): """Determine which attached net dev is most likely to have a connection and generate network state to run dhcp on that interface""" # by default use eth0 as primary interface - nconf = {'config': {'interfaces': {}, - 'dns': {'search': [], 'nameservers': []}, 'routes': [] - }, - 'version': 1 - } + nconf = {'config': [], 'version': 1} # get list of interfaces that could have connections invalid_interfaces = set(['lo']) @@ -506,21 +502,15 @@ def generate_fallback_config(): if DEFAULT_PRIMARY_INTERFACE in potential_interfaces: name = DEFAULT_PRIMARY_INTERFACE else: - potential_interfaces.sort( - key=lambda x: int(''.join(i for i in x if i in string.digits))) - name = potential_interfaces[0] + name = sorted(potential_interfaces)[0] sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') mac = util.load_file(sysfs_mac).strip() target_name = name - # generate net config for interface - nconf['config']['interfaces'][target_name] = { - 'mac_address': mac, 'name': target_name, 'type': 'physical', - 'mode': 'manual', 'inet': 'inet', - 'subnets': [{'type': 'dhcp4'}, {'type': 'dhcp6'}] - } - + nconf['config'].append( + {'type': 'physical', 'name': target_name, + 'mac_address': mac, 'subnets': [{'type': 'dhcp4'}]}) return nconf diff --git a/cloudinit/sources/DataSourceNoCloud.py b/cloudinit/sources/DataSourceNoCloud.py index 538df7d9..bd04a6fe 100644 --- a/cloudinit/sources/DataSourceNoCloud.py +++ b/cloudinit/sources/DataSourceNoCloud.py @@ -36,7 +36,9 @@ class DataSourceNoCloud(sources.DataSource): self.dsmode = 'local' self.seed = None self.cmdline_id = "ds=nocloud" - self.seed_dir = os.path.join(paths.seed_dir, 'nocloud') + self.seed_dirs = [os.path.join(paths.seed_dir, 'nocloud'), + os.path.join(paths.seed_dir, 'nocloud-net')] + self.seed_dir = None self.supported_seed_starts = ("/", "file://") def __str__(self): @@ -67,15 +69,15 @@ class DataSourceNoCloud(sources.DataSource): pp2d_kwargs = {'required': ['user-data', 'meta-data'], 'optional': ['vendor-data', 'network-config']} - try: - seeded = util.pathprefix2dict(self.seed_dir, **pp2d_kwargs) - found.append(self.seed_dir) - LOG.debug("Using seeded data from %s", self.seed_dir) - except ValueError as e: - pass - - if self.seed_dir in found: - mydata = _merge_new_seed(mydata, seeded) + for path in self.seed_dirs: + try: + seeded = util.pathprefix2dict(path, **pp2d_kwargs) + found.append(path) + LOG.debug("Using seeded data from %s", path) + mydata = _merge_new_seed(mydata, seeded) + break + except ValueError as e: + pass # If the datasource config had a 'seedfrom' entry, then that takes # precedence over a 'seedfrom' that was found in a filesystem @@ -188,21 +190,19 @@ class DataSourceNoCloud(sources.DataSource): # if this is the local datasource or 'seedfrom' was used # and the source of the seed was self.dsmode. # Then see if there is network config to apply. + # note this is obsolete network-interfaces style seeding. if self.dsmode in ("local", seeded_network): if mydata['meta-data'].get('network-interfaces'): LOG.debug("Updating network interfaces from %s", self) self.distro.apply_network( mydata['meta-data']['network-interfaces']) - elif mydata.get('network-config'): - LOG.debug("Updating network config from %s", self) - self.distro.apply_network_config(mydata['network-config'], - bring_up=False) if mydata['meta-data']['dsmode'] == self.dsmode: self.seed = ",".join(found) self.metadata = mydata['meta-data'] self.userdata_raw = mydata['user-data'] self.vendordata_raw = mydata['vendor-data'] + self._network_config = mydata['network-config'] return True LOG.debug("%s: not claiming datasource, dsmode=%s", self, @@ -222,6 +222,10 @@ class DataSourceNoCloud(sources.DataSource): return None return quick_id == current + @property + def network_config(self): + return self._network_config + def _quick_read_instance_id(cmdline_id, dirs=None): if dirs is None: @@ -312,7 +316,7 @@ class DataSourceNoCloudNet(DataSourceNoCloud): DataSourceNoCloud.__init__(self, sys_cfg, distro, paths) self.cmdline_id = "ds=nocloud-net" self.supported_seed_starts = ("http://", "https://", "ftp://") - self.seed_dir = os.path.join(paths.seed_dir, 'nocloud-net') + self.seed_dirs = [os.path.join(paths.seed_dir, 'nocloud-net')] self.dsmode = "net" diff --git a/cloudinit/stages.py b/cloudinit/stages.py index 8e681e29..73090025 100644 --- a/cloudinit/stages.py +++ b/cloudinit/stages.py @@ -589,6 +589,7 @@ class Init(object): LOG.info("network config is disabled") return + LOG.info("Applying configuration: %s", netcfg) return self.distro.apply_network_config(netcfg) -- cgit v1.2.3 From 7f0871dc5b141ff4bf601b6d96021eba8a3bb0ec Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Tue, 22 Mar 2016 05:40:05 -0400 Subject: write to 50-cloud-init.cfg and write systemd.link rules. --- cloudinit/distros/debian.py | 8 +++-- cloudinit/net/__init__.py | 48 ++++++++++++++++++++------ tests/unittests/test_distros/test_netconfig.py | 5 +-- 3 files changed, 46 insertions(+), 15 deletions(-) (limited to 'cloudinit/net') diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py index 909d6deb..b14fa3e2 100644 --- a/cloudinit/distros/debian.py +++ b/cloudinit/distros/debian.py @@ -46,7 +46,8 @@ APT_GET_WRAPPER = { class Distro(distros.Distro): hostname_conf_fn = "/etc/hostname" locale_conf_fn = "/etc/default/locale" - network_conf_fn = "/etc/network/interfaces" + network_conf_fn = "/etc/network/interfaces.d/50-cloud-init.cfg" + links_prefix = "/etc/systemd/network/50-cloud-init-" def __init__(self, name, cfg, paths): distros.Distro.__init__(self, name, cfg, paths) @@ -79,7 +80,10 @@ class Distro(distros.Distro): def _write_network_config(self, netconfig): ns = net.parse_net_config_data(netconfig) - net.render_network_state(network_state=ns, target="/") + net.render_network_state(target="/", network_state=ns, + eni=self.network_conf_fn, + links_prefix=self.links_prefix) + util.del_file("/etc/network/interfaces.d/eth0.cfg") return [] def _bring_up_interfaces(self, device_names): diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 63fad2fa..ae7b1c04 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -20,7 +20,6 @@ import errno import glob import os import re -import string from cloudinit import log as logging from cloudinit import util @@ -30,6 +29,7 @@ from . import network_state LOG = logging.getLogger(__name__) SYS_CLASS_NET = "/sys/class/net/" +LINKS_FNAME_PREFIX = "etc/systemd/network/50-cloud-init-" NET_CONFIG_OPTIONS = [ "address", "netmask", "broadcast", "network", "metric", "gateway", @@ -423,19 +423,45 @@ def render_interfaces(network_state): return content -def render_network_state(target, network_state): - eni = 'etc/network/interfaces' - netrules = 'etc/udev/rules.d/70-persistent-net.rules' +def render_network_state(target, network_state, eni="etc/network/interfaces", + links_prefix=LINKS_FNAME_PREFIX, + netrules='etc/udev/rules.d/70-persistent-net.rules'): - eni = os.path.sep.join((target, eni,)) - util.ensure_dir(os.path.dirname(eni)) - with open(eni, 'w+') as f: + fpeni = os.path.sep.join((target, eni,)) + util.ensure_dir(os.path.dirname(fpeni)) + with open(fpeni, 'w+') as f: f.write(render_interfaces(network_state)) - netrules = os.path.sep.join((target, netrules,)) - util.ensure_dir(os.path.dirname(netrules)) - with open(netrules, 'w+') as f: - f.write(render_persistent_net(network_state)) + if netrules: + netrules = os.path.sep.join((target, netrules,)) + util.ensure_dir(os.path.dirname(netrules)) + with open(netrules, 'w+') as f: + f.write(render_persistent_net(network_state)) + + if links_prefix: + render_systemd_links(target, network_state, links_prefix) + + +def render_systemd_links(target, network_state, + links_prefix=LINKS_FNAME_PREFIX): + fp_prefix = os.path.sep.join((target, links_prefix)) + for f in glob.glob(fp_prefix + "*"): + os.unlink(f) + + interfaces = network_state.get('interfaces') + for iface in interfaces.values(): + if (iface['type'] == 'physical' and 'name' in iface and + 'mac_address' in iface): + fname = fp_prefix + iface['name'] + ".link" + with open(fname, "w") as fp: + fp.write("\n".join([ + "[Match]", + "MACAddress=" + iface['mac_address'], + "", + "[Link]", + "Name=" + iface['name'], + "" + ])) def is_disabled_cfg(cfg): diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py index 6d30c5b8..2c2a424d 100644 --- a/tests/unittests/test_distros/test_netconfig.py +++ b/tests/unittests/test_distros/test_netconfig.py @@ -109,8 +109,9 @@ class TestNetCfgDistro(TestCase): ub_distro.apply_network(BASE_NET_CFG, False) self.assertEquals(len(write_bufs), 1) - self.assertIn('/etc/network/interfaces', write_bufs) - write_buf = write_bufs['/etc/network/interfaces'] + eni_name = '/etc/network/interfaces.d/50-cloud-init.cfg' + self.assertIn(eni_name, write_bufs) + write_buf = write_bufs[eni_name] self.assertEquals(str(write_buf).strip(), BASE_NET_CFG.strip()) self.assertEquals(write_buf.mode, 0o644) -- cgit v1.2.3