Ejemplo n.º 1
0
    def get_dracut_arguments(self, 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 iface: network interface used to connect to the target
        :param target_ip: IP of the iSCSI target
        :param hostname: static hostname to be configured
        """
        dracut_args = []
        if not self.nm_available:
            log.debug("Dracut arguments can't be obtained, no NetworkManager available.")
            return dracut_args

        if iface not in (device.get_iface() for device in self.nm_client.get_devices()):
            log.error("get dracut arguments for %s: device not found", iface)
            return dracut_args

        ifcfg = get_ifcfg_file_of_device(self.nm_client, iface)
        if not ifcfg:
            log.error("get dracut arguments for %s: no ifcfg file found", iface)
            return dracut_args

        dracut_args = list(get_dracut_arguments_from_ifcfg(self.nm_client, ifcfg, iface, target_ip, hostname))
        return dracut_args
Ejemplo n.º 2
0
    def get_dracut_arguments(self, 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 iface: network interface used to connect to the target
        :param target_ip: IP of the iSCSI target
        :param hostname: static hostname to be configured
        """
        dracut_args = []
        if not self.nm_available:
            log.debug(
                "Dracut arguments can't be obtained, no NetworkManager available."
            )
            return dracut_args

        if iface not in (device.get_iface()
                         for device in self.nm_client.get_devices()):
            log.error("get dracut arguments for %s: device not found", iface)
            return dracut_args

        ifcfg = get_ifcfg_file_of_device(self.nm_client, iface)
        if not ifcfg:
            log.error("get dracut arguments for %s: no ifcfg file found",
                      iface)
            return dracut_args

        dracut_args = list(
            get_dracut_arguments_from_ifcfg(self.nm_client, ifcfg, iface,
                                            target_ip, hostname))
        return dracut_args
Ejemplo n.º 3
0
    def run(self):
        """Run the connections consolidation.

        :returns: names of devices of which the connections have been consolidated
        :rtype: list(str)
        """
        consolidated_devices = []

        if not self._nm_client:
            log.debug("%s: No NetworkManager available.", self.name)
            return consolidated_devices

        for device in self._nm_client.get_devices():
            cons = device.get_available_connections()
            number_of_connections = len(cons)
            iface = device.get_iface()

            if number_of_connections < 2:
                continue

            # Ignore devices which are slaves
            if any(con.get_setting_connection().get_master() for con in cons):
                log.debug("%s: %d for %s - it is OK, device is a slave",
                          self.name, number_of_connections, iface)
                continue

            ifcfg_file = get_ifcfg_file_of_device(self._nm_client, iface)
            if not ifcfg_file:
                log.debug("%s: %d for %s - no ifcfg file found", self.name,
                          number_of_connections, iface)
                con_for_iface = self._select_persistent_connection_for_iface(
                    iface, cons)
                if not con_for_iface:
                    log.debug(
                        "%s: %d for %s - no suitable connection for the interface found",
                        self.name, number_of_connections, iface)
                    continue
            else:
                # Handle only ifcfgs created from boot options in initramfs
                # (Kickstart based ifcfgs are handled when applying kickstart)
                if ifcfg_file.is_from_kickstart:
                    continue
                con_for_iface = ifcfg_file.uuid

            log.debug("%s: %d for %s - ensure active ifcfg connection",
                      self.name, number_of_connections, iface)

            ensure_active_connection_for_device(self._nm_client,
                                                con_for_iface.get_uuid(),
                                                iface,
                                                only_replace=True)

            consolidated_devices.append(iface)

        return consolidated_devices
Ejemplo n.º 4
0
    def run(self):
        """Run dumping of missing ifcfg files.

        :returns: names of devices for which ifcfg file was created
        :rtype: list(str)
        """
        new_ifcfgs = []

        if not self._nm_client:
            log.debug("%s: No NetworkManager available.", self.name)
            return new_ifcfgs

        for device in self._nm_client.get_devices():
            if device.get_device_type() not in supported_wired_device_types:
                continue

            iface = device.get_iface()
            if get_ifcfg_file_of_device(self._nm_client, iface):
                continue

            cons = device.get_available_connections()
            n_cons = len(cons)
            device_is_slave = any(con.get_setting_connection().get_master() for con in cons)

            if n_cons == 0:
                log.debug("%s: creating default connection for %s", self.name, iface)
                add_connection_from_ksdata(self._nm_client, self._default_network_data, iface, activate=False,
                                           ifname_option_values=self._ifname_option_values)
            elif n_cons == 1:
                if device_is_slave:
                    log.debug("%s: not creating default connection for slave device %s",
                              self.name, iface)
                    continue
                con = cons[0]
                log.debug("%s: dumping default autoconnection %s for %s",
                          self.name, con.get_uuid(), iface)
                s_con = con.get_setting_connection()
                s_con.set_property(NM.SETTING_CONNECTION_ID, iface)
                s_con.set_property(NM.SETTING_CONNECTION_INTERFACE_NAME, iface)
                if not bound_hwaddr_of_device(self._nm_client, iface, self._ifname_option_values):
                    s_wired = con.get_setting_wired()
                    s_wired.set_property(NM.SETTING_WIRED_MAC_ADDRESS, None)
                else:
                    log.debug("%s: iface %s bound to mac address by ifname boot option",
                              self.name, iface)
                con.commit_changes(True, None)
            elif n_cons > 1:
                if not device_is_slave:
                    log.warning("%s: %d non-slave connections found for device %s",
                                self.name, n_cons, iface)
                continue

            new_ifcfgs.append(iface)

        return new_ifcfgs
Ejemplo n.º 5
0
    def consolidate_initramfs_connections(self):
        """Ensure devices configured in initramfs have no more than one NM connection.

        In case of multiple connections for device having ifcfg configuration from
        boot options, the connection should correspond to the ifcfg file.
        NetworkManager can be generating additional in-memory connection in case it
        fails to match device configuration to the ifcfg (#1433891).  By
        reactivating the device with ifcfg connection the generated in-memory
        connection will be deleted by NM.

        Don't enforce on slave devices for which having multiple connections can be
        valid (slave connection, regular device connection).
        """
        consolidated_devices = []

        for device in self.nm_client.get_devices():
            cons = device.get_available_connections()
            number_of_connections = len(cons)
            iface = device.get_iface()

            if number_of_connections < 2:
                continue

            # Ignore devices which are slaves
            if any(con.get_setting_connection().get_master() for con in cons):
                log.debug(
                    "consolidate %d initramfs connections for %s: it is OK, device is a slave",
                    number_of_connections, iface)
                continue

            ifcfg_file = get_ifcfg_file_of_device(self.nm_client, iface)
            if not ifcfg_file:
                log.error(
                    "consolidate %d initramfs connections for %s: no ifcfg file",
                    number_of_connections, iface)
                continue
            else:
                # Handle only ifcfgs created from boot options in initramfs
                # (Kickstart based ifcfgs are handled when applying kickstart)
                if ifcfg_file.is_from_kickstart:
                    continue

            log.debug(
                "consolidate %d initramfs connections for %s: ensure active ifcfg connection",
                number_of_connections, iface)

            ensure_active_connection_for_device(self.nm_client,
                                                ifcfg_file.uuid,
                                                iface,
                                                only_replace=True)

            consolidated_devices.append(iface)

        return consolidated_devices
Ejemplo n.º 6
0
    def run(self):
        """Run the connections consolidation.

        :returns: names of devices of which the connections have been consolidated
        :rtype: list(str)
        """
        consolidated_devices = []

        if not self._nm_client:
            log.debug("%s: No NetworkManager available.", self.name)
            return consolidated_devices

        for device in self._nm_client.get_devices():
            cons = device.get_available_connections()
            number_of_connections = len(cons)
            iface = device.get_iface()

            if number_of_connections < 2:
                continue

            # Ignore devices which are slaves
            if any(con.get_setting_connection().get_master() for con in cons):
                log.debug("%s: %d for %s - it is OK, device is a slave",
                          self.name, number_of_connections, iface)
                continue

            ifcfg_file = get_ifcfg_file_of_device(self._nm_client, iface)
            if not ifcfg_file:
                log.error("%s: %d for %s - no ifcfg file found",
                          self.name, number_of_connections, iface)
                continue
            else:
                # Handle only ifcfgs created from boot options in initramfs
                # (Kickstart based ifcfgs are handled when applying kickstart)
                if ifcfg_file.is_from_kickstart:
                    continue

            log.debug("%s: %d for %s - ensure active ifcfg connection",
                      self.name, number_of_connections, iface)

            ensure_active_connection_for_device(self._nm_client, ifcfg_file.uuid, iface, only_replace=True)

            consolidated_devices.append(iface)

        return consolidated_devices
Ejemplo n.º 7
0
    def run(self):
        """Run the kickstart application.

        :returns: names of devices to which kickstart was applied
        :rtype: list(str)
        """
        applied_devices = []

        if not self._network_data:
            log.debug("%s: No kickstart data.", self.name)
            return applied_devices

        if not self._nm_client:
            log.debug("%s: No NetworkManager available.", self.name)
            return applied_devices

        for network_data in self._network_data:
            # Wireless is not supported
            if network_data.essid:
                log.info(
                    "%s: Wireless devices configuration is not supported.",
                    self.name)
                continue

            device_name = get_device_name_from_network_data(
                self._nm_client, network_data, self._supported_devices,
                self._bootif)
            if not device_name:
                log.warning("%s: --device %s not found", self.name,
                            network_data.device)
                continue

            ifcfg_file = get_ifcfg_file_of_device(self._nm_client, device_name)
            if ifcfg_file and ifcfg_file.is_from_kickstart:
                if network_data.activate:
                    if ensure_active_connection_for_device(
                            self._nm_client, ifcfg_file.uuid, device_name):
                        applied_devices.append(device_name)
                continue

            # If there is no kickstart ifcfg from initramfs the command was added
            # in %pre section after switch root, so apply it now
            applied_devices.append(device_name)
            if ifcfg_file:
                # if the device was already configured in initramfs update the settings
                con_uuid = ifcfg_file.uuid
                log.debug(
                    "%s: pre kickstart - updating settings %s of device %s",
                    self.name, con_uuid, device_name)
                connection = self._nm_client.get_connection_by_uuid(con_uuid)
                update_connection_from_ksdata(self._nm_client,
                                              connection,
                                              network_data,
                                              device_name=device_name)
                if network_data.activate:
                    device = self._nm_client.get_device_by_iface(device_name)
                    self._nm_client.activate_connection_async(
                        connection, device, None, None)
                    log.debug(
                        "%s: pre kickstart - activating connection %s with device %s",
                        self.name, con_uuid, device_name)
            else:
                log.debug("%s: pre kickstart - adding connection for %s",
                          self.name, device_name)
                add_connection_from_ksdata(
                    self._nm_client,
                    network_data,
                    device_name,
                    activate=network_data.activate,
                    ifname_option_values=self._ifname_option_values)

        return applied_devices
Ejemplo n.º 8
0
    def run(self):
        """Run dumping of missing ifcfg files.

        :returns: names of devices for which ifcfg file was created
        :rtype: list(str)
        """
        new_ifcfgs = []

        if not self._nm_client:
            log.debug("%s: No NetworkManager available.", self.name)
            return new_ifcfgs

        for device in self._nm_client.get_devices():
            if device.get_device_type() not in supported_wired_device_types:
                continue

            iface = device.get_iface()
            if get_ifcfg_file_of_device(self._nm_client, iface):
                continue

            cons = device.get_available_connections()
            n_cons = len(cons)
            device_is_slave = any(con.get_setting_connection().get_master()
                                  for con in cons)

            if n_cons == 0:
                log.debug("%s: creating default connection for %s", self.name,
                          iface)
                add_connection_from_ksdata(
                    self._nm_client,
                    self._default_network_data,
                    iface,
                    activate=False,
                    ifname_option_values=self._ifname_option_values)
            elif n_cons == 1:
                if device_is_slave:
                    log.debug(
                        "%s: not creating default connection for slave device %s",
                        self.name, iface)
                    continue
                con = cons[0]
                self._update_connection(con, iface)
                log.debug("%s: dumping connection %s to ifcfg file for %s",
                          self.name, con.get_uuid(), iface)
                con.commit_changes(True, None)
            elif n_cons > 1:
                if not device_is_slave:
                    log.debug(
                        "%s: %d non-slave connections found for device %s",
                        self.name, n_cons, iface)
                    con = self._select_persistent_connection_for_device(
                        device, cons)
                    if not con:
                        log.warning(
                            "%s: none of the connections %s can be dumped as persistent",
                            self.name, [con.get_uuid() for con in cons])
                        continue
                    self._update_connection(con, iface)
                    log.debug(
                        "%s: dumping selected connection %s to ifcfg file for %s",
                        self.name, con.get_uuid(), iface)
                    con.commit_changes(True, None)

            new_ifcfgs.append(iface)

        return new_ifcfgs
Ejemplo n.º 9
0
    def run(self):
        """Run dumping of missing ifcfg files.

        :returns: names of devices for which ifcfg file was created
        :rtype: list(str)
        """
        new_ifcfgs = []

        if not self._nm_client:
            log.debug("%s: No NetworkManager available.", self.name)
            return new_ifcfgs

        dumped_device_types = supported_wired_device_types + virtual_device_types
        for device in self._nm_client.get_devices():
            if device.get_device_type() not in dumped_device_types:
                continue

            iface = device.get_iface()
            if get_ifcfg_file_of_device(self._nm_client, iface):
                continue

            cons = device.get_available_connections()
            log.debug("%s: %s connections found for device %s", self.name,
                      [con.get_uuid() for con in cons], iface)
            n_cons = len(cons)

            device_is_slave = any(con.get_setting_connection().get_master()
                                  for con in cons)
            if device_is_slave:
                # We have to dump persistent ifcfg files for slaves created in initramfs
                if n_cons == 1 and self._is_initramfs_connection(
                        cons[0], iface):
                    log.debug(
                        "%s: device %s has an initramfs slave connection",
                        self.name, iface)
                else:
                    log.debug(
                        "%s: not creating default connection for slave device %s",
                        self.name, iface)
                    continue

            # Devices activated in initramfs should have ONBOOT=yes
            has_initramfs_con = any(
                self._is_initramfs_connection(con, iface) for con in cons)
            if has_initramfs_con:
                log.debug("%s: device %s has initramfs connection", self.name,
                          iface)

            con = self._select_persistent_connection_for_device(device, cons)

            if not con:
                log.debug(
                    "%s: none of the connections can be dumped as persistent",
                    self.name)
                if n_cons == 1:
                    # TODO: Try to clone the persistent connection for the device
                    # from the connection which should be a generic (not bound
                    # to iface) connection created by NM in initramfs
                    pass
                elif n_cons > 1:
                    log.warning(
                        "%s: unexpected number of connections, not dumping any",
                        self.name)
                    continue

            if con:
                self._update_connection(con, iface)
                if has_initramfs_con:
                    update_connection_values(con, [
                        ("connection", NM.SETTING_CONNECTION_AUTOCONNECT, True)
                    ])
                log.debug("%s: dumping connection %s to ifcfg file for %s",
                          self.name, con.get_uuid(), iface)
                con.commit_changes(True, None)
            else:
                log.debug("%s: creating default connection for %s", self.name,
                          iface)
                network_data = copy.deepcopy(self._default_network_data)
                if has_initramfs_con:
                    network_data.onboot = True
                add_connection_from_ksdata(
                    self._nm_client,
                    network_data,
                    iface,
                    activate=False,
                    ifname_option_values=self._ifname_option_values)

            new_ifcfgs.append(iface)

        return new_ifcfgs
Ejemplo n.º 10
0
    def dump_missing_ifcfg_files(self):
        """Dump missing default ifcfg file for wired devices.

        Make sure each supported wired device has ifcfg file.

        For default auto connections created by NM upon start (which happens in
        case of missing ifcfg file, eg the file was not created in initramfs)
        rename the in-memory connection using device name and dump it into
        ifcfg file.

        If default auto connections are turned off by NM configuration (based
        on policy, eg on RHEL or server), the connection will be created by Anaconda
        and dumped into ifcfg file.

        The connection id (and consequently ifcfg file name) is set to device
        name.
        """
        if not self.nm_available:
            return []

        new_ifcfgs = []

        for device in self.nm_client.get_devices():
            if device.get_device_type() not in supported_wired_device_types:
                continue

            iface = device.get_iface()
            if get_ifcfg_file_of_device(self.nm_client, iface):
                continue

            cons = device.get_available_connections()
            n_cons = len(cons)
            device_is_slave = any(con.get_setting_connection().get_master()
                                  for con in cons)

            if n_cons == 0:
                data = self.get_kickstart_handler()
                default_data = data.NetworkData(onboot=False, ipv6="auto")
                log.debug(
                    "dump missing ifcfgs: creating default connection for %s",
                    iface)
                add_connection_from_ksdata(self.nm_client, default_data, iface)
            elif n_cons == 1:
                if device_is_slave:
                    log.debug(
                        "dump missing ifcfgs: not creating default connection for slave device %s",
                        iface)
                    continue
                con = cons[0]
                log.debug(
                    "dump missing ifcfgs: dumping default autoconnection %s for %s",
                    con.get_uuid(), iface)
                s_con = con.get_setting_connection()
                s_con.set_property(NM.SETTING_CONNECTION_ID, iface)
                s_con.set_property(NM.SETTING_CONNECTION_INTERFACE_NAME, iface)
                if not bound_hwaddr_of_device(self.nm_client, iface,
                                              self.ifname_option_values):
                    s_wired = con.get_setting_wired()
                    s_wired.set_property(NM.SETTING_WIRED_MAC_ADDRESS, None)
                else:
                    log.debug(
                        "dump missing ifcfgs: iface %s bound to mac address by ifname boot option"
                    )
                con.commit_changes(True, None)
            elif n_cons > 1:
                if not device_is_slave:
                    log.warning(
                        "dump missing ifcfgs: %d non-slave connections found for device %s",
                        n_cons, iface)
                continue

            new_ifcfgs.append(iface)

        return new_ifcfgs
Ejemplo n.º 11
0
    def apply_kickstart(self):
        """Apply kickstart configuration which has not already been applied.

        * Activate configurations created in initramfs if --activate is True.
        * Create configurations for %pre kickstart commands and activate eventually.

        :returns: list of devices to which kickstart configuration was applied
        """
        applied_devices = []

        if not self._original_network_data:
            log.debug("No kickstart data to apply.")
            return []

        for network_data in self._original_network_data:
            # Wireless is not supported
            if network_data.essid:
                log.info("Wireless devices configuration is not supported.")
                continue

            supported_devices = self.get_supported_devices()
            device_name = get_device_name_from_network_data(
                self.nm_client, network_data, supported_devices, self._bootif)
            if not device_name:
                log.warning("apply kickstart: --device %s not found",
                            network_data.device)
                continue

            ifcfg_file = get_ifcfg_file_of_device(self.nm_client, device_name)
            if ifcfg_file and ifcfg_file.is_from_kickstart:
                if network_data.activate:
                    if ensure_active_connection_for_device(
                            self.nm_client, ifcfg_file.uuid, device_name):
                        applied_devices.append(device_name)
                continue

            # If there is no kickstart ifcfg from initramfs the command was added
            # in %pre section after switch root, so apply it now
            applied_devices.append(device_name)
            if ifcfg_file:
                # if the device was already configured in initramfs update the settings
                con_uuid = ifcfg_file.uuid
                log.debug("pre kickstart - updating settings %s of device %s",
                          con_uuid, device_name)
                connection = self.nm_client.get_connection_by_uuid(con_uuid)
                update_connection_from_ksdata(self.nm_client,
                                              connection,
                                              network_data,
                                              device_name=device_name)
                if network_data.activate:
                    device = self.nm_client.get_device_by_iface(device_name)
                    self.nm_client.activate_connection_async(
                        connection, device, None, None)
                    log.debug(
                        "pre kickstart - activating connection %s with device %s",
                        con_uuid, device_name)
            else:
                log.debug("pre kickstart - adding connection for %s",
                          device_name)
                add_connection_from_ksdata(self.nm_client,
                                           network_data,
                                           device_name,
                                           activate=network_data.activate)

        return applied_devices
Ejemplo n.º 12
0
    def run(self):
        """Run dumping of missing ifcfg files.

        :returns: names of devices for which ifcfg file was created
        :rtype: list(str)
        """
        new_ifcfgs = []

        if not self._nm_client:
            log.debug("%s: No NetworkManager available.", self.name)
            return new_ifcfgs

        for device in self._nm_client.get_devices():
            if device.get_device_type() not in supported_wired_device_types:
                continue

            iface = device.get_iface()
            if get_ifcfg_file_of_device(self._nm_client, iface):
                continue

            cons = device.get_available_connections()
            n_cons = len(cons)
            device_is_slave = any(con.get_setting_connection().get_master()
                                  for con in cons)

            if n_cons == 0:
                log.debug("%s: creating default connection for %s", self.name,
                          iface)
                add_connection_from_ksdata(
                    self._nm_client,
                    self._default_network_data,
                    iface,
                    activate=False,
                    ifname_option_values=self._ifname_option_values)
            elif n_cons == 1:
                if device_is_slave:
                    log.debug(
                        "%s: not creating default connection for slave device %s",
                        self.name, iface)
                    continue
                con = cons[0]
                log.debug("%s: dumping default autoconnection %s for %s",
                          self.name, con.get_uuid(), iface)
                s_con = con.get_setting_connection()
                s_con.set_property(NM.SETTING_CONNECTION_ID, iface)
                s_con.set_property(NM.SETTING_CONNECTION_INTERFACE_NAME, iface)
                if not bound_hwaddr_of_device(self._nm_client, iface,
                                              self._ifname_option_values):
                    s_wired = con.get_setting_wired()
                    s_wired.set_property(NM.SETTING_WIRED_MAC_ADDRESS, None)
                else:
                    log.debug(
                        "%s: iface %s bound to mac address by ifname boot option",
                        self.name, iface)
                con.commit_changes(True, None)
            elif n_cons > 1:
                if not device_is_slave:
                    log.warning(
                        "%s: %d non-slave connections found for device %s",
                        self.name, n_cons, iface)
                continue

            new_ifcfgs.append(iface)

        return new_ifcfgs
Ejemplo n.º 13
0
    def run(self):
        """Run the kickstart application.

        :returns: names of devices to which kickstart was applied
        :rtype: list(str)
        """
        applied_devices = []

        if not self._network_data:
            log.debug("%s: No kickstart data.", self.name)
            return applied_devices

        if not self._nm_client:
            log.debug("%s: No NetworkManager available.", self.name)
            return applied_devices

        for network_data in self._network_data:
            # Wireless is not supported
            if network_data.essid:
                log.info("%s: Wireless devices configuration is not supported.", self.name)
                continue

            device_name = get_device_name_from_network_data(self._nm_client,
                                                            network_data,
                                                            self._supported_devices,
                                                            self._bootif)
            if not device_name:
                log.warning("%s: --device %s not found", self.name, network_data.device)
                continue

            ifcfg_file = get_ifcfg_file_of_device(self._nm_client, device_name)
            if ifcfg_file and ifcfg_file.is_from_kickstart:
                if network_data.activate:
                    if ensure_active_connection_for_device(self._nm_client, ifcfg_file.uuid,
                                                           device_name):
                        applied_devices.append(device_name)
                continue

            # If there is no kickstart ifcfg from initramfs the command was added
            # in %pre section after switch root, so apply it now
            applied_devices.append(device_name)
            if ifcfg_file:
                # if the device was already configured in initramfs update the settings
                con_uuid = ifcfg_file.uuid
                log.debug("%s: pre kickstart - updating settings %s of device %s",
                          self.name, con_uuid, device_name)
                connection = self._nm_client.get_connection_by_uuid(con_uuid)
                update_connection_from_ksdata(self._nm_client, connection, network_data,
                                              device_name=device_name)
                if network_data.activate:
                    device = self._nm_client.get_device_by_iface(device_name)
                    self._nm_client.activate_connection_async(connection, device, None, None)
                    log.debug("%s: pre kickstart - activating connection %s with device %s",
                              self.name, con_uuid, device_name)
            else:
                log.debug("%s: pre kickstart - adding connection for %s", self.name, device_name)
                add_connection_from_ksdata(self._nm_client, network_data, device_name,
                                           activate=network_data.activate,
                                           ifname_option_values=self._ifname_option_values)

        return applied_devices