nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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