From 6d69d7909d28a7e6a4bfd12f641dd5a19b70ee72 Mon Sep 17 00:00:00 2001
From: Chhavi Agarwal <chhagarw@in.ibm.com>
Date: Wed, 12 Apr 2017 01:26:27 -0400
Subject: [PATCH] Support for Cloud Images on ppc64le for rhel7 and centos7

In order to support {CentOS,RHEL}7 for building cloud images we need to
handle the differences in grub packaging from Ubuntu.  We also need to
populate the defualt location for cloud images for CentOS builds.

Change-Id: Ie0d82ff21a42b08c4cb94b7a5635f80bfabf684e
---
 .../elements/bootloader/finalise.d/50-bootloader | 16 +++++++++-------
 diskimage_builder/elements/bootloader/pkg-map    |  6 ++++--
 diskimage_builder/elements/centos7/README.rst    |  7 +++++++
 .../centos7/root.d/10-centos7-cloud-image        |  4 +++-
 .../elements/rhel7/root.d/10-rhel7-cloud-image   |  5 ++++-
 .../pre-install.d/01-override-yum-arch           |  6 +++---
 diskimage_builder/lib/common-defaults            |  5 +----
 7 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/diskimage_builder/elements/bootloader/finalise.d/50-bootloader b/diskimage_builder/elements/bootloader/finalise.d/50-bootloader
index d19846a78..875fbcf78 100755
--- a/diskimage_builder/elements/bootloader/finalise.d/50-bootloader
+++ b/diskimage_builder/elements/bootloader/finalise.d/50-bootloader
@@ -55,8 +55,11 @@ function install_grub2 {
     # Check for offline installation of grub
     if [ -f "/tmp/grub/install" ] ; then
         source /tmp/grub/install
+    # Right now we can't use pkg-map to branch by arch, so tag an architecture
+    # specific virtual package so we can install the rigth thing based on
+    # distribution.
     elif [[ "$ARCH" =~ "ppc" ]]; then
-        install-packages grub-ieee1275
+        install-packages -m bootloader grub-ppc64
     else
         install-packages -m bootloader grub-pc
     fi
@@ -117,12 +120,11 @@ function install_grub2 {
     fi
 
     if [[ "$ARCH" =~ "ppc" ]] ; then
-        # For PPC, we use the "boot" partition as the one to point
-        # grub-install to, not the loopback device.  ppc has a
-        # dedicated PReP boot partition.  This seems to only work on
-        # /dev/mapper/... nodes due to matching rules in grub
-        # XXX : this may not be true.  It seems to work,
-        # but requires some more investigation.
+        # For PPC (64-Bit regardless of Endian-ness), we use the "boot"
+        # partition as the one to point grub-install to, not the loopback
+        # device.  ppc has a dedicated PReP boot partition.
+        # For grub2 < 2.02~beta3 this needs to be a /dev/mapper/... node after
+        # that a dev/loopXpN node will work fine.
         $GRUBNAME --modules="part_msdos" $GRUB_OPTS ${DEVICES[boot]} --no-nvram
     else
         $GRUBNAME --modules="biosdisk part_msdos" $GRUB_OPTS $BOOT_DEV
diff --git a/diskimage_builder/elements/bootloader/pkg-map b/diskimage_builder/elements/bootloader/pkg-map
index 957519561..1436e54be 100644
--- a/diskimage_builder/elements/bootloader/pkg-map
+++ b/diskimage_builder/elements/bootloader/pkg-map
@@ -12,12 +12,14 @@
     },
     "redhat": {
       "extlinux": "syslinux-extlinux",
-      "grub-pc": "grub2-tools grub2"
+      "grub-pc": "grub2-tools grub2",
+      "grub-ppc64": "grub2-tools grub2"
     }
   },
   "default": {
     "dkms_package": "dkms",
     "extlinux": "extlinux",
-    "grub-pc": "grub-pc"
+    "grub-pc": "grub-pc",
+    "grub-ppc64": "grub-ieee1275"
   }
 }
diff --git a/diskimage_builder/elements/centos7/README.rst b/diskimage_builder/elements/centos7/README.rst
index f4e0a2b0a..939e21097 100644
--- a/diskimage_builder/elements/centos7/README.rst
+++ b/diskimage_builder/elements/centos7/README.rst
@@ -12,3 +12,10 @@ DIB_DISTRIBUTION_MIRROR:
                  before running bin/disk-image-create. This URL should point to
                  the directory containing the ``5/6/7`` directories.
    :Example: ``DIB_DISTRIBUTION_MIRROR=http://amirror.com/centos``
+
+DIB_CLOUD_IMAGES
+  :Required: No
+  :Description: Set the desired URL to fetch the images from.  ppc64le:
+                Currently the CentOS community is working on providing the
+                ppc64le images until then you'll need to set this to a local
+                image file.
diff --git a/diskimage_builder/elements/centos7/root.d/10-centos7-cloud-image b/diskimage_builder/elements/centos7/root.d/10-centos7-cloud-image
index 73a41b97b..86426b0ba 100755
--- a/diskimage_builder/elements/centos7/root.d/10-centos7-cloud-image
+++ b/diskimage_builder/elements/centos7/root.d/10-centos7-cloud-image
@@ -15,8 +15,10 @@ if [[ "amd64 x86_64" =~ "$ARCH" ]]; then
 elif [[ "arm64 aarch64" =~ "$ARCH" ]]; then
     ARCH="aarch64"
     DIB_CLOUD_IMAGES=${DIB_CLOUD_IMAGES:-http://cloud.centos.org/altarch/7/images/aarch64}
+elif [[ "ppc64le" =~ "$ARCH" ]]; then
+    DIB_CLOUD_IMAGES=${DIB_CLOUD_IMAGES:-http://cloud.centos.org/altarch/7/images/ppc64le}
 else
-    echo 'centos7 root element only support the x86_64 and aarch64 $ARCH value.'
+    echo 'centos7 root element only support the x86_64, aarch64 and ppc64le values for $ARCH'
     exit 1
 fi
 
diff --git a/diskimage_builder/elements/rhel7/root.d/10-rhel7-cloud-image b/diskimage_builder/elements/rhel7/root.d/10-rhel7-cloud-image
index bc54b1010..2016576c0 100755
--- a/diskimage_builder/elements/rhel7/root.d/10-rhel7-cloud-image
+++ b/diskimage_builder/elements/rhel7/root.d/10-rhel7-cloud-image
@@ -11,8 +11,11 @@ set -o pipefail
 
 if [[ "amd64 x86_64" =~ "$ARCH" ]]; then
     ARCH="x86_64"
+elif [[ "ppc64le" =~ "$ARCH" ]]; then
+    # We don't need to do anything here other than avoid the else clause
+    :
 else
-    echo 'rhel7 root element only support the x86_64 $ARCH value.'
+    echo 'rhel7 root element only supports x86_64 and ppc64le values for $ARCH'
     exit 1
 fi
 
diff --git a/diskimage_builder/elements/rpm-distro/pre-install.d/01-override-yum-arch b/diskimage_builder/elements/rpm-distro/pre-install.d/01-override-yum-arch
index c61114e79..505d3f2bc 100755
--- a/diskimage_builder/elements/rpm-distro/pre-install.d/01-override-yum-arch
+++ b/diskimage_builder/elements/rpm-distro/pre-install.d/01-override-yum-arch
@@ -15,9 +15,9 @@ elif [[ "amd64 x86_64" =~ "$ARCH" ]]; then
 elif [[ "$ARCH" = "ppc64" ]]; then
     basearch=ppc64
     arch=ppc64
-elif [[ "$ARCH" = "ppc64el" ]]; then
-    basearch=ppc64el
-    arch=ppc64el
+elif [[ "$ARCH" = "ppc64le" ]]; then
+    basearch=ppc64le
+    arch=ppc64le
 elif [[ "arm64 aarch64" =~ "$ARCH" ]]; then
     basearch=aarch64
     arch=aarch64
diff --git a/diskimage_builder/lib/common-defaults b/diskimage_builder/lib/common-defaults
index a7d571276..7370e5e3f 100644
--- a/diskimage_builder/lib/common-defaults
+++ b/diskimage_builder/lib/common-defaults
@@ -14,11 +14,11 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
-# options for create-baremetal-image.sh
 if [ -f /etc/debian_version ]; then
   _ARCH=$(dpkg --print-architecture)
 else
   _ARCH=$(uname -m)
+  # Standardise Architecture names
   case $_ARCH in
     "x86_64")
       _ARCH="amd64"
@@ -29,9 +29,6 @@ else
     "aarch64")
       _ARCH="arm64"
       ;;
-    *)
-      echo "WARNING: Unknown architecture: $_ARCH"
-      ;;
   esac
 fi
 ARCH=${ARCH:-$_ARCH}