nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0124-Reimplement-boot_counter.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Peter Jones <pjones@redhat.com>
8e15ce
Date: Thu, 4 Oct 2018 14:22:09 -0400
8e15ce
Subject: [PATCH] Reimplement boot_counter
8e15ce
8e15ce
This adds "increment" and "decrement" commands, and uses them to maintain our
8e15ce
variables in 01_fallback_counter.  It also simplifies the counter logic, so
8e15ce
that there are no nested tests that conflict with each other.
8e15ce
8e15ce
Apparently, this *really* wasn't tested well enough.
8e15ce
8e15ce
Resolves: rhbz#1614637
8e15ce
Signed-off-by: Peter Jones <pjones@redhat.com>
8e15ce
[lorbus: add comments and revert logic changes in 01_fallback_counting]
8e15ce
Signed-off-by: Christian Glombek <lorbus@fedoraproject.org>
8e15ce
---
8e15ce
 Makefile.util.def                   |   6 +++
8e15ce
 grub-core/Makefile.core.def         |   5 ++
8e15ce
 grub-core/commands/increment.c      | 105 ++++++++++++++++++++++++++++++++++++
8e15ce
 util/grub.d/01_fallback_counting.in |  22 ++++++++
8e15ce
 4 files changed, 138 insertions(+)
8e15ce
 create mode 100644 grub-core/commands/increment.c
8e15ce
 create mode 100644 util/grub.d/01_fallback_counting.in
8e15ce
8e15ce
diff --git a/Makefile.util.def b/Makefile.util.def
8e15ce
index c6375933faa..2e5e05b25f1 100644
8e15ce
--- a/Makefile.util.def
8e15ce
+++ b/Makefile.util.def
8e15ce
@@ -458,6 +458,12 @@ script = {
8e15ce
   installdir = grubconf;
8e15ce
 };
8e15ce
 
8e15ce
+script = {
8e15ce
+  name = '01_fallback_counting';
8e15ce
+  common = util/grub.d/01_fallback_counting.in;
8e15ce
+  installdir = grubconf;
8e15ce
+};
8e15ce
+
8e15ce
 script = {
8e15ce
   name = '01_menu_auto_hide';
8e15ce
   common = util/grub.d/01_menu_auto_hide.in;
8e15ce
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
8e15ce
index 498ca11762a..1e15345107e 100644
8e15ce
--- a/grub-core/Makefile.core.def
8e15ce
+++ b/grub-core/Makefile.core.def
8e15ce
@@ -398,6 +398,11 @@ kernel = {
8e15ce
   extra_dist = kern/mips/cache_flush.S;
8e15ce
 };
8e15ce
 
8e15ce
+module = {
8e15ce
+  name = increment;
8e15ce
+  common = commands/increment.c;
8e15ce
+};
8e15ce
+
8e15ce
 program = {
8e15ce
   name = grub-emu;
8e15ce
   mansection = 1;
8e15ce
diff --git a/grub-core/commands/increment.c b/grub-core/commands/increment.c
8e15ce
new file mode 100644
8e15ce
index 00000000000..79cf137656c
8e15ce
--- /dev/null
8e15ce
+++ b/grub-core/commands/increment.c
8e15ce
@@ -0,0 +1,105 @@
8e15ce
+/* increment.c - Commands to increment and decrement variables. */
8e15ce
+/*
8e15ce
+ *  GRUB  --  GRand Unified Bootloader
8e15ce
+ *  Copyright (C) 2006,2007,2008  Free Software Foundation, Inc.
8e15ce
+ *
8e15ce
+ *  GRUB is free software: you can redistribute it and/or modify
8e15ce
+ *  it under the terms of the GNU General Public License as published by
8e15ce
+ *  the Free Software Foundation, either version 3 of the License, or
8e15ce
+ *  (at your option) any later version.
8e15ce
+ *
8e15ce
+ *  GRUB is distributed in the hope that it will be useful,
8e15ce
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8e15ce
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8e15ce
+ *  GNU General Public License for more details.
8e15ce
+ *
8e15ce
+ *  You should have received a copy of the GNU General Public License
8e15ce
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
8e15ce
+ */
8e15ce
+
8e15ce
+#include <grub/dl.h>
8e15ce
+#include <grub/term.h>
8e15ce
+#include <grub/time.h>
8e15ce
+#include <grub/types.h>
8e15ce
+#include <grub/misc.h>
8e15ce
+#include <grub/extcmd.h>
8e15ce
+#include <grub/i18n.h>
8e15ce
+#include <grub/env.h>
8e15ce
+
8e15ce
+GRUB_MOD_LICENSE ("GPLv3+");
8e15ce
+
8e15ce
+typedef enum {
8e15ce
+    INCREMENT,
8e15ce
+    DECREMENT,
8e15ce
+} operation;
8e15ce
+
8e15ce
+static grub_err_t
8e15ce
+incr_decr(operation op, int argc, char **args)
8e15ce
+{
8e15ce
+  const char *old;
8e15ce
+  char *new;
8e15ce
+  long value;
8e15ce
+
8e15ce
+  if (argc < 1)
8e15ce
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_ ("no variable specified"));
8e15ce
+  if (argc > 1)
8e15ce
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_ ("too many arguments"));
8e15ce
+
8e15ce
+  old = grub_env_get (*args);
8e15ce
+  if (!old)
8e15ce
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("No such variable \"%s\""),
8e15ce
+		       *args);
8e15ce
+
8e15ce
+  value = grub_strtol (old, NULL, 0);
8e15ce
+  if (grub_errno != GRUB_ERR_NONE)
8e15ce
+    return grub_errno;
8e15ce
+
8e15ce
+  switch (op)
8e15ce
+    {
8e15ce
+    case INCREMENT:
8e15ce
+      value += 1;
8e15ce
+      break;
8e15ce
+    case DECREMENT:
8e15ce
+      value -= 1;
8e15ce
+      break;
8e15ce
+    }
8e15ce
+
8e15ce
+  new = grub_xasprintf ("%ld", value);
8e15ce
+  if (!new)
8e15ce
+    return grub_errno;
8e15ce
+
8e15ce
+  grub_env_set (*args, new);
8e15ce
+  grub_free (new);
8e15ce
+
8e15ce
+  return GRUB_ERR_NONE;
8e15ce
+}
8e15ce
+
8e15ce
+static grub_err_t
8e15ce
+grub_cmd_incr(struct grub_command *cmd UNUSED,
8e15ce
+              int argc, char **args)
8e15ce
+{
8e15ce
+  return incr_decr(INCREMENT, argc, args);
8e15ce
+}
8e15ce
+
8e15ce
+static grub_err_t
8e15ce
+grub_cmd_decr(struct grub_command *cmd UNUSED,
8e15ce
+              int argc, char **args)
8e15ce
+{
8e15ce
+  return incr_decr(DECREMENT, argc, args);
8e15ce
+}
8e15ce
+
8e15ce
+static grub_command_t cmd_incr, cmd_decr;
8e15ce
+
8e15ce
+GRUB_MOD_INIT(increment)
8e15ce
+{
8e15ce
+  cmd_incr = grub_register_command ("increment", grub_cmd_incr, N_("VARIABLE"),
8e15ce
+                                    N_("increment VARIABLE"));
8e15ce
+  cmd_decr = grub_register_command ("decrement", grub_cmd_decr, N_("VARIABLE"),
8e15ce
+                                    N_("decrement VARIABLE"));
8e15ce
+}
8e15ce
+
8e15ce
+GRUB_MOD_FINI(increment)
8e15ce
+{
8e15ce
+  grub_unregister_command (cmd_incr);
8e15ce
+  grub_unregister_command (cmd_decr);
8e15ce
+}
8e15ce
diff --git a/util/grub.d/01_fallback_counting.in b/util/grub.d/01_fallback_counting.in
8e15ce
new file mode 100644
8e15ce
index 00000000000..be0e770ea82
8e15ce
--- /dev/null
8e15ce
+++ b/util/grub.d/01_fallback_counting.in
8e15ce
@@ -0,0 +1,22 @@
8e15ce
+#! /bin/sh -e
8e15ce
+
8e15ce
+# Boot Counting
8e15ce
+# The boot_counter env var can be used to count down boot attempts after an
8e15ce
+# OSTree upgrade and choose the rollback deployment when 0 is reached.  Both
8e15ce
+# boot_counter and boot_success need to be (re-)set from userspace.
8e15ce
+cat << EOF
8e15ce
+insmod increment
8e15ce
+# Check if boot_counter exists and boot_success=0 to activate this behaviour.
8e15ce
+if [ -n "\${boot_counter}" -a "\${boot_success}" = "0" ]; then
8e15ce
+  # if countdown has ended, choose to boot rollback deployment (default=1 on
8e15ce
+  # OSTree-based systems)
8e15ce
+  if  [ "\${boot_counter}" = "0" -o "\${boot_counter}" = "-1" ]; then
8e15ce
+    set default=1
8e15ce
+    set boot_counter=-1
8e15ce
+  # otherwise decrement boot_counter
8e15ce
+  else
8e15ce
+    decrement boot_counter
8e15ce
+  fi
8e15ce
+  save_env boot_counter
8e15ce
+fi
8e15ce
+EOF