diff options
author | Ryan Harper <ryan.harper@canonical.com> | 2018-01-22 18:33:08 -0600 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2018-02-08 16:12:32 -0500 |
commit | 0d30c9575f9e3e4cfb7771cee992e7f669ac3e76 (patch) | |
tree | 7770933abd35e335924812c0e031041c5f55a353 /cloudinit/net/__init__.py | |
parent | 45289a00bf8c043c5783c527c4ea720e67e0524b (diff) | |
download | vyos-cloud-init-0d30c9575f9e3e4cfb7771cee992e7f669ac3e76.tar.gz vyos-cloud-init-0d30c9575f9e3e4cfb7771cee992e7f669ac3e76.zip |
net: accept network-config in netplan format for renaming interfaces
net.apply_network_config_names currently only accepts network-config
in version 1 format. When users include a netplan format network-config the
rename code does not find any of the 'set-name' directives and does not rename
any of the interfaces. This causes some netplan configurations to fail.
This patch adds support for parsing netplan format and extracts the needed
information (macaddress and set-name values) to allow cloud-init to issue
interface rename commands. We know raise a RuntimeError if presented with
an unknown config format.
LP: #1709715
Diffstat (limited to 'cloudinit/net/__init__.py')
-rw-r--r-- | cloudinit/net/__init__.py | 63 |
1 files changed, 46 insertions, 17 deletions
diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index c015e793..f69c0ef2 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -274,23 +274,52 @@ def apply_network_config_names(netcfg, strict_present=True, strict_busy=True): renames are only attempted for interfaces of type 'physical'. It is expected that the network system will create other devices with the correct name in place.""" - renames = [] - for ent in netcfg.get('config', {}): - if ent.get('type') != 'physical': - continue - mac = ent.get('mac_address') - if not mac: - continue - name = ent.get('name') - driver = ent.get('params', {}).get('driver') - device_id = ent.get('params', {}).get('device_id') - if not driver: - driver = device_driver(name) - if not device_id: - device_id = device_devid(name) - renames.append([mac, name, driver, device_id]) - - return _rename_interfaces(renames) + + def _version_1(netcfg): + renames = [] + for ent in netcfg.get('config', {}): + if ent.get('type') != 'physical': + continue + mac = ent.get('mac_address') + if not mac: + continue + name = ent.get('name') + driver = ent.get('params', {}).get('driver') + device_id = ent.get('params', {}).get('device_id') + if not driver: + driver = device_driver(name) + if not device_id: + device_id = device_devid(name) + renames.append([mac, name, driver, device_id]) + return renames + + def _version_2(netcfg): + renames = [] + for key, ent in netcfg.get('ethernets', {}).items(): + # only rename if configured to do so + name = ent.get('set-name') + if not name: + continue + # cloud-init requires macaddress for renaming + mac = ent.get('match', {}).get('macaddress') + if not mac: + continue + driver = ent.get('match', {}).get('driver') + device_id = ent.get('match', {}).get('device_id') + if not driver: + driver = device_driver(name) + if not device_id: + device_id = device_devid(name) + renames.append([mac, name, driver, device_id]) + return renames + + if netcfg.get('version') == 1: + return _rename_interfaces(_version_1(netcfg)) + elif netcfg.get('version') == 2: + return _rename_interfaces(_version_2(netcfg)) + + raise RuntimeError('Failed to apply network config names. Found bad' + ' network config version: %s' % netcfg.get('version')) def interface_has_own_mac(ifname, strict=False): |