From e2c0d16f84fa649b1be4ef79798dd82d5d974821 Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Wed, 20 Apr 2016 09:42:42 +1000
Subject: [PATCH] yum-minimal : better cleanup of initial yum failure

If the initial yum install into the chroot fails, we can leave behind
a lockfile and an incorrectly modified rpmmacros.

Change this so we run the cleanup unconditionally.

Change-Id: Ia9f9c4c845e5f34d33ff9a4ab7226c9175283757
---
 elements/yum-minimal/root.d/08-yum-chroot | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/elements/yum-minimal/root.d/08-yum-chroot b/elements/yum-minimal/root.d/08-yum-chroot
index 2d0850814..ebde3127b 100755
--- a/elements/yum-minimal/root.d/08-yum-chroot
+++ b/elements/yum-minimal/root.d/08-yum-chroot
@@ -124,15 +124,22 @@ function _install_pkg_manager {
     (
         flock -w 1200 9 || die "Can not lock .rpmmacros"
         echo "%_dbpath /var/lib/rpm" >> $HOME/.rpmmacros
+
         sudo -E yum -y \
             --setopt=cachedir=$YUM_CACHE/$ARCH/$DIB_RELEASE \
             --setopt=reposdir=$TARGET_ROOT/etc/yum.repos.d \
             --releasever=$DIB_RELEASE \
             --installroot $TARGET_ROOT \
-            install $@
+            install $@ && rc=$? || rc=$?
+
+        # We modified the base system - make sure we clean up always!
+        rm $HOME/.rpmmacros.dib.lock
         sed -i '$ d' $HOME/.rpmmacros
+        if [ $rc != 0 ]; then
+            die "Initial yum install to chroot failed!  Can not continue."
+        fi
     ) 9>$HOME/.rpmmacros.dib.lock
-    rm $HOME/.rpmmacros.dib.lock
+
     # Set gpg path back because subsequent actions will take place in
     # the chroot
     sudo sed -i "s,$TARGET_ROOT/etc/pki/rpm-gpg,/etc/pki/rpm-gpg,g" \