use no-kvmclock. Time should be still consistent, maybe running a bit slower on long...
[opensuse: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_HOST_ARCH BUILD_ROOT BUILD_RPMS BUILD_DIR BUILD_DEBUG
14 export BUILD_DIST
15
16 ccache=0
17 icecream=0
18 shell=
19 definesnstuff=()
20 repos=()
21 old_packages=()
22
23 # defaults for vm_img_mkfs
24 vm_img_mkfs_ext4='mkfs.ext4 -m 0 -q -F'
25 vm_img_tunefs_ext4='tune2fs -c 0 -o journal_data_writeback'
26 vm_img_mkfs_ext3='mkfs.ext3 -m 0 -q -F'
27 vm_img_tunefs_ext3='tune2fs -c 0 -o journal_data_writeback'
28 vm_img_mkfs_ext2='mkfs.ext2 -m 0 -q -F'
29 vm_img_tunefs_ext2='tune2fs -c 0'
30 vm_img_mkfs_reiserfs='mkreiserfs -q -f'
31
32 qemu_kernel=/boot/vmlinuz
33 qemu_initrd=/boot/initrd
34 qemu_bin=/usr/bin/qemu
35 uml_kernel=/boot/vmlinux-um
36 uml_initrd=/boot/initrd-um
37
38 kvm_bin=/usr/bin/qemu-kvm
39 # whether we have virtio support
40 kvm_virtio=
41
42 # need to restore build root owner for non-root builds
43 browner=0
44
45 # Default uid:gid for the build user
46 ABUILD_UID=399
47 ABUILD_GID=399
48
49 DO_INIT=true
50 DO_LINT=
51 DO_CHECKS=true
52 CLEAN_BUILD=
53 SPECFILES=()
54 SRCDIR=
55 BUILD_JOBS=
56 ABUILD_TARGET=
57 CREATE_BASELIBS=
58 USEUSEDFORBUILD=
59 LIST_STATE=
60 VM_IMAGE=
61 VM_SWAP=
62 VM_KERNEL=
63 VM_INITRD=
64 VMDISK_ROOTSIZE=4096
65 VMDISK_SWAPSIZE=1024
66 VMDISK_FILESYSTEM=ext3
67 MEMSIZE=
68 RUNNING_IN_VM=
69 RPMLIST=
70 RELEASE=
71 REASON=
72 NOROOTFORBUILD=
73 LOGFILE=
74 KILL=
75 CHANGELOG=
76 BUILD_DEBUG=
77 PERSONALITY_SYSCALL=
78 INCARNATION=
79 DISTURL=
80 LINKSOURCES=
81 OVERLAY=
82 RSYNCSRC=
83 RSYNCDEST=
84 RSYNCDONE=
85 SIGNDUMMY=
86
87 # list of archs which need emulator initialization
88 : ${EMULATOR_ARCHS:="armv4l armv5el armv6el armv7el armv8el mips mipsel mips64 mips64el ppc ppc64 sh4"}
89 export EMULATOR_ARCHS
90
91 # list of devices registered by binfmt handlers in /proc/sys/fs/binfmt_misc
92 : ${EMULATOR_DEVS:="arm mips mipsel mips64 mips64el ppc sh4 sparc"}
93 export EMULATOR_DEVS
94
95 : ${CACHE_DIR:=/var/cache/build}
96
97 # This is for insserv
98 export YAST_IS_RUNNING=instsys
99
100 unset LANGUAGE
101 unset LANG
102 export LC_ALL=POSIX
103 umask 022
104
105 echo_help () {
106     cat << EOT
107
108 Some comments for build
109 -----------------------
110
111 With build you can create rpm packages.  They will be built in a chroot
112 system.  This chroot system will be setup automatically.  Normally you can
113 simply call build with a spec file as parameter - nothing else has to be
114 set.
115
116 If you want to set the directory were the chroot system will be setup
117 (at the moment it uses $BUILD_ROOT),
118 simply set the the environment variable BUILD_ROOT.
119
120 Example:
121
122   export BUILD_ROOT=/var/tmp/mybuildroot
123
124
125 Normally build builds the complete package including src.rpm (rpmbuild -ba).
126 If you want let build only make the binary package, simply set
127
128    export BUILD_RPM_BUILD_STAGE=-bb
129
130 (or -bc, -bp, -bi, ...  see "Maximum RPM" for more details [*]).
131
132 When the build command succeeds, the rpm files can be found under
133 $BUILD_ROOT/usr/src/packages/RPMS/
134
135
136 Known Parameters:
137
138   --help      You already got it :)
139
140   --clean     Delete old build root before initializing it
141
142   --no-init   Skip initialization of build root and start with build
143               immediately.
144
145   --no-checks Do not run post-build checks
146
147   --repository PATH
148               Use package repository at PATH. Supported formats are
149               rpm-md and yast2.
150               Alternatively zypp://NAME specifies the zypp
151               repository NAME. The repo must be refreshed with zypp
152               so package meta data is available locally. With emtpy
153               NAME all enabled repositories are used.
154               a url can specify a remote repo.
155
156   --rpms path1:path2:...
157               Specify path where to find the RPMs for the build system
158
159   --arch arch1:arch2:...
160               Specify what architectures to select from the RPMs
161
162   --verify    Run verify when initializing the build root
163
164   --extra-packs pack
165               Also install package 'pack'
166
167   --root rootdir
168               Use 'rootdir' to setup chroot environment
169
170   --cachedir cachedir
171               Use 'cachedir' to cache remote repo's packages, the
172               default cache dir is /var/cache/build, every repo
173               given by --repository corresponds to a subdir named
174               as md5sum of its repo url, forx eaxmple:
175                  /var/cache/build/3e8ea9b47808629414a0cebc33ea285e
176
177   --oldpackages oldpackagesdir
178               Define a directory with a former build
179
180   --baselibs  Create -32bit/-64bit/-x86 rpms for other architectures
181
182   --list-state
183               List rpms that would be used to create a fresh build root.
184               Does not create the build root or perform a build.
185
186   --with X
187               enable feature X for build
188
189   --without X
190               disable feature X for build
191
192   --define 'X Y'
193               define macro X with value Y
194
195   --ccache
196               use ccache to speed up rebuilds
197
198   --icecream N
199               use N parallel build jobs with icecream
200   --overlay OVERLAY
201               Copy overlay filesystem to buildroot after installing
202               all RPMs. This must be a valid directory.
203
204   --rsync-src RSYNCSRC
205               Copy overlay folder (RSYNCSRC) to a folder (RSYNCDEST)
206               inside the buildroot using rsync.
207               It will "%define RSYNCDONE 1" for handling %setup in your
208               specfile. E.g.:
209               %prep
210               %if 0%{?RSYNCDONE}
211               %setup -n aaa_base -T -D -b 5 -b 7
212               %else
213               %setup -n aaa_base -b 5 -b 7
214               %endif
215
216   --rsync-dest RSYNCDEST
217               Copy overlay folder (RSYNCSRC) to a folder (RSYNCDEST)
218               inside the buildroot using rsync.
219
220   --uid uid:gid
221               Specify the uid and gid to use for the abuild user.
222               This is useful if you are hacking in the buildroot.
223               This must be set to the same value if the buildroot is re-used.
224
225   --vm-type TYPE
226               Use virtual machine instead of chroot
227               TYPE is one of xen|kvm|uml|qemu|lxc
228
229   --vm-disk FILE
230               Use FILE as disk for virtual machine.
231               Defaults to \$BUILD_ROOT.img if unset
232
233   --vm-swap FILE
234               Use FILE as swap space for virtual machine. The swap space is
235               also used for retrieving packages from the VM so it's size must be
236               sufficiently large
237
238   --vm-disk-size SIZEINMB
239   --vm-swap-size SIZEINMB
240   --vm-disk-filesystem TYPE
241               Defaults for automatic setup of VM root/swap files
242
243   --vm-kernel FILE
244   --vm-initrd FILE
245               Kernel and initrd to use for VM (kvm and qemu only)
246
247   --debug
248               enable creation of a debuginfo package
249
250 Remember to have fun!
251
252 [*] Maximum RPM: http://www.rpm.org/max-rpm/
253 EOT
254 }
255 usage () {
256     echo "Usage: `basename $0` [--no-init|--clean|--rpms path|--verify|--help] [dir-to-build|spec-to-build]"
257     cleanup_and_exit 1
258 }
259
260 #
261 #  cleanup_and_exit
262 #  return values: 0 -> success, new packages built
263 #                 1 -> error, build failed
264 #                 2 -> successfull build, but no changes to former built packages
265 #                 3 -> something wrong with build host
266 #
267 cleanup_and_exit () {
268     trap EXIT
269     test -z "$1" && set 0
270     if test -n "$RUNNING_IN_VM" ; then
271         chown $browner $BUILD_ROOT
272         cd /
273         if test -n "$VM_SWAP" -a -e "$VM_SWAP" ; then
274             swapoff "$VM_SWAP" 2>/dev/null
275             echo -n "BUILDSTATUS$1" >"$VM_SWAP"
276         fi
277         exec >&0 2>&0        # so that the logging tee finishes
278         sleep 1                # wait till tee terminates
279         if test "$VM_TYPE" != lxc; then
280             kill -9 -1        # goodbye cruel world
281             if ! test -x /sbin/halt ; then
282                 test -e /proc/sysrq-trigger || mount -n -tproc none /proc
283                 sync
284                 sleep 2 # like halt does
285                 if test -e /proc/sysrq-trigger; then
286                     echo o > /proc/sysrq-trigger
287                     sleep 5 # wait for sysrq to take effect
288                 else
289                     echo "Warning: VM doesn't support sysrq and /sbin/halt not installed"
290                 fi
291             else
292                 halt -f -p
293             fi
294             echo "Warning: clean shut down of the VM didn't work"
295         fi
296     else
297         umount -n $BUILD_ROOT/proc/sys/fs/binfmt_misc 2> /dev/null || true
298         umount -n $BUILD_ROOT/proc 2>/dev/null || true
299         umount -n $BUILD_ROOT/dev/pts 2>/dev/null || true
300         test "$VM_IMAGE" = 1 && VM_IMAGE=
301         [ -n "$VM_IMAGE" ] && umount $BUILD_ROOT 2>/dev/null || true
302     fi
303 #    echo "pid $$ exit $1"
304     exit $1
305 }
306
307 fail_exit()
308 {
309   cleanup_and_exit 1
310 }
311
312 shellquote()
313 {
314     for arg; do
315         arg=${arg/\\/\\\\}
316         arg=${arg/\$/\\\$}
317         arg=${arg/\"/\\\"}
318         arg=${arg/\`/\\\`}
319         echo -n " \"$arg\""
320     done
321 }
322
323 # create a shell script from command line. Used for preserving arguments
324 # through /bin/su -c
325 toshellscript()
326 {
327         echo "#!/bin/sh -x"
328         echo -n exec
329         shellquote "$@"
330         echo
331 }
332
333 setupccache()
334 {
335     if [ "$ccache" = 1 ]; then
336         if mkdir -p $BUILD_ROOT/var/lib/build/ccache/bin; then
337             for i in gcc g++ cc c++; do
338 #                ln -sf /usr/bin/ccache $BUILD_ROOT/var/lib/build/ccache/bin/$i
339                 rm -f $BUILD_ROOT/var/lib/build/ccache/bin/$i
340                 test -e $BUILD_ROOT/usr/bin/$i || continue
341                 echo '#! /bin/sh' > $BUILD_ROOT/var/lib/build/ccache/bin/$i
342                 echo "test -e /usr/bin/$i || exit 1" >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
343                 echo 'export PATH=/opt/icecream/bin:/usr/bin:$PATH' >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
344                 echo "ccache $i \"\$@\"" >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
345                 chmod 755 $BUILD_ROOT/var/lib/build/ccache/bin/$i
346                 echo "Installed ccache wrapper as $BUILD_ROOT/var/lib/build/ccache/bin/$i"
347             done
348         fi
349         mkdir -p "$BUILD_ROOT"/.ccache
350         chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT/.ccache"
351         echo "export CCACHE_DIR=/.ccache" > "$BUILD_ROOT"/etc/profile.d/build_ccache.sh
352         echo 'export PATH=/var/lib/build/ccache/bin:$PATH' >> "$BUILD_ROOT"/etc/profile.d/build_ccache.sh
353     else
354         rm -f "$BUILD_ROOT$builduserhome"/bin/{gcc,g++,cc,c++}
355         rm -f "$BUILD_ROOT"/var/lib/build/ccache/bin/{gcc,g++,cc,c++}
356     fi
357 }
358
359 setupicecream()
360 {
361     if [ "$icecream" -eq 0 ]; then
362         rm -rf "$BUILD_ROOT"/var/run/icecream
363         rm -f "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
364         return
365     fi
366
367     if ! chroot "$BUILD_ROOT" rpm -q icecream >/dev/null 2>/dev/null; then
368         echo "*** icecream package not installed ***"
369         false
370         return
371     fi
372
373     echo "using icecream with $icecream jobs"
374
375     if [ "$ccache" -ne 1 ]; then
376         echo 'export PATH=/opt/icecream/bin:$PATH' > "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
377     else
378         echo 'export CCACHE_PATH=/opt/icecream/bin' > "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
379     fi
380
381     local icecc_vers=(`shopt -s nullglob; echo $BUILD_ROOT/var/run/icecream/*.tar.{bz2,gz}`)
382     icecc_vers=${icecc_vers//$BUILD_ROOT/}
383
384     # XXX use changelog like autobuild does instead?
385     # only run create-env if compiler or glibc changed
386     if [ -z "$icecc_vers" \
387         -o ! -e "$BUILD_ROOT/$icecc_vers" \
388         -o "$BUILD_ROOT/usr/bin/gcc" -nt "$BUILD_ROOT/$icecc_vers" \
389         -o "$BUILD_ROOT/usr/bin/g++" -nt "$BUILD_ROOT/$icecc_vers" \
390         -o "$BUILD_ROOT/usr/bin/as" -nt "$BUILD_ROOT/$icecc_vers" \
391         -o "$BUILD_ROOT/lib/libc.so.6" -nt "$BUILD_ROOT/$icecc_vers" ]
392     then
393         rm -rf $BUILD_ROOT/var/run/icecream
394         mkdir -p $BUILD_ROOT/var/run/icecream
395         if [ -e "$BUILD_ROOT"/usr/bin/create-env ]; then
396           createenv=/usr/bin/create-env
397         elif [ -e "$BUILD_ROOT"/usr/lib/icecc/icecc-create-env ]; then
398           createenv="/usr/lib/icecc/icecc-create-env /usr/bin/gcc /usr/bin/g++" # XXX
399         elif [ -e "$BUILD_ROOT"/usr/lib64/icecc/icecc-create-env ]; then
400           createenv="/usr/lib64/icecc/icecc-create-env /usr/bin/gcc /usr/bin/g++" # XXX
401         else
402           echo "create-env not found"
403           false
404           return
405         fi
406         chroot $BUILD_ROOT bash -c "cd /var/run/icecream; $createenv" || cleanup_and_exit 1
407         icecc_vers=(`shopt -s nullglob; echo $BUILD_ROOT/var/run/icecream/*.tar.{bz2,gz}`)
408         icecc_vers=${icecc_vers//$BUILD_ROOT/}
409     else
410         echo "reusing existing icecream environment $icecc_vers"
411     fi
412     if [ -n "$icecc_vers" ]; then
413       echo "export ICECC_VERSION=$icecc_vers" >> "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
414     fi
415 }
416
417 setmemorylimit()
418 {
419     if [ -n "$VM_IMAGE" -o -n "$RUNNING_IN_VM" ]; then
420         return
421     fi
422     local mem
423     while read mem; do
424         case "$mem" in
425             MemTotal:*)
426                 set -- $mem
427                 eval "mem=\$(($2/3*4))"
428                 ulimit -v $mem
429                 echo "Memory limit set to ${mem}KB"
430                 break;
431             ;;
432         esac
433     done < <(cat /proc/meminfo) # cat for proc stuff
434 }
435
436 create_baselibs()
437 {
438     local pkgs=()
439
440     BASELIBS_CFG=
441
442     if test "$BUILDTYPE" == "dsc" ; then
443         pkgs=($DEBS)
444     else # spec and kiwi
445         if test -e $BUILD_ROOT$TOPDIR/SOURCES/baselibs.conf ; then
446             BASELIBS_CFG="-c $TOPDIR/SOURCES/baselibs.conf"
447         fi
448         if test -e $BUILD_ROOT/usr/lib/build/baselibs_global.conf; then
449             BASELIBS_GLOBAL="-c /usr/lib/build/baselibs_global.conf"
450         fi
451         pkgs=($RPMS)
452     fi
453
454     mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
455     # don't use -R as extracted sources, build root etc might be below $TOPDIR
456     chown "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"/* "$BUILD_ROOT$TOPDIR"/RPMS/* || true
457
458     local mkbaselibs="/usr/lib/build/mkbaselibs"
459     local whichone=''
460     # $BUILD_DIR is set to /.build when using a vm. So we need to
461     # hardcode /usr/lib/build instead of $BUILD_DIR to prefer
462     # mkbaselibs from the distro.
463     if test -f $BUILD_ROOT$mkbaselibs; then
464         if test -z "$BASELIBS_CFG" -a -e $BUILD_ROOT/usr/lib/build/baselibs.conf ; then
465             BASELIBS_CFG="-c /usr/lib/build/baselibs.conf"
466         fi
467     else
468         if test "$CREATE_BASELIBS" = 'internal'; then
469             echo "Warning: mkbaselibs missing in build root, skipping baselibs"
470             return
471         fi
472         # use external version
473         whichone=" (external)"
474         mkbaselibs="/.mkbaselibs/mkbaselibs"
475         rm -rf $BUILD_ROOT/.mkbaselibs
476         mkdir -p $BUILD_ROOT/.mkbaselibs
477         cp -f $BUILD_DIR/mkbaselibs $BUILD_ROOT/.mkbaselibs/
478         if test "$BUILDTYPE" == "dsc" ; then
479             cp -f $BUILD_DIR/baselibs_global-deb.conf $BUILD_ROOT/.mkbaselibs/baselibs_g.conf
480             cp -f $BUILD_ROOT$TOPDIR/SOURCES/baselibs-deb.conf $BUILD_ROOT/.mkbaselibs/baselibs-deb.conf
481             BASELIBS_CFG="-c /.mkbaselibs/baselibs-deb.conf"
482         else
483             cp -f $BUILD_DIR/baselibs_global.conf $BUILD_ROOT/.mkbaselibs/baselibs_g.conf
484             if test -z "$BASELIBS_CFG" -a -e $BUILD_DIR/baselibs.conf; then
485                 cp -f $BUILD_DIR/baselibs.conf $BUILD_ROOT/.mkbaselibs/baselibs.conf
486                 BASELIBS_CFG="-c /.mkbaselibs/baselibs.conf"
487             fi
488         fi
489         if test -e $BUILD_ROOT/.mkbaselibs/baselibs_g.conf; then
490             BASELIBS_GLOBAL="-c /.mkbaselibs/baselibs_g.conf"
491         fi
492     fi
493     echo "... creating baselibs$whichone"
494     chroot $BUILD_ROOT su -c "$mkbaselibs $BASELIBS_GLOBAL $BASELIBS_CFG ${pkgs[*]#$BUILD_ROOT}" - $BUILD_USER || cleanup_and_exit 1
495     rm -rf $BUILD_ROOT/.mkbaselibs
496 }
497
498 copy_oldpackages()
499 {
500     local i=0
501     local d
502     local dest
503     for d in "${old_packages[@]}"; do
504         dest="$BUILD_ROOT/.build.oldpackages"
505         test "$i" = 0 || dest="$dest$i"
506         if [ -d "$d" -a "$d" != "$dest" ] ; then
507             rm -rf "$dest"
508             mkdir -p "$dest"
509             cp -L $d/* "$dest"
510             : $((++i))
511         fi
512     done
513 }
514
515 vm_img_mkfs()
516 {
517     local fs="$1"
518     local img="$2"
519     local mkfs tunefs
520     eval "mkfs=\"\$vm_img_mkfs_${fs}\""
521     eval "tunefs=\"\$vm_img_tunefs_${fs}\""
522
523     if test -z "$mkfs"; then
524         echo "filesystem \"$fs\" isn't supported"
525         cleanup_and_exit 3
526     fi
527
528
529     echo "Creating $fs filesystem on $img"
530     $mkfs "$img"
531     if test -n "$tunefs" ; then
532         $tunefs "$img" || cleanup_and_exit 3
533     fi
534
535 }
536
537 detect_vm_2nd_stage()
538 {
539     if ! test "$0" = "/.build/build" ; then
540         return 1
541     fi
542     if test $$ -eq 1 ; then
543         # ignore special init signals if we're init
544         # we're using ' ' instead of '' so that the signal handlers
545         # are reset in the child processes
546         trap ' ' HUP TERM
547         $0 "$@"
548         cleanup_and_exit $?
549     fi
550     echo "2nd stage started in virtual machine"
551     BUILD_ROOT=/
552     BUILD_DIR=/.build
553     . $BUILD_DIR/build.data
554     echo "machine type: `uname -m`"
555     if test "$PERSONALITY" != 0 -a -z "$PERSONALITY_SET" ; then
556         export PERSONALITY_SET=true
557         echo "switching personality to $PERSONALITY..."
558         # this is 32bit perl/glibc, thus the 32bit syscall number
559         exec perl -e 'syscall(136, '$PERSONALITY') == -1 && warn("personality: $!\n");exec "/.build/build" || die("/.build/build: $!\n")'
560     fi
561     RUNNING_IN_VM=true
562     mount -orw -n -tproc none /proc
563     if test "$VM_TYPE" != 'lxc'; then
564         mount -n -o remount,noatime,rw /
565     fi
566 # qemu inside of xen does not work, check again with kvm later before enabling this
567 #    if [ -e /dev/kqemu ]; then
568 #        # allow abuild user to run qemu
569 #        chmod 0666 /dev/kqemu
570 #    fi
571     if test -n "$VM_SWAP" ; then
572         for i in 1 2 3 4 5 6 7 8 9 10 ; do
573             test -e "$VM_SWAP" && break
574             test $i = 1 && echo "waiting for $VM_SWAP to appear"
575             echo -n .
576             sleep 1
577         done
578         test $i = 1 || echo
579         # recreate the swap device manually if it didn't exist for some
580         # reason, hardcoded to hda2 atm
581         if ! test -b "$VM_SWAP" ; then
582             rm -f "$VM_SWAP"
583             umask 027
584             mknod "$VM_SWAP" b 3 2
585             umask 022
586         fi
587         swapon -v "$VM_SWAP" || exit 1
588     fi
589     HOST="$MYHOSTNAME"
590
591     return 0
592 }
593
594 find_spec_files()
595 {
596     local spec files
597     if [ -z "$SPECFILES" ]; then
598         set -- "`pwd`"
599     else
600         set -- "${SPECFILES[@]}"
601     fi
602     SPECFILES=()
603     for spec in "$@"; do
604         if [ "$spec" = "${spec#/}" ]; then
605             spec="`pwd`/$spec"
606         fi
607
608         if [ -d "$spec" ]; then
609             specs=("$spec"/*.spec)
610             if [ -n "$specs" ]; then
611                 SPECFILES=("${SPECFILES[@]}" "${specs[@]}")
612             else
613                 specs=("$spec"/*.spec)
614                 if [ -n "$specs" ]; then
615                     SPECFILES=("${SPECFILES[@]}" "${specs[@]}")
616                 fi
617             fi
618         else
619             SPECFILES[${#SPECFILES[@]}]="$spec";
620         fi
621     done
622
623     if test -z "$SPECFILES"; then
624         echo no spec files or src rpms found in $@. exit...
625         cleanup_and_exit 1
626     fi
627 }
628
629 become_root_or_fail()
630 {
631     if [ ! -w /root ]; then
632         echo "You have to be root to use $0" >&2
633         exit 1
634     fi
635     cleanup_and_exit 1
636 }
637
638 mkdir_build_root()
639 {
640     if [ -d "$BUILD_ROOT" ]; then
641         # check if it is owned by root
642         if [ -z "$RUNNING_IN_VM" -a \! -O "$BUILD_ROOT" -a "`stat -c %u $BUILD_ROOT`" -ne 0 ]; then
643             echo "BUILD_ROOT=$BUILD_ROOT must be owned by root. Exit..."
644             cleanup_and_exit 1
645         fi
646     else
647         test "$BUILD_ROOT" != "${BUILD_ROOT%/*}" && mkdir -p "${BUILD_ROOT%/*}"
648         if ! mkdir $BUILD_ROOT; then
649             echo "can not create BUILD_ROOT=$BUILD_ROOT. Exit..."
650             cleanup_and_exit 1
651         fi
652     fi
653
654     if [ ! -w "$BUILD_ROOT" ]; then
655         echo "Error: BUILD_ROOT=$BUILD_ROOT not writeable, try --clean."
656         cleanup_and_exit 3
657     fi
658
659     rm -rf "$BUILD_ROOT"/.build.packages
660     if [ -z "$VM_TYPE" -a -z "$RUNNING_IN_VM" ]; then
661        # don't touch this in VM
662        rm -rf "$BUILD_ROOT"/.build
663        mkdir -p "$BUILD_ROOT"/.build
664     fi
665 }
666
667 linux64()
668 {
669         perl -e 'syscall('$PERSONALITY_SYSCALL', 0); exec(@ARGV) || die("$ARGV[0]: $!\n")' "$@"
670 }
671
672 #### main ####
673
674 trap fail_exit EXIT
675
676 case `perl -V:archname` in
677     *x86_64*) PERSONALITY_SYSCALL=135 ;;
678     *i?86*)   PERSONALITY_SYSCALL=136 ;;
679 esac
680
681 shopt -s nullglob
682
683 if detect_vm_2nd_stage ; then
684     set "/.build-srcdir/$SPECFILE"
685
686 fi
687
688 export PATH=$BUILD_DIR:/sbin:/usr/sbin:$PATH
689
690 . $BUILD_DIR/common_functions || exit 1
691
692 export HOST
693
694 needarg()
695 {
696   if [ -z "$ARG" ]; then
697     echo "$PARAM needs an agrument" >&2
698     cleanup_and_exit 1
699   fi
700 }
701
702 while test -n "$1"; do
703   PARAM="$1"
704   ARG="$2"
705   [ "$ARG" = "${ARG#-}" ] || ARG=""
706   shift
707   case $PARAM in
708     *-*=*)
709       ARG=${PARAM#*=}
710       PARAM=${PARAM%%=*}
711       set -- "----noarg=$PARAM" "$@"
712   esac
713   case $PARAM in
714       *-help|-h)
715         echo_help
716         cleanup_and_exit
717       ;;
718       *-no*init)
719         DO_INIT=false
720       ;;
721       *-no*checks)
722         DO_CHECKS=false
723       ;;
724       *-clean)
725         CLEAN_BUILD='--clean'
726       ;;
727       *-kill)
728         KILL=true
729       ;;
730       *-rpms)
731         needarg
732         BUILD_RPMS="$ARG"
733         shift
734       ;;
735       *-arch)
736         needarg
737         BUILD_ARCH="$ARG"
738         shift
739       ;;
740       *-verify)
741         export VERIFY_BUILD_SYSTEM=true
742       ;;
743       *-target)
744         needarg
745         ABUILD_TARGET="$ARG"
746         shift
747       ;;
748       *-jobs)
749         needarg
750         BUILD_JOBS="$ARG"
751         shift
752       ;;
753       *-extra*packs|-X)
754         needarg
755         BUILD_EXTRA_PACKS="$BUILD_EXTRA_PACKS $ARG"
756         shift
757       ;;
758       *-lint)
759         DO_LINT=true
760         ;;
761       *-baselibs)
762         CREATE_BASELIBS=true
763         ;;
764       *-baselibs-internal)
765         CREATE_BASELIBS=internal
766         ;;
767       *-root)
768         needarg
769         BUILD_ROOT="$ARG"
770         shift
771       ;;
772       *-cachedir)
773         needarg
774         CACHE_DIR="$ARG"
775         shift
776       ;;
777       *-oldpackages)
778         needarg
779         old_packages=("${old_packages[@]}" "$ARG")
780         shift
781       ;;
782       *-dist)
783         needarg
784         BUILD_DIST="$ARG"
785         shift
786       ;;
787       *-xen|*-kvm|--uml|--qemu)
788         VM_TYPE=${PARAM##*-}
789         if [ -n "$ARG" ]; then
790             VM_IMAGE="$ARG"
791             shift
792         else
793             VM_IMAGE=1
794         fi
795       ;;
796       --lxc)
797         VM_TYPE=${PARAM##*-}
798       ;;
799       --vm-type)
800         needarg
801         VM_TYPE="$ARG"
802         case "$VM_TYPE" in
803             xen|kvm|uml|qemu|lxc) ;;
804             none|chroot) VM_TYPE='' ;;
805             *)
806                 echo "VM $VM_TYPE not supported"
807                 cleanup_and_exit
808             ;;
809         esac
810         shift
811       ;;
812       --vm-disk)
813         needarg
814         VM_IMAGE="$ARG"
815         shift
816       ;;
817       *-xenswap|*-swap)
818         needarg
819         VM_SWAP="$ARG"
820         shift
821       ;;
822       *-xenmemory|*-memory)
823         needarg
824         MEMSIZE="$ARG"
825         shift
826       ;;
827       *-vm-kernel)
828         needarg
829         VM_KERNEL="$ARG"
830         shift
831       ;;
832       *-vm-initrd)
833         needarg
834         VM_INITRD="$ARG"
835         shift
836       ;;
837       *-vmdisk-rootsize|--vm-disk-size)
838         needarg
839         VMDISK_ROOTSIZE="$ARG"
840         shift
841       ;;
842       *-vmdisk-swapsize|--vm-swap-size)
843         needarg
844         VMDISK_SWAPSIZE="$ARG"
845         shift
846       ;;
847       *-vmdisk-filesystem|--vm-disk-filesystem)
848         needarg
849         VMDISK_FILESYSTEM="$ARG"
850         shift
851       ;;
852       *-rpmlist)
853         needarg
854         RPMLIST="--rpmlist $ARG"
855         BUILD_RPMS=
856         shift
857       ;;
858       *-release)
859         needarg
860         RELEASE="$ARG"
861         shift
862       ;;
863       *-logfile)
864         needarg
865         LOGFILE="$ARG"
866         shift
867       ;;
868       *-reason)
869         needarg
870         REASON="$ARG"
871         shift
872       ;;
873       *-norootforbuild)
874         NOROOTFORBUILD=true
875       ;;
876       *-stage)
877         needarg
878         BUILD_RPM_BUILD_STAGE="$ARG"
879         shift
880       ;;
881       *-useusedforbuild)
882         USEUSEDFORBUILD="--useusedforbuild"
883       ;;
884       *-list*state)
885         LIST_STATE=true
886       ;;
887       --define|--with|--without)
888         needarg
889         definesnstuff[${#definesnstuff[@]}]="$PARAM";
890         definesnstuff[${#definesnstuff[@]}]="$ARG";
891         shift
892       ;;
893       --repository|--repo)
894         needarg
895         repos[${#repos[@]}]="$PARAM";
896         repos[${#repos[@]}]="$ARG";
897         shift
898       ;;
899       --icecream)
900         needarg
901         icecream="$ARG"
902         if [ "$icecream" -gt 0 ]; then
903                 BUILD_JOBS="$ARG"
904         fi
905         shift
906       ;;
907       --ccache)
908         ccache=1
909       ;;
910       --debug)
911         BUILD_DEBUG=1
912       ;;
913       --incarnation)
914         needarg
915         INCARNATION=$ARG
916         shift
917       ;;
918       --disturl)
919         needarg
920         DISTURL=$ARG
921         shift
922       ;;
923       --linksources)
924         LINKSOURCES=true
925       ;;
926       ----noarg)
927         echo "$ARG does not take an argument"
928         cleanup_and_exit
929       ;;
930       *-changelog)
931         CHANGELOG=true
932       ;;
933       --overlay)
934         needarg
935         OVERLAY=$ARG
936         shift
937       ;;
938       --rsync-src)
939         needarg
940         RSYNCSRC=$ARG
941         shift
942       ;;
943       --rsync-dest)
944         needarg
945         RSYNCDEST=$ARG
946         shift
947       ;;
948       --uid)
949         needarg
950         ABUILD_ID="$ARG"
951         if test -n "${ABUILD_ID//[0-9:]/}"; then
952             echo "--uid argument must be uid:gid"
953             cleanup_and_exit
954         fi
955         ABUILD_UID=${ABUILD_ID%:*}
956         ABUILD_GID=${ABUILD_ID#*:}
957         shift
958       ;;
959       --shell)
960           shell=1
961           shift
962       ;;
963       --signdummy)
964         SIGNDUMMY=1
965       ;;
966       -*)
967         echo Unknown Option "$PARAM". Exit.
968         cleanup_and_exit 1
969       ;;
970       *)
971         SPECFILES[${#SPECFILES[@]}]="$PARAM";
972       ;;
973     esac
974 done
975
976 if test "$VM_TYPE" = "lxc"; then
977     VM_IMAGE=''
978     VM_SWAP=''
979 fi
980
981 if test -n "$KILL" ; then
982     test -z "$SRCDIR" || usage
983     if test -n "$VM_IMAGE" -a -n "$VM_SWAP" -a -n "$VM_TYPE"; then
984         # mark job as failed so that we don't extract packages
985         echo -n "BUILDSTATUS1" >"$VM_SWAP"
986     fi
987     (set -C; > "$BUILD_ROOT/exit" 2>/dev/null || true)
988     if test "$VM_TYPE" = 'lxc'; then
989         LXCID=${BUILD_ROOT##*/}
990         lxc-stop -n "$LXCID" || true
991         lxc-destroy -n "$LXCID"
992     elif test -z "$VM_IMAGE" ; then
993         if ! $BUILD_DIR/killchroot -s 9 $BUILD_ROOT ; then
994             echo "could not kill build in $BUILD_ROOT"
995             cleanup_and_exit 1
996         fi
997     elif test "$VM_TYPE" = 'xen'; then
998         XENID="${VM_IMAGE%/root}"
999         XENID="${XENID%/tmpfs}"
1000         XENID="${XENID##*/}"
1001         if xm list "build:$XENID" >/dev/null 2>&1 ; then
1002             if ! xm destroy "build:$XENID" ; then
1003                 echo "could not kill xen build $XENID"
1004                 cleanup_and_exit 1
1005             fi
1006         fi
1007     elif test -n "$VM_TYPE"; then
1008         if ! fuser -k -TERM "$VM_IMAGE"; then
1009             echo "could not kill build in $VM_IMAGE"
1010             cleanup_and_exit 1
1011         fi
1012     else
1013         echo "don't know how to kill this build job"
1014         cleanup_and_exit 1
1015     fi
1016     cleanup_and_exit 0
1017 fi
1018
1019 if [ "$VM_TYPE" = 'kvm' -a -z "$RUNNING_IN_VM" ]; then
1020     if [ ! -r /dev/kvm -o ! -x "$qemu_bin"-kvm ]; then
1021         echo "host doesn't support kvm"
1022         cleanup_and_exit 3
1023     fi
1024     qemu_bin="$kvm_bin"
1025     if [ -n "$VM_KERNEL" ]; then
1026         qemu_kernel="$VM_KERNEL"
1027     fi
1028
1029     # check if a SUSE system with virtio initrd is running
1030     if [ -z "$VM_INITRD" -a -e /etc/sysconfig/kernel ]; then
1031        a=$( source /etc/sysconfig/kernel; echo $INITRD_MODULES )
1032        have_virtio_pci=""
1033        have_virtio_blk=""
1034        for i in $a; do
1035           [ "$i" == "virtio_pci" ] && have_virtio_pci="1"
1036           [ "$i" == "virtio_blk" ] && have_virtio_blk="1"
1037        done
1038        [ -n "$have_virtio_pci" -a -n "$have_virtio_blk" ] && VM_INITRD="/boot/initrd"
1039     fi
1040
1041     if [ -n "$VM_INITRD" ]; then
1042         qemu_initrd="$VM_INITRD"
1043         kvm_virtio=1
1044     elif [ -e "${qemu_initrd}-build" ]; then
1045         qemu_initrd="${qemu_initrd}-build"
1046         kvm_virtio=1
1047     else
1048         if [ -L "$qemu_initrd" ]; then
1049             qemu_initrd=`readlink -f "$qemu_initrd"` || cleanup_and_exit 3
1050         fi
1051         qemu_initrd_virtio="${qemu_initrd}-virtio"
1052
1053         if [ ! -e ${qemu_initrd_virtio} -o $qemu_kernel -nt ${qemu_initrd_virtio} ]; then
1054             mkinitrd_virtio_cmd=(env rootfstype="$VMDISK_FILESYSTEM" \
1055                     mkinitrd -d /dev/null \
1056                     -m "binfmt_misc virtio_pci virtio_blk" \
1057                     -k $qemu_kernel \
1058                     -i ${qemu_initrd_virtio})
1059             if [ ! -w /root -o -n "$RPMLIST" ]; then
1060                 echo "No initrd that provides virtio support found. virtio accelleration disabled."
1061                 echo "Run the following command as root to enable virtio:"
1062                 shellquote "${mkinitrd_virtio_cmd[@]}"
1063                 echo
1064             elif /sbin/modinfo virtio_pci >/dev/null 2>&1; then
1065                 echo "creating $qemu_initrd_virtio"
1066                 "${mkinitrd_virtio_cmd[@]}" || cleanup_and_exit 1
1067                 kvm_virtio=1
1068                 qemu_initrd="${qemu_initrd_virtio}"
1069             fi
1070         else
1071             kvm_virtio=1
1072             qemu_initrd="${qemu_initrd_virtio}"
1073         fi
1074     fi
1075
1076     if [ "$kvm_virtio" = 1 ]; then
1077         VM_SWAPDEV=/dev/vdb
1078         qemu_rootdev=/dev/vda
1079     else
1080         VM_SWAPDEV=/dev/sdb
1081         qemu_rootdev=/dev/sda
1082     fi
1083 fi
1084
1085 if [ "$VM_TYPE" = 'qemu' ]; then
1086     VM_SWAPDEV=/dev/sdb
1087     qemu_rootdev=/dev/sda
1088 fi
1089
1090 if [ -z "$RPMLIST" -a -z "$RUNNING_IN_VM" ]; then
1091     if [ -z "$repos" -a -z "$BUILD_RPMS" ]; then
1092         repos=(--repository 'zypp://')
1093     fi
1094 else
1095     repos=()
1096 fi
1097
1098 set_build_arch
1099
1100 if [ -n "$CLEAN_BUILD" ]; then
1101     DO_INIT=true
1102 fi
1103
1104 find_spec_files
1105
1106 if test -n "$LIST_STATE" ; then
1107     BUILD_ROOT=`mktemp -d /var/tmp/build-list-state-XXXXXX`
1108     test -d "$BUILD_ROOT" || cleanup_and_exit 3
1109     SPECFILE=$SPECFILES # only one specified anyways
1110     if test "$SPECFILE" != "${SPECFILE%.src.rpm}" ; then
1111        rm -rf $BUILD_ROOT/usr/src/packages
1112        mkdir -p $BUILD_ROOT/usr/src/packages/SOURCES $BUILD_ROOT/usr/src/packages/SPECS
1113        rpm -i --nodigest --nosignature --root $BUILD_ROOT $SPECFILE || {
1114            echo "could not install $SPECFILE." 2>&1
1115            rm -rf $BUILD_ROOT
1116            cleanup_and_exit 3
1117        }
1118        for SPECFILE in $BUILD_ROOT/usr/src/packages/SPECS/*.spec ; do : ; done
1119     fi
1120     init_buildsystem --cachedir "$CACHE_DIR" --list-state "${definesnstuff[@]}" "${repos[@]}" $USEUSEDFORBUILD $SPECFILE $BUILD_EXTRA_PACKS
1121     ERR=$?
1122     rm -rf $BUILD_ROOT
1123     cleanup_and_exit $ERR
1124 fi
1125
1126 if test -z "$RUNNING_IN_VM" ; then
1127     if test -n "$VM_IMAGE" ; then
1128         if test "$VM_IMAGE" = 1 ; then
1129             VM_IMAGE="$BUILD_ROOT.img"
1130             echo "using $VM_IMAGE as vm image"
1131             if test -z "$VM_SWAP"; then
1132                 VM_SWAP="$BUILD_ROOT.swap"
1133                 echo "using $VM_SWAP as vm swap"
1134             fi
1135         fi
1136         if [ "$VM_TYPE" = 'xen' ]; then
1137             # this should not be needed, but sometimes a xen instance got lost
1138             XENID="${VM_IMAGE%/root}"
1139             XENID="${XENID%/tmpfs}"
1140             XENID="${XENID##*/}"
1141             xm destroy "build:$XENID" >/dev/null 2>&1
1142         fi
1143         if test ! -e "$VM_IMAGE"; then
1144             echo "Creating $VM_IMAGE (${VMDISK_ROOTSIZE}M)"
1145             mkdir -p "${VM_IMAGE%/*}"
1146             dd if=/dev/zero of="$VM_IMAGE" bs=1 count=1 seek=$(( ${VMDISK_ROOTSIZE} * 1024 * 1024 - 1 )) || cleanup_and_exit 3
1147             if test -z "$CLEAN_BUILD" ; then
1148                 vm_img_mkfs "$VMDISK_FILESYSTEM" "$VM_IMAGE" || cleanup_and_exit 3
1149             fi
1150         fi
1151         if test ! -e "$VM_SWAP"; then
1152             # setup VM_SWAP
1153             echo "Creating $VM_SWAP (${VMDISK_SWAPSIZE}M)"
1154             mkdir -p "${VM_SWAP%/*}"
1155             dd if=/dev/zero of="$VM_SWAP" bs=1 count=1 seek=$(( ${VMDISK_SWAPSIZE} * 1024 * 1024 - 1 )) || cleanup_and_exit 3
1156         fi
1157         if test ! -e "$VM_IMAGE" ; then
1158             echo "you need to create $VM_IMAGE first"
1159             cleanup_and_exit 3
1160         fi
1161         if test -n "$CLEAN_BUILD" ; then
1162             vm_img_mkfs "$VMDISK_FILESYSTEM" "$VM_IMAGE" || cleanup_and_exit 3
1163         fi
1164         mkdir_build_root
1165         if [ -w /root ]; then
1166             mount -o loop $VM_IMAGE $BUILD_ROOT || cleanup_and_exit 3
1167         else
1168             if ! mount $BUILD_ROOT; then
1169                 echo "mounting the build root failed. An fstab entry is probably missing or incorrect."
1170                 echo "/etc/fstab should contain an entry like this:"
1171                 echo "$VM_IMAGE $BUILD_ROOT auto noauto,user,loop 0 0"
1172                 cleanup_and_exit 3
1173             fi
1174         fi
1175     else
1176         test -w /root || become_root_or_fail
1177     fi
1178     if test -n "$VM_SWAP" ; then
1179         dd if=/dev/zero of="$VM_SWAP" bs=12 count=1 conv=notrunc 2>/dev/null
1180         echo "mkswap $VM_SWAP"
1181         mkswap "$VM_SWAP"
1182     fi
1183 fi
1184
1185 mkdir_build_root
1186
1187 if [ "$BUILD_ROOT" = / ]; then
1188     read dummy dummy browner dummy < <(ls -ld /)
1189 fi
1190
1191 rm -f $BUILD_ROOT/exit
1192
1193 if [ -w /root ]; then
1194     mkdir -p $BUILD_ROOT/proc
1195     mkdir -p $BUILD_ROOT/dev/pts
1196     mount -n -tproc none $BUILD_ROOT/proc || true
1197     mount -n -tdevpts none $BUILD_ROOT/dev/pts
1198 fi
1199
1200 if test -z "$VM_IMAGE" -a -z "$LOGFILE"; then
1201     LOGFILE="$BUILD_ROOT/.build.log"
1202 fi
1203
1204 if test -n "$LOGFILE" -a -z "$shell" ; then
1205     echo  logging output to $LOGFILE...
1206     rm -f $LOGFILE
1207     touch $LOGFILE
1208     if test -n "$VM_IMAGE" ; then
1209         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
1210     else
1211         exec 1> >(exec -a 'build logging tee' tee -a $LOGFILE) 2>&1
1212     fi
1213 fi
1214
1215 setmemorylimit
1216
1217 #
1218 # say hello
1219 #
1220 test -z "$HOST" && HOST=`hostname`
1221
1222 if [ -z "$RUNNING_IN_VM" ]; then
1223     echo Using BUILD_ROOT=$BUILD_ROOT
1224     test -n "$BUILD_RPMS" && echo Using BUILD_RPMS=$BUILD_RPMS
1225     echo Using BUILD_ARCH=$BUILD_ARCH
1226     test -n "$VM_TYPE" && echo "Doing $VM_TYPE build${VM_IMAGE:+ in $VM_IMAGE}"
1227     echo
1228 fi
1229
1230 test "$BUILD_ARCH" = all && BUILD_ARCH=
1231 BUILD_USER_ABUILD_USED=
1232
1233 for SPECFILE in "${SPECFILES[@]}" ; do
1234
1235     SRCDIR="${SPECFILE%/*}"
1236     SPECFILE="${SPECFILE##*/}"
1237
1238     BUILDTYPE=
1239     case $SPECFILE in
1240       *.spec|*.src.rpm) BUILDTYPE=spec ;;
1241       *.dsc) BUILDTYPE=dsc ;;
1242       *.kiwi) BUILDTYPE=kiwi ;;
1243     esac
1244     if test -z "$BUILDTYPE" ; then
1245        echo "don't know how to build $SPECFILE"
1246        cleanup_and_exit 1
1247     fi
1248
1249     cd "$SRCDIR"
1250
1251     if [ -z "$RUNNING_IN_VM" ]; then
1252         echo
1253         echo "$HOST started \"build $SPECFILE\" at `date --utc`."
1254         echo
1255         test -n "$REASON" && echo "$REASON"
1256         echo
1257     fi
1258
1259     #
1260     # first setup building directory...
1261     #
1262     test -s "$SPECFILE" || {
1263        echo "$SPECFILE" is empty.  This should not happen...
1264        cleanup_and_exit 1
1265     }
1266
1267     if test "$SPECFILE" != "${SPECFILE%.src.rpm}" ; then
1268         echo processing src rpm $SRCDIR/$SPECFILE ...
1269         MYSRCDIR=$BUILD_ROOT/.build-srcdir
1270         rm -rf $MYSRCDIR
1271         mkdir -p $MYSRCDIR
1272         cd $MYSRCDIR || cleanup_and_exit 1
1273         $BUILD_DIR/unrpm -q $SRCDIR/$SPECFILE || {
1274             echo "could not install $SPECFILE."
1275             cleanup_and_exit 1
1276         }
1277         for SPECFILE in *.spec ; do : ; done
1278     else
1279         MYSRCDIR="$SRCDIR"
1280     fi
1281
1282     # FIX to work with baselibs_$PROJ etc
1283     if test "$BUILDTYPE" == "dsc" -a -e ${SRCDIR}/baselibs-deb.conf ; then
1284         # Set CREATE_BASELIBS if not set
1285         echo "dsc build and baselibs-deb.conf present: forcing --baselibs to true"
1286         CREATE_BASELIBS=true
1287     fi
1288
1289 # Currently local osc build does not allow extra .deb packages to be
1290 # specified on the command line. Both init_buildsystem and expanddeps
1291 # need to handle .deb dependencies first
1292 #    if test -n "$CREATE_BASELIBS" ; then
1293 #        case $BUILDTYPE in
1294 #            spec) ;;
1295 #            dsc) BUILD_EXTRA_PACKS="$BUILD_EXTRA_PACKS libparse-debcontrol-perl" ;;
1296 #        esac
1297 #    fi
1298
1299     echo processing specfile $MYSRCDIR/$SPECFILE ...
1300
1301     ADDITIONAL_PACKS=""
1302     test -z "$BUILD_EXTRA_PACKS" || ADDITIONAL_PACKS="$ADDITIONAL_PACKS $BUILD_EXTRA_PACKS"
1303     test -z "$CREATE_BASELIBS" || ADDITIONAL_PACKS="$ADDITIONAL_PACKS build"
1304     test "$ccache" = '0' || ADDITIONAL_PACKS="$ADDITIONAL_PACKS ccache"
1305     test "$icecream" = 0 || ADDITIONAL_PACKS="$ADDITIONAL_PACKS icecream gcc-c++"
1306     test -z "$DO_LINT" || ADDITIONAL_PACKS="$ADDITIONAL_PACKS rpmlint-Factory"
1307
1308     if test -n "$CHANGELOG" -a -z "$RUNNING_IN_VM" ; then
1309         rm -f $BUILD_ROOT/.build-changelog
1310         case $SPECFILE in
1311           *.dsc) CFFORMAT=debian ;;
1312           *) CFFORMAT=rpm ;;
1313         esac
1314         echo "running changelog2spec --target $CFFORMAT --file $MYSRCDIR/$SPECFILE"
1315         if ! $BUILD_DIR/changelog2spec --target $CFFORMAT --file "$MYSRCDIR/$SPECFILE" > $BUILD_ROOT/.build-changelog ; then
1316             rm -f $BUILD_ROOT/.build-changelog
1317         fi
1318     fi
1319
1320     if test -n "$VM_TYPE" -a -z "$RUNNING_IN_VM"; then
1321         rm -rf $BUILD_ROOT/.build
1322         mkdir -p $BUILD_ROOT/.build
1323         if test "$DO_INIT" = true ; then
1324             # do fist stage of init_buildsystem
1325             rm -f $BUILD_ROOT/.build.success
1326             set -- init_buildsystem --cachedir "$CACHE_DIR" --prepare "${definesnstuff[@]}" "${repos[@]}" $CLEAN_BUILD $USEUSEDFORBUILD $RPMLIST "$MYSRCDIR/$SPECFILE" $ADDITIONAL_PACKS
1327             echo "$* ..."
1328             "$@" || cleanup_and_exit 1
1329             check_exit
1330             if [ ! -w /root ]; then
1331                 # remove setuid bit if files belong to user to make e.g. mount work
1332                 find $BUILD_ROOT/{bin,sbin,usr/bin,usr/sbin} -type f -uid $UID -perm +4000 -print0 | xargs -0 --no-run-if-empty chmod -s
1333             fi
1334             copy_oldpackages
1335         fi
1336         # start up xen, rerun ourself
1337         cp -a $BUILD_DIR/. $BUILD_ROOT/.build
1338         if ! test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then
1339             rm -rf $BUILD_ROOT/.build-srcdir
1340             mkdir $BUILD_ROOT/.build-srcdir
1341             if test "$BUILDTYPE" = kiwi ; then
1342                 cp -pRL "$MYSRCDIR"/* $BUILD_ROOT/.build-srcdir
1343             else
1344                 cp -p "$MYSRCDIR"/* $BUILD_ROOT/.build-srcdir
1345             fi
1346             MYSRCDIR=$BUILD_ROOT/.build-srcdir
1347         else
1348             # cwd is at $BUILD_ROOT/.build-srcdir which we want to
1349             # umount later so step aside
1350             cd "$SRCDIR"
1351         fi
1352         Q="'\''"
1353         echo "SPECFILE='${SPECFILE//"'"/$Q}'" > $BUILD_ROOT/.build/build.data
1354         echo "BUILD_JOBS='${BUILD_JOBS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1355         echo "BUILD_ARCH='${BUILD_ARCH//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1356         echo "BUILD_RPMS='${BUILD_RPMS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1357         case $BUILD_DIST in
1358             */*)
1359                 cp $BUILD_DIST $BUILD_ROOT/.build/build.dist
1360                 BUILD_DIST=/.build/build.dist
1361                 ;;
1362         esac
1363         echo "BUILD_DIST='${BUILD_DIST//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1364         echo "RELEASE='${RELEASE//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1365         echo "BUILD_DEBUG='${BUILD_DEBUG//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1366         echo "SIGNDUMMY='${SIGNDUMMY//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1367         echo "DO_LINT='${DO_LINT//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1368         echo "DO_CHECKS='${DO_CHECKS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1369         echo "NOROOTFORBUILD='${NOROOTFORBUILD//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1370         echo "CREATE_BASELIBS='$CREATE_BASELIBS'" >> $BUILD_ROOT/.build/build.data
1371         echo "REASON='${REASON//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1372         echo "CHANGELOG='${CHANGELOG//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1373         echo "INCARNATION='${INCARNATION//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1374         echo "DISTURL='${DISTURL//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1375         echo "DO_INIT='${DO_INIT//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
1376         # FIXME: this depends on the kernel and vm.
1377         # could be hda2, sda2 for xen or hdb/sdb for qemu
1378         test -n "$VM_SWAP" && echo "VM_SWAP='${VM_SWAPDEV:-/dev/hda2}'" >> $BUILD_ROOT/.build/build.data
1379         PERSONALITY=0
1380         if test "$VM_TYPE" != 'lxc'; then
1381             test -n "$PERSONALITY_SYSCALL" && PERSONALITY=`perl -e 'print syscall('$PERSONALITY_SYSCALL', 0)."\n"'`
1382         fi
1383         echo "PERSONALITY='$PERSONALITY'" >> $BUILD_ROOT/.build/build.data
1384         echo "MYHOSTNAME='`hostname`'" >> $BUILD_ROOT/.build/build.data
1385         echo -n "definesnstuff=(" >> $BUILD_ROOT/.build/build.data
1386         shellquote "${definesnstuff[@]}" >> $BUILD_ROOT/.build/build.data
1387         echo ")" >> $BUILD_ROOT/.build/build.data
1388         echo -n "repos=(" >> $BUILD_ROOT/.build/build.data
1389         shellquote "${repos[@]}" >> $BUILD_ROOT/.build/build.data
1390         echo ")" >> $BUILD_ROOT/.build/build.data
1391         echo "VM_TYPE='$VM_TYPE'" >> $BUILD_ROOT/.build/build.data
1392         echo "shell='$shell'" >> $BUILD_ROOT/.build/build.data
1393         umount -n $BUILD_ROOT/proc/sys/fs/binfmt_misc 2> /dev/null || true
1394         umount -n $BUILD_ROOT/proc 2> /dev/null || true
1395         umount -n $BUILD_ROOT/dev/pts 2> /dev/null || true
1396         umount -n $BUILD_ROOT/mnt 2> /dev/null || true
1397
1398         if [ -n "$VM_IMAGE" ]; then
1399             check_exit
1400             # needs to work otherwise we have a corrupted file system
1401             umount $BUILD_ROOT || cleanup_and_exit 1
1402         fi
1403
1404         if check_use_emulator; then
1405             vm_init_script="/.build/initscript_qemu_vm"
1406         else
1407             vm_init_script="/.build/build"
1408         fi
1409
1410         if [ "$VM_TYPE" = 'xen' ]; then
1411                 XMROOT="file:$(readlink -f $VM_IMAGE)"
1412                 XMROOT=${XMROOT/#file:\/dev/phy:/dev}
1413                 XMROOT="disk=$XMROOT,hda1,w"
1414                 XMSWAP=
1415                 if test -n "$VM_SWAP" ; then
1416                     XMSWAP="file:$(readlink -f $VM_SWAP)"
1417                     XMSWAP=${XMSWAP/#file:\/dev/phy:/dev}
1418                     XMSWAP="disk=$XMSWAP,hda2,w"
1419                 fi
1420                 XENID="${VM_IMAGE%/root}"
1421                 XENID="${XENID%/tmpfs}"
1422                 XENID="${XENID##*/}"
1423
1424                 echo "booting XEN kernel ..."
1425                 if xm list "build:$XENID" >/dev/null 2>&1 ; then
1426                    echo "Instance already exist, something really went wrong..."
1427                    echo "Please report to your server admin, there might be multiple services running for same domain"
1428                    cleanup_and_exit 3
1429                 fi
1430                 set -- xm create -c $BUILD_DIR/xen.conf name="build:$XENID" ${MEMSIZE:+memory=$MEMSIZE} $XMROOT $XMSWAP extra="quiet init="$vm_init_script" elevator=noop panic=1 console=ttyS0"
1431                 if test "$PERSONALITY" != 0 ; then
1432                     # have to switch back to PER_LINUX to make xm work
1433                     set -- linux64 "$@"
1434                 fi
1435                 echo "$@"
1436                 "$@" || cleanup_and_exit 3
1437         elif [ "$VM_TYPE" = 'uml' ]; then
1438                 echo "booting UML kernel ..."
1439                 set -- $uml_kernel initrd=$uml_initrd root=/ubda init="$vm_init_script" panic=1 elevator=noop quiet ubd0=$VM_IMAGE ${MEMSIZE:+mem=$MEMSIZE}
1440                 echo "$@"
1441                 "$@"
1442         elif [ "$VM_TYPE" = 'qemu' -o "$VM_TYPE" = 'kvm' ]; then
1443                 echo "booting $VM_TYPE ..."
1444                 if [ "$kvm_virtio" = 1 ]; then
1445                     qemu_args=(-drive file="$VM_IMAGE",if=virtio -hda "$VM_IMAGE")
1446                     if [ -n "$VM_SWAP" ]; then
1447                         qemu_args=("${qemu_args[@]}" "-drive")
1448                         qemu_args=("${qemu_args[@]}" "file=$VM_SWAP,if=virtio")
1449                     fi
1450                 else
1451                     qemu_args=(-hda "$VM_IMAGE")
1452                     if [ -n "$VM_SWAP" ]; then
1453                         qemu_args=("${qemu_args[@]}" "-hdb")
1454                         qemu_args=("${qemu_args[@]}" "$VM_SWAP")
1455                     fi
1456                 fi
1457                 if [ -n "$BUILD_JOBS" -a "$icecream" = 0 ]; then
1458                     qemu_args=("${qemu_args[@]}" "-smp" "$BUILD_JOBS")
1459                 fi
1460
1461                 set -- $qemu_bin -no-reboot -nographic -net none \
1462                     -kernel $qemu_kernel \
1463                     -initrd $qemu_initrd \
1464                     -append "root=$qemu_rootdev panic=1 quiet no-kvmclock rw elevator=noop console=ttyS0 init=$vm_init_script" \
1465                     ${MEMSIZE:+-m $MEMSIZE} \
1466                     "${qemu_args[@]}"
1467
1468                 if test "$PERSONALITY" != 0 ; then
1469                     # have to switch back to PER_LINUX to make qemu work
1470                     set -- linux64 "$@"
1471                 fi
1472                 echo "$@"
1473                 "$@"
1474         elif [ "$VM_TYPE" = 'lxc' ]; then
1475                 echo "booting $VM_TYPE ..."
1476                 LXCCONF="$BUILD_ROOT/.build.lxc.conf"
1477                 rm -f "$LXCCONF"
1478                 cat $BUILD_DIR/lxc.conf > "$LXCCONF"
1479                 cat >> "$LXCCONF" <<-EOF
1480                 lxc.rootfs = $BUILD_ROOT
1481                 EOF
1482                 # XXX: do this always instead of leaking the hosts' one?
1483                 echo "rootfs / rootfs rw 0 0" > $BUILD_ROOT/etc/mtab
1484                 LXCID=${BUILD_ROOT##*/}
1485                 lxc-destroy -n "$LXCID" >/dev/null 2>&1 || true
1486                 lxc-create -n "$LXCID" -f "$LXCCONF" || cleanup_and_exit 1
1487                 lxc-start -n "$LXCID" "$vm_init_script"
1488                 BUILDSTATUS="$?"
1489                 test "$BUILDSTATUS" != 255 || BUILDSTATUS=3
1490                 cleanup_and_exit "$BUILDSTATUS"
1491         fi
1492         if test -n "$VM_SWAP" ; then
1493             BUILDSTATUS=`dd if="$VM_SWAP" bs=12 count=1 2>/dev/null`
1494             case $BUILDSTATUS in
1495               BUILDSTATUS[02])
1496                 mkdir -p $BUILD_ROOT/.build.packages
1497                 cd $BUILD_ROOT/.build.packages || cleanup_and_exit 1
1498                 echo "build: extracting built packages..."
1499                 extractbuild --disk "$VM_IMAGE" --input "$VM_SWAP" --skip 512 -v || cleanup_and_exit 3
1500                 # create same layout as with plain chroot
1501                 if test "$BUILDTYPE" = spec ; then
1502                     mkdir -p SRPMS
1503                     for i in *src.rpm *.desktopfiles ; do
1504                         test -e "$i" || continue
1505                         mv "$i" SRPMS/
1506                     done
1507                     for i in *.rpm ; do
1508                         test -e "$i" || continue
1509                         arch=${i%.rpm}
1510                         arch=${i%.delta}
1511                         arch=${arch##*\.}
1512                         mkdir -p RPMS/$arch
1513                         mv "$i" RPMS/$arch/
1514                     done
1515                 elif test "$BUILDTYPE" = dsc ; then
1516                     mkdir -p DEBS
1517                     find . -type f | while read i; do mv "$i" DEBS/; done
1518                 else
1519                     mkdir -p KIWI
1520                     find . -type f | while read i; do mv "$i" KIWI/; done
1521                 fi
1522                 cleanup_and_exit ${BUILDSTATUS#BUILDSTATUS}
1523                 ;;
1524               BUILDSTATUS*)
1525                 cleanup_and_exit ${BUILDSTATUS#BUILDSTATUS}
1526                 ;;
1527               *)
1528                 echo "No buildstatus set, either the packager broke the base system (glibc/bash/perl) or the build host has a kernel or hardware problem, OBS server will retry..."
1529                 cleanup_and_exit 3
1530                 ;;
1531             esac
1532             cleanup_and_exit 1
1533         fi
1534         cleanup_and_exit 0
1535     fi
1536
1537     if test "$DO_INIT" = true ; then
1538         #
1539         # create legacy .buildenv file
1540         #
1541         test -z "$INCARNATION" && INCARNATION=0
1542         echo "BUILD_INCARNATION=$INCARNATION" > $BUILD_ROOT/.buildenv
1543         CREATE_BUILD_BINARIES=
1544         egrep '^#[       ]*needsbinariesforbuild[       ]*$' >/dev/null <$MYSRCDIR/$SPECFILE && CREATE_BUILD_BINARIES=--create-build-binaries
1545         set -- init_buildsystem --cachedir "$CACHE_DIR" "${definesnstuff[@]}" "${repos[@]}" $CLEAN_BUILD $USEUSEDFORBUILD $CREATE_BUILD_BINARIES $RPMLIST "$MYSRCDIR/$SPECFILE" $ADDITIONAL_PACKS
1546         echo "$* ..."
1547         "$@" || cleanup_and_exit 1
1548         check_exit
1549         if df $BUILD_ROOT 2>/dev/null | grep -q "100%"; then
1550             df -h $BUILD_ROOT
1551             echo "build does not work on a completely full filesystem"
1552             cleanup_and_exit 1
1553         fi
1554         mount -n -tproc none $BUILD_ROOT/proc || true
1555         mount -n -tdevpts none $BUILD_ROOT/dev/pts
1556
1557         copy_oldpackages
1558     fi
1559
1560     if test -z "$BUILD_DIST" -a -e "$BUILD_ROOT/.guessed_dist" ; then
1561         read BUILD_DIST < $BUILD_ROOT/.guessed_dist
1562     fi
1563
1564     #
1565     # fix rpmrc if we are compiling for i686
1566     #
1567     test -f $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 && mv $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 $BUILD_ROOT/usr/lib/rpm/rpmrc
1568     if test -e $BUILD_ROOT/usr/lib/rpm/rpmrc -a "$BUILD_ARCH" != "${BUILD_ARCH#i686}" ; then
1569         mv $BUILD_ROOT/usr/lib/rpm/rpmrc $BUILD_ROOT/usr/lib/rpm/rpmrc_i586
1570         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
1571     fi
1572
1573     #
1574     # install dummy sign program if needed
1575     #
1576     test -f $BUILD_ROOT/usr/bin/sign_installed && mv $BUILD_ROOT/usr/bin/sign_installed $BUILD_ROOT/usr/bin/sign
1577     if test -n "$SIGNDUMMY" ; then
1578         test -f $BUILD_ROOT/usr/bin/sign && mv $BUILD_ROOT/usr/bin/sign $BUILD_ROOT/usr/bin/sign_installed
1579         cp $BUILD_DIR/signdummy $BUILD_ROOT/usr/bin/sign
1580         chmod 755 $BUILD_ROOT/usr/bin/sign
1581     fi
1582
1583     #
1584     # check if we want to build with the abuild user
1585     #
1586     BUILD_USER=abuild
1587     if test -x $BUILD_ROOT/bin/rpm ; then
1588         SUSE_VERSION=`chroot $BUILD_ROOT /bin/rpm --eval '%{?suse_version}' 2>/dev/null`
1589         if test -n "$SUSE_VERSION" && test "$SUSE_VERSION" -le 1020 ; then
1590             BUILD_USER=root
1591         fi
1592     fi
1593     if test "$BUILD_USER" = abuild ; then
1594         egrep '^#[       ]*needsrootforbuild[       ]*$' >/dev/null <$SPECFILE && BUILD_USER=root
1595     else
1596         egrep '^#[       ]*norootforbuild[       ]*$' >/dev/null <$SPECFILE && BUILD_USER=abuild
1597     fi
1598     test -n "$NOROOTFORBUILD" && BUILD_USER=abuild
1599
1600     if test $BUILD_USER = abuild ; then
1601         if ! egrep '^abuild:' >/dev/null <$BUILD_ROOT/etc/passwd ; then
1602             echo "abuild::${ABUILD_UID}:${ABUILD_GID}:Autobuild:/home/abuild:/bin/bash" >>$BUILD_ROOT/etc/passwd
1603             echo 'abuild:*:::::::' >>$BUILD_ROOT/etc/shadow # This is needed on Mandriva 2009
1604             echo 'abuild:*::' >>$BUILD_ROOT/etc/gshadow # This is needed on Ubuntu
1605             echo "abuild::${ABUILD_GID}:" >>$BUILD_ROOT/etc/group
1606             mkdir -p $BUILD_ROOT/home/abuild
1607             chown "$ABUILD_UID:$ABUILD_GID" $BUILD_ROOT/home/abuild
1608         else
1609             if ! egrep "^abuild:x?:${ABUILD_UID}:${ABUILD_GID}" >/dev/null <$BUILD_ROOT/etc/passwd ; then
1610                 echo "abuild user present in the buildroot ($BUILD_ROOT) but uid:gid does not match"
1611                 echo "buildroot currently using:"
1612                 egrep "^abuild:" <$BUILD_ROOT/etc/passwd
1613                 echo "build script attempting to use:"
1614                 echo "abuild::${ABUILD_UID}:${ABUILD_GID}:..."
1615                 echo "build aborting"
1616                 cleanup_and_exit 1
1617             fi
1618         fi
1619         if test -f $BUILD_ROOT/etc/shadow ; then
1620             sed -e "s@^root::@root:*:@" < $BUILD_ROOT/etc/shadow > $BUILD_ROOT/etc/shadow.t && mv $BUILD_ROOT/etc/shadow.t $BUILD_ROOT/etc/shadow
1621         fi
1622         if test -f $BUILD_ROOT/etc/gshadow ; then
1623             sed -e "s@^root::@root:*:@" < $BUILD_ROOT/etc/gshadow > $BUILD_ROOT/etc/gshadow.t && mv $BUILD_ROOT/etc/gshadow.t $BUILD_ROOT/etc/gshadow
1624         fi
1625         BUILD_USER_ABUILD_USED=true
1626     else
1627         # building as root
1628         ABUILD_UID=0
1629         ABUILD_GID=0
1630         if egrep '^abuild:' >/dev/null <$BUILD_ROOT/etc/passwd ; then
1631             rm -rf $BUILD_ROOT/home/abuild
1632             egrep -v '^abuild:' <$BUILD_ROOT/etc/passwd >$BUILD_ROOT/etc/passwd.new
1633             mv $BUILD_ROOT/etc/passwd.new $BUILD_ROOT/etc/passwd
1634             egrep -v '^abuild:' <$BUILD_ROOT/etc/group >$BUILD_ROOT/etc/group.new
1635             mv $BUILD_ROOT/etc/group.new $BUILD_ROOT/etc/group
1636             if test -f $BUILD_ROOT/etc/shadow ; then
1637               egrep -v '^abuild:' <$BUILD_ROOT/etc/shadow >$BUILD_ROOT/etc/shadow.new
1638               mv $BUILD_ROOT/etc/shadow.new $BUILD_ROOT/etc/shadow
1639             fi
1640             if test -f $BUILD_ROOT/etc/gshadow ; then
1641               egrep -v '^abuild:' <$BUILD_ROOT/etc/gshadow >$BUILD_ROOT/etc/gshadow.new
1642               mv $BUILD_ROOT/etc/gshadow.new $BUILD_ROOT/etc/gshadow
1643             fi
1644         fi
1645     fi
1646
1647     if test "$BUILDTYPE" = spec ; then
1648         TOPDIR=`chroot $BUILD_ROOT su -c "rpm --eval '%_topdir'" - $BUILD_USER`
1649         if test -z "$TOPDIR"; then
1650             echo "Error: TOPDIR empty"
1651             cleanup_and_exit 1
1652         fi
1653     else
1654         TOPDIR=/usr/src/packages
1655         mkdir -p $BUILD_ROOT$TOPDIR
1656     fi
1657
1658     rm -f $BUILD_ROOT/.build.packages
1659     ln -s ${TOPDIR#/} $BUILD_ROOT/.build.packages
1660
1661     mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1662     mount -n -tdevpts none $BUILD_ROOT/dev/pts 2> /dev/null
1663
1664     setupicecream
1665
1666     setupccache
1667
1668     # nasty hack to prevent rpath on known paths
1669     # FIXME: do this only for suse
1670     if test -d "$BUILD_ROOT/etc/profile.d" ; then
1671         echo "export SUSE_IGNORED_RPATHS=/etc/ld.so.conf" > "$BUILD_ROOT/etc/profile.d/buildsystem.sh"
1672     fi
1673
1674     #
1675     # now clean up RPM building directories
1676     #
1677     rm -rf $BUILD_ROOT$TOPDIR
1678     for i in BUILD RPMS/`uname -m` RPMS/i386 RPMS/noarch SOURCES SPECS SRPMS BUILDROOT; do
1679         mkdir -p $BUILD_ROOT$TOPDIR/$i
1680     done
1681     chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"
1682     check_exit
1683
1684     mkdir -p $BUILD_ROOT$TOPDIR/SOURCES
1685     if test "$BUILDTYPE" = kiwi ; then
1686         mkdir -p $BUILD_ROOT$TOPDIR/KIWI
1687         if test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then
1688             mv "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1689         else
1690             if test -z "$LINKSOURCES" ; then
1691                 cp -dLR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1692             else
1693                 cp -lR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1694             fi
1695             if test "$?" != 0 ; then
1696                 echo "source copy failed"
1697                 cleanup_and_exit 1
1698             fi
1699         fi
1700     else
1701         cp -p "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
1702     fi
1703     # strip prefix from autogenerated files of source services.
1704     for i in $BUILD_ROOT$TOPDIR/SOURCES/_service\:*; do
1705       mv "$i" "${i%/*}/${i##*:}"
1706     done
1707     SPECFILE="${SPECFILE##*:}"
1708
1709     test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir && rm -rf "$MYSRCDIR"
1710     CHANGELOGARGS=
1711     test -n "$CHANGELOG" -a -f "$BUILD_ROOT/.build-changelog" && CHANGELOGARGS="--changelog $BUILD_ROOT/.build-changelog"
1712
1713     if test "$BUILDTYPE" = spec ; then
1714         # do buildrequires/release substitution
1715         args=()
1716         if test -n "$RELEASE"; then
1717                 args=(--release "$RELEASE")
1718         fi
1719         substitutedeps "${args[@]}" --root "$BUILD_ROOT" --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$BUILD_DIR/configs" $CHANGELOGARGS "$BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE" "$BUILD_ROOT/.spec.new" || cleanup_and_exit 1
1720         # extract macros from configuration
1721         getmacros --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$BUILD_DIR/configs" > $BUILD_ROOT/root/.rpmmacros
1722         if test -n "$BUILD_DEBUG" ; then
1723             echo '
1724 %prep %?_suse_insert_debug_package%%prep
1725 %package %?_suse_insert_debug_package%%package
1726 %suse_insert_debug_package \
1727   %global _suse_insert_debug_package \\\
1728     %%undefine _suse_insert_debug_package \\\
1729     %%debug_package
1730
1731 ' >> $BUILD_ROOT/root/.rpmmacros
1732         fi
1733
1734         if [ -n "$BUILD_JOBS" ]; then
1735                 cat >> $BUILD_ROOT/root/.rpmmacros <<-EOF
1736                 %jobs $BUILD_JOBS
1737                 %_smp_mflags -j$BUILD_JOBS
1738                 EOF
1739         fi
1740         test $BUILD_USER = abuild && cp -p $BUILD_ROOT/root/.rpmmacros $BUILD_ROOT/home/abuild/.rpmmacros
1741         # extract optflags from configuration
1742         getoptflags --dist "$BUILD_DIST" --configdir "$BUILD_DIR/configs" --archpath "$BUILD_ARCH" ${BUILD_DEBUG:+--debug} > $BUILD_ROOT/root/.rpmrc
1743         test $BUILD_USER = abuild && cp -p $BUILD_ROOT/root/.rpmrc $BUILD_ROOT/home/abuild/.rpmrc
1744         if test -z "$ABUILD_TARGET"; then
1745             ABUILD_TARGET=$(getchangetarget --dist "$BUILD_DIST" --configdir "$BUILD_DIR/configs" --archpath "$BUILD_ARCH" )
1746             test -z "$ABUILD_TARGET" || echo "build target is $ABUILD_TARGET"
1747         fi
1748     fi
1749     if test -f $BUILD_ROOT/.spec.new ; then
1750         if ! cmp -s $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT/.spec.new ; then
1751             echo -----------------------------------------------------------------
1752             echo I have the following modifications for $SPECFILE:
1753             sed -e "/^%changelog/q" $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE > $BUILD_ROOT/.spec.t1
1754             sed -e "/^%changelog/q" $BUILD_ROOT/.spec.new > $BUILD_ROOT/.spec.t2
1755             diff $BUILD_ROOT/.spec.t1 $BUILD_ROOT/.spec.t2
1756             rm -f $BUILD_ROOT/.spec.t1 $BUILD_ROOT/.spec.t2
1757             mv $BUILD_ROOT/.spec.new $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE
1758         else
1759             rm -f $BUILD_ROOT/.spec.new
1760         fi
1761     fi
1762
1763     if test "$BUILDTYPE" = dsc ; then
1764         rm -rf $BUILD_ROOT$TOPDIR/BUILD
1765         mkdir -p $BUILD_ROOT$TOPDIR/SOURCES.DEB
1766         chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"
1767         DEB_TRANSFORM=
1768         DEB_SOURCEDIR=$TOPDIR/SOURCES
1769         DEB_DSCFILE=$SPECFILE
1770         for f in $BUILD_ROOT$TOPDIR/SOURCES/debian.* ; do
1771             test -f $f && DEB_TRANSFORM=true
1772         done
1773         if test -n "$DEB_TRANSFORM" ; then
1774             echo "running debian transformer..."
1775             if ! debtransform $CHANGELOGARGS $BUILD_ROOT$TOPDIR/SOURCES $BUILD_ROOT$TOPDIR/SOURCES/$SPECFILE $BUILD_ROOT$TOPDIR/SOURCES.DEB ; then
1776                 echo "debian transforming failed."
1777                 cleanup_and_exit 1
1778             fi
1779             DEB_SOURCEDIR=$TOPDIR/SOURCES.DEB
1780             for DEB_DSCFILE in $BUILD_ROOT/$DEB_SOURCEDIR/*.dsc ; do : ; done
1781             DEB_DSCFILE="${DEB_DSCFILE##*/}"
1782         fi
1783         chroot $BUILD_ROOT su -c "dpkg-source -x $DEB_SOURCEDIR/$DEB_DSCFILE $TOPDIR/BUILD" - $BUILD_USER
1784     fi
1785
1786     chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"
1787     cd $BUILD_ROOT$TOPDIR/SOURCES || cleanup_and_exit 1
1788
1789     echo -----------------------------------------------------------------
1790     if test "$BUILD_USER" = root ; then
1791         echo ----- building $SPECFILE
1792     else
1793         echo ----- building $SPECFILE "(user $BUILD_USER)"
1794     fi
1795     echo -----------------------------------------------------------------
1796     echo -----------------------------------------------------------------
1797     if [ -n "$RUNNING_IN_VM" ]; then
1798         if [ -x /sbin/ip ]; then
1799             ip addr add 127.0.0.1/8 dev lo
1800             ip link set lo up
1801         else
1802             ifconfig lo 127.0.0.1 up
1803         fi
1804         if [ -n "$MYHOSTNAME" ]; then
1805             hostname "$MYHOSTNAME"
1806         fi
1807     fi
1808
1809     BUILD_SUCCEEDED=false
1810
1811     if test -n "$OVERLAY" ; then
1812         if test -d "$OVERLAY"; then
1813             pushd $OVERLAY
1814             echo "Copying overlay to BUILD_ROOT"
1815             tar -cpf - . | (cd $BUILD_ROOT ; tar -xvf -)
1816             popd
1817         else
1818             echo "OVERLAY ($OVERLAY) is no directory - skipping"
1819         fi
1820     fi
1821
1822     if test -n "$RSYNCSRC" ; then
1823         if test -n "$RSYNCDEST"; then
1824             if test -d "$RSYNCSRC"; then
1825                 if ! test -d "$BUILD_ROOT/$RSYNCDEST"; then
1826                     echo "ATTENTION! Creating target directory ($BUILD_ROOT/$RSYNCDEST) as its not there."
1827                     mkdir -p $BUILD_ROOT/$RSYNCDEST
1828                 fi
1829                 echo "Running rsync ..."
1830                 rsync -av $RSYNCSRC/* $BUILD_ROOT/$RSYNCDEST/
1831                 chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT/$RSYNCDEST"
1832                 RSYNCDONE=true
1833                 echo "... done"
1834             else
1835                 echo "RSYNCSRC is no directory - skipping"
1836             fi
1837         else
1838             echo "RSYNCSRC given, but not RSYNCDEST - skipping"
1839         fi
1840     fi
1841
1842     if test "$BUILDTYPE" = spec ; then
1843         test -z "$BUILD_RPM_BUILD_STAGE" && BUILD_RPM_BUILD_STAGE=-ba
1844
1845         # XXX: move _srcdefattr to macro file?
1846         rpmbopts=("$BUILD_RPM_BUILD_STAGE" "--define" "_srcdefattr (-,root,root)")
1847         if [ -n "$ABUILD_TARGET" ]; then
1848                 rpmbopts[${#rpmbopts[@]}]="--target=$ABUILD_TARGET"
1849         fi
1850         if [ -n "$DISTURL" ]; then
1851                 rpmbopts[${#rpmbopts[@]}]='--define'
1852                 rpmbopts[${#rpmbopts[@]}]="disturl $DISTURL"
1853         fi
1854         if test -s "$BUILD_ROOT/usr/lib/rpm/mandriva/macros" ; then
1855                 rpmbopts[${#rpmbopts[@]}]='--eval'
1856                 rpmbopts[${#rpmbopts[@]}]="%undefine _enable_debug_packages"
1857         fi
1858         if [ -n "$BUILD_DEBUG" ]; then
1859                 rpmbopts[${#rpmbopts[@]}]='--eval'
1860                 rpmbopts[${#rpmbopts[@]}]="%suse_insert_debug_package"
1861         fi
1862         if [ -n "$RSYNCDONE" ] ; then
1863                 rpmbopts[${#rpmbopts[@]}]='--define'
1864                 rpmbopts[${#rpmbopts[@]}]="RSYNCDONE 1"
1865         fi
1866
1867         rpmbuild=rpmbuild
1868
1869         test -x $BUILD_ROOT/usr/bin/rpmbuild || rpmbuild=rpm
1870         # su involves a shell which would require even more
1871         # complicated quoting to bypass than this
1872         toshellscript $rpmbuild \
1873                 "${definesnstuff[@]}" \
1874                 "${rpmbopts[@]}" \
1875                 "$TOPDIR/SOURCES/$SPECFILE" \
1876                 > $BUILD_ROOT/.build.command
1877         chmod 755 $BUILD_ROOT/.build.command
1878         check_exit
1879         if test -n "$shell"; then
1880             chroot $BUILD_ROOT su -
1881         else
1882             chroot $BUILD_ROOT su -c /.build.command - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
1883         fi
1884     fi
1885
1886     if test "$BUILDTYPE" = dsc ; then
1887         # Checks to see if a build script should be used
1888         # this allows the build environment to be manipulated
1889         # and alternate build commands can be used
1890         DSC_BUILD_CMD="dpkg-buildpackage -us -uc -rfakeroot-tcp"
1891         if test -e $BUILD_ROOT/$TOPDIR/SOURCES/build.script ; then
1892             echo "Sourcing build.script to build - it should normally run 'dpkg-buildpackage -us -uc -rfakeroot-tcp'"
1893             DSC_BUILD_CMD="source $TOPDIR/SOURCES/build.script"
1894             chmod +x $BUILD_ROOT/$TOPDIR/SOURCES/build.script
1895         fi
1896
1897         if test -n "$shell"; then
1898             chroot $BUILD_ROOT su -
1899         else
1900             chroot $BUILD_ROOT su -c "cd $TOPDIR/BUILD && $DSC_BUILD_CMD" - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
1901         fi
1902
1903         mkdir -p $BUILD_ROOT/$TOPDIR/DEBS
1904         for DEB in $BUILD_ROOT/$TOPDIR/*.deb ; do
1905             test -e "$DEB" && mv "$DEB" "$BUILD_ROOT/$TOPDIR/DEBS"
1906         done
1907         # link sources over
1908         ln $BUILD_ROOT/$DEB_SOURCEDIR/$DEB_DSCFILE $BUILD_ROOT/$TOPDIR/DEBS/
1909         while read f ; do
1910             ln $BUILD_ROOT/$DEB_SOURCEDIR/$f $BUILD_ROOT/$TOPDIR/DEBS/
1911         done < <(sed -ne '/^Files:/,$s/^ ................................ [0-9][0-9]* //p' < $BUILD_ROOT/$DEB_SOURCEDIR/$DEB_DSCFILE)
1912     fi
1913
1914     if test "$BUILDTYPE" = kiwi ; then
1915         . $BUILD_DIR/build_kiwi.sh
1916         run_kiwi
1917     fi
1918
1919     test "$BUILD_SUCCEEDED" = true || cleanup_and_exit 1
1920     test -d "$SRCDIR" && cd "$SRCDIR"
1921 done
1922
1923 RPMS=`find $BUILD_ROOT/$TOPDIR/RPMS -type f -name "*.rpm" 2>/dev/null || true`
1924 DEBS=`find $BUILD_ROOT/$TOPDIR/DEBS -type f -name "*.deb" 2>/dev/null || true`
1925
1926 if test -n "$RPMS" -a -n "$BUILD_USER_ABUILD_USED" ; then
1927     echo "... checking for files with abuild user/group"
1928     BADFILE=
1929     while read un gn fn ; do
1930         if test "$un" = abuild -o "$gn" = abuild -o "$un" = ${ABUILD_UID} -o "$gn" = ${ABUILD_GID} ; then
1931             echo "  $un $gn $fn"
1932             BADFILE=true
1933         fi
1934     done < <(rpm -qp --qf '[%{FILEUSERNAME} %{FILEGROUPNAME} %{FILENAMES}\n]' $RPMS)
1935     if test -n "$BADFILE" ; then
1936         echo "please fix your filelist (e.g. add defattr)"
1937         cleanup_and_exit 1
1938     fi
1939 fi
1940
1941 if test -n "$RPMS" -a -d "$BUILD_ROOT/usr/lib/build/checks" ; then
1942     export PNAME=""
1943     export DO_RPM_REMOVE=true
1944     for SRPM in $BUILD_ROOT/$TOPDIR/SRPMS/*src.rpm ; do
1945         test -f "$SRPM" && PNAME=`rpm --nodigest --nosignature -qp --qf "%{NAME}" $SRPM`
1946     done
1947     for CHECKSCRIPT in $BUILD_ROOT/usr/lib/build/checks/* ; do
1948         echo "... running `basename $CHECKSCRIPT`"
1949         $CHECKSCRIPT || cleanup_and_exit 1
1950     done
1951 fi
1952
1953 if test -n "$RPMS" -a "$DO_CHECKS" != "false" -a -x "$BUILD_ROOT/opt/testing/bin/rpmlint" -a -d "$BUILD_ROOT/$TOPDIR/RPMS" ; then
1954     LINT_RPM_FILE_LIST=($(find $BUILD_ROOT/$TOPDIR/RPMS \
1955         \( -name "*-debuginfo-*" -o -name "*-debugsource-*" \
1956         -o -name "*-32bit-*" -o -name "*-64bit-*" \
1957         -o -name "*-x86-*" -o -name "*-ia32-*" \) -prune \
1958         -o -type f -name '*.rpm' -print))
1959     SRPM_FILE_LIST=($(find $BUILD_ROOT/$TOPDIR/SRPMS -type f -name "*.rpm"))
1960     echo
1961     echo "RPMLINT report:"
1962     echo "==============="
1963     chroot $BUILD_ROOT /opt/testing/bin/rpmlint --info ${LINT_RPM_FILE_LIST[*]#$BUILD_ROOT} ${SRPM_FILE_LIST[*]#$BUILD_ROOT} || cleanup_and_exit 1
1964     echo
1965 fi
1966
1967 if test \( -n "$RPMS" -o -n "$DEBS" \) -a -n "$CREATE_BASELIBS"; then
1968     create_baselibs
1969 fi
1970
1971 exitcode=0
1972 # post build scripts
1973 # TODO: don't hardcode. instead run scripts in a directory as it's done for the checks
1974 if test -n "$RPMS" \
1975         -a -d "$BUILD_ROOT/$TOPDIR/RPMS" \
1976         -a -d "$BUILD_ROOT/.build.oldpackages" \
1977         ; then
1978     if test -x "$BUILD_ROOT/usr/lib/build/same-build-result.sh" ; then
1979         echo "... comparing built packages with the former built"
1980         mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
1981         if chroot $BUILD_ROOT /usr/lib/build/same-build-result.sh /.build.oldpackages "$TOPDIR/RPMS" "$TOPDIR/SRPMS"; then
1982             > $BUILD_ROOT/.build/.same_result_marker
1983             # XXX: dirty build service hack. fix bs_worker. Search for
1984             # 'same_result_marker' for traces of a first try to get rid of this
1985             if test -n "$REASON" -a -n "$DISTURL"; then
1986                 exitcode=2
1987             fi
1988         fi
1989     fi
1990     if test -x "$BUILD_ROOT/usr/bin/makedeltarpm" \
1991         -a -x $BUILD_ROOT/usr/lib/build/mkdrpms; then
1992         echo "... creating delta rpms"
1993         ds=("$BUILD_ROOT/$TOPDIR"/RPMS/* "$BUILD_ROOT$TOPDIR/SRPMS")
1994         chroot $BUILD_ROOT /usr/lib/build/mkdrpms /.build.oldpackages "${ds[@]#$BUILD_ROOT}"
1995     fi
1996 fi
1997
1998 if test -n "$RUNNING_IN_VM" -a -n "$VM_SWAP"; then
1999     echo "... saving built packages"
2000     swapoff "$VM_SWAP"
2001     args="--padstart 512 --padend 512 -v"
2002     case "$BUILDTYPE" in
2003         spec)
2004             computeblocklists $args $TOPDIR/RPMS/*/*.{d,}rpm $TOPDIR/SRPMS/* > "$VM_SWAP"
2005             ;;
2006         dsc)
2007             computeblocklists $args $TOPDIR/DEBS/*.deb $TOPDIR/SOURCES.DEB/* > "$VM_SWAP"
2008             ;;
2009         kiwi)
2010             computeblocklists $args $TOPDIR/KIWI/* > "$VM_SWAP"
2011             ;;
2012     esac || cleanup_and_exit 1
2013 fi
2014
2015 echo
2016 echo "$HOST finished \"build $SPECFILE\" at `date --utc`."
2017 echo
2018
2019 cleanup_and_exit "$exitcode"