From 5caeba0c684aa43f70edcecb0a1c45ba6f0fbb6f Mon Sep 17 00:00:00 2001
From: Steve Baker <sbaker@redhat.com>
Date: Mon, 19 Apr 2021 09:16:02 +1200
Subject: [PATCH] Ensure redhat efi packages are reinstalled during finalise

The rhel-8.4 qcow2 base image already has the grub2-efi-x64 package
installed on its single partition which has files installed to
/boot/efi..., however a partitioned image will have an empty /boot/efi
partition when running 50-bootloader. This means dnf will not install
grub2-efi-x64 when requested and /boot/efi will remain empty.

This commit makes the following changes:
- Refactors redhat bootloader pkg-map for the following:
  - Make x86_64/amd64, arm64/aarch64 adjancent so they don't diverge
  - Map grub-efi to packages installed to /usr
  - Map grub-efi-{arch} to packages installed to /boot/efi
- Removes packages grub-efi-{arch} before installing grub-efi and
  grub-efi-{arch}

Change-Id: Ia197feea34f43bd870fed30829b740596e6b2f48
---
 .../bootloader/finalise.d/50-bootloader         |  3 ++-
 diskimage_builder/elements/bootloader/pkg-map   | 17 +++++++++--------
 .../elements/yum/bin/install-packages           |  2 +-
 3 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/diskimage_builder/elements/bootloader/finalise.d/50-bootloader b/diskimage_builder/elements/bootloader/finalise.d/50-bootloader
index 69810f496..171e8a7cb 100755
--- a/diskimage_builder/elements/bootloader/finalise.d/50-bootloader
+++ b/diskimage_builder/elements/bootloader/finalise.d/50-bootloader
@@ -65,7 +65,8 @@ function install_grub2 {
                 "${DIB_BLOCK_DEVICE}" == "gpt" ]]; then
         install-packages -m bootloader grub-pc
     elif [[ "${DIB_BLOCK_DEVICE}" == "efi" ]]; then
-        install-packages -m bootloader grub-efi-$ARCH
+        install-packages -e -m bootloader grub-efi-$ARCH
+        install-packages -m bootloader grub-efi grub-efi-$ARCH
     else
         echo "Failure: I'm not sure what bootloader to install"
         echo "Ensure you have included a block-device-* element"
diff --git a/diskimage_builder/elements/bootloader/pkg-map b/diskimage_builder/elements/bootloader/pkg-map
index 016f21a95..2df593f44 100644
--- a/diskimage_builder/elements/bootloader/pkg-map
+++ b/diskimage_builder/elements/bootloader/pkg-map
@@ -18,20 +18,21 @@
     "redhat": {
       "extlinux": "syslinux-extlinux",
       "grub-pc": "grub2-tools grub2",
-      "grub-efi-amd64": "grub2-tools grub2 grub2-pc grub2-efi-x64 grub2-efi-x64-modules efibootmgr shim-x64",
-      "grub-efi-arm64": "grub2-tools grub2-efi-aa64 grub2-efi-aa64-modules efibootmgr shim-aa64",
-      "grub-efi-aarch64": "grub2-tools grub2-efi-aa64 grub2-efi-aa64-modules efibootmgr shim-aa64",
-      "grub-efi": "grub2-tools grub2-efi efibootmgr",
-      "grub-efi-x86_64": "grub2-tools grub2 grub2-efi-x64 grub2-efi-x64-modules efibootmgr shim-x64",
-      "grub-ppc64": "grub2-tools grub2"
+      "grub-ppc64": "grub2-tools grub2",
+      "grub-efi": "grub2-tools grub2 efibootmgr",
+      "grub-efi-amd64": "grub2-efi-x64 grub2-efi-x64-modules shim-x64",
+      "grub-efi-x86_64": "grub2-efi-x64 grub2-efi-x64-modules shim-x64",
+      "grub-efi-arm64": "grub2-efi-aa64 grub2-efi-aa64-modules shim-aa64",
+      "grub-efi-aarch64": "grub2-efi-aa64 grub2-efi-aa64-modules shim-aa64"
     }
   },
   "default": {
     "dkms_package": "dkms",
     "extlinux": "extlinux",
     "grub-pc": "grub-pc",
-    "grub-efi-amd64": "grub-efi grub-pc-bin efibootmgr",
-    "grub-efi-arm64": "grub-efi-arm64 grub-efi-arm64-bin efibootmgr",
+    "grub-efi": "efibootmgr",
+    "grub-efi-amd64": "grub-efi grub-pc-bin",
+    "grub-efi-arm64": "grub-efi-arm64 grub-efi-arm64-bin",
     "grub-ppc64": "grub-ieee1275"
   }
 }
diff --git a/diskimage_builder/elements/yum/bin/install-packages b/diskimage_builder/elements/yum/bin/install-packages
index 218c72e64..1c0a0219a 100755
--- a/diskimage_builder/elements/yum/bin/install-packages
+++ b/diskimage_builder/elements/yum/bin/install-packages
@@ -53,7 +53,7 @@ while getopts "hud:em:" opt; do
             DOWNLOAD_PATH=$OPTARG
             ;;
         e)
-            ACTION="erase"
+            ACTION="remove"
             ;;
         m)
             MAP_ELEMENT=$OPTARG