nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0100-Search-for-specific-config-file-for-netboot.patch

f725e3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f725e3
From: Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
f725e3
Date: Tue, 27 Nov 2012 17:22:07 -0200
f725e3
Subject: [PATCH] Search for specific config file for netboot
f725e3
f725e3
This patch implements a search for a specific configuration when the config
f725e3
file is on a remoteserver. It uses the following order:
f725e3
   1) DHCP client UUID option.
f725e3
   2) MAC address (in lower case hexadecimal with dash separators);
f725e3
   3) IP (in upper case hexadecimal) or IPv6;
f725e3
   4) The original grub.cfg file.
f725e3
f725e3
This procedure is similar to what is used by pxelinux and yaboot:
f725e3
http://www.syslinux.org/wiki/index.php/PXELINUX#config
f725e3
f725e3
This should close the bugzilla:
f725e3
https://bugzilla.redhat.com/show_bug.cgi?id=873406
f725e3
---
f725e3
 grub-core/net/net.c     | 118 ++++++++++++++++++++++++++++++++++++++++++++++++
f725e3
 grub-core/normal/main.c |  18 ++++++--
f725e3
 include/grub/net.h      |   3 ++
f725e3
 3 files changed, 135 insertions(+), 4 deletions(-)
f725e3
f725e3
diff --git a/grub-core/net/net.c b/grub-core/net/net.c
f725e3
index f2e723bd409..578e057e18f 100644
f725e3
--- a/grub-core/net/net.c
f725e3
+++ b/grub-core/net/net.c
f725e3
@@ -1702,6 +1702,124 @@ grub_net_restore_hw (void)
f725e3
   return GRUB_ERR_NONE;
f725e3
 }
f725e3
 
f725e3
+grub_err_t
f725e3
+grub_net_search_configfile (char *config)
f725e3
+{
f725e3
+  grub_size_t config_len;
f725e3
+  char *suffix;
f725e3
+
f725e3
+  auto int search_through (grub_size_t num_tries, grub_size_t slice_size);
f725e3
+  int search_through (grub_size_t num_tries, grub_size_t slice_size)
f725e3
+    {
f725e3
+      while (num_tries-- > 0)
f725e3
+        {
f725e3
+	  grub_dprintf ("net", "probe %s\n", config);
f725e3
+
f725e3
+          grub_file_t file;
f725e3
+          file = grub_file_open (config);
f725e3
+
f725e3
+          if (file)
f725e3
+            {
f725e3
+              grub_file_close (file);
f725e3
+              grub_dprintf ("net", "found!\n");
f725e3
+              return 0;
f725e3
+            }
f725e3
+          else
f725e3
+            {
f725e3
+              if (grub_errno == GRUB_ERR_IO)
f725e3
+                grub_errno = GRUB_ERR_NONE;
f725e3
+            }
f725e3
+
f725e3
+          if (grub_strlen (suffix) < slice_size)
f725e3
+            break;
f725e3
+
f725e3
+          config[grub_strlen (config) - slice_size] = '\0';
f725e3
+        }
f725e3
+
f725e3
+      return 1;
f725e3
+    }
f725e3
+
f725e3
+  config_len = grub_strlen (config);
f725e3
+  config[config_len] = '-';
f725e3
+  suffix = config + config_len + 1;
f725e3
+
f725e3
+  struct grub_net_network_level_interface *inf;
f725e3
+  FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
f725e3
+    {
f725e3
+      /* By the Client UUID. */
f725e3
+
f725e3
+      char client_uuid_var[sizeof ("net_") + grub_strlen (inf->name) +
f725e3
+                           sizeof ("_clientuuid") + 1];
f725e3
+      grub_snprintf (client_uuid_var, sizeof (client_uuid_var),
f725e3
+                     "net_%s_clientuuid", inf->name);
f725e3
+
f725e3
+      const char *client_uuid;
f725e3
+      client_uuid = grub_env_get (client_uuid_var);
f725e3
+
f725e3
+      if (client_uuid)
f725e3
+        {
f725e3
+          grub_strcpy (suffix, client_uuid);
f725e3
+          if (search_through (1, 0) == 0) return GRUB_ERR_NONE;
f725e3
+        }
f725e3
+
f725e3
+      /* By the MAC address. */
f725e3
+
f725e3
+      /* Add ethernet type */
f725e3
+      grub_strcpy (suffix, "01-");
f725e3
+
f725e3
+      grub_net_hwaddr_to_str (&inf->hwaddress, suffix + 3);
f725e3
+
f725e3
+      char *ptr;
f725e3
+      for (ptr = suffix; *ptr; ptr++)
f725e3
+        if (*ptr == ':')
f725e3
+          *ptr = '-';
f725e3
+
f725e3
+      if (search_through (1, 0) == 0) return GRUB_ERR_NONE;
f725e3
+
f725e3
+      /* By IP address */
f725e3
+
f725e3
+      switch ((&inf->address)->type)
f725e3
+        {
f725e3
+        case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4:
f725e3
+            {
f725e3
+              grub_uint32_t n = grub_be_to_cpu32 ((&inf->address)->ipv4);
f725e3
+              grub_snprintf (suffix, GRUB_NET_MAX_STR_ADDR_LEN, "%02X%02X%02X%02X", \
f725e3
+                             ((n >> 24) & 0xff), ((n >> 16) & 0xff), \
f725e3
+                             ((n >> 8) & 0xff), ((n >> 0) & 0xff));
f725e3
+
f725e3
+              if (search_through (8, 1) == 0) return GRUB_ERR_NONE;
f725e3
+              break;
f725e3
+            }
f725e3
+        case GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6:
f725e3
+            {
f725e3
+              char buf[GRUB_NET_MAX_STR_ADDR_LEN];
f725e3
+              struct grub_net_network_level_address base;
f725e3
+              base.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
f725e3
+              grub_memcpy (&base.ipv6, ((&inf->address)->ipv6), 16);
f725e3
+              grub_net_addr_to_str (&base, buf);
f725e3
+
f725e3
+              for (ptr = buf; *ptr; ptr++)
f725e3
+                if (*ptr == ':')
f725e3
+                  *ptr = '-';
f725e3
+
f725e3
+              grub_snprintf (suffix, GRUB_NET_MAX_STR_ADDR_LEN, "%s", buf);
f725e3
+              if (search_through (1, 0) == 0) return GRUB_ERR_NONE;
f725e3
+              break;
f725e3
+            }
f725e3
+        case GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV:
f725e3
+          return grub_error (GRUB_ERR_BUG, "shouldn't reach here");
f725e3
+        default:
f725e3
+          return grub_error (GRUB_ERR_BUG,
f725e3
+                             "unsupported address type %d", (&inf->address)->type);
f725e3
+        }
f725e3
+    }
f725e3
+
f725e3
+  /* Remove the remaining minus sign at the end. */
f725e3
+  config[config_len] = '\0';
f725e3
+
f725e3
+  return GRUB_ERR_NONE;
f725e3
+}
f725e3
+
f725e3
 static struct grub_preboot *fini_hnd;
f725e3
 
f725e3
 static grub_command_t cmd_addaddr, cmd_deladdr, cmd_addroute, cmd_delroute;
f725e3
diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c
f725e3
index f72844c9f37..85d2a2862d4 100644
f725e3
--- a/grub-core/normal/main.c
f725e3
+++ b/grub-core/normal/main.c
f725e3
@@ -33,6 +33,7 @@
f725e3
 #include <grub/charset.h>
f725e3
 #include <grub/script_sh.h>
f725e3
 #include <grub/bufio.h>
f725e3
+#include <grub/net.h>
f725e3
 #ifdef GRUB_MACHINE_IEEE1275
f725e3
 #include <grub/ieee1275/ieee1275.h>
f725e3
 #endif
f725e3
@@ -365,10 +366,19 @@ grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
f725e3
 
f725e3
       prefix = grub_env_get ("prefix");
f725e3
       if (prefix)
f725e3
-	{
f725e3
-	  config = grub_xasprintf ("%s/grub.cfg", prefix);
f725e3
-	  if (! config)
f725e3
-	    goto quit;
f725e3
+        {
f725e3
+          grub_size_t config_len;
f725e3
+          config_len = grub_strlen (prefix) +
f725e3
+                      sizeof ("/grub.cfg-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
f725e3
+          config = grub_malloc (config_len);
f725e3
+
f725e3
+          if (! config)
f725e3
+            goto quit;
f725e3
+
f725e3
+          grub_snprintf (config, config_len, "%s/grub.cfg", prefix);
f725e3
+
f725e3
+          if (grub_strncmp (prefix + 1, "tftp", sizeof ("tftp") - 1) == 0)
f725e3
+            grub_net_search_configfile (config);
f725e3
 
f725e3
 	  grub_enter_normal_mode (config);
f725e3
 	  grub_free (config);
f725e3
diff --git a/include/grub/net.h b/include/grub/net.h
f725e3
index 59e5975b11f..88fc71ceffe 100644
f725e3
--- a/include/grub/net.h
f725e3
+++ b/include/grub/net.h
f725e3
@@ -542,4 +542,7 @@ extern char *grub_net_default_server;
f725e3
 
f725e3
 #define VLANTAG_IDENTIFIER 0x8100
f725e3
 
f725e3
+grub_err_t
f725e3
+grub_net_search_configfile (char *config);
f725e3
+
f725e3
 #endif /* ! GRUB_NET_HEADER */