From 7292c00f4ba9338c6776227392863e54d75621b3 Mon Sep 17 00:00:00 2001
From: Dinar Valeev <dvaleev@suse.de>
Date: Tue, 6 Sep 2011 14:04:32 +0200
Subject: [PATCH] use pbzip2 or pigz if available

---
 build_kiwi.sh |   34 +++++++++++++++++++++++-----------
 1 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/build_kiwi.sh b/build_kiwi.sh
index 09d7188..4cad280 100644
--- a/build_kiwi.sh
+++ b/build_kiwi.sh
@@ -4,6 +4,20 @@ run_kiwi()
     imagetype=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$SPECFILE imagetype)
     imagename=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$SPECFILE filename)
     imageversion=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$SPECFILE version)
+    GZIPCMD=""
+    BZIPCMD=""
+    # define gzip as pigz if it available in BUILDROOT
+    if [ -x "$BUILD_ROOT/usr/bin/pigz" ];then
+	GZIPCMD="pigz"
+    else
+	GZIPCMD="gzip"
+    fi
+    # define bzip2 as pbzip2 if it available in BUILDROOT
+    if [ -x "$BUILD_ROOT/usr/bin/pbzip2" ];then
+	BZIPCMD="pbzip2"
+    else
+	BZIPCMD="bzip2"
+    fi
     # prepare rpms as source and createrepo on the repositories
     ln -sf $TOPDIR/SOURCES/repos $BUILD_ROOT/repos
     cd $BUILD_ROOT/$TOPDIR/SOURCES/repos
@@ -114,10 +128,10 @@ run_kiwi()
 	for imgtype in $imagetype ; do
 	    echo "running kiwi --prepare for $imgtype..."
 	    # Do not use $BUILD_USER here, since we always need root permissions
-	    if chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --prepare $TOPDIR/SOURCES --logfile terminal --root $TOPDIR/KIWIROOT-$imgtype" - root < /dev/null ; then
+	    if chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --prepare $TOPDIR/SOURCES --logfile terminal --root $TOPDIR/KIWIROOT-$imgtype --gzip-cmd $GZIPCMD" - root < /dev/null ; then
 		echo "running kiwi --create for $imgtype..."
 		mkdir -p $BUILD_ROOT/$TOPDIR/KIWI-$imgtype
-		chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --create $TOPDIR/KIWIROOT-$imgtype --logfile terminal --type $imgtype -d $TOPDIR/KIWI-$imgtype" - root < /dev/null || cleanup_and_exit 1
+		chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --create $TOPDIR/KIWIROOT-$imgtype --logfile terminal --type $imgtype -d $TOPDIR/KIWI-$imgtype --gzip-cmd $GZIPCMD" - root < /dev/null || cleanup_and_exit 1
 	    else
 		cleanup_and_exit 1
 	    fi
@@ -157,8 +171,7 @@ if [ -e "$imageout.raw" ]; then
 	popd
 fi
 
-tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-raw.tar.bz2" \
-	--exclude="$imageout.iso" --exclude="$imageout.raw" *
+tar cvS --exclude="$imageout.iso" --exclude="$imageout.raw" * | $BZIPCMD -c > "/$TOPDIR/KIWI/$imageout$buildnum-raw.tar.bz2"
 cd /$TOPDIR/KIWI
 if [ -x /usr/bin/sha256sum ]; then
    /usr/bin/sha256sum "$imageout$buildnum-raw.tar.bz2" > "$imageout$buildnum-raw.tar.bz2.sha256"
@@ -176,12 +189,12 @@ for i in "$imageout.vmx" "$imageout.vmdk" "$imageout-disk*.vmdk" "$imageout.ovf"
 	ls \$i >& /dev/null && VMXFILES="\$VMXFILES \$i"
 done
 if [ -n "\$VMXFILES" ]; then
-	tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2" \$VMXFILES
+	tar cvS \$VMXFILES | $BZIPCMD -c > "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2"
 	SHAFILES="\$SHAFILES $imageout$buildnum-vmx.tar.bz2"
 fi
 
 if [ -e "$imageout.xenconfig" ]; then
-	tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2" $imageout.xenconfig $imageout.raw initrd-*
+	tar cvS $imageout.xenconfig $imageout.raw initrd-* | $BZIPCMD -c > "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2"
 	SHAFILES="\$SHAFILES $imageout$buildnum-vmx.tar.bz2"
 fi
 # FIXME: do we need a single .raw file in any case ?
@@ -199,12 +212,11 @@ EOF
 		    cat > $BUILD_ROOT/kiwi_post.sh << EOF
 echo "compressing xen images... "
 cd /$TOPDIR/KIWI-xen
-tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-xen.tar.bz2" \
-	`grep ^kernel $imageout.xenconfig | cut -d'"'  -f2` \
+tar cvS `grep ^kernel $imageout.xenconfig | cut -d'"'  -f2` \
 	`grep ^ramdisk $imageout.xenconfig | cut -d'"'  -f2` \
 	initrd-* \
 	"$imageout.xenconfig" \
-	"$imageout"
+	"$imageout" | $BZIPCMD -c > "/$TOPDIR/KIWI/$imageout$buildnum-xen.tar.bz2"
 if [ -x /usr/bin/sha256sum ]; then
    echo "Create sha256 file..."
    cd $TOPDIR/KIWI
@@ -216,7 +228,7 @@ EOF
 		    cat > $BUILD_ROOT/kiwi_post.sh << EOF
 echo "compressing pxe images... "
 cd /$TOPDIR/KIWI-pxe
-tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-pxe.tar.bz2" ${imageout}* initrd-*
+tar cvS ${imageout}* initrd-* | $BZIPCMD -c > "/$TOPDIR/KIWI/$imageout$buildnum-pxe.tar.bz2"
 if [ -x /usr/bin/sha256sum ]; then
    echo "Create sha256 file..."
    cd $TOPDIR/KIWI
@@ -243,7 +255,7 @@ EOF
 		    cat > $BUILD_ROOT/kiwi_post.sh << EOF
 echo "compressing unkown images... "
 cd /$TOPDIR/KIWI-$imgtype
-tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-$imgtype.tar.bz2" *
+tar cvS * | $BZIPCMD -c > "/$TOPDIR/KIWI/$imageout$buildnum-$imgtype.tar.bz2"
 if [ -x /usr/bin/sha256sum ]; then
    echo "Create sha256 file..."
    cd /$TOPDIR/KIWI
-- 
1.6.0.2

