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

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