diff options
| author | Sebastien Bacher <seb128@ubuntu.com> | 2019-01-25 11:24:41 +0100 |
|---|---|---|
| committer | Sebastien Bacher <seb128@ubuntu.com> | 2019-01-25 11:24:41 +0100 |
| commit | bbae86d3d2997a853ca0365e8eb7a3ca7489ee09 (patch) | |
| tree | 1f7fa49b47ab13aea3effbe839559d221f6323b4 /src/initrd | |
| parent | 404ebe62622150e77e311777dff8617eb974e834 (diff) | |
New upstream version 1.15.2
Diffstat (limited to 'src/initrd')
| -rw-r--r-- | src/initrd/meson.build | 32 | ||||
| -rw-r--r-- | src/initrd/nm-initrd-generator.c | 58 | ||||
| -rw-r--r-- | src/initrd/nmi-cmdline-reader.c | 68 | ||||
| -rw-r--r-- | src/initrd/tests/meson.build | 23 | ||||
| -rw-r--r-- | src/initrd/tests/test-cmdline-reader.c | 12 |
5 files changed, 132 insertions, 61 deletions
diff --git a/src/initrd/meson.build b/src/initrd/meson.build new file mode 100644 index 00000000..a12b718a --- /dev/null +++ b/src/initrd/meson.build @@ -0,0 +1,32 @@ +sources = files( + 'nmi-cmdline-reader.c', + 'nmi-ibft-reader.c', +) + +nm_cflags = ['-DNETWORKMANAGER_COMPILATION=NM_NETWORKMANAGER_COMPILATION_DAEMON'] + +libnmi_core = static_library( + 'nmi-core', + c_args: nm_cflags, + sources: sources, + include_directories: src_inc, + dependencies: nm_core_dep, +) + +name = 'nm-initrd-generator' +executable( + name, + name + '.c', + c_args: nm_cflags, + include_directories: src_inc, + dependencies: [ nm_core_dep ], + link_with: [libnetwork_manager_base, libnmi_core], + link_args: ldflags_linker_script_binary, + link_depends: linker_script_binary, + install: true, + install_dir: nm_libexecdir, +) + +if enable_tests + subdir('tests') +endif diff --git a/src/initrd/nm-initrd-generator.c b/src/initrd/nm-initrd-generator.c index eb9a38df..70f23e17 100644 --- a/src/initrd/nm-initrd-generator.c +++ b/src/initrd/nm-initrd-generator.c @@ -40,45 +40,39 @@ output_conn (gpointer key, gpointer value, gpointer user_data) const char *basename = key; NMConnection *connection = value; char *connections_dir = user_data; - GKeyFile *file; + gs_unref_keyfile GKeyFile *file = NULL; gs_free char *data = NULL; - GError *error = NULL; + gs_free_error GError *error = NULL; gsize len; - if (!nm_connection_normalize (connection, NULL, NULL, &error)) { - g_print ("%s\n", error->message); - g_error_free (error); - return; - } + if (!nm_connection_normalize (connection, NULL, NULL, &error)) + goto err_out; file = nm_keyfile_write (connection, NULL, NULL, &error); - if (file == NULL) { - g_print ("%s\n", error->message); - g_error_free (error); - return; - } + if (file == NULL) + goto err_out; data = g_key_file_to_data (file, &len, &error); - if (!data) { - g_print ("%s\n", error->message); - g_error_free (error); - } else if (connections_dir) { - gs_free char *basename_w_ext = g_strconcat (basename, ".nmconnection", NULL); - char *filename = g_build_filename (connections_dir, basename_w_ext, NULL); - - if (!nm_utils_file_set_contents (filename, data, len, 0600, &error)) { - g_print ("%s\n", error->message); - g_error_free (error); - } - g_free (filename); - } else { + if (!data) + goto err_out; + + if (connections_dir) { + gs_free char *filename = NULL; + gs_free char *full_filename = NULL; + + filename = nm_keyfile_utils_create_filename (basename, TRUE); + full_filename = g_build_filename (connections_dir, filename, NULL); + + 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_key_file_free (file); + return; +err_out: + g_print ("%s\n", error->message); } -#define DEFAULT_CONNECTIONS_DIR NMRUNDIR "/system-connections" #define DEFAULT_SYSFS_DIR "/sys" int @@ -90,7 +84,7 @@ main (int argc, char *argv[]) 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", DEFAULT_CONNECTIONS_DIR }, + { "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 }, @@ -110,7 +104,7 @@ main (int argc, char *argv[]) g_option_context_add_main_entries (option_context, option_entries, GETTEXT_PACKAGE); if (!g_option_context_parse (option_context, &argc, &argv, &error)) { - _LOGW (LOGD_CORE, "%s\n", error->message); + _LOGW (LOGD_CORE, "%s", error->message); return 1; } @@ -120,14 +114,14 @@ main (int argc, char *argv[]) } if (!connections_dir) - connections_dir = g_strdup (DEFAULT_CONNECTIONS_DIR); + connections_dir = g_strdup (NM_KEYFILE_PATH_NAME_RUN); if (!sysfs_dir) sysfs_dir = g_strdup (DEFAULT_SYSFS_DIR); if (dump_to_stdout) g_clear_pointer (&connections_dir, g_free); if (connections_dir && g_mkdir_with_parents (connections_dir, 0755) != 0) { - _LOGW (LOGD_CORE, "%s: %s\n", connections_dir, strerror (errno)); + _LOGW (LOGD_CORE, "%s: %s", connections_dir, strerror (errno)); return 1; } diff --git a/src/initrd/nmi-cmdline-reader.c b/src/initrd/nmi-cmdline-reader.c index e3b1bb63..e812b086 100644 --- a/src/initrd/nmi-cmdline-reader.c +++ b/src/initrd/nmi-cmdline-reader.c @@ -39,7 +39,17 @@ get_conn (GHashTable *connections, const char *ifname, const char *type_name) { NMConnection *connection; NMSetting *setting; - const char *basename = ifname ?: "default_connection"; + const char *basename; + NMConnectionMultiConnect multi_connect; + + if (ifname) { + basename = ifname; + multi_connect = NM_CONNECTION_MULTI_CONNECT_SINGLE; + } else { + /* This is essentially for the "ip=dhcp" scenario. */ + basename = "default_connection"; + multi_connect = NM_CONNECTION_MULTI_CONNECT_MULTIPLE; + } connection = g_hash_table_lookup (connections, (gpointer)basename); @@ -71,6 +81,7 @@ get_conn (GHashTable *connections, const char *ifname, const char *type_name) NM_SETTING_CONNECTION_ID, ifname ?: "Wired Connection", NM_SETTING_CONNECTION_UUID, nm_utils_uuid_generate_a (), NM_SETTING_CONNECTION_INTERFACE_NAME, ifname, + NM_SETTING_CONNECTION_MULTI_CONNECT, multi_connect, NULL); if (!type_name) @@ -132,7 +143,7 @@ _base_setting_set (NMConnection *connection, const char *property, const char *v GParamSpec *spec = g_object_class_find_property (object_class, property); if (!spec) { - _LOGW (LOGD_CORE, "'%s' does not support setting %s\n", type_name, property); + _LOGW (LOGD_CORE, "'%s' does not support setting %s", type_name, property); return; } @@ -151,7 +162,7 @@ _base_setting_set (NMConnection *connection, const char *property, const char *v } else if (G_IS_PARAM_SPEC_STRING (spec)) g_object_set (setting, property, value, NULL); else - _LOGW (LOGD_CORE, "Don't know how to set '%s' of %s\n", property, type_name); + _LOGW (LOGD_CORE, "Don't know how to set '%s' of %s", property, type_name); g_type_class_unref (object_class); } @@ -211,7 +222,7 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument) dns[1] = get_word (&argument, ':'); dns_addr_family[1] = guess_ip_address_family (dns[1]); if (argument && *argument) - _LOGW (LOGD_CORE, "Ignoring extra: '%s'.\n", argument); + _LOGW (LOGD_CORE, "Ignoring extra: '%s'.", argument); } else { mtu = tmp; macaddr = argument; @@ -236,12 +247,12 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument) index = g_hash_table_lookup (nic, "index"); if (!index) { - _LOGW (LOGD_CORE, "Ignoring an iBFT entry without an index\n"); + _LOGW (LOGD_CORE, "Ignoring an iBFT entry without an index"); continue; } if (!nmi_ibft_update_connection_from_nic (connection, nic, &error)) { - _LOGW (LOGD_CORE, "Unable to merge iBFT configuration: %s\n", error->message); + _LOGW (LOGD_CORE, "Unable to merge iBFT configuration: %s", error->message); g_error_free (error); } @@ -261,10 +272,10 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument) if (netmask && *netmask) { NMIPAddr addr; - if (nm_utils_parse_inaddr_bin (AF_INET, netmask, &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\n", client_ip); + _LOGW (LOGD_CORE, "Unrecognized address: %s", client_ip); } } @@ -273,7 +284,7 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument) NMIPAddress *address = NULL; NMIPAddr addr; - if (nm_utils_parse_inaddr_prefix_bin (client_ip_family, client_ip, &addr, + if (nm_utils_parse_inaddr_prefix_bin (client_ip_family, client_ip, NULL, &addr, client_ip_prefix == -1 ? &client_ip_prefix : NULL)) { if (client_ip_prefix == -1) { switch (client_ip_family) { @@ -288,11 +299,11 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument) address = nm_ip_address_new_binary (client_ip_family, &addr.addr_ptr, client_ip_prefix, &error); if (!address) { - _LOGW (LOGD_CORE, "Invalid address '%s': %s\n", client_ip, error->message); + _LOGW (LOGD_CORE, "Invalid address '%s': %s", client_ip, error->message); g_clear_error (&error); } } else { - _LOGW (LOGD_CORE, "Unrecognized address: %s\n", client_ip); + _LOGW (LOGD_CORE, "Unrecognized address: %s", client_ip); } if (address) { @@ -312,7 +323,7 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument) nm_setting_ip_config_add_address (s_ip6, address); break; default: - _LOGW (LOGD_CORE, "Unknown address family: %s\n", client_ip); + _LOGW (LOGD_CORE, "Unknown address family: %s", client_ip); break; } nm_ip_address_unref (address); @@ -377,12 +388,12 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument) ibft = nmi_ibft_read (sysfs_dir); nic = g_hash_table_lookup (ibft, mac_up); if (!nic) - _LOGW (LOGD_CORE, "No iBFT NIC for %s (%s)\n", ifname, mac_up); + _LOGW (LOGD_CORE, "No iBFT NIC for %s (%s)", ifname, mac_up); } if (nic) { if (!nmi_ibft_update_connection_from_nic (connection, nic, &error)) { - _LOGW (LOGD_CORE, "Unable to merge iBFT configuration: %s\n", error->message); + _LOGW (LOGD_CORE, "Unable to merge iBFT configuration: %s", error->message); g_clear_error (&error); } } @@ -403,11 +414,11 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument) g_object_set (s_ip6, NM_SETTING_IP_CONFIG_GATEWAY, gateway_ip, NULL); break; default: - _LOGW (LOGD_CORE, "Unknown address family: %s\n", gateway_ip); + _LOGW (LOGD_CORE, "Unknown address family: %s", gateway_ip); break; } } else { - _LOGW (LOGD_CORE, "Invalid gateway: %s\n", gateway_ip); + _LOGW (LOGD_CORE, "Invalid gateway: %s", gateway_ip); } } @@ -428,11 +439,11 @@ parse_ip (GHashTable *connections, const char *sysfs_dir, char *argument) nm_setting_ip_config_add_dns (s_ip6, dns[i]); break; default: - _LOGW (LOGD_CORE, "Unknown address family: %s\n", dns[i]); + _LOGW (LOGD_CORE, "Unknown address family: %s", dns[i]); break; } } else { - _LOGW (LOGD_CORE, "Invalid name server: %s\n", dns[i]); + _LOGW (LOGD_CORE, "Invalid name server: %s", dns[i]); } } @@ -496,7 +507,7 @@ parse_master (GHashTable *connections, char *argument, const char *type_name) } while (slaves && *slaves != '\0'); if (argument && *argument) - _LOGW (LOGD_CORE, "Ignoring extra: '%s'.\n", argument); + _LOGW (LOGD_CORE, "Ignoring extra: '%s'.", argument); } static void @@ -518,19 +529,18 @@ parse_rd_route (GHashTable *connections, char *argument) gateway = get_word (&argument, ':'); interface = get_word (&argument, ':'); - family = guess_ip_address_family (net); connection = get_conn (connections, interface, NULL); if (net && *net) { - if (!nm_utils_parse_inaddr_prefix_bin (family, net, &net_addr, &net_prefix)) { - _LOGW (LOGD_CORE, "Unrecognized address: %s\n", net); + if (!nm_utils_parse_inaddr_prefix_bin (family, net, &family, &net_addr, &net_prefix)) { + _LOGW (LOGD_CORE, "Unrecognized address: %s", net); return; } } - if (gateway && *net) { - if (!nm_utils_parse_inaddr_bin (family, gateway, &gateway_addr)) { - _LOGW (LOGD_CORE, "Unrecognized address: %s\n", gateway); + if (gateway && *gateway) { + if (!nm_utils_parse_inaddr_bin (family, gateway, &family, &gateway_addr)) { + _LOGW (LOGD_CORE, "Unrecognized address: %s", gateway); return; } } @@ -547,7 +557,7 @@ parse_rd_route (GHashTable *connections, char *argument) net_prefix = 128; break; default: - _LOGW (LOGD_CORE, "Unknown address family: %s\n", net); + _LOGW (LOGD_CORE, "Unknown address family: %s", net); return; } @@ -588,7 +598,7 @@ parse_vlan (GHashTable *connections, char *argument) NULL); if (argument && *argument) - _LOGW (LOGD_CORE, "Ignoring extra: '%s'.\n", argument); + _LOGW (LOGD_CORE, "Ignoring extra: '%s'.", argument); } static void @@ -624,14 +634,14 @@ parse_nameserver (GHashTable *connections, char *argument) s_ip = nm_connection_get_setting_ip6_config (connection); break; default: - _LOGW (LOGD_CORE, "Unknown address family: %s\n", dns); + _LOGW (LOGD_CORE, "Unknown address family: %s", dns); break; } nm_setting_ip_config_add_dns (s_ip, dns); if (argument && *argument) - _LOGW (LOGD_CORE, "xIgnoring extra: '%s'.\n", argument); + _LOGW (LOGD_CORE, "Ignoring extra: '%s'.", argument); } static void diff --git a/src/initrd/tests/meson.build b/src/initrd/tests/meson.build new file mode 100644 index 00000000..0ef72fff --- /dev/null +++ b/src/initrd/tests/meson.build @@ -0,0 +1,23 @@ +test_units = [ + 'test-ibft-reader', + 'test-cmdline-reader', +] + +cflags = [ + '-DTEST_INITRD_DIR="@0@"'.format(meson.current_source_dir()), +] + +foreach test_unit : test_units + exe = executable( + test_unit, + test_unit + '.c', + dependencies: test_nm_dep, + c_args: cflags, + link_with: libnmi_core, + ) + test( + 'initrd/' + test_unit, + test_script, + 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 4db71147..95084e92 100644 --- a/src/initrd/tests/test-cmdline-reader.c +++ b/src/initrd/tests/test-cmdline-reader.c @@ -62,6 +62,8 @@ test_auto (void) g_assert_cmpstr (nm_setting_connection_get_connection_type (s_con), ==, NM_SETTING_WIRED_SETTING_NAME); g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "Wired Connection"); g_assert_cmpint (nm_setting_connection_get_timestamp (s_con), ==, 0); + g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_MULTIPLE); + g_assert (nm_setting_connection_get_autoconnect (s_con)); s_wired = nm_connection_get_setting_wired (connection); @@ -328,6 +330,7 @@ test_some_more (void) g_assert_cmpstr (nm_setting_connection_get_connection_type (s_con), ==, NM_SETTING_WIRED_SETTING_NAME); g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "Wired Connection"); g_assert_cmpstr (nm_setting_connection_get_interface_name (s_con), ==, "eth1"); + g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_MULTIPLE); s_wired = nm_connection_get_setting_wired (connection); g_assert (s_wired); @@ -361,6 +364,7 @@ test_some_more (void) g_assert_cmpstr (nm_setting_connection_get_connection_type (s_con), ==, NM_SETTING_WIRED_SETTING_NAME); g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "ens10"); g_assert_cmpstr (nm_setting_connection_get_interface_name (s_con), ==, "ens10"); + g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_SINGLE); s_wired = nm_connection_get_setting_wired (connection); g_assert (s_wired); @@ -454,6 +458,7 @@ test_bond (void) g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "eth0"); g_assert_cmpstr (nm_setting_connection_get_slave_type (s_con), ==, NM_SETTING_BOND_SETTING_NAME); g_assert_cmpstr (nm_setting_connection_get_master (s_con), ==, master_uuid); + g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_SINGLE); connection = g_hash_table_lookup (connections, "eth1"); g_assert (connection); @@ -466,6 +471,7 @@ test_bond (void) g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "eth1"); g_assert_cmpstr (nm_setting_connection_get_slave_type (s_con), ==, NM_SETTING_BOND_SETTING_NAME); g_assert_cmpstr (nm_setting_connection_get_master (s_con), ==, master_uuid); + g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_SINGLE); } static void @@ -525,6 +531,7 @@ test_bond_default (void) g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "eth0"); g_assert_cmpstr (nm_setting_connection_get_slave_type (s_con), ==, NM_SETTING_BOND_SETTING_NAME); g_assert_cmpstr (nm_setting_connection_get_master (s_con), ==, master_uuid); + g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_SINGLE); } static void @@ -588,6 +595,7 @@ test_bridge (void) g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "eth0"); g_assert_cmpstr (nm_setting_connection_get_slave_type (s_con), ==, NM_SETTING_BRIDGE_SETTING_NAME); g_assert_cmpstr (nm_setting_connection_get_master (s_con), ==, master_uuid); + g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_SINGLE); connection = g_hash_table_lookup (connections, "eth1"); g_assert (connection); @@ -600,6 +608,7 @@ test_bridge (void) g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "eth1"); g_assert_cmpstr (nm_setting_connection_get_slave_type (s_con), ==, NM_SETTING_BRIDGE_SETTING_NAME); g_assert_cmpstr (nm_setting_connection_get_master (s_con), ==, master_uuid); + g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_SINGLE); } static void @@ -657,6 +666,7 @@ test_bridge_default (void) g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "eth0"); g_assert_cmpstr (nm_setting_connection_get_slave_type (s_con), ==, NM_SETTING_BRIDGE_SETTING_NAME); g_assert_cmpstr (nm_setting_connection_get_master (s_con), ==, master_uuid); + g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_SINGLE); } static void @@ -713,6 +723,7 @@ test_team (void) g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "eth0"); g_assert_cmpstr (nm_setting_connection_get_slave_type (s_con), ==, NM_SETTING_TEAM_SETTING_NAME); g_assert_cmpstr (nm_setting_connection_get_master (s_con), ==, master_uuid); + g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_SINGLE); connection = g_hash_table_lookup (connections, "eth1"); g_assert (connection); @@ -725,6 +736,7 @@ test_team (void) g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, "eth1"); g_assert_cmpstr (nm_setting_connection_get_slave_type (s_con), ==, NM_SETTING_TEAM_SETTING_NAME); g_assert_cmpstr (nm_setting_connection_get_master (s_con), ==, master_uuid); + g_assert_cmpint (nm_setting_connection_get_multi_connect (s_con), ==, NM_CONNECTION_MULTI_CONNECT_SINGLE); } static void |