nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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