1b6cf7bd8a6b3d7e242381daf9d7cf11d9a05a85
[tools/obs-build.git] / build
1 #!/bin/bash
2 # Script to build a package.  It uses init_buildsystem to setup a chroot
3 # building tree.  This script needs a directory as parameter.  This directory
4 # has to include sources and a spec file.
5 #
6 # BUILD_ROOT        here the packages will be built
7 #
8 # (c) 1997-2008 SuSE GmbH Nuernberg, Germany
9
10 test -z "$BUILD_DIR" && BUILD_DIR=/usr/lib/build
11 test -z "$BUILD_ROOT" && BUILD_ROOT=/var/tmp/build-root
12
13 export BUILD_ARCH BUILD_ROOT BUILD_RPMS BUILD_DIR
14
15 ccache=0
16 icecream=0
17 definesnstuff=()
18 repos=()
19
20 # mkreiserfs only works with qemu/uml if it is able to create a file
21 # system that is owned by the calling user (bnc#369006)
22 #vm_img_mkfs='mkreiserfs -q -f'
23 #vm_img_mkfs='mkfs.ext2 -m 0 -q -F'
24 vm_img_mkfs='mkfs.ext3 -m 0 -q -F'
25 vm_img_tunefs='tune2fs -c 0'
26 qemu_kernel=/boot/vmlinuz
27 qemu_initrd=/boot/initrd
28 qemu_bin=/usr/bin/qemu
29 uml_kernel=/boot/vmlinux-um
30 uml_initrd=/boot/initrd-um
31
32 kvm_kernel=/boot/vmlinuz
33 kvm_initrd=/boot/initrd
34 kvm_bin=/usr/bin/qemu-kvm
35 mkinitrd_virtio_cmd=(env rootfstype=ext3 mkinitrd -d /dev/null -m "virtio_pci virtio_blk" -k $kvm_kernel -i $kvm_initrd-virtio)
36 # whether we have virtio support
37 kvm_virtio=
38
39 DO_INIT=true
40 DO_LINT=
41 DO_CHECKS=true
42 CLEAN_BUILD=
43 SPECFILES=()
44 SRCDIR=
45 BUILD_JOBS=
46 ABUILD_TARGET_ARCH=
47 CREATE_BASELIBS=
48 USEUSEDFORBUILD=
49 LIST_STATE=
50 VM_IMAGE=
51 VM_SWAP=
52 MEMSIZE=
53 RUNNING_IN_VM=
54 RPMLIST=
55 RELEASE=
56 REASON=
57 NOROOTFORBUILD=
58 LOGFILE=
59 KILL=
60 CHANGELOG=
61 BUILD_DEBUG=
62 PERSONALITY_SYSCALL=
63 INCARNATION=
64 DISTURL=
65
66 export PATH=$BUILD_DIR:/sbin:/usr/sbin:$PATH
67
68 # This is for insserv
69 export YAST_IS_RUNNING=instsys
70
71 unset LANGUAGE
72 unset LANG
73 export LC_ALL=POSIX
74 umask 022
75
76 echo_help () {
77     cat << EOT
78
79 Some comments for build
80 -----------------------
81
82 With build you can create rpm packages.  They will be built in a chroot
83 system.  This chroot system will be setup automatically.  Normally you can
84 simply call build with a spec file as parameter - nothing else has to be
85 set.
86
87 If you want to set the directory were the chroot system will be setup
88 (at the moment it uses $BUILD_ROOT),
89 simply set the the environment variable BUILD_ROOT.
90
91 Example:
92
93   export BUILD_ROOT=/var/tmp/mybuildroot
94
95
96 Normally build builds the complete package including src.rpm (rpmbuild -ba).
97 If you want let build only make the binary package, simply set
98
99    export BUILD_RPM_BUILD_STAGE=-bb
100
101 (or -bc, -bp, -bi, ...  see "Maximum RPM" for more details [*]).
102
103 When the build command succeeds, the rpm files can be found under
104 $BUILD_ROOT/usr/src/packages/RPMS/
105
106
107 Known Parameters:
108
109   --help      You already got it :)
110
111   --clean     Delete old build root before initializing it
112
113   --no-init   Skip initialization of build root and start with build
114               immediately.
115
116   --no-checks Do not run post-build checks
117
118   --rpms path1:path2:...
119               Specify path where to find the RPMs for the build system
120
121   --arch arch1:arch2:...
122               Specify what architectures to select from the RPMs
123
124   --useusedforbuild
125               Do not expand dependencies but search the specfile for
126               usedforbuild lines.
127
128   --verify    Run verify when initializing the build root
129
130   --extra-packs pack
131               Also install package 'pack'
132
133   --root rootdir
134               Use 'rootdir' to setup chroot environment
135
136   --oldpackages oldpackagesdir
137               Define a directory with a former build
138
139   --baselibs  Create -32bit/-64bit/-x86 rpms for other architectures
140
141   --list-state
142               List rpms that would be used to create a fresh build root.
143               Does not create the build root or perform a build.
144
145   --with X
146               enable feature X for build
147
148   --without X
149               disable feature X for build
150
151   --define 'X Y'
152               define macro X with value Y
153
154   --ccache
155               use ccache to speed up rebuilds
156
157   --icecream N
158               use N parallel build jobs with icecream
159
160   --debug
161               enable creation of a debuginfo package
162
163 Remember to have fun!
164
165 [*] Maximum RPM: http://www.rpm.org/max-rpm/
166 EOT
167 }
168 usage () {
169     echo "Usage: `basename $0` [--no-init|--clean|--rpms path|--verify|--help] [dir-to-build|spec-to-build]"
170     cleanup_and_exit 1
171 }
172
173 #
174 #  cleanup_and_exit
175 #  return values: 0 -> success, new packages built
176 #                 1 -> error, build failed
177 #                 2 -> successfull build, but no changes to former built packages
178 #
179 cleanup_and_exit () {
180     trap EXIT
181     test -z "$1" && set 0
182     if test -n "$RUNNING_IN_VM" ; then
183         cd /
184         if test -n "$VM_SWAP" -a -e "$VM_SWAP" ; then
185             swapoff "$VM_SWAP" 2>/dev/null
186             echo -n "BUILDSTATUS$1" >"$VM_SWAP"
187         fi
188         exec >&0 2>&0   # so that the logging tee finishes
189         sleep 1         # wait till tee terminates
190         kill -9 -1      # goodbye cruel world
191         exec /bin/bash -c 'mount -n -o remount,ro / ; halt -f -p'
192         halt -f -p
193     else
194         umount -n $BUILD_ROOT/proc 2>/dev/null || true
195         umount -n $BUILD_ROOT/dev/pts 2>/dev/null || true
196         test "$VM_IMAGE" = 1 && VM_IMAGE=
197         [ -n "$VM_IMAGE" ] && umount $BUILD_ROOT 2>/dev/null || true
198     fi
199     exit $1
200 }
201
202 fail_exit()
203 {
204   cleanup_and_exit 1
205 }
206
207 shellquote()
208 {
209     for arg; do
210         arg=${arg/\\/\\\\}
211         arg=${arg/\$/\\\$}
212         arg=${arg/\"/\\\"}
213         arg=${arg/\`/\\\`}
214         echo -n " \"$arg\""
215     done
216 }
217
218 # create a shell script from command line. Used for preserving arguments
219 # through /bin/su -c
220 toshellscript()
221 {
222         echo "#!/bin/sh -x"
223         echo -n exec
224         shellquote "$@"
225         echo
226 }
227
228 setupccache()
229 {
230     if [ "$ccache" = 1 ]; then
231         if mkdir -p $BUILD_ROOT/var/lib/build/ccache/bin; then
232             for i in gcc g++ cc c++; do
233 #               ln -sf /usr/bin/ccache $BUILD_ROOT/var/lib/build/ccache/bin/$i
234                 rm -f $BUILD_ROOT/var/lib/build/ccache/bin/$i
235                 test -e $BUILD_ROOT/usr/bin/$i || continue
236                 echo '#! /bin/sh' > $BUILD_ROOT/var/lib/build/ccache/bin/$i
237                 echo "test -e /usr/bin/$i || exit 1" > $BUILD_ROOT/var/lib/build/ccache/bin/$i
238                 echo 'export PATH=/opt/icecream/bin:/usr/bin:$PATH' >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
239                 echo "ccache $i \"\$@\"" >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
240                 chmod 755 $BUILD_ROOT/var/lib/build/ccache/bin/$i
241                 echo "Installed ccache wrapper as $BUILD_ROOT/var/lib/build/ccache/bin/$i"
242             done
243         fi
244         mkdir -p "$BUILD_ROOT"/.ccache
245         chroot "$BUILD_ROOT" chown -R "$BUILD_USER" "/.ccache"
246         echo "export CCACHE_DIR=/.ccache" > "$BUILD_ROOT"/etc/profile.d/build_ccache.sh
247         echo 'export PATH=/var/lib/build/ccache/bin:$PATH' >> "$BUILD_ROOT"/etc/profile.d/build_ccache.sh
248     else
249         rm -f "$BUILD_ROOT$builduserhome"/bin/{gcc,g++,cc,c++}
250         rm -f "$BUILD_ROOT"/var/lib/build/ccache/bin/{gcc,g++,cc,c++}
251     fi
252 }
253
254 setupicecream()
255 {
256     if [ "$icecream" -eq 0 ]; then
257         rm -rf "$BUILD_ROOT"/var/run/icecream
258         rm -f "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
259         return
260     fi
261
262     if ! chroot "$BUILD_ROOT" rpm -q icecream >/dev/null 2>/dev/null; then
263         echo "*** icecream package not installed ***"
264         false
265         return
266     fi
267
268     echo "using icecream with $icecream jobs"
269
270     if [ "$ccache" -ne 1 ]; then
271         echo 'export PATH=/opt/icecream/bin:$PATH' > "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
272     else
273         echo 'export CCACHE_PATH=/opt/icecream/bin' > "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
274     fi
275
276     local icecc_vers=(`shopt -s nullglob; echo $BUILD_ROOT/var/run/icecream/*.tar.{bz2,gz}`)
277     icecc_vers=${icecc_vers//$BUILD_ROOT/}
278
279     # XXX use changelog like autobuild does instead?
280     # only run create-env if compiler or glibc changed
281     if [ -z "$icecc_vers" \
282         -o ! -e "$BUILD_ROOT/$icecc_vers" \
283         -o "$BUILD_ROOT/usr/bin/gcc" -nt "$BUILD_ROOT/$icecc_vers" \
284         -o "$BUILD_ROOT/usr/bin/g++" -nt "$BUILD_ROOT/$icecc_vers" \
285         -o "$BUILD_ROOT/usr/bin/as" -nt "$BUILD_ROOT/$icecc_vers" \
286         -o "$BUILD_ROOT/lib/libc.so.6" -nt "$BUILD_ROOT/$icecc_vers" ]
287     then
288         rm -rf $BUILD_ROOT/var/run/icecream
289         mkdir -p $BUILD_ROOT/var/run/icecream
290         if [ -e "$BUILD_ROOT"/usr/bin/create-env ]; then
291           createenv=/usr/bin/create-env
292         elif [ -e "$BUILD_ROOT"/usr/lib/icecc/icecc-create-env ]; then
293           createenv="/usr/lib/icecc/icecc-create-env /usr/bin/gcc /usr/bin/g++" # XXX
294         elif [ -e "$BUILD_ROOT"/usr/lib64/icecc/icecc-create-env ]; then
295           createenv="/usr/lib64/icecc/icecc-create-env /usr/bin/gcc /usr/bin/g++" # XXX
296         else
297           echo "create-env not found"
298           false
299           return
300         fi
301         chroot $BUILD_ROOT bash -c "cd /var/run/icecream; $createenv" || cleanup_and_exit 1
302         icecc_vers=(`shopt -s nullglob; echo $BUILD_ROOT/var/run/icecream/*.tar.{bz2,gz}`)
303         icecc_vers=${icecc_vers//$BUILD_ROOT/}
304     else
305         echo "reusing existing icecream environment $icecc_vers"
306     fi
307     if [ -n "$icecc_vers" ]; then
308       echo "export ICECC_VERSION=$icecc_vers" >> "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
309     fi
310 }
311
312 setmemorylimit()
313 {
314     if [ -n "$VM_IMAGE" -o -n "$RUNNING_IN_VM" ]; then
315         return
316     fi
317     local mem
318     while read mem; do
319         case "$mem" in
320             MemTotal:*)
321                 set -- $mem
322                 eval "mem=\$(($2/3*4))"
323                 ulimit -v $mem
324                 echo "Memory limit set to ${mem}KB"
325                 break;
326             ;;
327         esac
328     done < <(cat /proc/meminfo) # cat for proc stuff
329 }
330
331 function create_baselibs {
332     echo "... creating baselibs"
333     BRPMS=
334     for RPM in $BUILD_ROOT$TOPDIR/RPMS/*/*.rpm ; do
335         BRPMS="$BRPMS ${RPM#$BUILD_ROOT}"
336     done
337     BASELIBS_CFG=
338     if test -e $BUILD_ROOT$TOPDIR/SOURCES/baselibs.conf ; then
339         BASELIBS_CFG="-c $TOPDIR/SOURCES/baselibs.conf"
340     fi
341     if test -f $BUILD_ROOT/usr/lib/build/mkbaselibs ; then
342         if test -z "$BASELIBS_CFG" -a -e $BUILD_ROOT/usr/lib/build/baselibs.conf ; then
343             BASELIBS_CFG="-c /usr/lib/build/baselibs.conf"
344         fi
345         chroot $BUILD_ROOT /usr/lib/build/mkbaselibs -c /usr/lib/build/baselibs_global.conf $BASELIBS_CFG $BRPMS || cleanup_and_exit 1
346     else
347         # use external version
348         rm -rf $BUILD_ROOT/.mkbaselibs
349         mkdir -p $BUILD_ROOT/.mkbaselibs
350         cp -f $BUILD_DIR/mkbaselibs $BUILD_ROOT/.mkbaselibs
351         cp -f $BUILD_DIR/baselibs_global.conf $BUILD_ROOT/.mkbaselibs
352         if test -z "$BASELIBS_CFG" -a -e $BUILD_DIR/baselibs.conf ; then
353             cp -f $BUILD_DIR/baselibs.conf $BUILD_ROOT/.mkbaselibs/baselibs.conf
354             BASELIBS_CFG="-c /.mkbaselibs/baselibs.conf"
355         fi
356         chroot $BUILD_ROOT /.mkbaselibs/mkbaselibs -c /.mkbaselibs/baselibs_global.conf $BASELIBS_CFG $BRPMS || cleanup_and_exit 1
357         rm -rf $BUILD_ROOT/.mkbaselibs
358     fi
359 }
360
361 detect_vm_2nd_stage()
362 {
363     if ! test "$0" = "/.build/build" ; then
364         return 1
365     fi
366     echo "2nd stage started in virtual machine"
367     BUILD_ROOT=/
368     BUILD_DIR=/.build
369     . $BUILD_DIR/build.data
370     echo "machine type: `uname -m`"
371     if test "$PERSONALITY" != 0 -a -z "$PERSONALITY_SET" ; then
372         export PERSONALITY_SET=true
373         echo "switching personality to $PERSONALITY..."
374         # this is 32bit perl/glibc, thus the 32bit syscall number
375         exec perl -e 'syscall(136, '$PERSONALITY') == -1 && warn("personality: $!\n");exec "/.build/build" || die("/.build/build: $!\n")'
376     fi
377     PATH=$BUILD_DIR:$PATH
378     RUNNING_IN_VM=true
379     mount -orw -n -tproc none /proc
380     mount -n -o remount,rw /
381     if [ -e /dev/kqemu ]; then
382         # allow abuild user to run qemu
383         chmod 0666 /dev/kqemu
384     fi
385     if test -n "$VM_SWAP" ; then
386         for i in 1 2 3 4 5 6 7 8 9 10 ; do
387             test -e "$VM_SWAP" && break
388             test $i = 1 && echo "waiting for $VM_SWAP to appear"
389             echo -n .
390             sleep 1
391         done
392         test $i = 1 || echo
393         # recreate the swap device manually if it didn't exist for some
394         # reason, hardcoded to hda2 atm
395         if ! test -b "$VM_SWAP" ; then
396             rm -f "$VM_SWAP"
397             umask 027
398             mknod "$VM_SWAP" b 3 2
399             umask 022
400         fi
401         swapon -v "$VM_SWAP" || exit 1
402     fi
403     HOST="$MYHOSTNAME"
404
405     return 0
406 }
407
408 set_build_arch()
409 {
410     if [ -z "$BUILD_ARCH" ]; then
411         BUILD_ARCH=`uname -m`
412         test i686 = "$BUILD_ARCH" && BUILD_ARCH=i586 # XXX: why?
413     fi
414
415     case $BUILD_ARCH in
416       i686) BUILD_ARCH="i686:i586:i486:i386" ;;
417       i586) BUILD_ARCH="i586:i486:i386" ;;
418       i486) BUILD_ARCH="i486:i386" ;;
419       x86_64) BUILD_ARCH="x86_64:i686:i586:i486:i386" ;;
420     esac
421     if test "$BUILD_ARCH" != "${BUILD_ARCH#i686}" ; then
422         cpuflags=`grep ^flags /proc/cpuinfo`
423         cpuflags="$cpuflags "
424         test "$cpuflags" = "${cpuflags/ cx8 /}" -o "$cpuflags" = "${cpuflags/ cmov /}" && {
425             echo "Your cpu doesn't support i686 rpms. Exit."
426             exit 1
427         }
428     fi
429 }
430
431 find_spec_files()
432 {
433     local spec files
434     if [ -z "$SPECFILES" ]; then
435         set -- "`pwd`"
436     else
437         set -- "${SPECFILES[@]}"
438     fi
439     SPECFILES=()
440     for spec in "$@"; do
441         if [ "$spec" = "${spec#/}" ]; then
442             spec="`pwd`/$spec"
443         fi
444
445         if [ -d "$spec" ]; then
446             specs=("$spec"/*.spec)
447             if [ -n "$specs" ]; then
448                 SPECFILES=("${SPECFILES[@]}" "${specs[@]}")
449             else
450                 specs=("$spec"/*.spec)
451                 if [ -n "$specs" ]; then
452                     SPECFILES=("${SPECFILES[@]}" "${specs[@]}")
453                 fi
454             fi
455         else
456             SPECFILES[${#SPECFILES[@]}]="$spec";
457         fi
458     done
459
460     if test -z "$SPECFILES"; then
461         echo no spec files or src rpms found in $@. exit...
462         cleanup_and_exit 1
463     fi
464 }
465
466 become_root_or_fail()
467 {
468     if [ ! -w /root ]; then
469         echo "You have to be root to use $0" >&2
470         exit 1
471     fi
472     cleanup_and_exit 1
473 }
474
475 mkdir_build_root()
476 {
477     if [ -d "$BUILD_ROOT" ]; then
478         # check if it is owned by root
479         if [ -z "$RUNNING_IN_VM" -a \! -O "$BUILD_ROOT" -a "`stat -c %u $BUILD_ROOT`" -ne 0 ]; then
480             echo "BUILD_ROOT=$BUILD_ROOT must be owned by $USER. Exit..."
481             cleanup_and_exit 1
482         fi
483     else
484         test "$BUILD_ROOT" != "${BUILD_ROOT%/*}" && mkdir -p "${BUILD_ROOT%/*}"
485         if ! mkdir $BUILD_ROOT; then
486             echo "can not create BUILD_ROOT=$BUILD_ROOT. Exit..."
487             cleanup_and_exit 1
488         fi
489     fi
490     
491     rm -rf "$BUILD_ROOT"/.build.packages
492 }
493
494 linux64()
495 {
496         perl -e 'syscall('$PERSONALITY_SYSCALL', 0); exec(@ARGV) || die("$ARGV[0]: $!\n")' "$@"
497 }
498
499 #### main ####
500
501 trap fail_exit EXIT
502
503 case `perl -V:archname` in
504     *x86_64*) PERSONALITY_SYSCALL=135 ;;
505     *i?86*)   PERSONALITY_SYSCALL=136 ;;
506 esac
507
508 shopt -s nullglob
509
510 if detect_vm_2nd_stage ; then
511     set "/.build-srcdir/$SPECFILE"
512 fi
513 export HOST
514
515 while test -n "$1"; do
516   PARAM="$1"
517   ARG="$2"
518   shift
519   case $PARAM in
520     *-*=*)
521       ARG=${PARAM#*=}
522       PARAM=${PARAM%%=*}
523       set -- "----noarg=$PARAM" "$@"
524   esac
525   case $PARAM in
526       *-help|-h)
527         echo_help
528         cleanup_and_exit
529       ;;
530       *-no*init)
531         DO_INIT=false
532       ;;
533       *-no*checks)
534         DO_CHECKS=false
535       ;;
536       *-clean)
537         CLEAN_BUILD='--clean'
538       ;;
539       *-kill)
540         KILL=true
541       ;;
542       *-rpms)
543         BUILD_RPMS="$ARG"
544         if [ -z "$BUILD_RPMS" ] ; then
545           echo_help
546           cleanup_and_exit
547         fi
548         shift
549       ;;
550       *-arch)
551         BUILD_ARCH="$ARG"
552         shift
553       ;;
554       *-verify)
555         export VERIFY_BUILD_SYSTEM=true
556       ;;
557       *-target)
558         ABUILD_TARGET_ARCH="$ARG"
559         shift
560       ;;
561       *-jobs) 
562         BUILD_JOBS="$ARG"
563         shift
564       ;;
565       *-extra*packs|-X)
566         BUILD_EXTRA_PACKS="$BUILD_EXTRA_PACKS $ARG"
567         shift
568       ;;
569       *-lint)
570         DO_LINT=true
571         ;;
572       *-baselibs)
573         CREATE_BASELIBS=true
574         ;;
575       *-baselibs-internal)
576         CREATE_BASELIBS=internal
577         ;;
578       *-root)
579         BUILD_ROOT="$ARG"
580         shift
581       ;;
582       *-oldpackages)
583         OLD_PACKAGES="$ARG"
584         shift
585       ;;
586       *-dist) 
587         BUILD_DIST="$ARG"
588         export BUILD_DIST
589         shift
590       ;;
591       *-xen|*-kvm|--uml|--qemu)
592         VM_TYPE=${PARAM##*-}
593         if [ -n "$ARG" -a "$ARG" = "${ARG#-}" ]; then
594             VM_IMAGE="$ARG"
595             shift
596         else
597             VM_IMAGE=1
598         fi
599       ;;
600       *-xenswap|*-swap)
601         VM_SWAP="$ARG"
602         shift
603       ;;
604       *-xenmemory|*-memory)
605         MEMSIZE="$ARG"
606         shift
607       ;;
608       *-rpmlist)
609         RPMLIST="--rpmlist $ARG"
610         BUILD_RPMS=
611         shift
612       ;;
613       *-release)
614         RELEASE="$ARG"
615         shift
616       ;;
617       *-logfile)
618         LOGFILE="$ARG"
619         shift
620       ;;
621       *-reason)
622         REASON="$ARG"
623         shift
624       ;;
625       *-norootforbuild)
626         NOROOTFORBUILD=true
627       ;;
628       *-stage)
629         BUILD_RPM_BUILD_STAGE="$ARG"
630         shift
631       ;;
632       *-useusedforbuild)
633         USEUSEDFORBUILD="--useusedforbuild"
634       ;;
635       *-list*state)
636         LIST_STATE=true
637       ;;
638       --define|--with|--without)
639         definesnstuff[${#definesnstuff[@]}]="$PARAM";
640         definesnstuff[${#definesnstuff[@]}]="$ARG";
641         shift
642       ;;
643       --repository|--repo)
644         if [ -z "$ARG" ] ; then
645           echo_help
646           cleanup_and_exit
647         fi
648         repos[${#repos[@]}]="$PARAM";
649         repos[${#repos[@]}]="$ARG";
650         shift
651       ;;
652       --icecream)
653         if [ -z "$ARG" ] ; then
654           echo "--icecream needs an argument" >&2
655           echo_help
656           cleanup_and_exit 1
657         fi
658         icecream="$ARG"
659         if [ "$icecream" -gt 0 ]; then
660                 BUILD_JOBS="$ARG"
661         fi
662         shift
663       ;;
664       --ccache)
665         ccache=1
666       ;;
667       --debug)
668         BUILD_DEBUG=1
669       ;;
670       --incarnation)
671         INCARNATION=$ARG
672         shift
673       ;;
674       --disturl)
675         DISTURL=$ARG
676         shift
677       ;;
678       ----noarg)
679         echo "$ARG does not take an argument"
680         cleanup_and_exit
681       ;;
682       *-changelog)
683         CHANGELOG=true
684       ;;
685       -*)
686         echo Unknown Option "$PARAM". Exit.
687         cleanup_and_exit 1
688       ;;
689       *)
690         SPECFILES[${#SPECFILES[@]}]="$PARAM";
691       ;;
692     esac
693 done
694
695 # XXX: doesn't kill vm if in prepare stage (because there is none)
696 if test -n "$KILL" ; then
697     test -z "$SRCDIR" || usage
698     if test -z "$VM_IMAGE" ; then
699         if ! $BUILD_DIR/killchroot -s 9 $BUILD_ROOT ; then
700             echo "could not kill build in $BUILD_ROOT"
701             cleanup_and_exit 1
702         fi
703     elif test "$VM_TYPE" = 'xen'; then
704         XENID="${VM_IMAGE%/root}"
705         XENID="${XENID##*/}"
706         if xm list "build:$XENID" >/dev/null 2>&1 ; then
707             if ! xm destroy "build:$XENID" ; then
708                 echo "could not kill xen build $XENID"
709                 cleanup_and_exit 1
710             fi
711         fi
712     elif test -n "$VM_TYPE"; then
713         if ! fuser -k -TERM "$VM_IMAGE"; then
714             echo "could not kill build in $VM_IMAGE"
715             cleanup_and_exit 1
716         fi
717     else
718         echo "don't know how to kill this build job"
719         cleanup_and_exit 1
720     fi
721     cleanup_and_exit 0
722 fi
723
724 if [ "$VM_TYPE" = 'kvm' ]; then
725     if [ ! -r /dev/kvm -o ! -x "$qemu_bin"-kvm ]; then
726         echo ""
727     fi
728     qemu_bin="$kvm_bin"
729     qemu_kernel="$kvm_kernel"
730     qemu_initrd="$kvm_initrd"
731     if [ ! -e $qemu_initrd-virtio -o $qemu_kernel -nt $qemu_initrd-virtio ]; then
732         if [ ! -w /root ]; then
733             echo "No initrd that provides virtio support found. virtio accelleration disabled."
734             echo "Run the following command as root to enable virtio:"
735             shellquote "${mkinitrd_virtio_cmd[@]}"
736             echo
737         elif /sbin/modinfo virtio_pci >/dev/null 2>&1; then
738             echo "creating $qemu_initrd-virtio"
739             "${mkinitrd_virtio_cmd[@]}" || cleanup_and_exit 1
740             kvm_virtio=1
741         fi
742     else
743         kvm_virtio=1
744     fi
745
746     if [ "$kvm_virtio" = 1 ]; then
747         qemu_initrd="$qemu_initrd-virtio"
748         VM_SWAPDEV=/dev/vdb
749         qemu_rootdev=/dev/vda
750     else
751         VM_SWAPDEV=/dev/sdb
752         qemu_rootdev=/dev/sda
753     fi
754 fi
755
756 if [ "$VM_TYPE" = 'qemu' ]; then
757     VM_SWAPDEV=/dev/sdb
758     qemu_rootdev=/dev/sda
759 fi
760
761 if [ -z "$RPMLIST" ]; then
762     if [ -z "$repos" -a -z "$BUILD_RPMS" ]; then
763         BUILD_RPMS="/media/dvd/suse"
764     fi
765 else
766     repos=()
767 fi
768
769 set_build_arch
770
771 if [ -n "$CLEAN_BUILD" ]; then
772     DO_INIT=true
773 fi
774
775 find_spec_files
776
777 if test -n "$LIST_STATE" ; then
778     BUILD_ROOT=`mktemp -d /var/tmp/build-list-state-XXXXXX`
779     test -d "$BUILD_ROOT" || cleanup_and_exit 1
780     SPECFILE=$SPECFILES # only one specified anyways
781     if test "$SPECFILE" != "${SPECFILE%.src.rpm}" ; then
782        rm -rf $BUILD_ROOT/usr/src/packages
783        mkdir -p $BUILD_ROOT/usr/src/packages/SOURCES $BUILD_ROOT/usr/src/packages/SPECS
784        rpm -i --nodigest --nosignature --root $BUILD_ROOT $SPECFILE || {
785            echo "could not install $SPECFILE." 2>&1
786            rm -rf $BUILD_ROOT
787            cleanup_and_exit 1
788        }
789        for SPECFILE in $BUILD_ROOT/usr/src/packages/SPECS/*.spec ; do : ; done
790     fi
791     init_buildsystem --list-state "${definesnstuff[@]}" "${repos[@]}" $USEUSEDFORBUILD $SPECFILE $BUILD_EXTRA_PACKS
792     ERR=$?
793     rm -rf $BUILD_ROOT
794     cleanup_and_exit $ERR
795 fi
796
797 if test -z "$RUNNING_IN_VM" ; then
798     if test -n "$VM_IMAGE" ; then
799         if test "$VM_IMAGE" = 1 ; then
800             VM_IMAGE="$BUILD_ROOT.img"
801         fi
802         if test ! -e "$VM_IMAGE" ; then
803             echo "you need to create a file system on $VM_IMAGE first"
804             cleanup_and_exit 1
805         fi
806     fi
807     if test -n "$VM_IMAGE" ; then
808         if test -n "$CLEAN_BUILD" ; then
809             echo "Creating filesystem on $VM_IMAGE"
810             $vm_img_mkfs $VM_IMAGE || become_root_or_fail
811             if test -n "$vm_img_tunefs" ; then
812                 $vm_img_tunefs $VM_IMAGE || become_root_or_fail
813             fi
814         fi
815         mkdir_build_root
816         if [ -w /root ]; then
817             mount -o loop $VM_IMAGE $BUILD_ROOT || cleanup_and_exit 1
818         else
819             mount $BUILD_ROOT || become_root_or_fail
820         fi
821     else
822         test -w /root || become_root_or_fail
823     fi
824     if test -n "$VM_SWAP" ; then
825         dd if=/dev/zero of="$VM_SWAP" bs=12 count=1 conv=notrunc 2>/dev/null
826         mkswap "$VM_SWAP"
827     fi
828 fi
829
830 mkdir_build_root
831
832 rm -f $BUILD_ROOT/exit
833
834 if [ -w /root ]; then
835     mkdir -p $BUILD_ROOT/proc
836     mkdir -p $BUILD_ROOT/dev/pts
837     mount -n -tproc none $BUILD_ROOT/proc || true
838     mount -n -tdevpts none $BUILD_ROOT/dev/pts
839 fi
840
841 if test -z "$VM_IMAGE" -a -z "$LOGFILE"; then
842     LOGFILE="$BUILD_ROOT/.build.log"
843 fi
844
845 if test -n "$LOGFILE" ; then
846     echo  logging output to $LOGFILE...
847     rm -f $LOGFILE
848     touch $LOGFILE
849     if test -n "$VM_IMAGE" ; then
850         exec 1> >(exec -a 'build logging tee' perl -e 'open(F,">>",$ARGV[0])||die("$ARGV[0]: $!\n");$|=1;select(F);$|=1;while(<STDIN>){print STDOUT;s/^\r//s;s/\r\n/\n/gs;print F}' $LOGFILE) 2>&1
851     else
852         exec 1> >(exec -a 'build logging tee' tee -a $LOGFILE) 2>&1
853     fi
854 fi
855
856 setmemorylimit
857
858 #
859 # say hello
860 #
861 test -z "$HOST" && HOST=`hostname`
862
863 if [ -z "$RUNNING_IN_VM" ]; then
864     echo Using BUILD_ROOT=$BUILD_ROOT
865     test -n "$BUILD_RPMS" && echo Using BUILD_RPMS=$BUILD_RPMS
866     echo Using BUILD_ARCH=$BUILD_ARCH
867     test -n "$VM_TYPE" && echo "Doing $VM_TYPE build in $VM_IMAGE"
868     echo
869 fi
870
871 test "$BUILD_ARCH" = all && BUILD_ARCH=
872 BUILD_USER_ABUILD_USED=
873
874 for SPECFILE in "${SPECFILES[@]}" ; do
875
876     SRCDIR="${SPECFILE%/*}"
877     SPECFILE="${SPECFILE##*/}"
878
879     BUILDTYPE=
880     case $SPECFILE in
881       *.spec|*.src.rpm) BUILDTYPE=spec ;;
882       *.dsc) BUILDTYPE=dsc ;;
883       *.kiwi) BUILDTYPE=kiwi ;;
884     esac
885     if test -z "$BUILDTYPE" ; then
886        echo "don't know how to build $SPECFILE"
887        cleanup_and_exit 1
888     fi
889
890     cd "$SRCDIR"
891
892     if [ -z "$RUNNING_IN_VM" ]; then
893         echo
894         echo "$HOST started \"build $SPECFILE\" at `date`."
895         echo
896         test -n "$REASON" && echo "$REASON"
897         echo
898     fi
899
900     #
901     # first setup building directory...
902     #
903     test -s "$SPECFILE" || {
904        echo "$SPECFILE" is empty.  This should not happen...
905        cleanup_and_exit 1
906     }
907
908     if test "$SPECFILE" != "${SPECFILE%.src.rpm}" ; then
909         echo processing src rpm `pwd`/$SPECFILE...
910         TOPDIR=`rpm --eval '%_topdir'`
911         rm -rf $BUILD_ROOT$TOPDIR
912         mkdir -p $BUILD_ROOT$TOPDIR/SOURCES $BUILD_ROOT$TOPDIR/SPECS
913         rpm -i --nodigest --nosignature --root $BUILD_ROOT $SPECFILE || {
914             echo "could not install $SPECFILE."
915             cleanup_and_exit 1
916         }
917         rm -rf $BUILD_ROOT/.build-srcdir
918         mkdir -p $BUILD_ROOT/.build-srcdir
919         mv $BUILD_ROOT$TOPDIR/SOURCES/* $BUILD_ROOT/.build-srcdir
920         mv $BUILD_ROOT$TOPDIR/SPECS/* $BUILD_ROOT/.build-srcdir
921         MYSRCDIR=$BUILD_ROOT/.build-srcdir
922         cd $MYSRCDIR || cleanup_and_exit 1
923         for SPECFILE in *.spec ; do : ; done
924     else
925         echo processing specfile `pwd`/$SPECFILE...
926         MYSRCDIR="$SRCDIR"
927     fi
928
929     ADDITIONAL_PACKS=""
930     test -n "$BUILD_EXTRA_PACKS" && ADDITIONAL_PACKS="$ADDITIONAL_PACKS $BUILD_EXTRA_PACKS"
931     test -n "$CREATE_BASELIBS" && ADDITIONAL_PACKS="$ADDITIONAL_PACKS build"
932     test "$ccache" = '1' && ADDITIONAL_PACKS="$ADDITIONAL_PACKS ccache"
933     test "$icecream" -gt 1 && ADDITIONAL_PACKS="$ADDITIONAL_PACKS icecream gcc-c++"
934     test -n "$DO_LINT" && ADDITIONAL_PACKS="$ADDITIONAL_PACKS rpmlint-Factory"
935
936     if test -n "$CHANGELOG" -a -z "$RUNNING_IN_VM" ; then
937         rm -f $BUILD_ROOT/.build-changelog
938         case $SPECFILE in
939           *.dsc) CFFORMAT=debian ;;
940           *) CFFORMAT=rpm ;;
941         esac
942         echo "running changelog2spec --target $CFFORMAT --file $MYSRCDIR/$SPECFILE"
943         if ! $BUILD_DIR/changelog2spec --target $CFFORMAT --file "$MYSRCDIR/$SPECFILE" > $BUILD_ROOT/.build-changelog ; then
944             rm -f $BUILD_ROOT/.build-changelog
945         fi
946     fi
947
948     if test -n "$VM_TYPE"; then
949         rm -rf $BUILD_ROOT/.build
950         mkdir -p $BUILD_ROOT/.build
951         if test "$DO_INIT" = true ; then
952             # do fist stage of init_buildsystem
953             rm -f $BUILD_ROOT/.build.success
954             set -- init_buildsystem --prepare "${definesnstuff[@]}" "${repos[@]}" $CLEAN_BUILD $USEUSEDFORBUILD $RPMLIST "$MYSRCDIR/$SPECFILE" $ADDITIONAL_PACKS
955             echo "$* ..."
956             "$@" || cleanup_and_exit 1
957             if [ ! -w /root ]; then
958                 # remove setuid bit if files belong to user to make e.g. mount work
959                 find $BUILD_ROOT/{bin,sbin,usr/bin,usr/sbin} -type f -uid $UID -perm +4000 -print0 | xargs -0 --no-run-if-empty chmod -s
960             fi
961             if [ -d "$OLD_PACKAGES" ]; then
962                rm -rf $BUILD_ROOT/.build.oldpackages
963                cp -a $OLD_PACKAGES $BUILD_ROOT/.build.oldpackages
964             fi
965         fi
966         # start up xen, rerun ourself
967         cp -a $BUILD_DIR/. $BUILD_ROOT/.build
968         if ! test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then
969             rm -rf $BUILD_ROOT/.build-srcdir
970             mkdir $BUILD_ROOT/.build-srcdir
971             if test "$BUILDTYPE" = kiwi ; then
972                 cp -pRL "$MYSRCDIR"/* $BUILD_ROOT/.build-srcdir
973             else
974                 cp -p "$MYSRCDIR"/* $BUILD_ROOT/.build-srcdir
975             fi
976             MYSRCDIR=$BUILD_ROOT/.build-srcdir
977         fi
978         Q="'\''"
979         echo "SPECFILE='${SPECFILE//"'"/$Q}'" > $BUILD_ROOT/.build/build.data
980         echo "BUILD_JOBS='${BUILD_JOBS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
981         echo "BUILD_ARCH='${BUILD_ARCH//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
982         echo "BUILD_RPMS='${BUILD_RPMS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
983         case $BUILD_DIST in
984             */*)
985                 cp $BUILD_DIST $BUILD_ROOT/.build/build.dist
986                 BUILD_DIST=/.build/build.dist
987                 ;;
988         esac
989         echo "BUILD_DIST='${BUILD_DIST//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
990         echo "RELEASE='${RELEASE//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
991         echo "BUILD_DEBUG='${BUILD_DEBUG//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
992         echo "DO_LINT='${DO_LINT//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
993         echo "DO_CHECKS='${DO_CHECKS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
994         echo "NOROOTFORBUILD='${NOROOTFORBUILD//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
995         echo "CREATE_BASELIBS='$CREATE_BASELIBS'" >> $BUILD_ROOT/.build/build.data
996         echo "REASON='${REASON//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
997         echo "CHANGELOG='${CHANGELOG//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
998         echo "INCARNATION='${INCARNATION//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
999         echo "DISTURL='${DISTURL//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1000         # FIXME: this depends on the kernel and vm.
1001         # could be hda2, sda2 for xen or hdb/sdb for qemu
1002         test -n "$VM_SWAP" && echo "VM_SWAP='${VM_SWAPDEV:-/dev/hda2}'" >> $BUILD_ROOT/.build/build.data
1003         PERSONALITY=0
1004         test -n "$PERSONALITY_SYSCALL" && PERSONALITY=`perl -e 'print syscall('$PERSONALITY_SYSCALL', 0)."\n"'`
1005         echo "OLD_PACKAGES='$OLD_PACKAGES'" >> $BUILD_ROOT/.build/build.data
1006         echo "PERSONALITY='$PERSONALITY'" >> $BUILD_ROOT/.build/build.data
1007         echo "MYHOSTNAME='`hostname`'" >> $BUILD_ROOT/.build/build.data
1008         echo -n "definesnstuff=(" >> $BUILD_ROOT/.build/build.data
1009         shellquote "${definesnstuff[@]}" >> $BUILD_ROOT/.build/build.data
1010         echo ")" >> $BUILD_ROOT/.build/build.data
1011         echo -n "repos=(" >> $BUILD_ROOT/.build/build.data
1012         shellquote "${repos[@]}" >> $BUILD_ROOT/.build/build.data
1013         echo ")" >> $BUILD_ROOT/.build/build.data
1014         umount -n $BUILD_ROOT/proc 2> /dev/null || true
1015         umount -n $BUILD_ROOT/dev/pts 2> /dev/null || true
1016         umount -n $BUILD_ROOT/mnt 2> /dev/null || true
1017
1018         # needs to work otherwise we have a corrupted file system
1019         umount $BUILD_ROOT || cleanup_and_exit 1
1020
1021         if [ "$VM_TYPE" = 'xen' ]; then
1022                 XMROOT=file:$VM_IMAGE
1023                 XMROOT=${XMROOT/#file:\/dev/phy:/dev}
1024                 XMROOT="disk=$XMROOT,hda1,w"
1025                 XMSWAP=
1026                 if test -n "$VM_SWAP" ; then
1027                     XMSWAP=file:$VM_SWAP
1028                     XMSWAP=${XMSWAP/#file:\/dev/phy:/dev}
1029                     XMSWAP="disk=$XMSWAP,hda2,w"
1030                 fi
1031                 XENID="${VM_IMAGE%/root}"
1032                 XENID="${XENID##*/}"
1033                 CROSS_INIT_SCRIPT="/.build/build"
1034                 # to run the qemu initialization in the XEN chroot, we need to register it with a statically build shell
1035                 if [ x"$BUILD_ARCH" == xarmv4l -o x"$BUILD_ARCH" == xarmv5el -o x"$BUILD_ARCH" == xarmv7el -o x"$BUILD_ARCH" == xsh4 -o x"$BUILD_ARCH" == xppc ]; then
1036                     if [  -n "$(uname -m | grep '[x3-6]86')" ]; then
1037                         if [ -e /bin/bash-static -a -e /bin/mount-static ]; then
1038                             CROSS_INIT_SCRIPT="/.build/initscript_qemu_vm"
1039                         fi
1040                     fi
1041                 fi
1042
1043                 echo "booting XEN kernel ..."
1044                 set -- xm create -c $BUILD_DIR/xen.conf name="build:$XENID" ${MEMSIZE:+memory=$MEMSIZE} $XMROOT $XMSWAP extra="quiet init="$CROSS_INIT_SCRIPT" panic=1 console=ttyS0"
1045                 if test "$PERSONALITY" != 0 ; then
1046                     # have to switch back to PER_LINUX to make xm work
1047                     set -- linux64 "$@"
1048                 fi
1049                 echo "$@"
1050                 "$@" || true # somehow this is always false
1051         elif [ "$VM_TYPE" = 'uml' ]; then
1052                 echo "booting UML kernel ..."
1053                 set -- $uml_kernel initrd=$uml_initrd root=/ubda init=/.build/build panic=1 quiet ubd0=$VM_IMAGE ${MEMSIZE:+mem=$MEMSIZE}
1054                 echo "$@"
1055                 "$@"
1056         elif [ "$VM_TYPE" = 'qemu' -o "$VM_TYPE" = 'kvm' ]; then
1057                 echo "booting $VM_TYPE ..."
1058                 if [ "$kvm_virtio" = 1 ]; then
1059                     qemu_disks=(-drive file="$VM_IMAGE",if=virtio -hda "$VM_IMAGE")
1060                     if [ -n "$VM_SWAP" ]; then
1061                         qemu_disks[${#qemu_disks[@]}]="-drive"
1062                         qemu_disks[${#qemu_disks[@]}]="file=$VM_SWAP,if=virtio"
1063                     fi
1064                 else
1065                     qemu_disks=(-hda "$VM_IMAGE")
1066                     if [ -n "$VM_SWAP" ]; then
1067                         qemu_disks[${#qemu_disks[@]}]="-hdb"
1068                         qemu_disks[${#qemu_disks[@]}]="$VM_SWAP"
1069                     fi
1070                 fi
1071                 set -- $qemu_bin -no-reboot -nographic -net none -serial stdio \
1072                     -kernel $qemu_kernel \
1073                     -initrd $qemu_initrd \
1074                     -append "root=$qemu_rootdev panic=1 quiet noapic rw console=ttyS0 init=/.build/build" \
1075                     ${MEMSIZE:+-m $MEMSIZE} \
1076                     "${qemu_disks[@]}"
1077
1078                 if test "$PERSONALITY" != 0 ; then
1079                     # have to switch back to PER_LINUX to make qemu work
1080                     set -- linux64 "$@"
1081                 fi
1082                 echo "$@"
1083                 "$@"
1084         fi
1085         if test -n "$VM_SWAP" ; then
1086             BUILDSTATUS=`dd if="$VM_SWAP" bs=12 count=1 2>/dev/null`
1087             case $BUILDSTATUS in
1088               BUILDSTATUS[02])
1089                 mkdir -p $BUILD_ROOT/.build.packages 
1090                 cd $BUILD_ROOT/.build.packages || cleanup_and_exit 1
1091                 echo "build: extracting built packages..."
1092                 extractbuild --disk "$VM_IMAGE" --input "$VM_SWAP" --skip 512 -v
1093                 # create same layout as with plain chroot
1094                 if test "$BUILDTYPE" = spec ; then
1095                   mkdir -p SRPMS
1096                   for i in *src.rpm; do mv "$i" SRPMS/; done
1097                   for i in *.rpm; do
1098                     arch=${i%.rpm}
1099                     arch=${arch##*\.}
1100                     mkdir -p RPMS/$arch
1101                     mv "$i" RPMS/$arch/;
1102                   done
1103                 elif test "$BUILDTYPE" = dsc ; then
1104                   mkdir -p DEBS
1105                   find . -type f | while read i; do mv "$i" DEBS/; done
1106                 else
1107                   mkdir -p KIWI
1108                   find . -type f | while read i; do mv "$i" KIWI/; done
1109                 fi
1110                 cleanup_and_exit ${BUILDSTATUS#BUILDSTATUS}
1111                 ;;
1112               *)
1113                 cleanup_and_exit ${BUILDSTATUS#BUILDSTATUS}
1114                 ;;
1115             esac
1116             cleanup_and_exit 1
1117         fi
1118         cleanup_and_exit 0
1119     fi
1120
1121     if test "$DO_INIT" = true ; then
1122         #
1123         # create legacy .buildenv file
1124         #
1125         test -z "$INCARNATION" && INCARNATION=0
1126         echo "BUILD_INCARNATION=$INCARNATION" > $BUILD_ROOT/.buildenv
1127         CREATE_BUILD_BINARIES=
1128         egrep '^#[       ]*needsbinariesforbuild[       ]*$' >/dev/null <$MYSRCDIR/$SPECFILE && CREATE_BUILD_BINARIES=--create-build-binaries
1129         set -- init_buildsystem "${definesnstuff[@]}" "${repos[@]}" $CLEAN_BUILD $USEUSEDFORBUILD $CREATE_BUILD_BINARIES $RPMLIST "$MYSRCDIR/$SPECFILE" $ADDITIONAL_PACKS
1130         echo "$* ..."
1131         "$@" || cleanup_and_exit 1
1132         mount -n -tproc none $BUILD_ROOT/proc || true
1133         mount -n -tdevpts none $BUILD_ROOT/dev/pts
1134         if [ -d "$OLD_PACKAGES" ]; then
1135            [ -d "$BUILD_ROOT/.build.oldpackages" ] || cp -r $OLD_PACKAGES $BUILD_ROOT/.build.oldpackages
1136         fi
1137     fi
1138
1139     if test -z "$BUILD_DIST" -a -e "$BUILD_ROOT/.guessed_dist" ; then
1140         BUILD_DIST=`cat $BUILD_ROOT/.guessed_dist`
1141         echo "assuming dist $BUILD_DIST"
1142     fi
1143
1144     #
1145     # fix rpmrc if we are compiling for i686
1146     #
1147     test -f $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 && mv $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 $BUILD_ROOT/usr/lib/rpm/rpmrc
1148     if test -e $BUILD_ROOT/usr/lib/rpm/rpmrc -a "$BUILD_ARCH" != "${BUILD_ARCH#i686}" ; then
1149         mv $BUILD_ROOT/usr/lib/rpm/rpmrc $BUILD_ROOT/usr/lib/rpm/rpmrc_i586
1150         sed -e 's/^buildarchtranslate: athlon.*/buildarchtranslate: athlon: i686/' -e 's/^buildarchtranslate: i686.*/buildarchtranslate: i686: i686/' < $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 > $BUILD_ROOT/usr/lib/rpm/rpmrc
1151     fi
1152
1153     #
1154     # check if we want to build with the abuild user
1155     #
1156     BUILD_USER=abuild
1157     if test -x $BUILD_ROOT/bin/rpm ; then
1158         SUSE_VERSION=`chroot $BUILD_ROOT /bin/rpm --eval '%{?suse_version}' 2>/dev/null`
1159         if test -n "$SUSE_VERSION" && test "$SUSE_VERSION" -le 1020 ; then
1160             BUILD_USER=root
1161         fi
1162     fi
1163     if test "$BUILD_USER" = abuild ; then
1164         egrep '^#[       ]*needsrootforbuild[       ]*$' >/dev/null <$SPECFILE && BUILD_USER=root
1165     else
1166         egrep '^#[       ]*norootforbuild[       ]*$' >/dev/null <$SPECFILE && BUILD_USER=abuild
1167     fi
1168     test -n "$NOROOTFORBUILD" && BUILD_USER=abuild
1169     test "$BUILDTYPE" = kiwi && BUILD_USER=root
1170
1171     if test $BUILD_USER = abuild ; then
1172         if ! egrep '^abuild:' >/dev/null <$BUILD_ROOT/etc/passwd ; then
1173             echo 'abuild::399:399:Autobuild:/home/abuild:/bin/bash' >>$BUILD_ROOT/etc/passwd
1174             echo 'abuild:*:::::::' >>$BUILD_ROOT/etc/shadow # This is needed on Mandriva 2009
1175             echo 'abuild:*:::::::' >>$BUILD_ROOT/etc/gshadow # This is needed on Ubuntu 8.10
1176             echo 'abuild::399:' >>$BUILD_ROOT/etc/group
1177             mkdir -p $BUILD_ROOT/home/abuild
1178             chown 399:399 $BUILD_ROOT/home/abuild
1179         fi
1180         if test -f $BUILD_ROOT/etc/shadow ; then
1181             sed -e "s@^root::@root:*:@" < $BUILD_ROOT/etc/shadow > $BUILD_ROOT/etc/shadow.t && mv $BUILD_ROOT/etc/shadow.t $BUILD_ROOT/etc/shadow
1182         fi
1183         if test -f $BUILD_ROOT/etc/gshadow ; then
1184             sed -e "s@^root::@root:*:@" < $BUILD_ROOT/etc/gshadow > $BUILD_ROOT/etc/gshadow.t && mv $BUILD_ROOT/etc/gshadow.t $BUILD_ROOT/etc/gshadow
1185         fi
1186         BUILD_USER_ABUILD_USED=true
1187     else
1188         if egrep '^abuild:' >/dev/null <$BUILD_ROOT/etc/passwd ; then
1189             rm -rf $BUILD_ROOT/home/abuild
1190             egrep -v '^abuild:' <$BUILD_ROOT/etc/passwd >$BUILD_ROOT/etc/passwd.new
1191             mv $BUILD_ROOT/etc/passwd.new $BUILD_ROOT/etc/passwd
1192             egrep -v '^abuild:' <$BUILD_ROOT/etc/group >$BUILD_ROOT/etc/group.new
1193             mv $BUILD_ROOT/etc/group.new $BUILD_ROOT/etc/group
1194             if test -f $BUILD_ROOT/etc/shadow ; then
1195               egrep -v '^abuild:' <$BUILD_ROOT/etc/shadow >$BUILD_ROOT/etc/shadow.new
1196               mv $BUILD_ROOT/etc/shadow.new $BUILD_ROOT/etc/shadow
1197             fi
1198             if test -f $BUILD_ROOT/etc/gshadow ; then
1199               egrep -v '^abuild:' <$BUILD_ROOT/etc/gshadow >$BUILD_ROOT/etc/gshadow.new
1200               mv $BUILD_ROOT/etc/gshadow.new $BUILD_ROOT/etc/gshadow
1201             fi
1202         fi
1203     fi
1204
1205     if test "$BUILDTYPE" = spec ; then
1206         TOPDIR=`chroot $BUILD_ROOT su -c "rpm --eval '%_topdir'" - $BUILD_USER`
1207     else
1208         TOPDIR=/usr/src/packages
1209         mkdir -p $BUILD_ROOT$TOPDIR
1210     fi
1211
1212     rm -f $BUILD_ROOT/.build.packages
1213     ln -s ${TOPDIR#/} $BUILD_ROOT/.build.packages
1214
1215     mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1216     mount -n -tdevpts none $BUILD_ROOT/dev/pts 2> /dev/null
1217  
1218     setupicecream
1219
1220     setupccache
1221
1222     # nasty hack to prevent rpath on known paths
1223     # FIXME: do this only for suse
1224     if test -d "$BUILD_ROOT/etc/profile.d" ; then
1225         echo "export SUSE_IGNORED_RPATHS=/etc/ld.so.conf" > "$BUILD_ROOT/etc/profile.d/buildsystem.sh"
1226     fi
1227
1228     #
1229     # now clean up RPM building directories
1230     #
1231     rm -rf $BUILD_ROOT$TOPDIR
1232     for i in BUILD RPMS/`uname -m` RPMS/i386 RPMS/noarch SOURCES SPECS SRPMS BUILDROOT; do
1233         mkdir -p $BUILD_ROOT$TOPDIR/$i
1234         test $BUILD_USER = abuild && chown 399:399 $BUILD_ROOT$TOPDIR/$i
1235     done
1236     test -e $BUILD_ROOT/exit && cleanup_and_exit
1237
1238     mkdir -p $BUILD_ROOT$TOPDIR/SOURCES
1239     if test "$BUILDTYPE" = kiwi ; then
1240         mkdir -p $BUILD_ROOT$TOPDIR/KIWI
1241         if test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then
1242             mv "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1243         else
1244             cp -dLR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1245         fi
1246     else
1247         cp -p "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1248     fi
1249     test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir && rm -rf "$MYSRCDIR"
1250
1251     CHANGELOGARGS=
1252     test -n "$CHANGELOG" -a -f "$BUILD_ROOT/.build-changelog" && CHANGELOGARGS="--changelog $BUILD_ROOT/.build-changelog"
1253
1254     if test "$BUILDTYPE" = spec ; then
1255         # do buildrequires/release substitution
1256         args=()
1257         if test -n "$RELEASE"; then
1258                 args=(--release "$RELEASE")
1259         fi
1260         substitutedeps "${args[@]}" --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$BUILD_DIR/configs" $CHANGELOGARGS "$BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE" "$BUILD_ROOT/.spec.new" || cleanup_and_exit 1
1261         # extract macros from configuration
1262         getmacros --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$BUILD_DIR/configs" > $BUILD_ROOT/root/.rpmmacros
1263         if test -n "$BUILD_DEBUG" ; then
1264             echo '
1265 %prep %?_suse_insert_debug_package%%prep
1266 %package %?_suse_insert_debug_package%%package
1267 %suse_insert_debug_package \
1268   %global _suse_insert_debug_package \\\
1269     %%undefine _suse_insert_debug_package \\\
1270     %%debug_package
1271
1272 ' >> $BUILD_ROOT/root/.rpmmacros
1273         fi
1274         test $BUILD_USER = abuild && cp -p $BUILD_ROOT/root/.rpmmacros $BUILD_ROOT/home/abuild/.rpmmacros
1275         # extract optflags from configuration
1276         getoptflags --dist "$BUILD_DIST" --configdir "$BUILD_DIR/configs" --archpath "$BUILD_ARCH" ${BUILD_DEBUG:+--debug} > $BUILD_ROOT/root/.rpmrc
1277         test $BUILD_USER = abuild && cp -p $BUILD_ROOT/root/.rpmrc $BUILD_ROOT/home/abuild/.rpmrc
1278     fi
1279     if test -f $BUILD_ROOT/.spec.new ; then
1280         if ! cmp -s $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT/.spec.new ; then
1281             echo -----------------------------------------------------------------
1282             echo I have the following modifications for $SPECFILE:
1283             diff $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT/.spec.new
1284             mv $BUILD_ROOT/.spec.new $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE
1285         else
1286             rm -f $BUILD_ROOT/.spec.new
1287         fi
1288     fi
1289
1290     if test "$BUILDTYPE" = dsc ; then
1291         rm -rf $BUILD_ROOT$TOPDIR/BUILD
1292         test $BUILD_USER = abuild && chown 399:399 $BUILD_ROOT$TOPDIR
1293         DEB_TRANSFORM=
1294         DEB_SOURCEDIR=$TOPDIR/SOURCES
1295         DEB_DSCFILE=$SPECFILE
1296         for f in $BUILD_ROOT$TOPDIR/SOURCES/debian.* ; do
1297             test -f $f && DEB_TRANSFORM=true
1298         done
1299         if test -n "$DEB_TRANSFORM" ; then
1300             echo "running debian transformer..."
1301             mkdir -p $BUILD_ROOT$TOPDIR/SOURCES.DEB
1302             if ! debtransform $CHANGELOGARGS $BUILD_ROOT$TOPDIR/SOURCES $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT$TOPDIR/SOURCES.DEB ; then
1303                 echo "debian transforming failed."
1304                 cleanup_and_exit 1
1305             fi
1306             DEB_SOURCEDIR=$TOPDIR/SOURCES.DEB
1307             for DEB_DSCFILE in $BUILD_ROOT/$DEB_SOURCEDIR/*.dsc ; do : ; done
1308             DEB_DSCFILE="${DEB_DSCFILE##*/}"
1309         fi
1310         chroot $BUILD_ROOT su -c "dpkg-source -x $DEB_SOURCEDIR/$DEB_DSCFILE $TOPDIR/BUILD" - $BUILD_USER
1311     fi
1312
1313     if test $BUILD_USER = abuild ; then
1314         chown -R 399:399 $BUILD_ROOT$TOPDIR/*
1315     else
1316         chown -R root:root $BUILD_ROOT$TOPDIR/*
1317     fi
1318     cd $BUILD_ROOT$TOPDIR/SOURCES || cleanup_and_exit 1
1319
1320     echo -----------------------------------------------------------------
1321     if test "$BUILD_USER" = root ; then
1322         echo ----- building $SPECFILE
1323     else
1324         echo ----- building $SPECFILE "(user $BUILD_USER)"
1325     fi
1326     echo -----------------------------------------------------------------
1327     echo -----------------------------------------------------------------
1328     test -n "$RUNNING_IN_VM" && ifconfig lo 127.0.0.1 up
1329     test -n "$RUNNING_IN_VM" -a -n "$MYHOSTNAME" && hostname "$MYHOSTNAME"
1330
1331     BUILD_SUCCEEDED=false
1332
1333     if test "$BUILDTYPE" = spec ; then
1334         test -z "$BUILD_RPM_BUILD_STAGE" && BUILD_RPM_BUILD_STAGE=-ba
1335
1336         # XXX: move _srcdefattr to macro file?
1337         rpmbopts=("$BUILD_RPM_BUILD_STAGE" "--define" "_srcdefattr (-,root,root)")
1338         if [ -n "$ABUILD_TARGET_ARCH" ]; then
1339                 rpmbopts[${#rpmbopts[@]}]="--target=$ABUILD_TARGET_ARCH"
1340         fi
1341         if [ -n "$BUILD_JOBS" ]; then
1342                 rpmbopts[${#rpmbopts[@]}]='--define'
1343                 rpmbopts[${#rpmbopts[@]}]="jobs $BUILD_JOBS"
1344         fi
1345         if [ -n "$DISTURL" ]; then
1346                 rpmbopts[${#rpmbopts[@]}]='--define'
1347                 rpmbopts[${#rpmbopts[@]}]="disturl $DISTURL"
1348         fi
1349         if test -s "$BUILD_ROOT/usr/lib/rpm/mandriva/macros" ; then
1350                 rpmbopts[${#rpmbopts[@]}]='--eval'
1351                 rpmbopts[${#rpmbopts[@]}]="%undefine _enable_debug_packages"
1352         fi
1353         if [ -n "$BUILD_DEBUG" ]; then
1354                 rpmbopts[${#rpmbopts[@]}]='--eval'
1355                 rpmbopts[${#rpmbopts[@]}]="%suse_insert_debug_package"
1356         fi
1357         rpmbuild=rpmbuild
1358
1359         test -x $BUILD_ROOT/usr/bin/rpmbuild || rpmbuild=rpm
1360         # su involves a shell which would require even more
1361         # complicated quoting to bypass than this
1362         toshellscript $rpmbuild \
1363                 "${definesnstuff[@]}" \
1364                 "${rpmbopts[@]}" \
1365                 "$TOPDIR/SOURCES/$SPECFILE" \
1366                 > $BUILD_ROOT/.build.command
1367         chmod 755 $BUILD_ROOT/.build.command
1368         chroot $BUILD_ROOT su -c /.build.command - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
1369     fi
1370
1371     if test "$BUILDTYPE" = dsc ; then
1372         chroot $BUILD_ROOT su -c "cd $TOPDIR/BUILD && dpkg-buildpackage -us -uc -rfakeroot-tcp" - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
1373         mkdir -p $BUILD_ROOT/$TOPDIR/DEBS
1374         for DEB in $BUILD_ROOT/$TOPDIR/*.deb ; do
1375             test -e "$DEB" && mv "$DEB" "$BUILD_ROOT/$TOPDIR/DEBS"
1376         done
1377         # link sources over
1378         ln $BUILD_ROOT/$DEB_SOURCEDIR/$DEB_DSCFILE $BUILD_ROOT/$TOPDIR/DEBS/
1379         while read f ; do
1380             ln $BUILD_ROOT/$DEB_SOURCEDIR/$f $BUILD_ROOT/$TOPDIR/DEBS/
1381         done < <(sed -ne '/^Files:/,$s/^ ................................ [0-9][0-9]* //p' < $BUILD_ROOT/$DEB_SOURCEDIR/$DEB_DSCFILE)
1382     fi
1383
1384     if test "$BUILDTYPE" = kiwi ; then
1385         # run createrepo on the repositories
1386         if test -d $BUILD_ROOT/$TOPDIR/SOURCES/repos ; then
1387             (
1388             ln -sf $TOPDIR/SOURCES/repos $BUILD_ROOT/repos
1389             cd $BUILD_ROOT/$TOPDIR/SOURCES/repos
1390             for r in */* ; do
1391                 test -L $r && continue
1392                 test -d $r || continue
1393                 repo="$TOPDIR/SOURCES/repos/$r/"
1394                 # create compatibility link
1395                 rc="${r//:/:/}"
1396                 if test "$rc" != "$r" ; then
1397                     rl="${rc//[^\/]}"
1398                     rl="${rl//?/../}"
1399                     mkdir -p "${rc%/*}"
1400                     ln -s $rl$r "${rc%/*}/${rc##*/}"
1401                     repo="$TOPDIR/SOURCES/repos/${rc%/*}/${rc##*/}/"
1402                 fi
1403                 echo "creating repodata for $repo"
1404                 chroot $BUILD_ROOT createrepo "$repo"
1405             done
1406             )
1407         fi
1408         # unpack root tar
1409         for t in $BUILD_ROOT/$TOPDIR/SOURCES/root.tar* ; do
1410             test -f $t || continue
1411             mkdir -p $BUILD_ROOT/$TOPDIR/SOURCES/root
1412             chroot $BUILD_ROOT tar -C $TOPDIR/SOURCES/root -xf "$TOPDIR/SOURCES/${t##*/}"
1413         done
1414         # fix script permissions
1415         chmod a+x $BUILD_ROOT/$TOPDIR/SOURCES/*.sh
1416         # unpack tar files in image directories
1417         if test -d $BUILD_ROOT/$TOPDIR/SOURCES/images ; then
1418             (
1419             cd $BUILD_ROOT/$TOPDIR/SOURCES/images
1420             for r in */* ; do
1421                 test -L $r && continue
1422                 test -d $r || continue
1423                 for t in $r/root.tar* ; do
1424                     test -f $t || continue
1425                     mkdir -p $r/root
1426                     chroot $BUILD_ROOT tar -C $TOPDIR/SOURCES/images/$r/root -xf "$TOPDIR/SOURCES/images/$r/${t##*/}"
1427                 done
1428                 # fix script permissions
1429                 chmod a+x $BUILD_ROOT/$TOPDIR/SOURCES/images/$r/*.sh
1430                 # create compatibility link
1431                 rc="${r//:/:/}"
1432                 if test "$rc" != "$r" ; then
1433                     rl="${rc//[^\/]}"
1434                     rl="${rl//?/../}"
1435                     mkdir -p "${rc%/*}"
1436                     ln -s $rl$r "${rc%/*}/${rc##*/}"
1437                 fi
1438             done
1439             )
1440         fi
1441         rm -f $BUILD_ROOT/$TOPDIR/SOURCES/config.xml
1442         ln -s $SPECFILE $BUILD_ROOT/$TOPDIR/SOURCES/config.xml
1443         imagetype=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$SPECFILE imagetype)
1444         imagename=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$SPECFILE filename)
1445         imageversion=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$SPECFILE version)
1446         chroot $BUILD_ROOT su -c "kiwi --version" -
1447         if test "$imagetype" = product ; then
1448             echo "running kiwi --create-instsource..."
1449             chroot $BUILD_ROOT su -c "kiwi --root $TOPDIR/KIWIROOT -v -v --instsource-local --logfile terminal -p $TOPDIR/SOURCES --create-instsource $TOPDIR/SOURCES" - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
1450             for i in $BUILD_ROOT/$TOPDIR/KIWIROOT/main/* ; do
1451                 test -d "$i" || continue
1452                 n="${i##*/}"
1453                 test "$n" = scripts && continue
1454                 test "$n" != "${n%0}" && continue
1455                 chroot $BUILD_ROOT su -c "suse-isolinux $TOPDIR/KIWIROOT/main/$n $TOPDIR/KIWI/$n.iso" - $BUILD_USER
1456             done
1457         else
1458             BUILD_SUCCEEDED=true
1459             if [ -z "$RUNNING_IN_VM" ]; then
1460                 # NOTE: this must be done with the outer system, because it loads the dm-mod kernel modules, which needs to fit to the kernel.
1461                 echo "starting device mapper for kiwi..."
1462                 [ -x /etc/init.d/boot.device-mapper ] && /etc/init.d/boot.device-mapper start
1463             fi
1464             for imgtype in $imagetype ; do
1465                 echo "running kiwi --prepare for $imgtype..."
1466                 if chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --prepare $TOPDIR/SOURCES --logfile terminal --root $TOPDIR/KIWIROOT-$imgtype" - $BUILD_USER < /dev/null ; then
1467                     echo "running kiwi --create for $imgtype..."
1468                     mkdir -p $BUILD_ROOT/$TOPDIR/KIWI-$imgtype
1469                     chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --create $TOPDIR/KIWIROOT-$imgtype --logfile terminal --type $imgtype -d $TOPDIR/KIWI-$imgtype" - $BUILD_USER < /dev/null || cleanup_and_exit 1
1470                 else
1471                     cleanup_and_exit 1
1472                 fi
1473             done
1474
1475             # create tar.gz of images, in case it makes sense
1476             imagearch=`uname -m`
1477             buildnum=""
1478             if test -n "$RELEASE"; then
1479                 buildnum="-Build$RELEASE"
1480             fi
1481             for imgtype in $imagetype ; do
1482                 case "$imgtype" in
1483                     oem)
1484                         pushd $BUILD_ROOT/$TOPDIR/KIWI-oem > /dev/null
1485                         echo "compressing images... "
1486                         tar cvzfS $BUILD_ROOT/$TOPDIR/KIWI/$imagename.$imagearch-$imageversion$buildnum-raw.tar.gz \
1487                           --exclude=$imagename.$imagearch-$imageversion.iso *
1488                         if [ -e $imagename.$imagearch-$imageversion.iso ]; then
1489                           echo "Copy iso file and create md5..."
1490                           mv $imagename.$imagearch-$imageversion.iso \
1491                              $BUILD_ROOT/$TOPDIR/KIWI/$imagename.$imagearch-$imageversion$buildnum.iso
1492                           pushd $BUILD_ROOT/$TOPDIR/KIWI > /dev/null
1493                           md5sum $imagename.$imagearch-$imageversion$buildnum.iso \
1494                                  > "$imagename.$imagearch-$imageversion$buildnum.iso.md5"
1495                           popd > /dev/null
1496                         fi
1497                         popd > /dev/null
1498                         ;;
1499                     vmx)
1500                         pushd $BUILD_ROOT/$TOPDIR/KIWI-vmx > /dev/null
1501                         echo "compressing images... "
1502                         tar cvzfS $BUILD_ROOT/$TOPDIR/KIWI/$imagename.$imagearch-$imageversion$buildnum-vmx.tar.gz \
1503                           $imagename.$imagearch-$imageversion.vmx \
1504                           $imagename.$imagearch-$imageversion.vmdk
1505                         
1506                         if [ -e $imagename.$imagearch-$imageversion.ovf ] ; then
1507                           tar cvzfS $BUILD_ROOT/$TOPDIR/KIWI/$imagename.$imagearch-$imageversion$buildnum-ovf.tar.gz \
1508                             $imagename.$imagearch-$imageversion.ovf \
1509                             $imagename.$imagearch-$imageversion-disk*.vmdk
1510                         fi
1511                         popd > /dev/null
1512                         ;;
1513                     xen)
1514                         pushd $BUILD_ROOT/$TOPDIR/KIWI-xen > /dev/null
1515                         echo "compressing images... "
1516                         tar cvzfS $BUILD_ROOT/$TOPDIR/KIWI/$imagename.$imagearch-$imageversion$buildnum-xen.tar.gz \
1517                           `grep ^kernel $imagename.$imagearch-$imageversion.xenconfig | cut -d'"'  -f2` \
1518                           `grep ^ramdisk $imagename.$imagearch-$imageversion.xenconfig | cut -d'"'  -f2` \
1519                           $imagename.$imagearch-$imageversion.xenconfig \
1520                           $imagename.$imagearch-$imageversion
1521                         popd > /dev/null
1522                         ;;
1523                     pxe)
1524                         pushd $BUILD_ROOT/$TOPDIR/KIWI-pxe > /dev/null
1525                         echo "compressing images... "
1526                         tar cvzfS $BUILD_ROOT/$TOPDIR/KIWI/$imagename.$imagearch-$imageversion$buildnum-pxe.tar.gz \
1527                           $imagename.$imagearch-$imageversion.config \
1528                           $imagename.$imagearch-$imageversion.md5 \
1529                           $imagename.$imagearch-$imageversion.gz \
1530                           initrd-*
1531                         popd > /dev/null
1532                         ;;
1533                     iso)
1534                         pushd $BUILD_ROOT/$TOPDIR/KIWI-iso > /dev/null
1535                         echo "creating md5 sum for iso images... "
1536                         for i in *.iso; do
1537                           pushd $BUILD_ROOT/$TOPDIR/KIWI/ > /dev/null
1538                           mv $BUILD_ROOT/$TOPDIR/KIWI-iso/$i ${i%.iso}$buildnum.iso
1539                           md5sum ${i%.iso}$buildnum.iso > ${i%.iso}$buildnum.iso.md5
1540                           popd > /dev/null
1541                         done
1542                         popd > /dev/null
1543                         ;;
1544                     *)
1545                         pushd $BUILD_ROOT/$TOPDIR/KIWI-$imgtype > /dev/null
1546                         echo "compressing unkown images... "
1547                         tar cvzfS $BUILD_ROOT/$TOPDIR/KIWI/$imagename.$imagearch-$imageversion$buildnum-$imgtype.tar.gz \
1548                           *
1549                         popd > /dev/null
1550                         ;;
1551                 esac
1552             done
1553         fi
1554     fi
1555
1556     test "$BUILD_SUCCEEDED" = true || cleanup_and_exit 1
1557     test -d "$SRCDIR" && cd "$SRCDIR"
1558 done
1559
1560 RPMS=`find $BUILD_ROOT/$TOPDIR/RPMS -type f -name "*.rpm"`
1561
1562 if test -n "$RPMS" -a -n "$BUILD_USER_ABUILD_USED" ; then
1563     echo "... checking for files with abuild user/group"
1564     BADFILE=
1565     while read un gn fn ; do
1566         if test "$un" = abuild -o "$gn" = abuild -o "$un" = 399 -o "$gn" = 399 ; then
1567             echo "  $un $gn $fn"
1568             BADFILE=true
1569         fi
1570     done < <(rpm -qp --qf '[%{FILEUSERNAME} %{FILEGROUPNAME} %{FILENAMES}\n]' $RPMS)
1571     if test -n "$BADFILE" ; then
1572         echo "please fix your filelist (e.g. add defattr)"
1573         cleanup_and_exit 1
1574     fi
1575 fi
1576
1577 if test -n "$RPMS" -a -d "$BUILD_ROOT/usr/lib/build/checks" ; then
1578     mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1579     export PNAME=""
1580     export DO_RPM_REMOVE=true
1581     for SRPM in $BUILD_ROOT/$TOPDIR/SRPMS/*src.rpm ; do
1582         test -f "$SRPM" && PNAME=`rpm --nodigest --nosignature -qp --qf "%{NAME}" $SRPM`
1583     done
1584     for CHECKSCRIPT in $BUILD_ROOT/usr/lib/build/checks/* ; do
1585         echo "... running `basename $CHECKSCRIPT`"
1586         $CHECKSCRIPT || cleanup_and_exit 1
1587     done
1588     umount -n $BUILD_ROOT/proc 2> /dev/null
1589 fi
1590
1591 if test -n "$RPMS" -a "$DO_CHECKS" != "false" -a -x "$BUILD_ROOT/opt/testing/bin/rpmlint" -a -d "$BUILD_ROOT/$TOPDIR/RPMS" ; then
1592     LINT_RPM_FILE_LIST=($(find $BUILD_ROOT/$TOPDIR/RPMS \
1593         \( -name "*-debuginfo-*" -o -name "*-debugsource-*" \
1594         -o -name "*-32bit-*" -o -name "*-64bit-*" \
1595         -o -name "*-x86-*" -o -name "*-ia32-*" \) -prune \
1596         -o -type f -name '*.rpm' -print))
1597     SRPM_FILE_LIST=($(find $BUILD_ROOT/$TOPDIR/SRPMS -type f -name "*.rpm"))
1598     echo
1599     echo "RPMLINT report:"
1600     echo "==============="
1601     chroot $BUILD_ROOT /opt/testing/bin/rpmlint --info ${LINT_RPM_FILE_LIST[*]#$BUILD_ROOT} ${SRPM_FILE_LIST[*]#$BUILD_ROOT} || cleanup_and_exit 1
1602 fi
1603
1604 if test -n "$RPMS" -a -n "$CREATE_BASELIBS" -a -d "$BUILD_ROOT/$TOPDIR/RPMS" ; then
1605     if test -f $BUILD_ROOT/usr/lib/build/mkbaselibs -o "$CREATE_BASELIBS" != internal ; then
1606         mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1607         create_baselibs
1608         umount -n $BUILD_ROOT/proc 2> /dev/null
1609     fi
1610 fi
1611
1612 # IMPLEMENT ME: place code for creating binary package deltas here
1613
1614 if test -n "$RUNNING_IN_VM" -a -n "$VM_SWAP"; then
1615     echo "... saving built packages"
1616     swapoff "$VM_SWAP"
1617     args="--padstart 512 --padend 512 -v"
1618     case "$BUILDTYPE" in
1619         spec)
1620             computeblocklists $args $TOPDIR/RPMS/*/*.rpm $TOPDIR/SRPMS/*.rpm > "$VM_SWAP"
1621             ;;
1622         dsc)
1623             computeblocklists $args $TOPDIR/DEBS/*.deb > "$VM_SWAP"
1624             ;;
1625         kiwi)
1626             computeblocklists $args $TOPDIR/KIWI/* > "$VM_SWAP"
1627             ;;
1628     esac || cleanup_and_exit 1
1629 fi
1630
1631 if test -n "$RPMS" -a -d "$BUILD_ROOT/$TOPDIR/RPMS" -a -d "$BUILD_ROOT/.build.oldpackages" -a -x "$BUILD_ROOT/usr/lib/build/same-build-result.sh" ; then
1632     echo "... comparing built packages with the former built"
1633     # exit with 2, if packages built successfull, but have no changes to former built packages.
1634     chroot $BUILD_ROOT /usr/lib/build/same-build-result.sh /.build.oldpackages "$TOPDIR/RPMS" "$TOPDIR/SRPMS" && cleanup_and_exit 2
1635 fi
1636
1637 echo
1638 echo "$HOST finished \"build $SPECFILE\" at `date`."
1639 echo
1640
1641 cleanup_and_exit 0