1 From 38d458ddd69cb7dd6e7f58f9e9f3197c6b6184f3 Mon Sep 17 00:00:00 2001
2 From: Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
3 Date: Tue, 27 Nov 2012 17:22:07 -0200
4 Subject: [PATCH 3/3] Search for specific config file for netboot
6 This patch implements a search for a specific configuration when the config
7 file is on a remoteserver. It uses the following order:
8 1) DHCP client UUID option.
9 2) MAC address (in lower case hexadecimal with dash separators);
10 3) IP (in upper case hexadecimal) or IPv6;
11 4) The original grub.cfg file.
13 This procedure is similar to what is used by pxelinux and yaboot:
14 http://www.syslinux.org/wiki/index.php/PXELINUX#config
16 This should close the bugzilla:
17 https://bugzilla.redhat.com/show_bug.cgi?id=873406
19 grub-core/net/net.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++
20 grub-core/normal/main.c | 18 ++++++--
21 include/grub/net.h | 3 ++
22 3 files changed, 135 insertions(+), 4 deletions(-)
24 diff --git a/grub-core/net/net.c b/grub-core/net/net.c
25 index 01c5d32..49c32c5 100644
26 --- a/grub-core/net/net.c
27 +++ b/grub-core/net/net.c
28 @@ -1548,6 +1548,124 @@ grub_net_restore_hw (void)
33 +grub_net_search_configfile (char *config)
35 + grub_size_t config_len;
38 + auto int search_through (grub_size_t num_tries, grub_size_t slice_size);
39 + int search_through (grub_size_t num_tries, grub_size_t slice_size)
41 + while (num_tries-- > 0)
43 + grub_dprintf ("net", "probe %s\n", config);
46 + file = grub_file_open (config);
50 + grub_file_close (file);
51 + grub_dprintf ("net", "found!\n");
56 + if (grub_errno == GRUB_ERR_IO)
57 + grub_errno = GRUB_ERR_NONE;
60 + if (grub_strlen (suffix) < slice_size)
63 + config[grub_strlen (config) - slice_size] = '\0';
69 + config_len = grub_strlen (config);
70 + config[config_len] = '-';
71 + suffix = config + config_len + 1;
73 + struct grub_net_network_level_interface *inf;
74 + FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
76 + /* By the Client UUID. */
78 + char client_uuid_var[sizeof ("net_") + grub_strlen (inf->name) +
79 + sizeof ("_clientuuid") + 1];
80 + grub_snprintf (client_uuid_var, sizeof (client_uuid_var),
81 + "net_%s_clientuuid", inf->name);
83 + const char *client_uuid;
84 + client_uuid = grub_env_get (client_uuid_var);
88 + grub_strcpy (suffix, client_uuid);
89 + if (search_through (1, 0) == 0) return GRUB_ERR_NONE;
92 + /* By the MAC address. */
94 + /* Add ethernet type */
95 + grub_strcpy (suffix, "01-");
97 + grub_net_hwaddr_to_str (&inf->hwaddress, suffix + 3);
100 + for (ptr = suffix; *ptr; ptr++)
104 + if (search_through (1, 0) == 0) return GRUB_ERR_NONE;
106 + /* By IP address */
108 + switch ((&inf->address)->type)
110 + case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
112 + grub_uint32_t n = grub_be_to_cpu32 ((&inf->address)->ipv4);
113 + grub_snprintf (suffix, GRUB_NET_MAX_STR_ADDR_LEN, "%02X%02X%02X%02X", \
114 + ((n >> 24) & 0xff), ((n >> 16) & 0xff), \
115 + ((n >> 8) & 0xff), ((n >> 0) & 0xff));
117 + if (search_through (8, 1) == 0) return GRUB_ERR_NONE;
120 + case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
122 + char buf[GRUB_NET_MAX_STR_ADDR_LEN];
123 + struct grub_net_network_level_address base;
124 + base.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
125 + grub_memcpy (&base.ipv6, ((&inf->address)->ipv6), 16);
126 + grub_net_addr_to_str (&base, buf);
128 + for (ptr = buf; *ptr; ptr++)
132 + grub_snprintf (suffix, GRUB_NET_MAX_STR_ADDR_LEN, "%s", buf);
133 + if (search_through (1, 0) == 0) return GRUB_ERR_NONE;
136 + case GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV:
137 + return grub_error (GRUB_ERR_BUG, "shouldn't reach here");
139 + return grub_error (GRUB_ERR_BUG,
140 + "unsupported address type %d", (&inf->address)->type);
144 + /* Remove the remaining minus sign at the end. */
145 + config[config_len] = '\0';
147 + return GRUB_ERR_NONE;
150 static struct grub_preboot *fini_hnd;
152 static grub_command_t cmd_addaddr, cmd_deladdr, cmd_addroute, cmd_delroute;
153 diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c
154 index aa0b3e5..cc519a5 100644
155 --- a/grub-core/normal/main.c
156 +++ b/grub-core/normal/main.c
158 #include <grub/i18n.h>
159 #include <grub/charset.h>
160 #include <grub/script_sh.h>
161 +#include <grub/net.h>
163 GRUB_MOD_LICENSE ("GPLv3+");
165 @@ -379,10 +380,19 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
167 prefix = grub_env_get ("prefix");
170 - config = grub_xasprintf ("%s/grub.cfg", prefix);
174 + grub_size_t config_len;
175 + config_len = grub_strlen (prefix) +
176 + sizeof ("/grub.cfg-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
177 + config = grub_malloc (config_len);
182 + grub_snprintf (config, config_len, "%s/grub.cfg", prefix);
184 + if (grub_strncmp (prefix + 1, "tftp", sizeof ("tftp") - 1) == 0)
185 + grub_net_search_configfile (config);
187 grub_enter_normal_mode (config);
189 diff --git a/include/grub/net.h b/include/grub/net.h
190 index 45348dd..09b8d56 100644
191 --- a/include/grub/net.h
192 +++ b/include/grub/net.h
193 @@ -534,6 +534,9 @@ extern char *grub_net_default_server;
194 #define GRUB_NET_TRIES 40
195 #define GRUB_NET_INTERVAL 400
197 #define VLANTAG_IDENTIFIER 0x8100
200 +grub_net_search_configfile (char *config);
202 #endif /* ! GRUB_NET_HEADER */