initrd: print generated configuration snippets

Instead of just printing something like "*** Carrier timeout 10sec",
print the actual configuration snippet that was generated.
This commit is contained in:
Beniamino Galvani
2022-05-26 23:28:45 +02:00
parent a216739e09
commit 2c5846feec

View File

@@ -137,6 +137,8 @@ main(int argc, char *argv[])
gs_free char *hostname = NULL; gs_free char *hostname = NULL;
int errsv; int errsv;
gint64 carrier_timeout_sec = 0; gint64 carrier_timeout_sec = 0;
gs_unref_array GArray *confs = NULL;
guint i;
option_context = g_option_context_new( option_context = g_option_context_new(
"-- [ip=...] [rd.route=...] [bridge=...] [bond=...] [team=...] [vlan=...] " "-- [ip=...] [rd.route=...] [bridge=...] [bond=...] [team=...] [vlan=...] "
@@ -178,15 +180,45 @@ main(int argc, char *argv[])
&hostname, &hostname,
&carrier_timeout_sec); &carrier_timeout_sec);
confs = g_array_new(FALSE, FALSE, sizeof(NMUtilsNamedValue));
g_array_set_clear_func(confs, (GDestroyNotify) nm_utils_named_value_clear_with_g_free);
if (carrier_timeout_sec != 0) {
nm_auto_unref_keyfile GKeyFile *keyfile = NULL;
NMUtilsNamedValue v;
keyfile = g_key_file_new();
g_key_file_set_list_separator(keyfile, NM_CONFIG_KEYFILE_LIST_SEPARATOR);
g_key_file_set_value(keyfile,
NM_CONFIG_KEYFILE_GROUPPREFIX_DEVICE "-15-carrier-timeout",
NM_CONFIG_KEYFILE_KEY_MATCH_DEVICE,
"*");
g_key_file_set_int64(keyfile,
NM_CONFIG_KEYFILE_GROUPPREFIX_DEVICE "-15-carrier-timeout",
NM_CONFIG_KEYFILE_KEY_DEVICE_CARRIER_WAIT_TIMEOUT,
carrier_timeout_sec * 1000);
v = (NMUtilsNamedValue){
.name = g_strdup_printf("%s/15-carrier-timeout.conf", run_config_dir),
.value_str = g_key_file_to_data(keyfile, NULL, NULL),
};
g_array_append_val(confs, v);
}
if (dump_to_stdout) { if (dump_to_stdout) {
nm_clear_g_free(&connections_dir); nm_clear_g_free(&connections_dir);
nm_clear_g_free(&initrd_dir); nm_clear_g_free(&initrd_dir);
nm_clear_g_free(&run_config_dir); nm_clear_g_free(&run_config_dir);
if (hostname) if (hostname)
g_print("\n*** Hostname '%s' ***\n", hostname); g_print("\n*** Hostname '%s' ***\n", hostname);
if (carrier_timeout_sec != 0)
g_print("\n*** Carrier Wait Timeout %" G_GINT64_FORMAT " sec ***\n", for (i = 0; i < confs->len; i++) {
carrier_timeout_sec); NMUtilsNamedValue *v = &g_array_index(confs, NMUtilsNamedValue, i);
gs_free char *name = g_path_get_basename(v->name);
g_print("\n*** Configuration '%s' ***\n\n%s\n", name, v->value_str);
}
} else { } else {
if (g_mkdir_with_parents(connections_dir, 0755) != 0) { if (g_mkdir_with_parents(connections_dir, 0755) != 0) {
errsv = errno; errsv = errno;
@@ -216,25 +248,12 @@ main(int argc, char *argv[])
return 1; return 1;
} }
} }
if (carrier_timeout_sec != 0) {
nm_auto_unref_keyfile GKeyFile *keyfile = NULL;
gs_free char *filename = NULL;
keyfile = g_key_file_new(); for (i = 0; i < confs->len; i++) {
g_key_file_set_list_separator(keyfile, NM_CONFIG_KEYFILE_LIST_SEPARATOR); NMUtilsNamedValue *v = &g_array_index(confs, NMUtilsNamedValue, i);
filename = g_strdup_printf("%s/15-carrier-timeout.conf", run_config_dir);
g_key_file_set_value(keyfile, if (!g_file_set_contents(v->name, v->value_str, strlen(v->value_str), &error)) {
NM_CONFIG_KEYFILE_GROUPPREFIX_DEVICE "-15-carrier-timeout", _LOGW(LOGD_CORE, "%s: %s", v->name, error->message);
NM_CONFIG_KEYFILE_KEY_MATCH_DEVICE,
"*");
g_key_file_set_int64(keyfile,
NM_CONFIG_KEYFILE_GROUPPREFIX_DEVICE "-15-carrier-timeout",
NM_CONFIG_KEYFILE_KEY_DEVICE_CARRIER_WAIT_TIMEOUT,
carrier_timeout_sec * 1000);
if (!g_key_file_save_to_file(keyfile, filename, &error)) {
_LOGW(LOGD_CORE, "%s: %s", filename, error->message);
return 1; return 1;
} }
} }