def _get_dracut_znet_argument_from_connection(connection): """Get dracut znet (s390) configuration for given NM connection. :param connection: NetworkManager connection :type connection: NM.RemoteConnection :returns: dracut znet argument or "" if the configuration does not apply :rtype: str """ argument = "" wired_setting = connection.get_setting_wired() if wired_setting and is_s390(): nettype = wired_setting.get_s390_nettype() subchannels = wired_setting.get_s390_subchannels() if nettype and subchannels: argument = "rd.znet={},{}".format(nettype, subchannels) options = wired_setting.get_property(NM.SETTING_WIRED_S390_OPTIONS) if options: options_string = ','.join("{}={}".format(key, val) for key, val in options.items()) argument += ",{}".format(options_string) return argument
def create_connections_from_ksdata(nm_client, network_data, device_name, ifname_option_values=None): """Create NM connections from kickstart configuration. :param network_data: kickstart configuration :type network_data: pykickstart NetworkData :param device_name: name of the device to be configured by kickstart :type device_name: str :param ifname_option_values: list of ifname boot option values :type ifname_option_values: list(str) :return: list of tuples (CONNECTION, NAME_OF_DEVICE_TO_BE_ACTIVATED) :rtype: list((NM.RemoteConnection, str)) """ ifname_option_values = ifname_option_values or [] connections = [] device_to_activate = device_name con_uuid = NM.utils_uuid_generate() con = NM.SimpleConnection.new() update_connection_ip_settings_from_ksdata(con, network_data) s_con = NM.SettingConnection.new() s_con.props.uuid = con_uuid s_con.props.id = device_name s_con.props.interface_name = device_name s_con.props.autoconnect = network_data.onboot con.add_setting(s_con) # type "bond" if network_data.bondslaves: _update_bond_connection_from_ksdata(con, network_data) for i, slave in enumerate(network_data.bondslaves.split(","), 1): slave_con = create_slave_connection('bond', i, slave, device_name) bind_connection(nm_client, slave_con, network_data.bindto, slave) connections.append((slave_con, slave)) # type "team" elif network_data.teamslaves: _update_team_connection_from_ksdata(con, network_data) for i, (slave, cfg) in enumerate(network_data.teamslaves, 1): s_team_port = NM.SettingTeamPort.new() s_team_port.props.config = cfg slave_con = create_slave_connection('team', i, slave, device_name, settings=[s_team_port]) bind_connection(nm_client, slave_con, network_data.bindto, slave) connections.append((slave_con, slave)) # type "vlan" elif network_data.vlanid: device_to_activate = _update_vlan_connection_from_ksdata( con, network_data) # type "bridge" elif network_data.bridgeslaves: # bridge connection is autoactivated _update_bridge_connection_from_ksdata(con, network_data) for i, slave in enumerate(network_data.bridgeslaves.split(","), 1): slave_con = create_slave_connection('bridge', i, slave, device_name) bind_connection(nm_client, slave_con, network_data.bindto, slave) connections.append((slave_con, slave)) # type "infiniband" elif is_infiniband_device(nm_client, device_name): _update_infiniband_connection_from_ksdata(con, network_data) # type "802-3-ethernet" else: bound_mac = bound_hwaddr_of_device(nm_client, device_name, ifname_option_values) _update_ethernet_connection_from_ksdata(con, network_data, bound_mac) if bound_mac: log.debug("add connection: mac %s is bound to name %s", bound_mac, device_name) else: bind_connection(nm_client, con, network_data.bindto, device_name) # Add s390 settings if is_s390(): s390cfg = get_s390_settings(device_name) _update_wired_connection_with_s390_settings(con, s390cfg) connections.insert(0, (con, device_to_activate)) return connections
def get_ifcfg_file_of_device(nm_client, device_name, device_hwaddr=None, root_path=""): """Get ifcfg file for the device specified by name. :param nm_client: instance of NetworkManager client :type nm_client: NM.Client :param device_name: name of the device :type device_name: str :param device_hwaddr: hardware address of the device :type device_hwaddr: str :param root_path: search in the filesystem specified by root path :type root_path: str :returns: ifcfg file object :rtype: IfcfgFile """ # hwaddr is supplementary (--bindto=mac) ifcfgs = [] for file_path in get_ifcfg_files_paths( os.path.normpath(root_path + IFCFG_DIR)): ifcfg = IfcfgFile(file_path) ifcfg.read() device_type = ifcfg.get("TYPE") or ifcfg.get("DEVICETYPE") if device_type == "Wireless": # TODO check ESSID against active ssid of the device pass elif device_type in ("Bond", "Team", "Bridge", "Infiniband"): if ifcfg.get("DEVICE") == device_name: ifcfgs.append(ifcfg) elif device_type == "Vlan": interface_name = ifcfg.get("DEVICE") if interface_name: if interface_name == device_name: ifcfgs.append(ifcfg) else: physdev = ifcfg.get("PHYSDEV") if len(physdev) == NM_CONNECTION_UUID_LENGTH: physdev = get_iface_from_connection(nm_client, physdev) vlanid = ifcfg.get("VLAN_ID") generated_dev_name = default_ks_vlan_interface_name( physdev, vlanid) if device_name == generated_dev_name: ifcfgs.append(ifcfg) elif device_type == "Ethernet": # Ignore slaves if ifcfg.get("MASTER") or ifcfg.get("TEAM_MASTER") or ifcfg.get( "BRIDGE"): continue device = ifcfg.get("DEVICE") hwaddr = ifcfg.get("HWADDR") if device: if device == device_name: ifcfgs.append(ifcfg) elif hwaddr: if device_hwaddr: if device_hwaddr.upper() == hwaddr.upper(): ifcfgs.append(ifcfg) else: iface = get_iface_from_hwaddr(nm_client, hwaddr) if iface == device_name: ifcfgs.append(ifcfg) elif is_s390(): # s390 setting generated in dracut with net.ifnames=0 # has neither DEVICE nor HWADDR (#1249750) if ifcfg.get("NAME") == device_name: ifcfgs.append(ifcfg) if len(ifcfgs) > 1: log.debug("Unexpected number of ifcfg files found for %s: %s", device_name, [ifcfg.path for ifcfg in ifcfgs]) if ifcfgs: return ifcfgs[0] else: log.debug("Ifcfg file for %s not found", device_name)
def get_dracut_arguments_from_ifcfg(nm_client, ifcfg, iface, target_ip, hostname): """Get dracut arguments for the iface and iSCSI target. The dracut arguments would activate the iface in initramfs so that the iSCSI target can be attached (for example to mount root filesystem). :param nm_client: instance of NetworkManager client :type nm_client: NM.Client :param ifcfg: ifcfg file object :type ifcfg: IfcfgFile :param iface: network interface used to connect to the target :type iface: str :param target_ip: IP of the iSCSI target :type target_ip: str :param hostname: static hostname to be configured :type hostname: str :returns: dracut arguments :rtype: set(str) """ netargs = set() if ifcfg.get('BOOTPROTO') == 'ibft': netargs.add("rd.iscsi.ibft") elif target_ip: if hostname is None: hostname = "" # if using ipv6 if ':' in target_ip: if ifcfg.get('DHCPV6C') == "yes": # XXX combination with autoconf not yet clear, # support for dhcpv6 is not yet implemented in NM/ifcfg-rh netargs.add("ip=%s:dhcp6" % iface) elif ifcfg.get('IPV6_AUTOCONF') == "yes": netargs.add("ip=%s:auto6" % iface) elif ifcfg.get('IPV6ADDR'): ipaddr = "[%s]" % ifcfg.get('IPV6ADDR') if ifcfg.get('IPV6_DEFAULTGW'): gateway = "[%s]" % ifcfg.get('IPV6_DEFAULTGW') else: gateway = "" netargs.add("ip=%s::%s::%s:%s:none" % (ipaddr, gateway, hostname, iface)) else: if util.lowerASCII(ifcfg.get('bootproto')) == 'dhcp': netargs.add("ip=%s:dhcp" % iface) else: cfgidx = '' if ifcfg.get('IPADDR0'): cfgidx = '0' if ifcfg.get('GATEWAY%s' % cfgidx): gateway = ifcfg.get('GATEWAY%s' % cfgidx) else: gateway = "" netmask = ifcfg.get('NETMASK%s' % cfgidx) prefix = ifcfg.get('PREFIX%s' % cfgidx) if not netmask and prefix: netmask = prefix2netmask(int(prefix)) ipaddr = ifcfg.get('IPADDR%s' % cfgidx) netargs.add("ip=%s::%s:%s:%s:%s:none" % (ipaddr, gateway, netmask, hostname, iface)) hwaddr = ifcfg.get("HWADDR") if hwaddr: netargs.add("ifname=%s:%s" % (iface, hwaddr.lower())) if ifcfg.get("TYPE") == "Team" or ifcfg.get("DEVICETYPE") == "Team": slaves = get_slaves_from_ifcfgs(nm_client, "TEAM_MASTER", [iface, ifcfg.get("UUID")]) netargs.add("team=%s:%s" % (iface, ",".join(s_iface for s_iface, _uuid in slaves))) if ifcfg.get("TYPE") == "Vlan": physdev_spec = ifcfg.get("PHYSDEV") physdev = None # physical device can be specified by connection uuid (eg from nm-c-e) if len(physdev_spec) == NM_CONNECTION_UUID_LENGTH: ifcfg = get_ifcfg_file([("UUID", physdev_spec)]) if ifcfg: # On s390 with net.ifnames=0 there is no DEVICE physdev = ifcfg.get("DEVICE") or ifcfg.get("NAME") else: ifcfg = get_ifcfg_file_of_device(nm_client, physdev_spec) if ifcfg: physdev = physdev_spec if physdev: netargs.add("vlan=%s:%s" % (iface, physdev)) else: log.error("can't find ifcfg of parent of vlan device %s specified by %s", iface, physdev_spec) return netargs # For vlan ifcfg now refers to the physical device file nettype = ifcfg.get("NETTYPE") subchannels = ifcfg.get("SUBCHANNELS") if is_s390() and nettype and subchannels: znet = "rd.znet=%s,%s" % (nettype, subchannels) options = ifcfg.get("OPTIONS").strip("'\"") if options: options = filter(lambda x: x != '', options.split(' ')) znet += ",%s" % (','.join(options)) netargs.add(znet) return netargs
def add_connection_from_ksdata(nm_client, network_data, device_name, activate=False, ifname_option_values=None): """Add NM connection created from kickstart configuration. :param network_data: kickstart configuration :type network_data: pykickstart NetworkData :param device_name: name of the device to be configured by kickstart :type device_name: str :param activate: activate the added connection :type activate: bool :param ifname_option_values: list of ifname boot option values :type ifname_option_values: list(str) """ ifname_option_values = ifname_option_values or [] added_connections = [] device_to_activate = device_name con_uuid = NM.utils_uuid_generate() con = NM.SimpleConnection.new() update_connection_ip_settings_from_ksdata(con, network_data) s_con = NM.SettingConnection.new() s_con.props.uuid = con_uuid s_con.props.id = device_name s_con.props.interface_name = device_name # HACK preventing NM to autoactivate the connection # The real network --onboot value (ifcfg ONBOOT) will be set later by # update_onboot s_con.props.autoconnect = False con.add_setting(s_con) # type "bond" if network_data.bondslaves: _update_bond_connection_from_ksdata(con, network_data) for i, slave in enumerate(network_data.bondslaves.split(","), 1): slave_con = create_slave_connection('bond', i, slave, device_name) bind_connection(nm_client, slave_con, network_data.bindto, slave) added_connections.append((slave_con, slave)) # type "team" elif network_data.teamslaves: _update_team_connection_from_ksdata(con, network_data) for i, (slave, cfg) in enumerate(network_data.teamslaves, 1): s_team_port = NM.SettingTeamPort.new() s_team_port.props.config = cfg slave_con = create_slave_connection('team', i, slave, device_name, settings=[s_team_port]) bind_connection(nm_client, slave_con, network_data.bindto, slave) added_connections.append((slave_con, slave)) # type "vlan" elif network_data.vlanid: device_to_activate = _update_vlan_connection_from_ksdata( con, network_data) # type "bridge" elif network_data.bridgeslaves: # bridge connection is autoactivated _update_bridge_connection_from_ksdata(con, network_data) for i, slave in enumerate(network_data.bridgeslaves.split(","), 1): slave_con = create_slave_connection('bridge', i, slave, device_name) bind_connection(nm_client, slave_con, network_data.bindto, slave) added_connections.append((slave_con, slave)) # type "infiniband" elif is_infiniband_device(nm_client, device_name): _update_infiniband_connection_from_ksdata(con, network_data) # type "802-3-ethernet" else: bound_mac = bound_hwaddr_of_device(nm_client, device_name, ifname_option_values) _update_ethernet_connection_from_ksdata(con, network_data, bound_mac) if bound_mac: log.debug("add connection: mac %s is bound to name %s", bound_mac, device_name) else: bind_connection(nm_client, con, network_data.bindto, device_name) # Add s390 settings if is_s390(): s390cfg = get_s390_settings(device_name) _update_wired_connection_with_s390_settings(con, s390cfg) added_connections.insert(0, (con, device_to_activate)) for con, device_name in added_connections: log.debug("add connection: %s for %s\n%s", con_uuid, device_name, con.to_dbus(NM.ConnectionSerializationFlags.NO_SECRETS)) device_to_activate = device_name if activate else None nm_client.add_connection_async(con, True, None, _connection_added_cb, device_to_activate) return added_connections
def get_dracut_arguments_from_ifcfg(nm_client, ifcfg, iface, target_ip, hostname): """Get dracut arguments for the iface and iSCSI target. The dracut arguments would activate the iface in initramfs so that the iSCSI target can be attached (for example to mount root filesystem). :param nm_client: instance of NetworkManager client :type nm_client: NM.Client :param ifcfg: ifcfg file object :type ifcfg: IfcfgFile :param iface: network interface used to connect to the target :type iface: str :param target_ip: IP of the iSCSI target :type target_ip: str :param hostname: static hostname to be configured :type hostname: str :returns: dracut arguments :rtype: set(str) """ netargs = set() if ifcfg.get('BOOTPROTO') == 'ibft': netargs.add("rd.iscsi.ibft") elif target_ip: if hostname is None: hostname = "" # if using ipv6 if ':' in target_ip: if ifcfg.get('DHCPV6C') == "yes": # XXX combination with autoconf not yet clear, # support for dhcpv6 is not yet implemented in NM/ifcfg-rh netargs.add("ip=%s:dhcp6" % iface) elif ifcfg.get('IPV6_AUTOCONF') == "yes": netargs.add("ip=%s:auto6" % iface) elif ifcfg.get('IPV6ADDR'): ipaddr = "[%s]" % ifcfg.get('IPV6ADDR') if ifcfg.get('IPV6_DEFAULTGW'): gateway = "[%s]" % ifcfg.get('IPV6_DEFAULTGW') else: gateway = "" netargs.add("ip=%s::%s::%s:%s:none" % (ipaddr, gateway, hostname, iface)) else: if util.lowerASCII(ifcfg.get('bootproto')) == 'dhcp': netargs.add("ip=%s:dhcp" % iface) else: cfgidx = '' if ifcfg.get('IPADDR0'): cfgidx = '0' if ifcfg.get('GATEWAY%s' % cfgidx): gateway = ifcfg.get('GATEWAY%s' % cfgidx) else: gateway = "" netmask = ifcfg.get('NETMASK%s' % cfgidx) prefix = ifcfg.get('PREFIX%s' % cfgidx) if not netmask and prefix: netmask = prefix2netmask(int(prefix)) ipaddr = ifcfg.get('IPADDR%s' % cfgidx) netargs.add("ip=%s::%s:%s:%s:%s:none" % (ipaddr, gateway, netmask, hostname, iface)) hwaddr = ifcfg.get("HWADDR") if hwaddr: netargs.add("ifname=%s:%s" % (iface, hwaddr.lower())) if ifcfg.get("TYPE") == "Team" or ifcfg.get("DEVICETYPE") == "Team": slaves = sorted(get_slaves_from_ifcfgs(nm_client, "TEAM_MASTER", [iface, ifcfg.get("UUID")])) netargs.add("team=%s:%s" % (iface, ",".join(s_iface for s_iface, _uuid in slaves))) if ifcfg.get("TYPE") == "Vlan": physdev_spec = ifcfg.get("PHYSDEV") physdev = None # physical device can be specified by connection uuid (eg from nm-c-e) if len(physdev_spec) == NM_CONNECTION_UUID_LENGTH: ifcfg = get_ifcfg_file([("UUID", physdev_spec)]) if ifcfg: # On s390 with net.ifnames=0 there is no DEVICE physdev = ifcfg.get("DEVICE") or ifcfg.get("NAME") else: ifcfg = get_ifcfg_file_of_device(nm_client, physdev_spec) if ifcfg: physdev = physdev_spec if physdev: netargs.add("vlan=%s:%s" % (iface, physdev)) else: log.error("can't find ifcfg of parent of vlan device %s specified by %s", iface, physdev_spec) return netargs # For vlan ifcfg now refers to the physical device file nettype = ifcfg.get("NETTYPE") subchannels = ifcfg.get("SUBCHANNELS") if is_s390() and nettype and subchannels: znet = "rd.znet=%s,%s" % (nettype, subchannels) options = ifcfg.get("OPTIONS").strip("'\"") if options: options = filter(lambda x: x != '', options.split(' ')) znet += ",%s" % (','.join(options)) netargs.add(znet) return netargs
def get_ifcfg_file_of_device(nm_client, device_name, device_hwaddr=None, root_path=""): """Get ifcfg file for the device specified by name. :param nm_client: instance of NetworkManager client :type nm_client: NM.Client :param device_name: name of the device :type device_name: str :param hwaddr: hardware address of the device :type hwaddr: str :param root_path: search in the filesystem specified by root path :type root_path: str :returns: ifcfg file object :rtype: IfcfgFile """ # hwaddr is supplementary (--bindto=mac) ifcfgs = [] for file_path in get_ifcfg_files_paths(os.path.normpath(root_path + IFCFG_DIR)): ifcfg = IfcfgFile(file_path) ifcfg.read() device_type = ifcfg.get("TYPE") or ifcfg.get("DEVICETYPE") if device_type == "Wireless": # TODO check ESSID against active ssid of the device pass elif device_type in ("Bond", "Team", "Bridge", "Infiniband"): if ifcfg.get("DEVICE") == device_name: ifcfgs.append(ifcfg) elif device_type == "Vlan": interface_name = ifcfg.get("DEVICE") if interface_name: if interface_name == device_name: ifcfgs.append(ifcfg) else: physdev = ifcfg.get("PHYSDEV") if len(physdev) == NM_CONNECTION_UUID_LENGTH: physdev = get_iface_from_connection(nm_client, physdev) vlanid = ifcfg.get("VLAN_ID") generated_dev_name = default_ks_vlan_interface_name(physdev, vlanid) if device_name == generated_dev_name: ifcfgs.append(ifcfg) elif device_type == "Ethernet": # Ignore slaves if ifcfg.get("MASTER") or ifcfg.get("TEAM_MASTER") or ifcfg.get("BRIDGE"): continue device = ifcfg.get("DEVICE") hwaddr = ifcfg.get("HWADDR") if device: if device == device_name: ifcfgs.append(ifcfg) elif hwaddr: if device_hwaddr: if device_hwaddr.upper() == hwaddr.upper(): ifcfgs.append(ifcfg) else: iface = get_iface_from_hwaddr(nm_client, hwaddr) if iface == device_name: ifcfgs.append(ifcfg) elif is_s390(): # s390 setting generated in dracut with net.ifnames=0 # has neither DEVICE nor HWADDR (#1249750) if ifcfg.get("NAME") == device_name: ifcfgs.append(ifcfg) if len(ifcfgs) > 1: log.debug("Unexpected number of ifcfg files found for %s: %s", device_name, [ifcfg.path for ifcfg in ifcfgs]) if ifcfgs: return ifcfgs[0] else: log.debug("Ifcfg file for %s not found", device_name)
def add_connection_from_ksdata(nm_client, network_data, device_name, activate=False, ifname_option_values=None): """Add NM connection created from kickstart configuration. :param network_data: kickstart configuration :type network_data: pykickstart NetworkData :param device_name: name of the device to be configured by kickstart :type device_name: str :param activate: activate the added connection :type activate: bool :param ifname_option_values: list of ifname boot option values :type ifname_option_values: list(str) """ ifname_option_values = ifname_option_values or [] added_connections = [] device_to_activate = device_name con_uuid = NM.utils_uuid_generate() con = NM.SimpleConnection.new() update_connection_ip_settings_from_ksdata(con, network_data) s_con = NM.SettingConnection.new() s_con.props.uuid = con_uuid s_con.props.id = device_name s_con.props.interface_name = device_name # HACK preventing NM to autoactivate the connection # The real network --onboot value (ifcfg ONBOOT) will be set later by # update_onboot s_con.props.autoconnect = False con.add_setting(s_con) # type "bond" if network_data.bondslaves: _update_bond_connection_from_ksdata(con, network_data) for i, slave in enumerate(network_data.bondslaves.split(","), 1): slave_con = create_slave_connection('bond', i, slave, device_name) bind_connection(nm_client, slave_con, network_data.bindto, slave) added_connections.append((slave_con, slave)) # type "team" elif network_data.teamslaves: _update_team_connection_from_ksdata(con, network_data) for i, (slave, cfg) in enumerate(network_data.teamslaves, 1): s_team_port = NM.SettingTeamPort.new() s_team_port.props.config = cfg slave_con = create_slave_connection('team', i, slave, device_name, settings=[s_team_port]) bind_connection(nm_client, slave_con, network_data.bindto, slave) added_connections.append((slave_con, slave)) # type "vlan" elif network_data.vlanid: device_to_activate = _update_vlan_connection_from_ksdata(con, network_data) # type "bridge" elif network_data.bridgeslaves: # bridge connection is autoactivated _update_bridge_connection_from_ksdata(con, network_data) for i, slave in enumerate(network_data.bridgeslaves.split(","), 1): slave_con = create_slave_connection('bridge', i, slave, device_name) bind_connection(nm_client, slave_con, network_data.bindto, slave) added_connections.append((slave_con, slave)) # type "infiniband" elif is_infiniband_device(nm_client, device_name): _update_infiniband_connection_from_ksdata(con, network_data) # type "802-3-ethernet" else: bound_mac = bound_hwaddr_of_device(nm_client, device_name, ifname_option_values) _update_ethernet_connection_from_ksdata(con, network_data, bound_mac) if bound_mac: log.debug("add connection: mac %s is bound to name %s", bound_mac, device_name) else: bind_connection(nm_client, con, network_data.bindto, device_name) # Add s390 settings if is_s390(): s390cfg = get_s390_settings(device_name) _update_wired_connection_with_s390_settings(con, s390cfg) added_connections.insert(0, (con, device_to_activate)) for con, device_name in added_connections: log.debug("add connection: %s for %s\n%s", con_uuid, device_name, con.to_dbus(NM.ConnectionSerializationFlags.NO_SECRETS)) device_to_activate = device_name if activate else None nm_client.add_connection_async(con, True, None, _connection_added_cb, device_to_activate) return added_connections