diff --git a/src/initrd/nm-initrd-generator.c b/src/initrd/nm-initrd-generator.c index 974213ba17..a04ffb7d4f 100644 --- a/src/initrd/nm-initrd-generator.c +++ b/src/initrd/nm-initrd-generator.c @@ -85,6 +85,7 @@ main (int argc, char *argv[]) }; GOptionContext *option_context; 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=...] " @@ -122,7 +123,10 @@ 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); @@ -131,5 +135,19 @@ main (int argc, char *argv[]) _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; } diff --git a/src/initrd/nm-initrd-generator.h b/src/initrd/nm-initrd-generator.h index cba383d1e0..9719992125 100644 --- a/src/initrd/nm-initrd-generator.h +++ b/src/initrd/nm-initrd-generator.h @@ -28,6 +28,6 @@ gboolean nmi_ibft_update_connection_from_nic (NMConnection *connection, GHashTab NMConnection *nmi_dt_reader_parse (const char *sysfs_dir); -GHashTable *nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv); +GHashTable *nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv, char **hostname); #endif /* __NM_INITRD_GENERATOR_H__ */ diff --git a/src/initrd/nmi-cmdline-reader.c b/src/initrd/nmi-cmdline-reader.c index 2eb6d7e5be..cf8cddc392 100644 --- a/src/initrd/nmi-cmdline-reader.c +++ b/src/initrd/nmi-cmdline-reader.c @@ -7,6 +7,7 @@ #include "nm-core-internal.h" #include "nm-initrd-generator.h" +#include "systemd/nm-sd-utils-shared.h" /*****************************************************************************/ @@ -22,6 +23,7 @@ typedef struct { GPtrArray *array; NMConnection *bootdev_connection; /* connection for bootdev=$ifname */ NMConnection *default_connection; /* connection not bound to any ifname */ + char *hostname; } Reader; static Reader * @@ -45,6 +47,7 @@ reader_destroy (Reader *reader, gboolean free_hash) g_ptr_array_unref (reader->array); hash = g_steal_pointer (&reader->hash); + nm_clear_g_free (&reader->hostname); nm_g_slice_free (reader); if (!free_hash) return g_steal_pointer (&hash); @@ -345,6 +348,14 @@ reader_parse_ip (Reader *reader, const char *sysfs_dir, char *argument) ifname = tmp; } + if (client_hostname && !nm_sd_hostname_is_valid (client_hostname, FALSE)) + client_hostname = NULL; + + if (client_hostname) { + g_free (reader->hostname); + reader->hostname = g_strdup (client_hostname); + } + /* :{none|off|dhcp|on|any|dhcp6|auto6|ibft} */ kind = get_word (&argument, ':'); @@ -875,7 +886,7 @@ reader_add_nameservers (Reader *reader, GPtrArray *nameservers) } GHashTable * -nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv) +nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv, char **hostname) { Reader *reader; const char *tag; @@ -1013,5 +1024,8 @@ nmi_cmdline_reader_parse (const char *sysfs_dir, const char *const*argv) reader_set_ignore_auto_dns (reader); g_hash_table_foreach (reader->hash, _normalize_conn, NULL); + + NM_SET_OUT (hostname, g_steal_pointer (&reader->hostname)); + return reader_destroy (reader, FALSE); } diff --git a/src/initrd/tests/test-cmdline-reader.c b/src/initrd/tests/test-cmdline-reader.c index b2b06e27c3..65f279a92f 100644 --- a/src/initrd/tests/test-cmdline-reader.c +++ b/src/initrd/tests/test-cmdline-reader.c @@ -30,10 +30,12 @@ test_auto (void) NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 1); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "default_connection"); g_assert (connection); @@ -81,10 +83,12 @@ test_if_auto_with_mtu (void) NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 1); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "eth0"); g_assert (connection); @@ -115,10 +119,13 @@ test_if_dhcp6 (void) NMConnection *connection; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 1); + g_assert_cmpstr (hostname, ==, NULL); + connection = g_hash_table_lookup (connections, "eth1"); g_assert (connection); nmtst_assert_connection_verifies_without_normalization (connection); @@ -145,10 +152,12 @@ test_if_auto_with_mtu_and_mac (void) NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 1); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "eth2"); g_assert (connection); @@ -183,10 +192,12 @@ test_if_ip4_manual (void) NMConnection *connection; NMSettingIPConfig *s_ip4; NMIPAddress *ip_addr; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 2); + g_assert_cmpstr (hostname, ==, "hostname1.example.com"); connection = g_hash_table_lookup (connections, "eth3"); g_assert (connection); @@ -237,10 +248,12 @@ test_if_ip6_manual (void) NMConnection *connection; NMSettingIPConfig *s_ip6; NMIPAddress *ip_addr; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 1); + g_assert_cmpstr (hostname, ==, "hostname0.example.com"); connection = g_hash_table_lookup (connections, "eth4"); g_assert (connection); @@ -274,10 +287,12 @@ test_multiple_merge (void) NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; NMIPAddress *ip_addr; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 1); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "eth0"); g_assert (connection); @@ -317,10 +332,12 @@ test_multiple_bootdev (void) NMConnection *connection; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 2); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "eth3"); g_assert (connection); @@ -344,10 +361,12 @@ test_bootdev (void) const char *const*ARGV = NM_MAKE_STRV ("vlan=vlan2:ens5", "bootdev=ens3"); NMConnection *connection; NMSettingConnection *s_con; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 2); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "ens3"); g_assert (connection); @@ -384,10 +403,12 @@ test_some_more (void) NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; NMIPRoute *ip_route; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 2); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "eth1"); g_assert (connection); @@ -466,10 +487,12 @@ test_bond (void) NMSettingBond *s_bond; NMIPRoute *ip_route; const char *master_uuid; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 3); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "bong0"); g_assert (connection); @@ -545,10 +568,12 @@ test_bond_default (void) NMSettingIPConfig *s_ip6; NMSettingBond *s_bond; const char *master_uuid; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 2); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "bond0"); @@ -606,10 +631,12 @@ test_bridge (void) NMSettingBridge *s_bridge; NMIPRoute *ip_route; const char *master_uuid; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 3); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "bridge0"); g_assert (connection); @@ -682,10 +709,12 @@ test_bridge_default (void) NMSettingIPConfig *s_ip6; NMSettingBridge *s_bridge; const char *master_uuid; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 2); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "br0"); @@ -740,10 +769,12 @@ test_team (void) NMSettingIPConfig *s_ip6; NMSettingTeam *s_team; const char *master_uuid; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 3); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "team0"); g_assert (connection); @@ -806,10 +837,12 @@ test_ibft_ip_dev (void) gs_unref_hashtable GHashTable *connections = NULL; NMSettingConnection *s_con; NMConnection *connection; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 1); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "eth0"); g_assert (connection); @@ -825,10 +858,12 @@ _test_ibft_ip (const char *const*ARGV) { gs_unref_hashtable GHashTable *connections = NULL; NMConnection *connection; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 2); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "ibft0"); g_assert (connection); @@ -864,10 +899,12 @@ test_ignore_extra (void) { gs_unref_hashtable GHashTable *connections = NULL; const char *const*ARGV = NM_MAKE_STRV ("blabla", "extra", "lalala"); + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 0); + g_assert_cmpstr (hostname, ==, NULL); } static void @@ -886,10 +923,12 @@ test_rd_znet (void) { .name = "portno", .value_str = "1" }, }; int i_s390_options_keys; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 2); + g_assert_cmpstr (hostname, ==, "foo.example.com"); connection = g_hash_table_lookup (connections, "enc800"); g_assert (NM_IS_CONNECTION (connection)); @@ -963,10 +1002,12 @@ test_rd_znet_legacy (void) "net.ifnames=0"); NMConnection *connection; NMSettingConnection *s_con; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 2); + g_assert_cmpstr (hostname, ==, "foo.example.com"); connection = g_hash_table_lookup (connections, "eth0"); g_assert (NM_IS_CONNECTION (connection)); @@ -1001,10 +1042,12 @@ test_bootif (void) NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 1); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "default_connection"); g_assert (connection); @@ -1037,10 +1080,12 @@ test_bootif_hwtype (void) NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 2); + g_assert_cmpstr (hostname, ==, NULL); connection = g_hash_table_lookup (connections, "eth0"); g_assert (connection); @@ -1100,10 +1145,12 @@ test_nameserver (void) "nameserver=[2606:4700:4700::1111]"); NMConnection *connection; NMSettingIPConfig *s_ip; + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 3); + g_assert_cmpstr (hostname, ==, "foo.example.com"); connection = g_hash_table_lookup (connections, "eth0"); g_assert (connection); @@ -1140,10 +1187,12 @@ test_bootif_off (void) { gs_unref_hashtable GHashTable *connections = NULL; const char *const*ARGV = NM_MAKE_STRV ("BOOTIF=01-00-53-AB-cd-02-03", "rd.bootif=0"); + gs_free char *hostname = NULL; - connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV); + connections = nmi_cmdline_reader_parse (TEST_INITRD_DIR "/sysfs", ARGV, &hostname); g_assert (connections); g_assert_cmpint (g_hash_table_size (connections), ==, 0); + g_assert_cmpstr (hostname, ==, NULL); } NMTST_DEFINE ();