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