about summary refs log tree commit diff
path: root/src/initrd/nm-initrd-generator.c
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2020-06-28 18:58:14 +0200
committerMichael Biebl <biebl@debian.org>2020-06-28 18:58:14 +0200
commita54ac63bbf9b2c71026ac9028a8ffaf186cf3c82 (patch)
tree6a32883bd916c4096357b35298beff6db8bcd0b5 /src/initrd/nm-initrd-generator.c
parent45e8e1149027529194982212c804c0468aa01d98 (diff)
New upstream version 1.25.90 upstream/1.25.90
Diffstat (limited to 'src/initrd/nm-initrd-generator.c')
-rw-r--r--src/initrd/nm-initrd-generator.c42
1 files changed, 35 insertions, 7 deletions
diff --git a/src/initrd/nm-initrd-generator.c b/src/initrd/nm-initrd-generator.c
index dccb21f8..ac81dde0 100644
--- a/src/initrd/nm-initrd-generator.c
+++ b/src/initrd/nm-initrd-generator.c
@@ -33,7 +33,7 @@ output_conn (gpointer key, gpointer value, gpointer user_data)
 	if (!nm_connection_normalize (connection, NULL, NULL, &error))
 		goto err_out;
 
-	file = nm_keyfile_write (connection, NULL, NULL, &error);
+	file = nm_keyfile_write (connection, NM_KEYFILE_HANDLER_FLAGS_NONE, NULL, NULL, &error);
 	if (file == NULL)
 		goto err_out;
 
@@ -64,24 +64,28 @@ err_out:
 }
 
 #define DEFAULT_SYSFS_DIR        "/sys"
+#define DEFAULT_INITRD_DATA_DIR  NMRUNDIR "/initrd"
 
 int
 main (int argc, char *argv[])
 {
 	GHashTable *connections;
 	gs_free char *connections_dir = NULL;
+	gs_free char *initrd_dir = NULL;
 	gs_free char *sysfs_dir = NULL;
 	gboolean dump_to_stdout = FALSE;
 	gs_strfreev char **remaining = NULL;
 	GOptionEntry option_entries[] = {
-		{ "connections-dir", 'c', 0, G_OPTION_ARG_FILENAME, &connections_dir, "Output connection directory", NM_KEYFILE_PATH_NAME_RUN },
-		{ "sysfs-dir", 'd', 0, G_OPTION_ARG_FILENAME, &sysfs_dir, "The sysfs mount point", DEFAULT_SYSFS_DIR },
-		{ "stdout", 's', 0, G_OPTION_ARG_NONE, &dump_to_stdout, "Dump connections to standard output", NULL },
-		{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &remaining, NULL, NULL },
+		{ "connections-dir",  'c',  0, G_OPTION_ARG_FILENAME,     &connections_dir, "Output connection directory",         NM_KEYFILE_PATH_NAME_RUN },
+		{ "initrd-data-dir",  'i',  0, G_OPTION_ARG_FILENAME,     &initrd_dir,      "Output initrd data directory",        DEFAULT_INITRD_DATA_DIR },
+		{ "sysfs-dir",        'd',  0, G_OPTION_ARG_FILENAME,     &sysfs_dir,       "The sysfs mount point",               DEFAULT_SYSFS_DIR },
+		{ "stdout",           's',  0, G_OPTION_ARG_NONE,         &dump_to_stdout,  "Dump connections to standard output", NULL },
+		{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &remaining,       NULL,                                  NULL },
 		{ NULL }
 	};
 	GOptionContext *option_context;
-	GError *error = NULL;
+	gs_free_error GError *error = NULL;
+	gs_free char *hostname = NULL;
 	int errsv;
 
 	option_context = g_option_context_new ("-- [ip=...] [rd.route=...] [bridge=...] [bond=...] [team=...] [vlan=...] "
@@ -108,6 +112,8 @@ main (int argc, char *argv[])
 		connections_dir = g_strdup (NM_KEYFILE_PATH_NAME_RUN);
 	if (!sysfs_dir)
 		sysfs_dir = g_strdup (DEFAULT_SYSFS_DIR);
+	if (!initrd_dir)
+		initrd_dir = g_strdup (DEFAULT_INITRD_DATA_DIR);
 	if (dump_to_stdout)
 		nm_clear_g_free (&connections_dir);
 
@@ -117,9 +123,31 @@ main (int argc, char *argv[])
 		return 1;
 	}
 
-	connections = nmi_cmdline_reader_parse (sysfs_dir, (const char *const*) remaining);
+	connections = nmi_cmdline_reader_parse (sysfs_dir,
+	                                        (const char *const*) remaining,
+	                                        &hostname);
+
 	g_hash_table_foreach (connections, output_conn, connections_dir);
 	g_hash_table_destroy (connections);
 
+	if (g_mkdir_with_parents (initrd_dir, 0755) != 0) {
+		errsv = errno;
+		_LOGW (LOGD_CORE, "%s: %s", initrd_dir, nm_strerror_native (errsv));
+		return 1;
+	}
+
+	if (hostname) {
+		gs_free char *hostname_file = NULL;
+		gs_free char *data = NULL;
+
+		hostname_file = g_strdup_printf ("%s/hostname", initrd_dir);
+		data = g_strdup_printf ("%s\n", hostname);
+
+		if (!g_file_set_contents (hostname_file, data, strlen (data), &error)) {
+			_LOGW (LOGD_CORE, "%s: %s", hostname_file, error->message);
+			return 1;
+		}
+	}
+
 	return 0;
 }