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