nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0156-Use-Distribution-Package-Sort-for-grub2-mkconfig-112.patch

f725e3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f725e3
From: Robert Marshall <rmarshall@redhat.com>
f725e3
Date: Mon, 16 Mar 2015 14:14:19 -0400
f725e3
Subject: [PATCH] Use Distribution Package Sort for grub2-mkconfig (#1124074)
f725e3
f725e3
Users reported that newly installed kernels on their systems installed
f725e3
with grub-mkconfig would not appear on the grub boot list in order
f725e3
starting with the most recent. Added an option for rpm-based systems to
f725e3
use the rpm-sort library to sort kernels instead.
f725e3
f725e3
Resolves rhbz#1124074
f725e3
---
f725e3
 configure.ac              |  29 +++++
f725e3
 Makefile.util.def         |  16 +++
f725e3
 util/grub-rpm-sort.c      | 281 ++++++++++++++++++++++++++++++++++++++++++++++
f725e3
 .gitignore                |   1 +
f725e3
 util/grub-mkconfig_lib.in |   8 +-
f725e3
 util/grub-rpm-sort.8      |  12 ++
f725e3
 6 files changed, 346 insertions(+), 1 deletion(-)
f725e3
 create mode 100644 util/grub-rpm-sort.c
f725e3
 create mode 100644 util/grub-rpm-sort.8
f725e3
f725e3
diff --git a/configure.ac b/configure.ac
f725e3
index 048ce7c89fa..de17e65c4b7 100644
f725e3
--- a/configure.ac
f725e3
+++ b/configure.ac
f725e3
@@ -65,6 +65,7 @@ grub_TRANSFORM([grub-mkrelpath])
f725e3
 grub_TRANSFORM([grub-mkrescue])
f725e3
 grub_TRANSFORM([grub-probe])
f725e3
 grub_TRANSFORM([grub-reboot])
f725e3
+grub_TRANSFORM([grub-rpm-sort])
f725e3
 grub_TRANSFORM([grub-script-check])
f725e3
 grub_TRANSFORM([grub-set-default])
f725e3
 grub_TRANSFORM([grub-sparc64-setup])
f725e3
@@ -89,6 +90,7 @@ grub_TRANSFORM([grub-mkrescue.1])
f725e3
 grub_TRANSFORM([grub-mkstandalone.3])
f725e3
 grub_TRANSFORM([grub-ofpathname.3])
f725e3
 grub_TRANSFORM([grub-probe.3])
f725e3
+grub_TRANSFORM([grub-rpm-sort.8])
f725e3
 grub_TRANSFORM([grub-reboot.3])
f725e3
 grub_TRANSFORM([grub-render-label.3])
f725e3
 grub_TRANSFORM([grub-script-check.3])
f725e3
@@ -1543,6 +1545,33 @@ fi
f725e3
 
f725e3
 AC_SUBST([LIBDEVMAPPER])
f725e3
 
f725e3
+AC_ARG_ENABLE([rpm-sort],
f725e3
+              [AS_HELP_STRING([--enable-rpm-sort],
f725e3
+                              [enable native rpm sorting of kernels in grub (default=guessed)])])
f725e3
+if test x"$enable_rpm-sort" = xno ; then
f725e3
+  rpm_sort_excuse="explicitly disabled"
f725e3
+fi
f725e3
+
f725e3
+if test x"$rpm_sort_excuse" = x ; then
f725e3
+  # Check for rpmlib header.
f725e3
+  AC_CHECK_HEADER([rpm/rpmlib.h], [],
f725e3
+               [rpm_sort_excuse="need rpm/rpmlib header"])
f725e3
+fi
f725e3
+
f725e3
+if test x"$rpm_sort_excuse" = x ; then
f725e3
+  # Check for rpm library.
f725e3
+  AC_CHECK_LIB([rpm], [rpmvercmp], [],
f725e3
+               [rpm_sort_excuse="rpmlib missing rpmvercmp"])
f725e3
+fi
f725e3
+
f725e3
+if test x"$rpm_sort_excuse" = x ; then
f725e3
+   LIBRPM="-lrpm";
f725e3
+   AC_DEFINE([HAVE_RPM], [1],
f725e3
+             [Define to 1 if you have the rpm library.])
f725e3
+fi
f725e3
+
f725e3
+AC_SUBST([LIBRPM])
f725e3
+
f725e3
 LIBGEOM=
f725e3
 if test x$host_kernel = xkfreebsd; then
f725e3
   AC_CHECK_LIB([geom], [geom_gettree], [],
f725e3
diff --git a/Makefile.util.def b/Makefile.util.def
f725e3
index 87029a10dfb..3ac75720466 100644
f725e3
--- a/Makefile.util.def
f725e3
+++ b/Makefile.util.def
f725e3
@@ -675,6 +675,22 @@ program = {
f725e3
   ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBUTIL) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
f725e3
 };
f725e3
 
f725e3
+program = {
f725e3
+  name = grub-rpm-sort;
f725e3
+  mansection = 8;
f725e3
+  installdir = sbin;
f725e3
+
f725e3
+  common = grub-core/kern/emu/misc.c;
f725e3
+  common = grub-core/kern/emu/argp_common.c;
f725e3
+  common = grub-core/osdep/init.c;
f725e3
+  common = util/misc.c;
f725e3
+  common = util/grub-rpm-sort.c;
f725e3
+
f725e3
+  ldadd = grub-core/gnulib/libgnu.a;
f725e3
+  ldadd = libgrubkern.a;
f725e3
+  ldadd = '$(LIBDEVMAPPER) $(LIBRPM)';
f725e3
+};
f725e3
+
f725e3
 script = {
f725e3
   name = grub-mkconfig;
f725e3
   common = util/grub-mkconfig.in;
f725e3
diff --git a/util/grub-rpm-sort.c b/util/grub-rpm-sort.c
f725e3
new file mode 100644
f725e3
index 00000000000..f33bd1ed568
f725e3
--- /dev/null
f725e3
+++ b/util/grub-rpm-sort.c
f725e3
@@ -0,0 +1,281 @@
f725e3
+#include <config.h>
f725e3
+#include <grub/types.h>
f725e3
+#include <grub/util/misc.h>
f725e3
+#include <stdio.h>
f725e3
+#include <stdlib.h>
f725e3
+#include <unistd.h>
f725e3
+#include <errno.h>
f725e3
+#include <assert.h>
f725e3
+#include <argp.h>
f725e3
+#include <rpm/rpmlib.h>
f725e3
+
f725e3
+static size_t
f725e3
+read_file (const char *input, char **ret)
f725e3
+{
f725e3
+  FILE *in;
f725e3
+  size_t s;
f725e3
+  size_t sz = 2048;
f725e3
+  size_t offset = 0;
f725e3
+  char *text;
f725e3
+
f725e3
+  if (!strcmp(input, "-"))
f725e3
+    in = stdin;
f725e3
+  else
f725e3
+    in = grub_util_fopen(input, "r");
f725e3
+
f725e3
+  text = xmalloc (sz);
f725e3
+
f725e3
+  if (!in)
f725e3
+    grub_util_error (_("cannot open `%s': %s"), input, strerror (errno));
f725e3
+
f725e3
+  while ((s = fread (text + offset, 1, sz - offset, in)) != 0)
f725e3
+    {
f725e3
+      offset += s;
f725e3
+      if (sz - offset == 0)
f725e3
+	{
f725e3
+	  sz += 2048;
f725e3
+	  text = xrealloc (text, sz);
f725e3
+	}
f725e3
+    }
f725e3
+
f725e3
+  text[offset] = '\0';
f725e3
+  *ret = text;
f725e3
+
f725e3
+  if (in != stdin)
f725e3
+    fclose(in);
f725e3
+
f725e3
+  return offset + 1;
f725e3
+}
f725e3
+
f725e3
+/* returns name/version/release */
f725e3
+/* NULL string pointer returned if nothing found */
f725e3
+static void
f725e3
+split_package_string (char *package_string, char **name,
f725e3
+                     char **version, char **release)
f725e3
+{
f725e3
+  char *package_version, *package_release;
f725e3
+
f725e3
+  /* Release */
f725e3
+  package_release = strrchr (package_string, '-');
f725e3
+
f725e3
+  if (package_release != NULL)
f725e3
+      *package_release++ = '\0';
f725e3
+
f725e3
+  *release = package_release;
f725e3
+
f725e3
+  /* Version */
f725e3
+  package_version = strrchr(package_string, '-');
f725e3
+
f725e3
+  if (package_version != NULL)
f725e3
+      *package_version++ = '\0';
f725e3
+
f725e3
+  *version = package_version;
f725e3
+  /* Name */
f725e3
+  *name = package_string;
f725e3
+
f725e3
+  /* Bubble up non-null values from release to name */
f725e3
+  if (*name == NULL)
f725e3
+    {
f725e3
+      *name = (*version == NULL ? *release : *version);
f725e3
+      *version = *release;
f725e3
+      *release = NULL;
f725e3
+    }
f725e3
+  if (*version == NULL)
f725e3
+    {
f725e3
+      *version = *release;
f725e3
+      *release = NULL;
f725e3
+    }
f725e3
+}
f725e3
+
f725e3
+/*
f725e3
+ * package name-version-release comparator for qsort
f725e3
+ * expects p, q which are pointers to character strings (char *)
f725e3
+ * which will not be altered in this function
f725e3
+ */
f725e3
+static int
f725e3
+package_version_compare (const void *p, const void *q)
f725e3
+{
f725e3
+  char *local_p, *local_q;
f725e3
+  char *lhs_name, *lhs_version, *lhs_release;
f725e3
+  char *rhs_name, *rhs_version, *rhs_release;
f725e3
+  int vercmpflag = 0;
f725e3
+
f725e3
+  local_p = alloca (strlen (*(char * const *)p) + 1);
f725e3
+  local_q = alloca (strlen (*(char * const *)q) + 1);
f725e3
+
f725e3
+  /* make sure these allocated */
f725e3
+  assert (local_p);
f725e3
+  assert (local_q);
f725e3
+
f725e3
+  strcpy (local_p, *(char * const *)p);
f725e3
+  strcpy (local_q, *(char * const *)q);
f725e3
+
f725e3
+  split_package_string (local_p, &lhs_name, &lhs_version, &lhs_release);
f725e3
+  split_package_string (local_q, &rhs_name, &rhs_version, &rhs_release);
f725e3
+
f725e3
+  /* Check Name and return if unequal */
f725e3
+  vercmpflag = rpmvercmp ((lhs_name == NULL ? "" : lhs_name),
f725e3
+                          (rhs_name == NULL ? "" : rhs_name));
f725e3
+  if (vercmpflag != 0)
f725e3
+    return vercmpflag;
f725e3
+
f725e3
+  /* Check version and return if unequal */
f725e3
+  vercmpflag = rpmvercmp ((lhs_version == NULL ? "" : lhs_version),
f725e3
+                          (rhs_version == NULL ? "" : rhs_version));
f725e3
+  if (vercmpflag != 0)
f725e3
+    return vercmpflag;
f725e3
+
f725e3
+  /* Check release and return the version compare value */
f725e3
+  vercmpflag = rpmvercmp ((lhs_release == NULL ? "" : lhs_release),
f725e3
+                          (rhs_release == NULL ? "" : rhs_release));
f725e3
+
f725e3
+  return vercmpflag;
f725e3
+}
f725e3
+
f725e3
+static void
f725e3
+add_input (const char *filename, char ***package_names, size_t *n_package_names)
f725e3
+{
f725e3
+  char *orig_input_buffer = NULL;
f725e3
+  char *input_buffer;
f725e3
+  char *position_of_newline;
f725e3
+  char **names = *package_names;
f725e3
+  char **new_names = NULL;
f725e3
+  size_t n_names = *n_package_names;
f725e3
+
f725e3
+  if (!*package_names)
f725e3
+    new_names = names = xmalloc (sizeof (char *) * 2);
f725e3
+
f725e3
+  if (read_file (filename, &orig_input_buffer) < 2)
f725e3
+    {
f725e3
+      if (new_names)
f725e3
+	free (new_names);
f725e3
+      if (orig_input_buffer)
f725e3
+	free (orig_input_buffer);
f725e3
+      return;
f725e3
+    }
f725e3
+
f725e3
+  input_buffer = orig_input_buffer;
f725e3
+  while (input_buffer && *input_buffer &&
f725e3
+	 (position_of_newline = strchrnul (input_buffer, '\n')))
f725e3
+    {
f725e3
+      size_t sz = position_of_newline - input_buffer;
f725e3
+      char *new;
f725e3
+
f725e3
+      if (sz == 0)
f725e3
+	{
f725e3
+	  input_buffer = position_of_newline + 1;
f725e3
+	  continue;
f725e3
+	}
f725e3
+
f725e3
+      new = xmalloc (sz+1);
f725e3
+      strncpy (new, input_buffer, sz);
f725e3
+      new[sz] = '\0';
f725e3
+
f725e3
+      names = xrealloc (names, sizeof (char *) * (n_names + 1));
f725e3
+      names[n_names] = new;
f725e3
+      n_names++;
f725e3
+
f725e3
+      /* move buffer ahead to next line */
f725e3
+      input_buffer = position_of_newline + 1;
f725e3
+      if (*position_of_newline == '\0')
f725e3
+	input_buffer = NULL;
f725e3
+    }
f725e3
+
f725e3
+  free (orig_input_buffer);
f725e3
+
f725e3
+  *package_names = names;
f725e3
+  *n_package_names = n_names;
f725e3
+}
f725e3
+
f725e3
+static char *
f725e3
+help_filter (int key, const char *text, void *input __attribute__ ((unused)))
f725e3
+{
f725e3
+  return (char *)text;
f725e3
+}
f725e3
+
f725e3
+static struct argp_option options[] = {
f725e3
+  { 0, }
f725e3
+};
f725e3
+
f725e3
+struct arguments
f725e3
+{
f725e3
+  size_t ninputs;
f725e3
+  size_t input_max;
f725e3
+  char **inputs;
f725e3
+};
f725e3
+
f725e3
+static error_t
f725e3
+argp_parser (int key, char *arg, struct argp_state *state)
f725e3
+{
f725e3
+  struct arguments *arguments = state->input;
f725e3
+  switch (key)
f725e3
+    {
f725e3
+    case ARGP_KEY_ARG:
f725e3
+      assert (arguments->ninputs < arguments->input_max);
f725e3
+      arguments->inputs[arguments->ninputs++] = xstrdup (arg);
f725e3
+      break;
f725e3
+    default:
f725e3
+      return ARGP_ERR_UNKNOWN;
f725e3
+    }
f725e3
+  return 0;
f725e3
+}
f725e3
+
f725e3
+static struct argp argp = {
f725e3
+  options, argp_parser, N_("[INPUT_FILES]"),
f725e3
+  N_("Sort a list of strings in RPM version sort order."),
f725e3
+  NULL, help_filter, NULL
f725e3
+};
f725e3
+
f725e3
+int
f725e3
+main (int argc, char *argv[])
f725e3
+{
f725e3
+  struct arguments arguments;
f725e3
+  char **package_names = NULL;
f725e3
+  size_t n_package_names = 0;
f725e3
+  int i;
f725e3
+
f725e3
+  grub_util_host_init (&argc, &argv);
f725e3
+
f725e3
+  memset (&arguments, 0, sizeof (struct arguments));
f725e3
+  arguments.input_max = argc+1;
f725e3
+  arguments.inputs = xmalloc ((arguments.input_max + 1)
f725e3
+			      * sizeof (arguments.inputs[0]));
f725e3
+  memset (arguments.inputs, 0, (arguments.input_max + 1)
f725e3
+	  * sizeof (arguments.inputs[0]));
f725e3
+
f725e3
+  /* Parse our arguments */
f725e3
+  if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
f725e3
+    grub_util_error ("%s", _("Error in parsing command line arguments\n"));
f725e3
+
f725e3
+  /* If there's no inputs in argv, add one for stdin */
f725e3
+  if (!arguments.ninputs)
f725e3
+    {
f725e3
+      arguments.ninputs = 1;
f725e3
+      arguments.inputs[0] = xmalloc (2);
f725e3
+      strcpy(arguments.inputs[0], "-");
f725e3
+    }
f725e3
+
f725e3
+  for (i = 0; i < arguments.ninputs; i++)
f725e3
+    add_input(arguments.inputs[i], &package_names, &n_package_names);
f725e3
+
f725e3
+  if (package_names == NULL || n_package_names < 1)
f725e3
+    grub_util_error ("%s", _("Invalid input\n"));
f725e3
+
f725e3
+  qsort (package_names, n_package_names, sizeof (char *),
f725e3
+	 package_version_compare);
f725e3
+
f725e3
+  /* send sorted list to stdout */
f725e3
+  for (i = 0; i < n_package_names; i++)
f725e3
+    {
f725e3
+      fprintf (stdout, "%s\n", package_names[i]);
f725e3
+      free (package_names[i]);
f725e3
+    }
f725e3
+
f725e3
+  free (package_names);
f725e3
+  for (i = 0; i < arguments.ninputs; i++)
f725e3
+    free (arguments.inputs[i]);
f725e3
+
f725e3
+  free (arguments.inputs);
f725e3
+
f725e3
+  return 0;
f725e3
+}
f725e3
diff --git a/.gitignore b/.gitignore
f725e3
index 18ab8e81278..b6c45776a4d 100644
f725e3
--- a/.gitignore
f725e3
+++ b/.gitignore
f725e3
@@ -237,6 +237,7 @@ po/POTFILES.in
f725e3
 po/POTFILES-shell.in
f725e3
 /grub-glue-efi
f725e3
 /grub-render-label
f725e3
+grub-rpm-sort
f725e3
 /grub-glue-efi.exe
f725e3
 /grub-render-label.exe
f725e3
 grub-core/gnulib/locale.h
f725e3
diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in
f725e3
index 5c9ed84a5e7..e85b60c322a 100644
f725e3
--- a/util/grub-mkconfig_lib.in
f725e3
+++ b/util/grub-mkconfig_lib.in
f725e3
@@ -213,6 +213,12 @@ version_sort ()
f725e3
    esac
f725e3
 }
f725e3
 
f725e3
+if [ "x$RPMLIB" = x ]; then
f725e3
+  kernel_sort=version_sort
f725e3
+else
f725e3
+  kernel_sort="${sbindir}/grub-rpm-sort"
f725e3
+fi
f725e3
+
f725e3
 version_test_numeric ()
f725e3
 {
f725e3
   version_test_numeric_a="$1"
f725e3
@@ -229,7 +235,7 @@ version_test_numeric ()
f725e3
     version_test_numeric_a="$version_test_numeric_b"
f725e3
     version_test_numeric_b="$version_test_numeric_c"
f725e3
   fi
f725e3
-  if (echo "$version_test_numeric_a" ; echo "$version_test_numeric_b") | version_sort | head -n 1 | grep -qx "$version_test_numeric_b" ; then
f725e3
+  if (echo "$version_test_numeric_a" ; echo "$version_test_numeric_b") | "$kernel_sort" | head -n 1 | grep -qx "$version_test_numeric_b" ; then
f725e3
     return 0
f725e3
   else
f725e3
     return 1
f725e3
diff --git a/util/grub-rpm-sort.8 b/util/grub-rpm-sort.8
f725e3
new file mode 100644
f725e3
index 00000000000..8ce21488448
f725e3
--- /dev/null
f725e3
+++ b/util/grub-rpm-sort.8
f725e3
@@ -0,0 +1,12 @@
f725e3
+.TH GRUB-RPM-SORT 8 "Wed Feb 26 2014"
f725e3
+.SH NAME
f725e3
+\fBgrub-rpm-sort\fR \(em Sort input according to RPM version compare.
f725e3
+
f725e3
+.SH SYNOPSIS
f725e3
+\fBgrub-rpm-sort\fR [OPTIONS].
f725e3
+
f725e3
+.SH DESCRIPTION
f725e3
+You should not normally run this program directly.  Use grub-mkconfig instead.
f725e3
+
f725e3
+.SH SEE ALSO
f725e3
+.BR "info grub"