summaryrefslogtreecommitdiff
path: root/build-recipe-kiwi
blob: 5d230b9dfef236fe8c430db2fc59474368725626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#
# 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"
    
    chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"
    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/
	else
	    cp -lR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
	fi
	if test "$?" != 0 ; then 
	    cleanup_and_exit 1 "source copy failed"
	fi
    fi

    # extract macros from configuration
    # some post scripts might call rpm-build and rely on the macros
    queryconfig rawmacros --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$CONFIG_DIR" > $BUILD_ROOT/root/.rpmmacros
}

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() {
    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/. ;;
	    *.packages) mv $i $BUILD_ROOT/$TOPDIR/OTHER/. ;;
	    *.report) mv $i $BUILD_ROOT/$TOPDIR/OTHER/. ;;
	    scripts) ;;
	    *0) ;;
	    *) test -d $i -a "$drop_repo" != true  && mv $i $BUILD_ROOT/$TOPDIR/KIWI/. ;;
	esac
    done
    popd
}

build_kiwi_product() {
    echo "running kiwi --create-instsource..."
    # 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"
    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

    # move created product to destination
    perform_product_bundle
}

perform_image_build() {
    local imgtype=$1
    local legacy=$2
    local profile=$3
    local prepare_call
    local create_call
    # 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
    echo "Cleanup zypper image build cache"
    if [ -d "$BUILD_ROOT/var/cache/kiwi/zypper" ];then
        rm -rf $BUILD_ROOT/var/cache/kiwi/zypper
    fi
    if test -n "$profile" -a "$profile" != __not__set ; then
	echo "running kiwi --prepare for $imgtype, profile $profile..."
    else
	echo "running kiwi --prepare for $imgtype..."
    fi
    # Do not use $BUILD_USER here, since we always need root permissions
    prepare_call="cd $TOPDIR/SOURCES && rm -rf $TOPDIR/KIWIROOT-$imgtype"
    if [ "$legacy" = "true" ]; then
        prepare_call="$prepare_call && /usr/sbin/kiwi --logfile terminal"
    else
        prepare_call="$prepare_call && LANG=en_US.UTF-8 /usr/bin/kiwi"
        prepare_call="$prepare_call --compat -- --debug"
    fi
    prepare_call="$prepare_call --prepare $TOPDIR/SOURCES"
    prepare_call="$prepare_call --root $TOPDIR/KIWIROOT-$imgtype"
    if test -n "$profile" -a "$profile" != __not__set ; then
        prepare_call="$prepare_call --add-profile $profile"
    fi
    prepare_call="$prepare_call $KIWI_PARAMETERS"
    echo "$prepare_call"
    chroot $BUILD_ROOT \
        su -c "$prepare_call" - root < /dev/null || cleanup_and_exit 1

    if test -n "$profile" -a "$profile" != __not__set ; then
	echo "running kiwi --create for $imgtype, profile $profile..."
    else
	echo "running kiwi --create for $imgtype..."
    fi
    mkdir -p $BUILD_ROOT/$TOPDIR/KIWI-$imgtype
    create_call="cd $TOPDIR/SOURCES"
    if [ "$legacy" = "true" ]; then
        create_call="$create_call && LANG=en_US.UTF-8 /usr/sbin/kiwi"
        create_call="$create_call --logfile terminal"
    else
        create_call="$create_call && /usr/bin/kiwi --compat -- --debug"
    fi
    create_call="$create_call --create $TOPDIR/KIWIROOT-$imgtype"
    create_call="$create_call --type $imgtype"
    create_call="$create_call -d $TOPDIR/KIWI-$imgtype"
    if test -n "$profile" -a "$profile" != __not__set ; then
        create_call="$create_call --add-profile $profile"
    fi
    create_call="$create_call $KIWI_PARAMETERS"
    echo "$create_call"
    chroot $BUILD_ROOT \
        su -c "$create_call" - root < /dev/null || cleanup_and_exit 1
}

perform_image_bundle() {
    local imgtype=$1
    local legacy=$2
    local profile=$3
    local bundle_call

    local kiwi_profile=""
    if [ "$profile" != "__not__set" ]; then
        kiwi_profile="${profile}-"
    fi

    rm -rf "/$TOPDIR/KIWI.bundle"
    if [ "$legacy" = "true" ]; then
        bundle_call="/usr/sbin/kiwi --bundle-build $TOPDIR/KIWI-$imgtype"
        bundle_call="$bundle_call -d /$TOPDIR/KIWI.bundle/"
        bundle_call="$bundle_call --bundle-id ${kiwi_profile}Build$RELEASE"
    else
        bundle_call="LANG=en_US.UTF-8 /usr/bin/kiwi result bundle"
        bundle_call="$bundle_call --target-dir $TOPDIR/KIWI-$imgtype"
        bundle_call="$bundle_call --id ${kiwi_profile}Build$RELEASE"
        bundle_call="$bundle_call --bundle-dir /$TOPDIR/KIWI.bundle/"
    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"
        # success
        return 0
    fi
    # need another way to bundle
    return 1
}

is_legacy_kiwi() {
    if [ -L "$BUILD_ROOT/usr/bin/kiwi" ];then
        # The next generation of kiwi is installed to /usr/bin
        # If this file is found we expect the new kiwi to be
        # installed
        return 1
    fi
    # in any other case stick to the legacy kiwi
    return 0
}

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=$(queryconfig \
        --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" \
        --archpath "$BUILD_ARCH" buildflags kiwiprofile
    )
    if test -z "$kiwi_profile"; then
        kiwi_profile=__not__set
    fi
    local legacy=false
    if is_legacy_kiwi ; then
        legacy=true
    fi
    RUN_BUNDLE=true
    for imgtype in $imagetype ; do
	for prof in ${kiwi_profile//,/ } ; do
	    perform_image_build $imgtype $legacy $prof
	    if perform_image_bundle $imgtype $legacy $prof; then
		# bundling successful, skip legacy bundler
		unset RUN_BUNDLE
	    fi
	done
    done
    BUILD_SUCCEEDED=true

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

recipe_build_kiwi() {
    imagetype=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE imagetype)
    imagename=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE filename)
    imageversion=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE version)
    drop_repo=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE drop_repository)

    # prepare rpms as source and createrepo on the repositories
    ln -sf $TOPDIR/SOURCES/repos $BUILD_ROOT/repos
    cd $BUILD_ROOT/$TOPDIR/SOURCES/repos
    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
	    echo "creating repodata for $repo"
	    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
    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

    # 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
	chroot $BUILD_ROOT su -c /usr/lib/build/kiwi_post_run || cleanup_and_exit 1
    fi
}

recipe_resultdirs_kiwi() {
    echo KIWI
}