summary refs log tree commit diff
path: root/src/initrd
diff options
context:
space:
mode:
Diffstat (limited to 'src/initrd')
-rw-r--r--src/initrd/nm-initrd-generator.c2
-rw-r--r--src/initrd/nmi-cmdline-reader.c61
-rw-r--r--src/initrd/nmi-ibft-reader.c2
-rw-r--r--src/initrd/tests/meson.build1
-rw-r--r--src/initrd/tests/test-cmdline-reader.c48
-rw-r--r--src/initrd/tests/test-ibft-reader.c6
6 files changed, 98 insertions, 22 deletions
diff --git a/src/initrd/nm-initrd-generator.c b/src/initrd/nm-initrd-generator.c
index 4c1c4037..c916459d 100644
--- a/src/initrd/nm-initrd-generator.c
+++ b/src/initrd/nm-initrd-generator.c
@@ -66,7 +66,7 @@ output_conn (gpointer key, gpointer value, gpointer user_data)
 		if (!nm_utils_file_set_contents (full_filename, data, len, 0600, &error))
 			goto err_out;
 	} else
-		g_print ("\n*** Connection '%s' ***\n\n%s\n", basename, data);
+		g_print ("\n*** Connection '%s' ***\n\n%s", basename, data);
 
 	return;
 err_out:
diff --git a/src/initrd/nmi-cmdline-reader.c b/src/initrd/nmi-cmdline-reader.c
index b9c75c1b..ccdd1f29 100644
--- a/src/initrd/nmi-cmdline-reader.c
+++ b/src/initrd/nmi-cmdline-reader.c
@@ -32,6 +32,20 @@
 
 /*****************************************************************************/
 
+static gboolean
+_connection_matches_type (gpointer key, gpointer value, gpointer user_data)
+{
+	NMConnection *connection = value;
+	const char *type_name = user_data;
+	NMSettingConnection *s_con;
+
+	s_con = nm_connection_get_setting_connection (connection);
+	if (type_name == NULL)
+		return nm_setting_connection_get_master (s_con) == NULL;
+	else
+		return strcmp (nm_setting_connection_get_connection_type (s_con), type_name) == 0;
+}
+
 static NMConnection *
 get_conn (GHashTable *connections, const char *ifname, const char *type_name)
 {
@@ -49,7 +63,18 @@ get_conn (GHashTable *connections, const char *ifname, const char *type_name)
 		multi_connect = NM_CONNECTION_MULTI_CONNECT_MULTIPLE;
 	}
 
-	connection = g_hash_table_lookup (connections, (gpointer)basename);
+	connection = g_hash_table_lookup (connections, (gpointer) basename);
+	if (!connection && !ifname) {
+		/*
+		 * If ifname was not given, we'll match the connection by type.
+		 * If the type was not given either, then we're happy with any connection but slaves.
+		 * This is so that things like "bond=bond0:eth1,eth2 nameserver=1.3.3.7 end up
+		 * slapping the nameserver to the most reasonable connection (bond0).
+		 */
+		connection = g_hash_table_find (connections,
+		                                _connection_matches_type,
+		                                (gpointer) type_name);
+	}
 
 	if (connection) {
 		setting = (NMSetting *)nm_connection_get_setting_connection (connection);
@@ -270,11 +295,13 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument)
 	if (netmask && *netmask) {
 		NMIPAddr addr;
 
-		if (nm_utils_parse_inaddr_bin (AF_INET, netmask, NULL, &addr)) {
+		if (nm_utils_parse_inaddr_bin (AF_INET, netmask, NULL, &addr))
 			client_ip_prefix = nm_utils_ip4_netmask_to_prefix (addr.addr4);
-		} else {
-			_LOGW (LOGD_CORE, "Unrecognized address: %s", client_ip);
-		}
+		else
+			client_ip_prefix = _nm_utils_ascii_str_to_int64 (netmask, 10, 0, 32, -1);
+
+		if (client_ip_prefix == -1)
+			_LOGW (LOGD_CORE, "Invalid IP mask: %s", netmask);
 	}
 
 	/* Static IP configuration might be present. */
@@ -332,7 +359,7 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument)
 	if (g_strcmp0 (kind, "none") == 0 || (g_strcmp0 (kind, "off") == 0)) {
 		if (nm_setting_ip_config_get_num_addresses (s_ip6) == 0) {
 			g_object_set (s_ip6,
-			              NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE,
+			              NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_DISABLED,
 			              NULL);
 		}
 		if (nm_setting_ip_config_get_num_addresses (s_ip4) == 0) {
@@ -347,7 +374,7 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument)
 		              NULL);
 		if (nm_setting_ip_config_get_num_addresses (s_ip6) == 0) {
 			g_object_set (s_ip6,
-			              NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE,
+			              NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_DISABLED,
 			              NULL);
 		}
 	} else if (g_strcmp0 (kind, "dhcp6") == 0) {
@@ -398,7 +425,7 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument)
 	}
 
 	if (peer && *peer)
-		_LOGW (LOGD_CORE, "Ignoring peer: %s (not implemented)\b", peer);
+		_LOGW (LOGD_CORE, "Ignoring peer: %s (not implemented)\n", peer);
 
 	if (gateway_ip && *gateway_ip) {
 		int addr_family = guess_ip_address_family (gateway_ip);
@@ -656,7 +683,6 @@ parse_rd_peerdns (GHashTable *connections, char *argument)
 	              NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, auto_dns,
 	              NULL);
 
-
 	s_ip = nm_connection_get_setting_ip6_config (connection);
 	g_object_set (s_ip,
 	              NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, auto_dns,
@@ -678,6 +704,7 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, char **argv)
 	const char *tag;
 	char *argument;
 	gboolean ignore_bootif = FALSE;
+	gboolean neednet = FALSE;
 	char *bootif = NULL;
 	int i;
 
@@ -706,6 +733,8 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, char **argv)
 			parse_rd_peerdns (connections, argument);
 		else if (strcmp (tag, "rd.bootif") == 0)
 			ignore_bootif = !_nm_utils_ascii_str_to_bool (argument, TRUE);
+		else if (strcmp (tag, "rd.neednet") == 0)
+			neednet = _nm_utils_ascii_str_to_bool (argument, TRUE);
 		else if (strcasecmp (tag, "BOOTIF") == 0)
 			bootif = argument;
 	}
@@ -716,6 +745,16 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, char **argv)
 		NMConnection *connection;
 		NMSettingWired *s_wired;
 
+		if (   !nm_utils_hwaddr_valid (bootif, ETH_ALEN)
+		    && g_str_has_prefix (bootif, "01-")
+		    && nm_utils_hwaddr_valid (&bootif[3], ETH_ALEN)) {
+			/*
+			 * BOOTIF MAC address can be prefixed with a hardware type identifier.
+			 * "01" stays for "wired", no other are known.
+			 */
+			bootif += 3;
+		}
+
 		connection = get_conn (connections, NULL, NM_SETTING_WIRED_SETTING_NAME);
 
 		s_wired = nm_connection_get_setting_wired (connection);
@@ -723,6 +762,10 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, char **argv)
 		              NM_SETTING_WIRED_MAC_ADDRESS, bootif,
 		              NULL);
 	}
+	if (neednet && g_hash_table_size (connections) == 0) {
+		/* Make sure there's some connection. */
+		get_conn (connections, NULL, NM_SETTING_WIRED_SETTING_NAME);
+	}
 
 	g_hash_table_foreach (connections, _normalize_conn, NULL);
 
diff --git a/src/initrd/nmi-ibft-reader.c b/src/initrd/nmi-ibft-reader.c
index efac5307..2db38e2c 100644
--- a/src/initrd/nmi-ibft-reader.c
+++ b/src/initrd/nmi-ibft-reader.c
@@ -182,7 +182,7 @@ ip_setting_add_from_block (GHashTable *nic,
 	case AF_INET:
 		s_ip = s_ip4;
 		g_object_set (s_ip6, NM_SETTING_IP_CONFIG_METHOD,
-		              NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL);
+		              NM_SETTING_IP6_CONFIG_METHOD_DISABLED, NULL);
 		break;
 	case AF_INET6:
 		s_ip = s_ip6;
diff --git a/src/initrd/tests/meson.build b/src/initrd/tests/meson.build
index 0ef72fff..20cf6af2 100644
--- a/src/initrd/tests/meson.build
+++ b/src/initrd/tests/meson.build
@@ -18,6 +18,7 @@ foreach test_unit : test_units
   test(
     'initrd/' + test_unit,
     test_script,
+    timeout: default_test_timeout,
     args: test_args + [exe.full_path()],
   )
 endforeach
diff --git a/src/initrd/tests/test-cmdline-reader.c b/src/initrd/tests/test-cmdline-reader.c
index 1a87505a..9bae73a2 100644
--- a/src/initrd/tests/test-cmdline-reader.c
+++ b/src/initrd/tests/test-cmdline-reader.c
@@ -193,14 +193,16 @@ test_if_ip4_manual (void)
 	gs_unref_hashtable GHashTable *connections = NULL;
 	gs_strfreev char **argv = g_strdupv ((char *[]){
 		"ip=192.0.2.2::192.0.2.1:255.255.255.0:"
-		"hostname0.example.com:eth3::192.0.2.53", NULL });
+		"hostname0.example.com:eth3::192.0.2.53",
+		"ip=203.0.113.2::203.0.113.1:26:"
+		"hostname1.example.com:eth4", NULL });
 	NMConnection *connection;
 	NMSettingIPConfig *s_ip4;
 	NMIPAddress *ip_addr;
 
 	connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", argv);
 	g_assert (connections);
-	g_assert_cmpint (g_hash_table_size (connections), ==, 1);
+	g_assert_cmpint (g_hash_table_size (connections), ==, 2);
 
 	connection = g_hash_table_lookup (connections, "eth3");
 	g_assert (connection);
@@ -221,8 +223,26 @@ test_if_ip4_manual (void)
 	g_assert_cmpint (nm_ip_address_get_prefix (ip_addr), ==, 24);
 	g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "192.0.2.1");
 	g_assert_cmpstr (nm_setting_ip_config_get_dhcp_hostname (s_ip4), ==, "hostname0.example.com");
-}
 
+	connection = g_hash_table_lookup (connections, "eth4");
+	g_assert (connection);
+	nmtst_assert_connection_verifies_without_normalization (connection);
+	g_assert_cmpstr (nm_connection_get_id (connection), ==, "eth4");
+
+	s_ip4 = nm_connection_get_setting_ip4_config (connection);
+	g_assert (s_ip4);
+	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL);
+	g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip4));
+	g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 0);
+	g_assert_cmpint (nm_setting_ip_config_get_num_routes (s_ip4), ==, 0);
+	g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 1);
+	ip_addr = nm_setting_ip_config_get_address (s_ip4, 0);
+	g_assert (ip_addr);
+	g_assert_cmpstr (nm_ip_address_get_address (ip_addr), ==, "203.0.113.2");
+	g_assert_cmpint (nm_ip_address_get_prefix (ip_addr), ==, 26);
+	g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "203.0.113.1");
+	g_assert_cmpstr (nm_setting_ip_config_get_dhcp_hostname (s_ip4), ==, "hostname1.example.com");
+}
 
 static void
 test_if_ip6_manual (void)
@@ -266,8 +286,12 @@ static void
 test_multiple (void)
 {
 	gs_unref_hashtable GHashTable *connections = NULL;
-	gs_strfreev char **argv = g_strdupv ((char *[]){ "ip=192.0.2.2:::::eth0", "ip=[2001:db8::2]:::::eth0", NULL });
+	gs_strfreev char **argv = g_strdupv ((char *[]){ "ip=192.0.2.2:::::eth0",
+	                                                 "ip=[2001:db8::2]:::::eth0",
+	                                                 "BOOTIF=00:53:AB:cd:02:03",
+	                                                 NULL });
 	NMConnection *connection;
+	NMSettingWired *s_wired;
 	NMSettingIPConfig *s_ip4;
 	NMSettingIPConfig *s_ip6;
 	NMIPAddress *ip_addr;
@@ -281,6 +305,10 @@ test_multiple (void)
 	nmtst_assert_connection_verifies_without_normalization (connection);
 	g_assert_cmpstr (nm_connection_get_id (connection), ==, "eth0");
 
+	s_wired = nm_connection_get_setting_wired (connection);
+	g_assert (s_wired);
+	g_assert_cmpstr (nm_setting_wired_get_mac_address (s_wired), ==, "00:53:AB:CD:02:03");
+
 	s_ip4 = nm_connection_get_setting_ip4_config (connection);
 	g_assert (s_ip4);
 	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL);
@@ -306,7 +334,7 @@ test_some_more (void)
 	gs_unref_hashtable GHashTable *connections = NULL;
 	gs_strfreev char **argv = g_strdupv ((char *[]){ "bootdev=eth1", "hail", "nameserver=[2001:DB8:3::53]",
 	                                                 "satan", "nameserver=192.0.2.53", "worship",
-	                                                 "BOOTIF=00-53-AB-cd-02-03", "doom", "rd.peerdns=0",
+	                                                 "BOOTIF=01-00-53-AB-cd-02-03", "doom", "rd.peerdns=0",
 	                                                 "rd.route=[2001:DB8:3::/48]:[2001:DB8:2::1]:ens10",
 	                                                 NULL });
 	NMConnection *connection;
@@ -387,7 +415,7 @@ static void
 test_no_bootif (void)
 {
 	gs_unref_hashtable GHashTable *connections = NULL;
-	gs_strfreev char **argv = g_strdupv ((char *[]){ "BOOTIF=00-53-AB-cd-02-03", "rd.bootif=0", NULL });
+	gs_strfreev char **argv = g_strdupv ((char *[]){ "BOOTIF=01-00-53-AB-cd-02-03", "rd.bootif=0", NULL });
 
 	connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", argv);
 	g_assert (connections);
@@ -398,7 +426,10 @@ static void
 test_bond (void)
 {
 	gs_unref_hashtable GHashTable *connections = NULL;
-	gs_strfreev char **argv = g_strdupv ((char *[]){ "rd.route=192.0.2.53::bong0", "bond=bong0:eth0,eth1:mode=balance-rr", NULL });
+	gs_strfreev char **argv = g_strdupv ((char *[]){ "rd.route=192.0.2.53::bong0",
+	                                                 "bond=bong0:eth0,eth1:mode=balance-rr",
+	                                                 "nameserver=203.0.113.53",
+	                                                 NULL });
 	NMConnection *connection;
 	NMSettingConnection *s_con;
 	NMSettingIPConfig *s_ip4;
@@ -423,7 +454,8 @@ test_bond (void)
 	g_assert (s_ip4);
 	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO);
 	g_assert (!nm_setting_ip_config_get_ignore_auto_dns (s_ip4));
-	g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 0);
+	g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 1);
+	g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 0), ==, "203.0.113.53");
 	g_assert (!nm_setting_ip_config_get_gateway (s_ip4));
 	g_assert_cmpint (nm_setting_ip_config_get_num_routes (s_ip4), ==, 1);
 	ip_route = nm_setting_ip_config_get_route (s_ip4, 0);
diff --git a/src/initrd/tests/test-ibft-reader.c b/src/initrd/tests/test-ibft-reader.c
index 64362d18..a49d0abf 100644
--- a/src/initrd/tests/test-ibft-reader.c
+++ b/src/initrd/tests/test-ibft-reader.c
@@ -96,7 +96,7 @@ test_read_ibft_dhcp (void)
 
 	s_ip6 = nm_connection_get_setting_ip6_config (connection);
 	g_assert (s_ip6);
-	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE);
+	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_DISABLED);
 
 	g_object_unref (connection);
 }
@@ -152,7 +152,7 @@ test_read_ibft_static (void)
 
 	s_ip6 = nm_connection_get_setting_ip6_config (connection);
 	g_assert (s_ip6);
-	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE);
+	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_DISABLED);
 
 	g_object_unref (connection);
 }
@@ -263,7 +263,7 @@ test_read_ibft (void)
 
 	s_ip6 = nm_connection_get_setting_ip6_config (connection);
 	g_assert (nm_setting_ip_config_get_num_addresses (s_ip6) == 0);
-	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE);
+	g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_DISABLED);
 	g_object_unref (connection);
 }