예제 #1
0
    def get_dracut_arguments(self, iface, target_ip, hostname, ibft):
        """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
        :param ibft: the device should be configured from iBFT
        """
        log.debug("Getting dracut arguments for iface %s target %s (ibft==%s)",
                  iface, target_ip, ibft)
        dracut_args = []

        if not self.nm_available:
            log.debug(
                "Get dracut arguments: can't be obtained, no NetworkManager available."
            )
            return dracut_args

        if iface and 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

        connections = self.nm_client.get_connections()

        target_connections = []
        if ibft:
            target_connections = [
                con for con in connections if is_ibft_connection(con)
            ]
        else:
            for cfg in self._device_configurations.get_for_device(iface):
                uuid = cfg.connection_uuid
                if uuid:
                    connection = self.nm_client.get_connection_by_uuid(uuid)
                    if connection:
                        target_connections.append(connection)

        if target_connections:
            if len(target_connections) > 1:
                log.debug(
                    "Get dracut arguments: "
                    "multiple connections found for traget %s: %s, taking the first one",
                    [con.get_uuid() for con in target_connections], target_ip)
            connection = target_connections[0]
        else:
            log.error(
                "Get dracut arguments: can't find connection for target %s",
                target_ip)
            return dracut_args

        dracut_args = list(
            get_dracut_arguments_from_connection(self.nm_client, connection,
                                                 iface, target_ip, hostname,
                                                 ibft))
        return dracut_args
예제 #2
0
    def get_dracut_arguments_from_connection_test(
            self, is_s390, get_slaves_from_connections_mock,
            get_connections_available_for_iface):
        nm_client = Mock()

        CON_UUID = "44755f4c-ee12-45b4-ba5e-e10f83de51af"

        # ibft connection
        cons_attrs = [
            {
                "get_uuid.return_value": CON_UUID,
                "get_setting_wired.return_value": None,
            },
        ]
        con = self._get_mock_objects_from_attrs(cons_attrs)[0]
        self.assertSetEqual(
            get_dracut_arguments_from_connection(nm_client,
                                                 con,
                                                 "",
                                                 "10.34.39.2",
                                                 "my.host.name",
                                                 ibft=True),
            set(["rd.iscsi.ibft"]))

        # ibft connection on s390 with missing s390 options
        is_s390.return_value = True
        wired_setting_attrs = {
            "get_s390_nettype.return_value": "",
            "get_s390_subchannels.return_value": "",
            "get_s390_options.return_value": "",
        }
        wired_setting = self._get_mock_objects_from_attrs(
            [wired_setting_attrs])[0]

        cons_attrs = [
            {
                "get_uuid.return_value": CON_UUID,
                "get_setting_wired.return_value": wired_setting,
            },
        ]
        con = self._get_mock_objects_from_attrs(cons_attrs)[0]
        self.assertSetEqual(
            get_dracut_arguments_from_connection(nm_client,
                                                 con,
                                                 "",
                                                 "10.34.39.2",
                                                 "my.host.name",
                                                 ibft=True),
            set(["rd.iscsi.ibft"]))

        # ibft connection on s390 with s390 options
        is_s390.return_value = True
        wired_setting_attrs = {
            "get_s390_nettype.return_value": "qeth",
            "get_s390_subchannels.return_value": "0.0.0900,0.0.0901,0.0.0902",
            "get_s390_options.return_value": {
                "layer2": "1",
                "portname": "FOOBAR",
                "portno": "0"
            },
        }
        wired_setting = self._get_mock_objects_from_attrs(
            [wired_setting_attrs])[0]

        cons_attrs = [
            {
                "get_uuid.return_value": CON_UUID,
                "get_setting_wired.return_value": wired_setting,
            },
        ]
        con = self._get_mock_objects_from_attrs(cons_attrs)[0]
        self.assertSetEqual(
            get_dracut_arguments_from_connection(nm_client,
                                                 con,
                                                 "",
                                                 "10.34.39.2",
                                                 "my.host.name",
                                                 ibft=True),
            set([
                "rd.iscsi.ibft",
                "rd.znet=qeth,0.0.0900,0.0.0901,0.0.0902,layer2=1,portname=FOOBAR,portno=0"
            ]))

        # IPv4 config auto, IPv6 config auto, mac address specified
        is_s390.return_value = False
        ip4_config_attrs = {
            "get_method.return_value": NM.SETTING_IP4_CONFIG_METHOD_AUTO,
        }
        ip4_config = self._get_mock_objects_from_attrs([ip4_config_attrs])[0]
        ip6_config_attrs = {
            "get_method.return_value": NM.SETTING_IP6_CONFIG_METHOD_AUTO,
        }
        ip6_config = self._get_mock_objects_from_attrs([ip6_config_attrs])[0]
        wired_setting_attrs = {
            "get_mac_address.return_value": "11:11:11:11:11:AA",
        }
        wired_setting = self._get_mock_objects_from_attrs(
            [wired_setting_attrs])[0]
        cons_attrs = [
            {
                "get_uuid.return_value": CON_UUID,
                "get_setting_ip4_config.return_value": ip4_config,
                "get_setting_ip6_config.return_value": ip6_config,
                "get_setting_wired.return_value": wired_setting,
                "get_connection_type.return_value": "802-3-ethernet",
            },
        ]
        con = self._get_mock_objects_from_attrs(cons_attrs)[0]
        # IPv4 target
        self.assertSetEqual(
            get_dracut_arguments_from_connection(nm_client,
                                                 con,
                                                 "ens3",
                                                 "10.34.39.2",
                                                 "my.host.name",
                                                 ibft=False),
            set(["ip=ens3:dhcp", "ifname=ens3:11:11:11:11:11:aa"]))
        # IPv6 target
        self.assertSetEqual(
            get_dracut_arguments_from_connection(nm_client,
                                                 con,
                                                 "ens3",
                                                 "2001::cafe:beef",
                                                 "my.host.name",
                                                 ibft=False),
            set(["ip=ens3:auto6", "ifname=ens3:11:11:11:11:11:aa"]))

        # IPv4 config static, mac address not specified, s390
        is_s390.return_value = True
        address_attrs = {
            "get_address.return_value": "10.34.39.44",
            "get_prefix.return_value": 24,
        }
        address = self._get_mock_objects_from_attrs([address_attrs])[0]
        ip4_config_attrs = {
            "get_method.return_value": NM.SETTING_IP4_CONFIG_METHOD_MANUAL,
            "get_num_addresses.return_value": 1,
            "get_address.return_value": address,
            "get_gateway.return_value": "10.34.39.2",
        }
        ip4_config = self._get_mock_objects_from_attrs([ip4_config_attrs])[0]
        wired_setting_attrs = {
            "get_mac_address.return_value": None,
            "get_s390_nettype.return_value": "qeth",
            "get_s390_subchannels.return_value": "0.0.0900,0.0.0901,0.0.0902",
            "get_s390_options.return_value": {
                "layer2": "1",
                "portname": "FOOBAR",
                "portno": "0"
            },
        }
        wired_setting = self._get_mock_objects_from_attrs(
            [wired_setting_attrs])[0]
        cons_attrs = [
            {
                "get_uuid.return_value": CON_UUID,
                "get_setting_ip4_config.return_value": ip4_config,
                "get_setting_wired.return_value": wired_setting,
                "get_connection_type.return_value": "802-3-ethernet",
            },
        ]
        con = self._get_mock_objects_from_attrs(cons_attrs)[0]
        self.assertSetEqual(
            get_dracut_arguments_from_connection(nm_client,
                                                 con,
                                                 "ens4",
                                                 "10.40.49.4",
                                                 "my.host.name",
                                                 ibft=False),
            set([
                "ip=10.34.39.44::10.34.39.2:255.255.255.0:my.host.name:ens4:none",
                "rd.znet=qeth,0.0.0900,0.0.0901,0.0.0902,layer2=1,portname=FOOBAR,portno=0"
            ]))

        # IPv6 config dhcp
        is_s390.return_value = False
        ip6_config_attrs = {
            "get_method.return_value": NM.SETTING_IP6_CONFIG_METHOD_DHCP,
        }
        ip6_config = self._get_mock_objects_from_attrs([ip6_config_attrs])[0]
        wired_setting_attrs = {
            "get_mac_address.return_value": None,
        }
        wired_setting = self._get_mock_objects_from_attrs(
            [wired_setting_attrs])[0]
        cons_attrs = [
            {
                "get_uuid.return_value": CON_UUID,
                "get_setting_ip6_config.return_value": ip6_config,
                "get_setting_wired.return_value": wired_setting,
                "get_connection_type.return_value": "802-3-ethernet",
            },
        ]
        con = self._get_mock_objects_from_attrs(cons_attrs)[0]
        self.assertSetEqual(
            get_dracut_arguments_from_connection(nm_client,
                                                 con,
                                                 "ens3",
                                                 "2001::cafe:beef",
                                                 "my.host.name",
                                                 ibft=False),
            set(["ip=ens3:dhcp6"]))

        # IPv6 config manual
        is_s390.return_value = False
        address_attrs = {
            "get_address.return_value": "2001::5",
            "get_prefix.return_value": 64,
        }
        address = self._get_mock_objects_from_attrs([address_attrs])[0]
        ip6_config_attrs = {
            "get_method.return_value": NM.SETTING_IP6_CONFIG_METHOD_MANUAL,
            "get_num_addresses.return_value": 1,
            "get_address.return_value": address,
            "get_gateway.return_value": "2001::1",
        }
        ip6_config = self._get_mock_objects_from_attrs([ip6_config_attrs])[0]
        wired_setting_attrs = {
            "get_mac_address.return_value": None,
        }
        wired_setting = self._get_mock_objects_from_attrs(
            [wired_setting_attrs])[0]
        cons_attrs = [
            {
                "get_uuid.return_value": CON_UUID,
                "get_setting_ip6_config.return_value": ip6_config,
                "get_setting_wired.return_value": wired_setting,
                "get_connection_type.return_value": "802-3-ethernet",
            },
        ]
        con = self._get_mock_objects_from_attrs(cons_attrs)[0]
        self.assertSetEqual(
            get_dracut_arguments_from_connection(nm_client,
                                                 con,
                                                 "ens3",
                                                 "2001::cafe:beef",
                                                 "my.host.name",
                                                 ibft=False),
            set(["ip=[2001::5/64]::[2001::1]::my.host.name:ens3:none"]))

        # IPv4 config auto, team
        is_s390.return_value = False
        ip4_config_attrs = {
            "get_method.return_value": NM.SETTING_IP4_CONFIG_METHOD_AUTO,
        }
        ip4_config = self._get_mock_objects_from_attrs([ip4_config_attrs])[0]
        cons_attrs = [
            # team master
            {
                "get_uuid.return_value": CON_UUID,
                "get_setting_ip4_config.return_value": ip4_config,
                "get_setting_wired.return_value": None,
                "get_connection_type.return_value": "team",
            },
        ]
        con = self._get_mock_objects_from_attrs(cons_attrs)[0]
        get_slaves_from_connections_mock.return_value = set([
            ("ens7", "6a6b4586-1e4c-451f-87fa-09b059ceba3d"),
            ("ens8", "ac4a0747-d1ea-4119-903b-18f3adad9116"),
        ])
        # IPv4 target
        self.assertSetEqual(
            get_dracut_arguments_from_connection(nm_client,
                                                 con,
                                                 "team0",
                                                 "10.34.39.2",
                                                 "my.host.name",
                                                 ibft=False),
            set(["ip=team0:dhcp", "team=team0:ens7,ens8"]))

        # IPv4 config auto, vlan, s390, parent specified by interface name
        is_s390.return_value = True
        ip4_config_attrs = {
            "get_method.return_value": NM.SETTING_IP4_CONFIG_METHOD_AUTO,
        }
        ip4_config = self._get_mock_objects_from_attrs([ip4_config_attrs])[0]
        setting_vlan_attrs = {
            "get_parent.return_value": "ens11",
        }
        setting_vlan = self._get_mock_objects_from_attrs([setting_vlan_attrs
                                                          ])[0]
        cons_attrs = [
            {
                "get_uuid.return_value": CON_UUID,
                "get_setting_ip4_config.return_value": ip4_config,
                "get_setting_wired.return_value": None,
                "get_connection_type.return_value": "vlan",
                "get_setting_vlan.return_value": setting_vlan,
            },
        ]
        con = self._get_mock_objects_from_attrs(cons_attrs)[0]
        # Mock parent connection
        wired_setting_attrs = {
            "get_mac_address.return_value": None,
            "get_s390_nettype.return_value": "qeth",
            "get_s390_subchannels.return_value": "0.0.0900,0.0.0901,0.0.0902",
            "get_s390_options.return_value": {
                "layer2": "1",
                "portname": "FOOBAR",
                "portno": "0"
            },
        }
        wired_setting = self._get_mock_objects_from_attrs(
            [wired_setting_attrs])[0]
        parent_cons_attrs = [
            {
                "get_uuid.return_value": CON_UUID,
                "get_setting_wired.return_value": wired_setting,
                "get_connection_type.return_value": "802-3-ethernet",
            },
        ]
        parent_cons = self._get_mock_objects_from_attrs(parent_cons_attrs)
        get_connections_available_for_iface.return_value = parent_cons
        # IPv4 target
        self.assertSetEqual(
            get_dracut_arguments_from_connection(nm_client,
                                                 con,
                                                 "ens11.111",
                                                 "10.34.39.2",
                                                 "my.host.name",
                                                 ibft=False),
            set([
                "ip=ens11.111:dhcp", "vlan=ens11.111:ens11",
                "rd.znet=qeth,0.0.0900,0.0.0901,0.0.0902,layer2=1,portname=FOOBAR,portno=0"
            ]))

        # IPv4 config auto, vlan, parent specified by connection uuid
        VLAN_PARENT_UUID = "5e6ead30-d133-4c8c-ba59-818c5ced6a7c"
        is_s390.return_value = False
        ip4_config_attrs = {
            "get_method.return_value": NM.SETTING_IP4_CONFIG_METHOD_AUTO,
        }
        ip4_config = self._get_mock_objects_from_attrs([ip4_config_attrs])[0]
        setting_vlan_attrs = {
            "get_parent.return_value": VLAN_PARENT_UUID,
        }
        setting_vlan = self._get_mock_objects_from_attrs([setting_vlan_attrs
                                                          ])[0]
        cons_attrs = [
            {
                "get_uuid.return_value": CON_UUID,
                "get_setting_ip4_config.return_value": ip4_config,
                "get_setting_wired.return_value": None,
                "get_connection_type.return_value": "vlan",
                "get_setting_vlan.return_value": setting_vlan,
            },
        ]
        con = self._get_mock_objects_from_attrs(cons_attrs)[0]
        # Mock parent connection
        parent_cons_attrs = [
            {
                "get_interface_name.return_value": "ens12",
            },
        ]
        parent_con = self._get_mock_objects_from_attrs(parent_cons_attrs)[0]
        nm_client.get_connection_by_uuid.return_value = parent_con
        # IPv4 target
        self.assertSetEqual(
            get_dracut_arguments_from_connection(nm_client,
                                                 con,
                                                 "ens12.111",
                                                 "10.34.39.2",
                                                 "my.host.name",
                                                 ibft=False),
            set(["ip=ens12.111:dhcp", "vlan=ens12.111:ens12"]))

        # IPv4 config auto, vlan, parent specified by connection uuid, s390 (we
        # need the parent connection in s390 case, not only parent iface)
        is_s390.return_value = True
        ip4_config_attrs = {
            "get_method.return_value": NM.SETTING_IP4_CONFIG_METHOD_AUTO,
        }
        ip4_config = self._get_mock_objects_from_attrs([ip4_config_attrs])[0]
        setting_vlan_attrs = {
            "get_parent.return_value": "ens13",
        }
        setting_vlan = self._get_mock_objects_from_attrs([setting_vlan_attrs
                                                          ])[0]
        cons_attrs = [
            {
                "get_uuid.return_value": CON_UUID,
                "get_setting_ip4_config.return_value": ip4_config,
                "get_setting_wired.return_value": None,
                "get_connection_type.return_value": "vlan",
                "get_setting_vlan.return_value": setting_vlan,
            },
        ]
        con = self._get_mock_objects_from_attrs(cons_attrs)[0]
        # Mock parent connection
        wired_setting_attrs = {
            "get_mac_address.return_value": None,
            "get_s390_nettype.return_value": "qeth",
            "get_s390_subchannels.return_value": "0.0.0900,0.0.0901,0.0.0902",
            "get_s390_options.return_value": {
                "layer2": "1",
                "portname": "FOOBAR",
                "portno": "0"
            },
        }
        wired_setting = self._get_mock_objects_from_attrs(
            [wired_setting_attrs])[0]
        parent_cons_attrs = [
            {
                # On s390 with net.ifnames=0 the iface is identified by NAME, not DEVICE
                "get_interface_name.return_value": None,
                "get_id.return_value": "ens13",
                "get_setting_wired.return_value": wired_setting,
                "get_connection_type.return_value": "802-3-ethernet",
            },
        ]
        parent_cons = self._get_mock_objects_from_attrs(parent_cons_attrs)
        nm_client.get_connection_by_uuid.return_value = parent_con
        # IPv4 target
        self.assertSetEqual(
            get_dracut_arguments_from_connection(nm_client,
                                                 con,
                                                 "ens13.111",
                                                 "10.34.39.2",
                                                 "my.host.name",
                                                 ibft=False),
            set([
                "ip=ens13.111:dhcp", "vlan=ens13.111:ens13",
                "rd.znet=qeth,0.0.0900,0.0.0901,0.0.0902,layer2=1,portname=FOOBAR,portno=0"
            ]))