Hello World

File build-recipe-kiwi of Package build

#
# KIWI specific functions. Handle with care.
#
################################################################
#
# Copyright (c) 1995-2014 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################


############################################################

# post scriptlet generation functions for legacy bundling
#
# used for kiwi versions below 5.06.106 that do not support
# a bundling method
#

kiwi_post_oem() {
    cat <<-EOF
	echo "compressing oem images... "
	cd /$TOPDIR/KIWI-oem
	# do not store compressed file _and_ uncompressed one
	[ -e "$imageout.gz" ] && rm -f "$imageout"
	if [ -e "$imageout.iso" ]; then
	    echo "take iso file and create sha256..."
	    mv "$imageout.iso" "/$TOPDIR/KIWI/$imageout$buildnum.iso"
	    pushd /$TOPDIR/KIWI
	    if [ -x /usr/bin/sha256sum ]; then
		/usr/bin/sha256sum "$imageout$buildnum.iso" > "$imageout$buildnum.iso.sha256"
	    fi
	    popd
	fi
	if [ -e "$imageout.install.iso" ]; then
	    echo "take install.iso file and create sha256..."
	    mv "$imageout.install.iso" "/$TOPDIR/KIWI/$imageout$buildnum.install.iso"
	    pushd /$TOPDIR/KIWI
	    if [ -x /usr/bin/sha256sum ]; then
		/usr/bin/sha256sum "$imageout$buildnum.install.iso" > "$imageout$buildnum.install.iso.sha256"
	    fi
	    popd
	fi
	if [ -e "$imageout.qcow2" ]; then
	    mv "$imageout.qcow2" "/$TOPDIR/KIWI/$imageout$buildnum.qcow2"
	    pushd /$TOPDIR/KIWI
	    if [ -x /usr/bin/sha256sum ]; then
		echo "Create sha256 file..."
		/usr/bin/sha256sum "$imageout$buildnum.qcow2" > "$imageout$buildnum.qcow2.sha256"
	    fi
	    popd
	fi
	if [ -e "$imageout.raw.install.raw" ]; then
	    compress_tool="bzip2"
	    compress_suffix="bz2"
	    if [ -x /usr/bin/xz ]; then
		# take xz to get support for sparse files
		compress_tool="xz -2"
		compress_suffix="xz"
	    fi
	    mv "$imageout.raw.install.raw" "/$TOPDIR/KIWI/$imageout$buildnum.raw.install.raw"
	    pushd /$TOPDIR/KIWI
	    echo "\$compress_tool raw.install.raw file..."
	    \$compress_tool "$imageout$buildnum.raw.install.raw"
	    if [ -x /usr/bin/sha256sum ]; then
		echo "Create sha256 file..."
		/usr/bin/sha256sum "$imageout$buildnum.raw.install.raw.\${compress_suffix}" > "$imageout$buildnum.raw.install.raw.\${compress_suffix}.sha256"
	    fi
	    popd
	fi
	if [ -e "$imageout.raw" ]; then
	    compress_tool="bzip2"
	    compress_suffix="bz2"
	    if [ -x /usr/bin/xz ]; then
		# take xz to get support for sparse files
		compress_tool="xz -2"
		compress_suffix="xz"
	    fi
	    mv "$imageout.raw" "/$TOPDIR/KIWI/$imageout$buildnum.raw"
	    pushd /$TOPDIR/KIWI
	    echo "\$compress_tool raw file..."
	    \$compress_tool "$imageout$buildnum.raw"
	    if [ -x /usr/bin/sha256sum ]; then
		echo "Create sha256 file..."
		/usr/bin/sha256sum "$imageout$buildnum.raw.\${compress_suffix}" > "$imageout$buildnum.raw.\${compress_suffix}.sha256"
	    fi
	    popd
	fi
	EOF
}

kiwi_post_vmx() {
    cat <<-EOF
	echo "compressing vmx images... "
	cd /$TOPDIR/KIWI-vmx
	compress_tool="bzip2"
	compress_suffix="bz2"
	if [ -x /usr/bin/xz ]; then
	    # take xz to get support for sparse files
	    compress_tool="xz -2"
	    compress_suffix="xz"
	fi
	VMXFILES=""
	SHAFILES=""
	for suffix in "ovf" "qcow2" "ova" "tar" "vhdfixed" "vhd"; do
	    if [ -e "$imageout.\$suffix" ]; then
		if [ "\$suffix" == "vhd" -o "\$suffix" == "vhdfixed" ]; then 
		    mv "$imageout.\$suffix" "/$TOPDIR/KIWI/$imageout$buildnum.\$suffix"
		    pushd /$TOPDIR/KIWI
		    echo "\$compress_tool \$suffix file..."
		    \$compress_tool "$imageout$buildnum.\$suffix"
		    SHAFILES="\$SHAFILES $imageout$buildnum.\$suffix.\${compress_suffix}"
		    popd
		elif [ "\$suffix" == "ovf" ]; then 
		    mv "$imageout.\${suffix}/$imageout.\$suffix" "/$TOPDIR/KIWI/$imageout$buildnum.\$suffix"
		    SHAFILES="\$SHAFILES $imageout$buildnum.\$suffix"
		else 
		    mv "$imageout.\$suffix" "/$TOPDIR/KIWI/$imageout$buildnum.\$suffix"
		    SHAFILES="\$SHAFILES $imageout$buildnum.\$suffix"
		fi
	    fi
	done
	# This option has a number of format parameters
	for i in "$imageout.vmx" "$imageout.vmdk" "$imageout-disk*.vmdk"; do
	    test -e \$i && VMXFILES="\$VMXFILES \$i"
	done
	# take raw files as fallback
	if [ -n "\$VMXFILES" ]; then
	    tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2" \$VMXFILES
	    SHAFILES="\$SHAFILES $imageout$buildnum-vmx.tar.bz2"
	elif [ -z "\$SHAFILES" -a -e  "$imageout.raw" ]; then
	    mv "$imageout.raw" "/$TOPDIR/KIWI/$imageout$buildnum-vmx.raw"
	    pushd /$TOPDIR/KIWI
	    echo "\$compress_tool raw file..."
	    \$compress_tool "$imageout$buildnum-vmx.raw"
	    SHAFILES="\$SHAFILES $imageout$buildnum-vmx.raw.\${compress_suffix}"
	    popd
	fi
	if [ -e "$imageout.box" ]; then
	    tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx-box.tar.bz2" $imageout.box $imageout.json
	    SHAFILES="\$SHAFILES $imageout$buildnum-vmx-box.tar.bz2"
	fi
	if [ -e "$imageout.xenconfig" ]; then
	    tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2" $imageout.xenconfig $imageout.raw initrd-*
	    SHAFILES="\$SHAFILES $imageout$buildnum-vmx.tar.bz2"
	fi
	# FIXME: do we need a single .raw file in any case ?

	cd /$TOPDIR/KIWI
	if [ -n "\$SHAFILES" -a -x /usr/bin/sha256sum ]; then
	    for i in \$SHAFILES; do
		echo "Create sha256 file..."
		/usr/bin/sha256sum "\$i" > "\$i.sha256"
	    done
	fi
	tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-raw.tar.bz2" \
	    --exclude="$imageout.iso" --exclude="$imageout.raw" --exclude="$imageout.qcow2" *
	cd /$TOPDIR/KIWI
	if [ -x /usr/bin/sha256sum ]; then
	    /usr/bin/sha256sum "$imageout$buildnum-raw.tar.bz2" > "$imageout$buildnum-raw.tar.bz2.sha256"
	fi
	EOF
}

kiwi_post_xen() {
    cat <<-EOF
	echo "compressing xen images... "
	cd /$TOPDIR/KIWI-xen
	# do not store compressed file _and_ uncompressed one
	[ -e "$imageout.gz" ] && rm -f "$imageout"
	tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-xen.tar.bz2" \
		`grep ^kernel $imageout.xenconfig | cut -d'"'  -f2` \
		`grep ^ramdisk $imageout.xenconfig | cut -d'"'  -f2` \
		initrd-* \
		"$imageout.xenconfig" \
		"$imageout"
	if [ -x /usr/bin/sha256sum ]; then
	    echo "Create sha256 file..."
	    cd $TOPDIR/KIWI
	    /usr/bin/sha256sum "$imageout$buildnum-xen.tar.bz2" > "$imageout$buildnum-xen.tar.bz2.sha256"
	fi
	EOF
}

kiwi_post_pxe() {
    cat <<-EOF
	echo "compressing pxe images... "
	cd /$TOPDIR/KIWI-pxe
	# do not store compressed file _and_ uncompressed one
	[ -e "$imageout.gz" ] && rm -f "$imageout"
	tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-pxe.tar.bz2" ${imageout}* initrd-*
	if [ -x /usr/bin/sha256sum ]; then
	    echo "Create sha256 file..."
	    cd $TOPDIR/KIWI
	    /usr/bin/sha256sum "$imageout$buildnum-pxe.tar.bz2" > "$imageout$buildnum-pxe.tar.bz2.sha256"
	fi
	EOF
}

kiwi_post_iso() {
    cat <<-EOF
	cd /$TOPDIR/KIWI-iso
	for i in *.iso; do
	    mv "\$i" "/$TOPDIR/KIWI/\${i%.iso}$buildnum.iso"
	done
	if [ -x /usr/bin/sha256sum ]; then
	    echo "creating sha256 sum for iso images... "
	    cd $TOPDIR/KIWI
	    for i in *.iso; do
		/usr/bin/sha256sum "\$i" > "\$i.sha256"
	    done
	fi
	EOF
}

kiwi_post_tbz() {
    cat <<-EOF
	cd /$TOPDIR/KIWI-tbz
	for i in *.tbz; do
	    file=\$(readlink -f "\$i")
	    [ -z "\$file" ] && echo readlink failed for $i
	    mv "\$file" "/$TOPDIR/KIWI/\${i%.tbz}$buildnum.tbz"
	done
	if [ -x /usr/bin/sha256sum ]; then
	    echo "creating sha256 sum for tar balls... "
	    cd $TOPDIR/KIWI
	    for i in *.tbz; do
		/usr/bin/sha256sum "\$i" > "\$i.sha256"
	    done
	fi
	EOF
}

kiwi_post_unknown() {
    cat <<-EOF
	echo "compressing unkown images... "
	cd /$TOPDIR/KIWI-$imgtype
	# do not store compressed file _and_ uncompressed one
	[ -e "$imageout.gz" ] && rm -f "$imageout"
	tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-$imgtype.tar.bz2" *
	if [ -x /usr/bin/sha256sum ]; then
	    echo "Create sha256 file..."
	    cd /$TOPDIR/KIWI
	    /usr/bin/sha256sum "$imageout$buildnum-$imgtype.tar.bz2" > "$imageout$buildnum-$imgtype.tar.bz2.sha256"
	fi
	EOF
}

legacy_image_bundle() {
    # create tar.gz of images, in case it makes sense
    buildnum=
    if test -n "$RELEASE"; then
	buildnum="-Build$RELEASE"
    fi
    imagearch=`uname -m`
    imageout="$imagename.$imagearch-$imageversion"
    for imgtype in $imagetype ; do
	case "$imgtype" in
	    oem) kiwi_post_oem   > $BUILD_ROOT/kiwi_post.sh ;;
	    vmx) kiwi_post_vmx   > $BUILD_ROOT/kiwi_post.sh ;;
	    xen) kiwi_post_xen   > $BUILD_ROOT/kiwi_post.sh ;;
	    pxe) kiwi_post_pxe   > $BUILD_ROOT/kiwi_post.sh ;;
	    iso) kiwi_post_iso   > $BUILD_ROOT/kiwi_post.sh ;;
	    tbz) kiwi_post_tbz   > $BUILD_ROOT/kiwi_post.sh ;;
	    *) kiwi_post_unknown > $BUILD_ROOT/kiwi_post.sh ;;
	esac
	cat >> $BUILD_ROOT/kiwi_post.sh <<-EOF
		cd /$TOPDIR/KIWI-$imgtype
		if [ -e "$imageout.channel" ]; then
		    echo "Found kiwi channel list file, exporting as well..."
		    cp "$imageout.channel" "/$TOPDIR/OTHER/$imageout$buildnum-$imgtype.channel"
		fi
		if [ -e "$imageout.packages" ]; then
		    echo "Found kiwi package list file, exporting as well..."
		    cp "$imageout.packages" "/$TOPDIR/OTHER/$imageout$buildnum-$imgtype.packages"
		fi
		if [ -e "$imageout.verified" ]; then
		    echo "Found rpm verification report, exporting as well..."
		    cp "$imageout.verified" "/$TOPDIR/OTHER/$imageout$buildnum-$imgtype.verified"
		fi
		EOF
	chroot $BUILD_ROOT su -c "sh -e /kiwi_post.sh" || cleanup_and_exit 1
	rm -f $BUILD_ROOT/kiwi_post.sh
    done
}


############################################################

recipe_setup_kiwi() {
    TOPDIR=/usr/src/packages
    test "$DO_INIT_TOPDIR" = false || rm -rf "$BUILD_ROOT$TOPDIR"
    mkdir -p "$BUILD_ROOT$TOPDIR"
    mkdir -p "$BUILD_ROOT$TOPDIR/OTHER"
    mkdir -p "$BUILD_ROOT$TOPDIR/SOURCES"
    mkdir -p "$BUILD_ROOT$TOPDIR/KIWI"
    # compat, older build versions did not clean TOPDIR ...
    mkdir -p "$BUILD_ROOT$TOPDIR/BUILD"
    mkdir -p "$BUILD_ROOT$TOPDIR/RPMS"
    mkdir -p "$BUILD_ROOT$TOPDIR/SRPMS"
    
    if test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then 
	mv "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
    else
	if test -z "$LINKSOURCES" ; then 
	    cp -dLR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/ || cleanup_and_exit 1 "source copy failed"
	else
	    cp -lR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/ || cleanup_and_exit 1 "source copy failed"
	fi
    fi
    chown -hR "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"

    if test -z "$ABUILD_TARGET"; then
        ABUILD_TARGET=$(queryconfig target --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH" )
        test -z "$ABUILD_TARGET" || echo "build target is $ABUILD_TARGET"
    fi  
}

recipe_prepare_kiwi() {
    :
}

## obsolete with current kiwi versions, only needed for kiwi 3.01 version
run_suse_isolinux() {
    for i in $BUILD_ROOT/$TOPDIR/KIWIROOT/main/* ; do
	test -d "$i" || continue
	i="${i##*/}"
	test "$i" = scripts && continue
	test "$i" != "${i%0}" && continue
	chroot $BUILD_ROOT su -c "suse-isolinux $TOPDIR/KIWIROOT/main/$i $TOPDIR/KIWI/$i.iso" - $BUILD_USER
    done
}

perform_product_bundle() {
    local milestone=$(kiwi_query_recipe milestone)
    pushd $BUILD_ROOT/$TOPDIR/KIWIROOT/main
    for i in * ; do
	test -e "$i" || continue
	case $i in
	    *.iso) if [ -x /usr/bin/sha256sum ]; then
                       /usr/bin/sha256sum "$i" > "$i.sha256"
                       mv "$i.sha256" $BUILD_ROOT/$TOPDIR/KIWI/.
                   fi
                   mv "$i" $BUILD_ROOT/$TOPDIR/KIWI/.
                   test -n "$milestone" && echo "$milestone" > $BUILD_ROOT/$TOPDIR/OTHER/${i%.iso}.milestone ;;
	    *.packages) mv $i $BUILD_ROOT/$TOPDIR/OTHER/. ;;
	    *.sbom.json) mv $i $BUILD_ROOT/$TOPDIR/OTHER/. ;;
	    *.report)
		mv $i $BUILD_ROOT/$TOPDIR/OTHER/.
		test -n "$milestone" && echo "$milestone" > $BUILD_ROOT/$TOPDIR/OTHER/${i%.report}.milestone
		;;
	    scripts) ;;
	    *0) ;;
            *) if test -d $i -a "$drop_repo" != true; then
                  mv $i $BUILD_ROOT/$TOPDIR/KIWI/.
                  test -n "$milestone" && echo "$milestone" > $BUILD_ROOT/$TOPDIR/OTHER/${i}.milestone
               fi ;;
        esac
    done
    popd
}

build_kiwi_product() {
    echo "running product builder..."
    # runs always as abuild user
    mkdir -p "$BUILD_ROOT/$TOPDIR/KIWIROOT"
    # XXX: again?
    chroot "$BUILD_ROOT" chown -R abuild.abuild "$TOPDIR"
    chroot "$BUILD_ROOT" rm -rf "$TOPDIR/KIWIROOT"
    local checksums
    if test -f "$BUILD_ROOT/$TOPDIR/SOURCES/repos/.createrepo_checksums" ; then
	checksums="CREATEREPO_CHECKSUMS=$TOPDIR/SOURCES/repos/.createrepo_checksums"
    fi
    # concatenate all modulemd data into a single _modulemd.yaml file
    if test -d "$BUILD_ROOT/$TOPDIR/SOURCES/repos" ; then
	local d m
	for d in $(find "$BUILD_ROOT/$TOPDIR/SOURCES/repos" -type d -name modulemd) ; do
	    for m in "$d"/*.modulemd.yaml "$d"/*.modulemd-defaults.yaml "$d"/modules.yaml ; do
		test -s "$m" && cat "$m" >> "$BUILD_ROOT/$TOPDIR/SOURCES/repos/_modulemd.yaml"
	    done
	done
    fi
    if test -x "$BUILD_ROOT/usr/bin/product-builder"; then
       # current implementation of product generation
       chroot "$BUILD_ROOT" su -c "APPID=- LANG=POSIX BUILD_FLAVOR=$BUILD_FLAVOR $checksums /usr/bin/product-builder --root $TOPDIR/KIWIROOT $VERBOSE_OPTION --logfile terminal --create-instsource $TOPDIR/SOURCES" - abuild < /dev/null && BUILD_SUCCEEDED=true
    else
      # before openSUSE Leap 43 and SLE 13
      ver=`chroot "$BUILD_ROOT" su -c "/usr/sbin/kiwi --version | sed -n 's,.*kiwi version v\(.*\),\1,p'"`
      test -n "$ver" || ver=`chroot "$BUILD_ROOT" su -c "/usr/sbin/kiwi --version | sed -n 's,.* vnr: \(.*\),\1,p'"`
      if test "${ver:0:1}" == "3" ; then
          # old style kiwi 3 builds
          chroot "$BUILD_ROOT" su -c "APPID=- LANG=POSIX /usr/sbin/kiwi --root $TOPDIR/KIWIROOT -v --logfile terminal -p $TOPDIR/SOURCES --instsource-local --create-instsource $TOPDIR/SOURCES" - abuild < /dev/null && BUILD_SUCCEEDED=true
          test ${ver:2:2} == "01" && run_suse_isolinux
      else
          VERBOSE_OPTION="-v 2"
          # broken kiwi version, not accepting verbose level
          test "${ver:0:1}" == "4" -a "${ver:2:2}" -lt 90 && VERBOSE_OPTION="-v -v"
          chroot "$BUILD_ROOT" su -c "APPID=- LANG=POSIX /usr/sbin/kiwi --root $TOPDIR/KIWIROOT $VERBOSE_OPTION --logfile terminal -p $TOPDIR/SOURCES --create-instsource $TOPDIR/SOURCES" - abuild < /dev/null && BUILD_SUCCEEDED=true
      fi
    fi

    # move created product to destination
    # NOTE: older kiwi versions exit also in error case with 0
    if test "$BUILD_SUCCEEDED" = true ; then
        perform_product_bundle
    fi
}

perform_image_build() {
    local imgtype=$1
    local profile=$2

    # When people test mixed build with legacy and new kiwi version
    # the zypper cache was used in different ways. Therefore this
    # needs a cleanup before the build starts
    if test -d "$BUILD_ROOT/var/cache/kiwi/zypper" ; then
	echo "cleaning up zypper image build cache"
	rm -rf $BUILD_ROOT/var/cache/kiwi/zypper
    fi

    # Do not use $BUILD_USER here, since we always need root permissions

    if test "$kiwi_legacy" = true ; then
	# old kiwi, do a prepare and a create call
	local prepare_call
	echo "running kiwi prepare for $imgtype${profile:+, profile $profile}..."
	prepare_call="cd $TOPDIR/SOURCES && rm -rf $TOPDIR/KIWIROOT-$imgtype"
	prepare_call="$prepare_call && $kiwi_path --logfile terminal"
	prepare_call="$prepare_call --prepare $TOPDIR/SOURCES"
	prepare_call="$prepare_call --root $TOPDIR/KIWIROOT-$imgtype"
	test -n "$profile" && prepare_call="$prepare_call --add-profile $profile"
	prepare_call="$prepare_call $KIWI_PARAMETERS"
	echo "$prepare_call"
	chroot $BUILD_ROOT su -c "$prepare_call" - root </dev/null || cleanup_and_exit 1
	mkdir -p $BUILD_ROOT/$TOPDIR/KIWI-$imgtype
	echo "running kiwi create for $imgtype${profile:+, profile $profile}..."
	local create_call
	create_call="cd $TOPDIR/SOURCES"
	create_call="$create_call && $kiwi_path --logfile terminal"
	create_call="$create_call --create $TOPDIR/KIWIROOT-$imgtype"
	create_call="$create_call --type $imgtype"
	create_call="$create_call -d $TOPDIR/KIWI-$imgtype"
	test -n "$profile" && create_call="$create_call --add-profile $profile"
	create_call="$create_call $KIWI_PARAMETERS"
	echo "$create_call"
	chroot $BUILD_ROOT su -c "$create_call" - root < /dev/null || cleanup_and_exit 1
    else
	# new kiwi, do a "system build" call
	mkdir -p $BUILD_ROOT/$TOPDIR/KIWI-$imgtype
	local build_call
	echo "running kiwi system build for $imgtype${profile:+, profile $profile}..."
	build_call="cd $TOPDIR/SOURCES && rm -rf $TOPDIR/KIWI-$imgtype"
        # cross prepare needed?
	if test -n "$ABUILD_TARGET" ; then
	    local target="${ABUILD_TARGET%%-*}"
	    if test -n "$target" && ! check_native_arch "$target" ; then
		build_call="$build_call && LANG=en_US.UTF-8 $kiwi_path system crossprepare --target-arch $target --init /usr/lib/build/initvm.`uname -m` --target-dir $TOPDIR/KIWI-$imgtype"
	    fi
	fi
	build_call="$build_call && LANG=en_US.UTF-8 $kiwi_path --debug $KIWI_MAIN_PARAMETERS"
	test -n "$profile" && build_call="$build_call --profile $profile"
	if test -n "$ABUILD_TARGET" ; then
	    local target="${ABUILD_TARGET%%-*}"
	    if test -n "$target" && ! check_native_arch "$target" ; then
                # cross build target
		build_call="$build_call --target-arch=$target"
	    fi
	fi
	build_call="$build_call system build --allow-existing-root --description $TOPDIR/SOURCES --target-dir $TOPDIR/KIWI-$imgtype"
	build_call="$build_call $KIWI_BUILD_PARAMETERS"
	echo "$build_call"
	chroot $BUILD_ROOT su -c "$build_call" - root < /dev/null || cleanup_and_exit 1
	# ceate compat link
	ln -sf "KIWI-$imgtype/build/image-root" "$BUILD_ROOT/$TOPDIR/KIWIROOT-$imgtype"
    fi
}

perform_image_bundle() {
    local imgtype=$1
    local profile=$2
    if test -n "$BUILD_SKIP_BUNDLE"; then
       mv $BUILD_ROOT/$TOPDIR/KIWI-$imgtype/* $BUILD_ROOT/$TOPDIR/KIWI/
       return 0
    fi

    local bundle_call
    local bundle_id="Build$RELEASE"
    test -n "$profile" && bundle_id="${profile}-$bundle_id"

    rm -rf "/$TOPDIR/KIWI.bundle"
    if test "$kiwi_legacy" = true ; then
        bundle_call="$kiwi_path --bundle-build $TOPDIR/KIWI-$imgtype"
        bundle_call="$bundle_call -d /$TOPDIR/KIWI.bundle/"
        bundle_call="$bundle_call --bundle-id $bundle_id"
    else
        bundle_call="LANG=en_US.UTF-8 $kiwi_path result bundle --target-dir $TOPDIR/KIWI-$imgtype"
        bundle_call="$bundle_call --id $bundle_id"
        bundle_call="$bundle_call --bundle-dir /$TOPDIR/KIWI.bundle/"
	bundle_call="$bundle_call $KIWI_BUNDLE_PARAMETERS"
    fi
    echo "$bundle_call"
    if chroot $BUILD_ROOT su -c "$bundle_call" - root < /dev/null; then
        mv "$BUILD_ROOT/$TOPDIR/KIWI.bundle/"* "$BUILD_ROOT/$TOPDIR/KIWI/" || cleanup_and_exit 1
        rmdir "$BUILD_ROOT/$TOPDIR/KIWI.bundle"
        return 0	# success
    fi
    # need another way to bundle
    return 1
}

# translate kiwi8 parameters to kiwi9
kiwi_translate_parameters() {
    local i lasti
    local add_repo_url=() add_repo_type=() add_repo_alias=() add_repo_priority=()
    local set_repo_url set_repo_type set_repo_alias set_repo_priority

    # return values
    KIWI_MAIN_PARAMETERS=
    KIWI_BUILD_PARAMETERS=
    KIWI_BUNDLE_PARAMETERS=
    KIWI_DERIVED_CONTAINER=
    for i in $KIWI_PARAMETERS ; do
	if test -n "$lasti" ; then
	    i="$lasti=$i"
	    lasti=
	fi
	case "$i" in
	    --add-profile|-t|--type|-l|--logfile|-d|--dest-dir|--root|--add-repo|--add-repoalias|--add-repoprio|--add-repopriority|--add-repotype|--set-repo|--set-repoalias|--set-repoprio|--set-repopriority|--set-repotype)
		lasti="$i"
		;;
	esac
	test -n "$lasti" && continue
	case "$i" in
	    --version|--debug) KIWI_MAIN_PARAMETERS="$KIWI_MAIN_PARAMETERS $i" ;;
	    --add-profile=*) KIWI_MAIN_PARAMETERS="$KIWI_MAIN_PARAMETERS --profile ${i#*=}" ;;
	    -t=|--type=*) KIWI_MAIN_PARAMETERS="$KIWI_MAIN_PARAMETERS --type ${i#*=}" ;;
	    -l=|--logfile=*) KIWI_MAIN_PARAMETERS="$KIWI_MAIN_PARAMETERS --logfile ${i#*=}" ;;
	    -d=*|--dest-dir=*) KIWI_BUILD_PARAMETERS="$KIWI_BUILD_PARAMETERS --target-dir ${i#*=}" ;;
	    --root=*) echo "WARNING: ignored unsupported kiwi option $i" ;; # unsupported for system build...
	    --ignore-repos) KIWI_BUILD_PARAMETERS="$KIWI_BUILD_PARAMETERS --ignore-repos-used-for-build" ;;
	    --recycle-root) KIWI_BUILD_PARAMETERS="$KIWI_BUILD_PARAMETERS --allow-existing-root" ;;
	    --add-repo=*) add_repo_url[${#add_repo_url[@]}]="${i#*=}" ;;
	    --add-repotype=*) add_repo_type[${#add_repo_type[@]}]="${i#*=}" ;;
	    --add-repoalias=*) add_repo_alias[${#add_repo_alias[@]}]="${i#*=}" ;;
	    --add-repopriority=*|--add-repoprio=*) add_repo_priority[${#add_repo_priority[@]}]="${i#*=}" ;;
	    --set-repo=*) set_repo_url="${i#*=}" ;;
	    --set-repotype=*) set_repo_type="${i#*=}" ;;
	    --set-repoalias=*) set_repo_alias="${i#*=}" ;;
	    --set-repopriority=*|--set-repoprio=*) set_repo_priority="${i#*=}" ;;
	    --main:*) KIWI_MAIN_PARAMETERS="$KIWI_MAIN_PARAMETERS --${i#--main:}" ;;
	    --bundle:*) KIWI_BUNDLE_PARAMETERS="$KIWI_BUNDLE_PARAMETERS --${i#--bundle:}" ;;
	    --set-container-derived-from=dir://./containers/*.tar)
		# old kiwi 9 versions cannot handle uncompressed containers
		if test -f "$BUILD_ROOT/$TOPDIR/SOURCES/containers/${i#*containers/}" ; then
		    gzip -f -1 < "$BUILD_ROOT/$TOPDIR/SOURCES/containers/${i#*containers/}" > "$BUILD_ROOT/$TOPDIR/SOURCES/containers/${i#*containers/}.gz" || cleanup_and_exit 1 "container compression"
		    i="$i.gz"
		    KIWI_DERIVED_CONTAINER="$BUILD_ROOT/$TOPDIR/SOURCES/containers/${i#*containers/}"
		fi
	        KIWI_BUILD_PARAMETERS="$KIWI_BUILD_PARAMETERS $i"
		;;
	    --set-container-derived-from=dir://./containers/*.tar*)
		if test -f "$BUILD_ROOT/$TOPDIR/SOURCES/containers/${i#*containers/}" ; then
		    KIWI_DERIVED_CONTAINER="$BUILD_ROOT/$TOPDIR/SOURCES/containers/${i#*containers/}"
		fi
	        KIWI_BUILD_PARAMETERS="$KIWI_BUILD_PARAMETERS $i"
		;;
	    # everything else just gues through to kiwi build
	    *) KIWI_BUILD_PARAMETERS="$KIWI_BUILD_PARAMETERS $i" ;;
	esac
    done
    # now add the repo options
    lasti=0
    for i in "${add_repo_url[@]}" ; do
	local repo_prio="${add_repo_priority[$lasti]}"
	test -z "$repo_prio" && repo_prio=99
	KIWI_BUILD_PARAMETERS="$KIWI_BUILD_PARAMETERS --add-repo $i,${add_repo_type[$lasti]},${add_repo_alias[$lasti]},$repo_prio"
	let lasti++
    done
    if test -n "$set_repo_url" ; then
	test -z "$set_repo_priority" && set_repo_priority=99
	KIWI_BUILD_PARAMETERS="$KIWI_BUILD_PARAMETERS --set-repo $set_repo_url,$set_repo_type,$set_repo_alias,$set_repo_priority"
    fi
}

kiwi_query_recipe() {
    perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show "$BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE" "$1" "${BUILD_ARCH%%:*}" "$BUILD_FLAVOR"
}

build_kiwi_appliance() {
    if test -z "$RUNNING_IN_VM" ; then
        # NOTE: this must be done with the outer system, because it loads
        # the dm-mod kernel modules, which needs to fit to the kernel.
        echo "starting device mapper for kiwi..."
        test -x /etc/init.d/boot.device-mapper && \
            /etc/init.d/boot.device-mapper start
    fi
    local kiwi_profile
    kiwi_profile=$(kiwi_query_recipe profiles)
    if test -z "$kiwi_profile"; then
	kiwi_profile=$(queryconfig \
	    --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" \
	    --archpath "$BUILD_ARCH" buildflags kiwiprofile
	)
    fi
    if test -z "$kiwi_profile"; then
        kiwi_profile=__not__set
    fi
    if test "$kiwi_profile" = __excluded ; then
	cleanup_and_exit 1 "none of the selected profiles can be built on this architecture"
    fi

    local kiwi_path=/usr/bin/kiwi kiwi_legacy=false
    if ! test -L "$BUILD_ROOT/usr/bin/kiwi" ; then
	kiwi_path=/usr/sbin/kiwi
        kiwi_legacy=true
    fi

    if test "$kiwi_legacy" != true ; then
	kiwi_translate_parameters
    fi

    local dep rpm_db_backend
    for dep in $(kiwi_query_recipe deps) ; do
	test rpm-ndb = "$dep" && rpm_db_backend=ndb
    done
    if test -n "$rpm_db_backend" ; then
	cp $BUILD_ROOT/root/.rpmmacros $BUILD_ROOT/root/.rpmmacros.save
	echo "configuring $rpm_db_backend database for image builds"
	echo "%_db_backend $rpm_db_backend" >> $BUILD_ROOT/root/.rpmmacros
    fi

    local run_bundle=true
    for imgtype in $imagetype ; do
	for prof in ${kiwi_profile//,/ } ; do
	    test "$prof" = __not__set && prof=
	    perform_image_build $imgtype $prof
	    if perform_image_bundle $imgtype $prof ; then
		# bundling successful, skip legacy bundler
		run_bundle=
	    fi
	done
    done
    BUILD_SUCCEEDED=true

    if test -n "$rpm_db_backend" ; then
	mv $BUILD_ROOT/root/.rpmmacros.save $BUILD_ROOT/root/.rpmmacros
    fi

    if test -n "$run_bundle"; then
	# results are not bundled yet
	legacy_image_bundle
    fi

    local milestone=$(kiwi_query_recipe milestone)
    if test -n "$milestone"; then
        for i in $BUILD_ROOT/$TOPDIR/KIWI/*.packages; do
            test -e "$i" || continue
            echo "$milestone" > "${i%.packages}.milestone"
        done
    fi
}

postprocess_kiwi_containers() {
    local r

    # uncompress containers, the compression is useless and just slows down further processing
    for r in $BUILD_ROOT/$TOPDIR/KIWI/*.tar.xz ; do
	test -e "$r" || continue
	r="${r%.xz}"
	xzdec <"$r.xz" >"$r" || cleanup_and_exit 1 "xzdec failed"
	if test -e "$r.xz.sha256" ; then
	    # also generate sha256sum
	    (cd "$BUILD_ROOT/$TOPDIR/KIWI" && sha256sum "${r##*/}") > "$r.sha256"
	fi
	rm -f "$r.xz" "$r.xz.sha256"
    done

    # create container info
    local args=(--arch "${BUILD_ARCH%%:*}" --buildflavor "$BUILD_FLAVOR")
    test -n "$DISTURL" && args=("${args[@]}" --disturl "$DISTURL")
    test -n "$RELEASE" && args=("${args[@]}" --release "$RELEASE")
    for r in $BUILD_ROOT/$TOPDIR/KIWI/*.tar ; do
	test -e "$r" && perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::showcontainerinfo -- "${args[@]}" $BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE "$r" > "${r%.tar}.containerinfo"
	if test -s "${r%.tar}.containerinfo" ; then
	    # create sbom if requested
	    if test -n "$(queryconfig --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH" buildflags spdx)" ; then
		echo "Generating sbom file for ${r##*/}"
		generate_sbom "$r" > "${r%.tar}.sbom.json"
		test -s "${r%.tar}.sbom.json" || rm -f "${r%.tar}.sbom.json"
	    fi
	else
	    rm -f "${r%.tar}.containerinfo"
	fi
    done
    if test -n "$KIWI_DERIVED_CONTAINER" ; then
	for r in $BUILD_ROOT/$TOPDIR/KIWI/*.packages ; do
	    test -e "$r" || continue
	    r="${r%.packages}"
	    for rr in $r*.containerinfo ; do
		test -e "$rr" || continue
		echo "creating base package information"
		cp --remove-destination "$BUILD_DIR/create_container_package_list" "$BUILD_ROOT/tmp/create_container_package_list"
		chroot "$BUILD_ROOT" /bin/bash /tmp/create_container_package_list "${KIWI_DERIVED_CONTAINER#$BUILD_ROOT}" > "$r.basepackages"
		rm -f "$BUILD_ROOT/tmp/create_derived_package_list"
		break
	    done
	done
    fi
}

createrepo_debian_kiwi() {
    local dir=$1
    local prp=$2
    local arch=$(chroot $BUILD_ROOT su -c "dpkg-architecture -qDEB_BUILD_ARCH")
    cat >"${BUILD_ROOT}/.createrepo_debian.tmp.sh" <<-EOF
	cd "$dir" || exit 1
	dpkg-scanpackages -m . > Packages
	gzip -c9 < Packages > Packages.gz
	dpkg-scansources . > Sources
	gzip -c9 < Sources > Sources.gz
	EOF
    chroot $BUILD_ROOT su -c "sh /.createrepo_debian.tmp.sh" - root
    cat > "${BUILD_ROOT}/$dir/Release" <<-EOF
	Origin: obs:/$prp
	Label: ${prp%/*}
	Codename: ${prp#*/}
	Architectures: $arch
	Description: obs repository for $prp
	EOF
    echo "SHA256:" >> "${BUILD_ROOT}/$dir/Release"
    for file in "${BUILD_ROOT}/$dir/Packages"* "${BUILD_ROOT}/$dir/Sources"*; do
	local SUM=( $(sha256sum ${file}) )
	local SIZE=$(stat -c '%s' ${file})
	echo " ${SUM} ${SIZE} ${file#${BUILD_ROOT}/$dir/}" >> "${BUILD_ROOT}/$dir/Release"
    done
}

createrepo_debian_dist_kiwi() {
    local dir=$1
    local prp=$2
    local dist=$3
    local arch=$(chroot $BUILD_ROOT su -c "dpkg-architecture -qDEB_BUILD_ARCH")
    mkdir -p "${BUILD_ROOT}/$dir/dists/${dist}/main/binary-$arch"
    mkdir -p "${BUILD_ROOT}/$dir/dists/${dist}/main/source"
    cat >"${BUILD_ROOT}/.createrepo_debian.tmp.sh" <<-EOF
	cd "$dir" || exit 1
	dpkg-scanpackages -m . > dists/${dist}/main/binary-$arch/Packages
	gzip -c9 < dists/${dist}/main/binary-$arch/Packages > dists/${dist}/main/binary-$arch/Packages.gz
	dpkg-scansources . > dists/${dist}/main/source/Sources
	gzip -c9 < dists/${dist}/main/source/Sources > dists/${dist}/main/source/Sources.gz
	EOF
    chroot $BUILD_ROOT su -c "sh /.createrepo_debian.tmp.sh" - root
    ln -s ${dist} ${BUILD_ROOT}/$dir/dists/stable

    cat > "${BUILD_ROOT}/$dir/dists/${dist}/Release" <<-EOF
	Origin: obs:/$prp
	Label: ${prp%/*}
	Suite: stable
	Codename: $dist
	Architectures: $arch
	Description: obs repository for $prp
	Components: main
	EOF
    echo "SHA256:" >> "${BUILD_ROOT}/$dir/dists/${dist}/Release"
    for file in "${BUILD_ROOT}/$dir/dists/${dist}/main"/*/Packages* "${BUILD_ROOT}/$dir/$dists/main/source"/Sources*; do
	local SUM=( $(sha256sum ${file}) )
	local SIZE=$(stat -c '%s' ${file})
	echo " ${SUM} ${SIZE} ${file#${BUILD_ROOT}/$dir/dists/${dist}/}" >> "${BUILD_ROOT}/$dir/dists/${dist}/Release"
    done
}

recipe_build_kiwi() {
    imagetype=$(kiwi_query_recipe imagetype)
    imagename=$(kiwi_query_recipe filename)
    imageversion=$(kiwi_query_recipe version)
    drop_repo=$(kiwi_query_recipe drop_repository)

    if test -z "$imagetype" ; then
	cleanup_and_exit 1 "no imagetype set for this architecture/profile"
    fi
   
    # prepare rpms as source and createrepo on the repositories
    ln -sf $TOPDIR/SOURCES/repos $BUILD_ROOT/repos
    test -d $BUILD_ROOT/$TOPDIR/SOURCES/containers && ln -sf $TOPDIR/SOURCES/containers $BUILD_ROOT/containers
    cd $BUILD_ROOT/$TOPDIR/SOURCES/repos
    DEBDIST=
    if test -x "$BUILD_ROOT/usr/sbin/debootstrap" ; then
	DEBDIST=$(sed -ne 's/.*<repository.*distribution="\([^"]*\)".*/\1/p' <"$BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE" | head -n 1)
	echo "using distribution '$DEBDIST'"
    fi
    for r in *[^:]/* ; do
        test -L $r && continue
        test -d $r || continue
        repo="$TOPDIR/SOURCES/repos/$r/"
        # create compatibility link for old kiwi versions
        rc="${r//:/:/}"
        if test "$rc" != "$r" ; then
	    rl="${rc//[^\/]}"
	    rl="${rl//?/../}"
	    mkdir -p "${rc%/*}"
	    ln -s $rl$r "${rc%/*}/${rc##*/}"
	    repo="$TOPDIR/SOURCES/repos/${rc%/*}/${rc##*/}/"
        fi
        if test "$imagetype" != product -a "$DO_INIT" != "false" ; then
	    if test -x "$BUILD_ROOT/usr/sbin/debootstrap" ; then
		echo "creating debian repodata for $r"
		createrepo_debian_kiwi "$repo" "$r"
		test -n "$DEBDIST" && createrepo_debian_dist_kiwi "$repo" "$r" "$DEBDIST"
	    elif test -x "$BUILD_ROOT/usr/bin/repo-add" ; then
		echo "creating Arch Linux repodata for $r"
		r_db="${repo}/${r//[:|\/]/_}.db.tar.gz"
		chroot $BUILD_ROOT su -c "repo-add ${r_db} ${repo}*"
	    else
		echo "creating repodata for $r"
		if chroot $BUILD_ROOT createrepo --no-database --simple-md-filenames --help >/dev/null 2>&1 ; then
		    chroot $BUILD_ROOT createrepo --no-database --simple-md-filenames "$repo"
		else
		    chroot $BUILD_ROOT createrepo "$repo"
		fi
	    fi
        fi
    done

    # unpack root tar
    for t in $BUILD_ROOT/$TOPDIR/SOURCES/root.tar* ; do
	test -f $t || continue
	mkdir -p $BUILD_ROOT/$TOPDIR/SOURCES/root
	chroot $BUILD_ROOT tar -C $TOPDIR/SOURCES/root -xf "$TOPDIR/SOURCES/${t##*/}"
    done

    # copy requested files to the root directory
    while read s t ; do
        test -z "$t" && t="$s"
	case $s in
	'' | . | .. | */* | root ) ;;
	*)
	    test -e "$BUILD_ROOT/$TOPDIR/SOURCES/$s" || continue
	    t="root/${t#/}"
	    echo "copying $s to $t"
	    chroot $BUILD_ROOT mkdir -p "$TOPDIR/SOURCES/${t%/*}"
	    chroot $BUILD_ROOT cp -af "$TOPDIR/SOURCES/$s" "$TOPDIR/SOURCES/$t"
	    ;;
	esac
    done < <(kiwi_query_recipe copytoimage)

    # fix script permissions
    chmod a+x $BUILD_ROOT/$TOPDIR/SOURCES/*.sh 2>/dev/null

    # unpack tar files in image directories
    if test -d $BUILD_ROOT/$TOPDIR/SOURCES/images ; then
	(
	cd $BUILD_ROOT/$TOPDIR/SOURCES/images
	for r in */* ; do
	    test -L $r && continue
	    test -d $r || continue
	    for t in $r/root.tar* ; do
		test -f $t || continue
		mkdir -p $r/root
		chroot $BUILD_ROOT tar -C $TOPDIR/SOURCES/images/$r/root -xf "$TOPDIR/SOURCES/images/$r/${t##*/}"
	    done
	    # fix script permissions
	    chmod a+x $BUILD_ROOT/$TOPDIR/SOURCES/images/$r/*.sh 2>/dev/null
	    # create compatibility link for old kiwi versions
	    rc="${r//:/:/}"
	    if test "$rc" != "$r" ; then
		rl="${rc//[^\/]}"
		rl="${rl//?/../}"
		mkdir -p "${rc%/*}"
		ln -s $rl$r "${rc%/*}/${rc##*/}"
	    fi
	done
	)
    fi

    rm -f $BUILD_ROOT/$TOPDIR/SOURCES/config.xml
    ln -s $RECIPEFILE $BUILD_ROOT/$TOPDIR/SOURCES/config.xml

    if test "$imagetype" = product ; then
	build_kiwi_product
    else
	build_kiwi_appliance
    fi

    # Hook for running post kiwi build scripts like QA scripts if installed
    if test -x $BUILD_ROOT/usr/lib/build/kiwi_post_run ; then
        if ! chroot $BUILD_ROOT su -c /usr/lib/build/kiwi_post_run ; then
	    cleanup_and_exit 1 "/usr/lib/build/kiwi_post_run script failed!"
        fi
    fi

    # postprocess docker images (but not for legacy kiwi)
    if test -L "$BUILD_ROOT/usr/bin/kiwi" ; then
	for imgtype in $imagetype ; do
	    if test "$imgtype" = docker ; then
		postprocess_kiwi_containers
		break
	    fi
	done
    fi
}

recipe_resultdirs_kiwi() {
    echo KIWI
}

recipe_cleanup_kiwi() {
    :
}