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