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"""
# get list of interfaces that could have connections
invalid_interfaces = set(['lo'])
potential_interfaces = set(get_devicelist())
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:
if interface.startswith("veth"):
continue
if os.path.exists(sys_dev_path(interface, "bridge")):
# skip any bridges
continue
carrier = read_sys_net_int(interface, 'carrier')
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
dormant = read_sys_net_int(interface, 'dormant')
if dormant:
possibly_connected.append(interface)
continue
operstate = read_sys_net_safe(interface, 'operstate')
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 eth0 exists use it above anything else, otherwise get the interface
# that we can read 'first' (using the sorted defintion of first).
names = list(sorted(potential_interfaces))
if DEFAULT_PRIMARY_INTERFACE in names:
names.remove(DEFAULT_PRIMARY_INTERFACE)
names.insert(0, DEFAULT_PRIMARY_INTERFACE)
target_name = None
target_mac = None
for name in names:
mac = read_sys_net_safe(name, 'address')
if mac:
target_name = name
target_mac = mac
break
if target_mac and target_name:
nconf = {'config': [], 'version': 1}
nconf['config'].append(
{'type': 'physical', 'name': target_name,
'mac_address': target_mac, 'subnets': [{'type': 'dhcp'}]})
return nconf
else:
# can't read any interfaces addresses (or there are none); give up
return None