dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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