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