- support tilde in rpm version comparison
[platform/upstream/build.git] / init_buildsystem
1 #!/bin/bash
2 # Script to create a complete system to build packages in a chroot
3 # environment.  This script ensures, that all packages given as
4 # parameter are installed. All other packges will be deleted.
5 #
6 # BUILD_ROOT  here the packages will be installed/deleted
7 # BUILD_RPMS  here we get our packages to install
8 # BUILD_ARCH  path of the architectures we try
9 #
10 # (c) 1997-2005 SuSE GmbH Nuernberg, Germany
11
12 #
13 # needed globals variables
14 #
15 export SRC
16 export YAST_IS_RUNNING="instsys"
17 export DEBIAN_FRONTEND=noninteractive
18 export DEBIAN_PRIORITY=critical
19 export BUILD_DIR=${BUILD_DIR:-/usr/lib/build}
20 # need to restore build root owner for non-root builds
21 browner=0
22 definesnstuff=()
23 repos=()
24
25 . $BUILD_DIR/common_functions || exit 1
26
27 # should RPMs be installed with --force ?
28 USE_FORCE=false
29
30 BUILD_IS_RUNNING=$BUILD_ROOT/not-ready
31 TMPFILE=$BUILD_ROOT/tmpfile
32 #buildhost removed so that id can be generated from repo files
33 #RPMIDFMT="%{NAME}-%{VERSION}-%{RELEASE} %{BUILDHOST}-%{BUILDTIME}\n"
34 RPMIDFMT="%{NAME}-%{VERSION}-%{RELEASE} %{BUILDTIME}\n"
35
36 PREPARE_VM=
37 USEUSEDFORBUILD=
38 LIST_STATE=
39 RPMLIST=
40 CLEAN_BUILD=
41 CREATE_BUILD_BINARIES=
42
43 while test -n "$1" ; do
44     case "$1" in
45         --prepare)
46             shift
47             PREPARE_VM=true
48             ;;
49         --create-build-binaries)
50             shift
51             CREATE_BUILD_BINARIES=true
52             ;;
53         --useusedforbuild)
54             shift
55             USEUSEDFORBUILD=--useusedforbuild
56             ;;
57         --list-state)
58             shift
59             LIST_STATE=true
60             ;;
61         --rpmlist)
62             shift
63             RPMLIST=$1
64             shift
65             ;;
66         --define|--with|--without)
67             definesnstuff[${#definesnstuff[@]}]="$1";
68             definesnstuff[${#definesnstuff[@]}]="$2";
69             shift 2
70             ;;
71         --repository|--repo)
72             repos[${#repos[@]}]="$2";
73             shift 2;
74             ;;
75         --clean)
76             CLEAN_BUILD="$1"
77             shift
78         ;;
79         --cachedir)
80             CACHE_DIR="$2"
81             shift 2
82             ;;
83         *)
84             break
85             ;;
86     esac
87 done
88 PKGS=("$@")
89
90 #
91 # needed functions
92 #
93
94 cleanup_and_exit()
95 {
96     trap EXIT
97     [ "$BUILD_ROOT" != / ] || chown $browner $BUILD_ROOT
98     # umount so init_buildsystem can be used standalone
99 # XXX: use stat -f /dev/pts/ -c %T  to check whether it's mounted and not suppress errors then?
100     umount -n $BUILD_ROOT/proc/sys/fs/binfmt_misc 2> /dev/null || true
101     umount -n $BUILD_ROOT/proc 2> /dev/null || true
102     umount -n $BUILD_ROOT/dev/pts 2> /dev/null || true
103     umount -n $BUILD_ROOT/mnt 2> /dev/null || true
104     exit ${1:-0}
105 }
106
107 clean_build_root()
108 {
109         test -n "$BUILD_ROOT" && {
110             umount -n $BUILD_ROOT/proc/sys/fs/binfmt_misc 2> /dev/null || true
111             umount -n $BUILD_ROOT/proc 2> /dev/null || true
112             umount -n $BUILD_ROOT/dev/pts 2> /dev/null || true
113             umount -n $BUILD_ROOT/mnt 2> /dev/null || true
114             rm -rf -- $BUILD_ROOT/*
115             rm -rf -- $BUILD_ROOT/.build
116             rm -rf -- $BUILD_ROOT/.root
117             rm -rf -- $BUILD_ROOT/.init_b_cache
118             mkdir -p $BUILD_ROOT/proc
119             mkdir -p $BUILD_ROOT/dev/pts
120             if [ "$UID" = '0' ]; then
121                     mount -n -tproc none $BUILD_ROOT/proc
122                     mount -n -tdevpts none $BUILD_ROOT/dev/pts
123             fi
124         }
125 }
126
127 preinstall()
128 {
129     check_exit
130     echo "preinstalling $1..."
131     cd $BUILD_ROOT || cleanup_and_exit 1
132     if test -x /usr/bin/bsdtar ; then
133         CPIO="/usr/bin/bsdtar --chroot -o --numeric-owner -x -f-"
134         TAR="/usr/bin/bsdtar --chroot -o --numeric-owner -x"
135     else
136         # cpio isn't safe so we require bsdtar for VMs. chroot is
137         # unsafe anyways so it's ok for that.
138         if [ -n "$PREPARE_VM" ]; then
139             echo "Error: setting up a VM requires bsdtar for security reasons."
140             echo "Please install bsdtar"
141             cleanup_and_exit 1
142         fi
143         CPIO="cpio --extract --unconditional --preserve-modification-time --make-directories --no-absolute-filenames --quiet"
144         TAR="tar -x"
145     fi
146     if test -e "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" ; then
147         PAYLOADDECOMPRESS=cat
148         case `rpm -qp --nodigest --nosignature --qf "%{PAYLOADCOMPRESSOR}\n" "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm"` in
149             lzma) rpm --showrc | grep PayloadIsLzma > /dev/null || PAYLOADDECOMPRESS="lzma -d" ;;
150             xz) rpm --showrc | grep PayloadIsXz > /dev/null || PAYLOADDECOMPRESS="xz -d" ;;
151         esac
152         if test "$PAYLOADDECOMPRESS" = "lzma -d" ; then
153             if ! lzma </dev/null >/dev/null 2>&1 ; then
154                 test -f "$BUILD_DIR/lzmadec.sh" && PAYLOADDECOMPRESS="bash $BUILD_DIR/lzmadec.sh"
155             fi
156         fi
157         if test "$PAYLOADDECOMPRESS" = "xz -d" ; then
158             if ! xz </dev/null >/dev/null 2>&1 ; then
159                 test -f "$BUILD_DIR/xzdec.sh" && PAYLOADDECOMPRESS="bash $BUILD_DIR/xzdec.sh"
160             fi
161         fi
162         if test "$PAYLOADDECOMPRESS" = cat ; then
163             rpm2cpio "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" | $CPIO
164         else
165             rpm2cpio "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" | $PAYLOADDECOMPRESS | $CPIO
166         fi
167         if test -e ".init_b_cache/scripts/$1.run" ; then
168             rpm -qp --nodigest --nosignature --qf "%{PREIN}" "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" > ".init_b_cache/scripts/$1.pre"
169             rpm -qp --nodigest --nosignature --qf "%{POSTIN}" "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" > ".init_b_cache/scripts/$1.post"
170             echo -n '(none)' > .init_b_cache/scripts/.none
171             cmp -s ".init_b_cache/scripts/$1.pre" .init_b_cache/scripts/.none && rm -f ".init_b_cache/scripts/$1.pre"
172             cmp -s ".init_b_cache/scripts/$1.post" .init_b_cache/scripts/.none && rm -f ".init_b_cache/scripts/$1.post"
173             rm -f .init_b_cache/scripts/.none
174         fi
175     elif test -e "$BUILD_ROOT/.init_b_cache/rpms/$1.deb" ; then
176         ar x "$BUILD_ROOT/.init_b_cache/rpms/$1.deb" control.tar.gz data.tar.gz
177         mkdir -p .init_b_cache/scripts/control
178         $TAR -C .init_b_cache/scripts/control -z -f control.tar.gz
179         $TAR -z -f data.tar.gz
180         if test -e ".init_b_cache/scripts/$1.run" ; then
181             test -e .init_b_cache/scripts/control/preinst && mv .init_b_cache/scripts/control/preinst ".init_b_cache/scripts/$1.pre"
182             test -e .init_b_cache/scripts/control/postinst && mv .init_b_cache/scripts/control/postinst ".init_b_cache/scripts/$1.post"
183         fi
184         rm -rf .init_b_cache/scripts/control control.tar.gz data.tar.gz
185     elif test -e "$BUILD_ROOT/.init_b_cache/rpms/$1.arch" ; then
186         $TAR -z -f "$BUILD_ROOT/.init_b_cache/rpms/$1.arch"
187         if test -f .INSTALL ; then
188             cat .INSTALL > ".init_b_cache/scripts/$1.post"
189             echo 'type post_install >/dev/null 2>&1 && post_install' >> ".init_b_cache/scripts/$1.post"
190         fi
191         rm -f .PKGINFO .INSTALL
192     else
193         echo "warning: package $1 does not exist"
194     fi
195 }
196
197 run_pkg_scripts()
198 {
199     chroot $BUILD_ROOT /sbin/ldconfig 2>/dev/null
200     for PKG in $PACKAGES_TO_RUNSCRIPTS ; do
201         if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre" ; then
202             echo "running $PKG preinstall script"
203             if test -e "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" ; then
204                 chroot $BUILD_ROOT sh ".init_b_cache/scripts/$PKG.pre" 0
205             else
206                 chroot $BUILD_ROOT ".init_b_cache/scripts/$PKG.pre" install < /dev/null
207             fi
208             rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre"
209         fi
210         if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post" ; then
211             echo "running $PKG postinstall script"
212             if test -e "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" ; then
213                 chroot $BUILD_ROOT sh ".init_b_cache/scripts/$PKG.post" 1
214             else
215                 chroot $BUILD_ROOT ".init_b_cache/scripts/$PKG.post" configure '' < /dev/null
216             fi
217             rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post"
218         fi
219         check_exit
220     done
221 }
222
223 init_db()
224 {
225     if test $PSUF = rpm ; then
226         echo initializing rpm db...
227         if ! test -e $BUILD_ROOT/usr/lib/rpm/cpuinfo.yaml; then
228             # rpm v5 does not have initdb
229             chroot $BUILD_ROOT rpm --initdb || cleanup_and_exit 1
230         fi
231         # hack: add nofsync to db config to speed up install
232         mkdir -p $BUILD_ROOT/root
233         echo '%__dbi_perms perms=0644 nofsync' > $BUILD_ROOT/.rpmmacros
234         echo '%__dbi_perms perms=0644 nofsync' > $BUILD_ROOT/root/.rpmmacros
235     elif test $PSUF = deb ; then
236         # force dpkg into database to make epoch test work
237         if ! test "$BUILD_ROOT/.init_b_cache/rpms/dpkg.deb" -ef "$BUILD_ROOT/.init_b_cache/dpkg.deb" ; then
238             rm -f $BUILD_ROOT/.init_b_cache/dpkg.deb
239             cp $BUILD_ROOT/.init_b_cache/rpms/dpkg.deb $BUILD_ROOT/.init_b_cache/dpkg.deb || cleanup_and_exit 1
240         fi
241         chroot $BUILD_ROOT dpkg -i --force all .init_b_cache/dpkg.deb >/dev/null 2>&1
242     elif test $PSUF = arch ; then
243         mkdir -p $BUILD_ROOT/var/lib/pacman/sync
244         touch $BUILD_ROOT/var/lib/pacman/sync/core.db
245         touch $BUILD_ROOT/var/lib/pacman/sync/extra.db
246         touch $BUILD_ROOT/var/lib/pacman/sync/community.db
247     fi
248 }
249
250 reorder()
251 {
252     test -z "$*" && return
253     rm -f $BUILD_ROOT/.init_b_cache/order.manifest
254     for PKG in "$@" ; do
255         echo "$PKG" >> $BUILD_ROOT/.init_b_cache/order.manifest
256     done
257     $BUILD_DIR/order --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir $BUILD_DIR/configs --manifest $BUILD_ROOT/.init_b_cache/order.manifest $BUILD_ROOT/.init_b_cache/rpms
258     rm -f $BUILD_ROOT/.init_b_cache/order.manifest
259 }
260
261 create_devs()
262 {
263     local com file mode arg
264
265     mkdir -m 755 -p $BUILD_ROOT/dev/pts
266     test -f $BUILD_ROOT/dev/shm && rm -f $BUILD_ROOT/dev/shm
267     mkdir -m 755 -p $BUILD_ROOT/dev/shm
268     while read com file mode arg ; do
269         rm -f $BUILD_ROOT/dev/$file
270         if test $com = ln ; then
271             ln -s $arg $BUILD_ROOT/dev/$file
272             continue
273         fi
274         $com -m $mode $BUILD_ROOT/dev/$file $arg
275     done << DEVLIST
276         mknod null    666 c 1 3
277         mknod zero    666 c 1 5
278         mknod full    622 c 1 7
279         mknod random  666 c 1 8
280         mknod urandom 644 c 1 9
281         mknod tty     666 c 5 0
282         mknod ptmx    666 c 5 2
283         mknod loop0   640 b 7 0
284         mknod loop1   640 b 7 1
285         mknod loop2   640 b 7 2
286         mknod loop3   640 b 7 3
287         ln    fd      777 /proc/self/fd
288         ln    stdin   777 fd/0
289         ln    stdout  777 fd/1
290         ln    stderr  777 fd/2
291 DEVLIST
292 }
293
294 # check whether the repo list contains a plain "zypp://". Add all
295 # enabled zypp repos in this case
296 maybe_add_all_zypp_repos()
297 {
298     local i j
299     r=()
300     for i in "${repos[@]}"; do
301         if [ "$i" = "zypp://" ]; then
302             while read j; do
303                 j="${j#/etc/zypp/repos.d/}"
304                 r=("${r[@]}" "zypp://${j%.repo}")
305             done < <(grep -l enabled=1 /etc/zypp/repos.d/*.repo)
306         else
307             r=("${r[@]}" "$i")
308         fi
309     done
310     repos=("${r[@]}")
311 }
312
313 validate_cache_file()
314 {
315     local findonly=''
316     maybe_add_all_zypp_repos
317     if ! test -f $CACHE_FILE || ! test -f $CACHE_FILE.id || \
318         test "${repos[*]} ${BUILD_RPMS//:/ /}" != "$(cat $CACHE_FILE.id 2>/dev/null)"; then
319         rm -f $CACHE_FILE.id
320     else
321         for SRC in "${repos[@]}" ${BUILD_RPMS//:/ /}; do
322             test -n "$SRC" || SRC=.
323             if [ "${SRC#zypp://}" != "$SRC" ]; then
324                 SRC="/var/cache/zypp/raw" # XXX can't use name here as we'd need to parse the file
325             fi
326             if test "$SRC" -nt $CACHE_FILE; then
327                 rm -f $CACHE_FILE.id
328                 break
329             fi
330         done
331     fi
332     if ! test -f $CACHE_FILE.id ; then
333         test -z "$LIST_STATE" && echo initializing $CACHE_FILE ...
334         for SRC in "${repos[@]}" -- ${BUILD_RPMS//:/ /}; do
335             if test "$SRC" = '--' ; then
336                 findonly=1
337                 continue
338             fi
339             test -z "$SRC" && SRC=`pwd`
340             if [ "${SRC#zypp://}" != "$SRC" ]; then
341                 set -- $BUILD_DIR/createrepomddeps "$SRC"
342             elif [ "${SRC#http://}" != "$SRC" -o "${SRC#https://}" != "$SRC" -o "${SRC#ftp://}" != "$SRC" -o "${SRC#ftps://}" != "$SRC" ]; then
343                 mkdir -p "$(getcachedir "$SRC")"
344                 set -- $BUILD_DIR/createrepomddeps --cachedir="$CACHE_DIR" "$SRC"
345             elif [ "${SRC#arch@http://}" != "$SRC" -o "${SRC#arch@https://}" != "$SRC" -o "${SRC#arch@ftp://}" != "$SRC" -o "${SRC#arch@ftps://}" != "$SRC" ]; then
346                 mkdir -p "$(getcachedir "$SRC")"
347                 set -- $BUILD_DIR/createrepomddeps --cachedir="$CACHE_DIR" "$SRC"
348             elif [ ! -e "$SRC" ]; then
349                 echo "*** $SRC does not exist" >&2
350                 cleanup_and_exit 1
351             elif [ -z "$findonly" -a -e "$SRC"/suse/setup/descr/packages -o -e "$SRC"/suse/setup/descr/packages.gz ]; then
352                 set -- $BUILD_DIR/createyastdeps "$SRC"
353             elif [ -z "$findonly" -a -e "$SRC"/repodata/repomd.xml ]; then
354                 set -- $BUILD_DIR/createrepomddeps "$SRC"
355             else
356                 set -- $BUILD_DIR/createrpmdeps "$SRC"
357             fi
358             echo "$@" >&2
359             "$@" || cleanup_and_exit 1
360             echo D:
361         done > $CACHE_FILE.new
362
363         mv $CACHE_FILE.new $CACHE_FILE
364         echo "${repos[*]} ${BUILD_RPMS//:/ /}" > $CACHE_FILE.id
365     fi
366 }
367
368 #
369 # Copy qemu static binaries for cross-build emulation mode.
370 # Preferentially install from $BUILD_DIR (potentially provided by build
371 # server) in preference to build worker's /usr/bin (fallback).
372 # If initvm is available it will be in $BUILD_DIR; otherwise prepare to
373 # fall back to initscript_qemu_vm shell script with corresponding static
374 # bash and mount programs.
375 #
376
377 check_copy_qemu()
378 {
379     local arch
380
381     for arch in $EMULATOR_DEVS; do
382         if test -e $BUILD_DIR/qemu-$arch; then
383             return 0
384         fi
385     done
386     return 1
387 }
388
389 copy_qemu()
390 {
391     local path dest
392
393     echo "copying qemu"
394
395     mkdir -p $BUILD_ROOT/usr/bin
396
397     if check_copy_qemu; then
398
399         for path in $BUILD_DIR/qemu-*; do
400             if file $path | grep -q static; then
401                 dest="$BUILD_ROOT/usr/bin/${path##*/}"
402                 if [ -f "$path" -a ! -x "$dest" ]; then
403                     echo -n " $path"            # report copy
404                     #echo install -m755 "$path" "$dest"
405                     install -m755 "$path" "$dest"
406                 fi
407             fi
408         done
409
410     else
411
412         for path in /usr/bin/qemu-*; do
413             if file $path | grep -q static; then
414                 dest="$BUILD_ROOT/usr/bin/${path##*/}"
415                 if [ -f "$path" -a ! -x "$dest" ]; then
416                     echo -n " $path"            # report copy
417                     #echo install -m755 "$path" "$dest"
418                     install -m755 "$path" "$dest"
419                 fi
420             fi
421         done
422
423         if [ -e /usr/sbin/qemu-binfmt-conf.sh \
424             -a ! -e $BUILD_ROOT/usr/sbin/qemu-binfmt-conf.sh ]; then
425             echo " /usr/sbin/qemu-binfmt-conf.sh"               # report copy
426             mkdir -p $BUILD_ROOT/usr/sbin
427             install -m755 /usr/sbin/qemu-binfmt-conf.sh $BUILD_ROOT/usr/sbin
428         fi
429
430     fi
431     echo ""
432
433     # Below for backward compatibility when /.build/initvm is not present
434
435     if [ -n "$PREPARE_VM" ]; then
436         if [ -x /bin/bash-static -a -x /bin/mount-static ]; then
437             echo " /bin/bash-static /bin/mount-static"  # report copy
438             mkdir -p $BUILD_ROOT/bin
439             install -m755 /bin/bash-static $BUILD_ROOT/bin
440             install -m755 /bin/mount-static $BUILD_ROOT/bin
441         fi
442     fi
443 }
444
445 check_binfmt_registered()
446 {
447     local arch
448     for arch in $EMULATOR_DEVS; do
449         if test -e /proc/sys/fs/binfmt_misc/$arch; then
450             return 0
451         fi
452     done
453     return 1
454 }
455
456 fail_exit()
457 {
458   cleanup_and_exit 1
459 }
460
461 # modify $SRC
462 downloadpkg()
463 {
464     local url="$1"
465     local cachedir
466
467     if [ "${url:0:7}" == "zypp://" ] ; then
468         cachedir="/var/cache/zypp/packages/"
469         SRC="$cachedir${url#zypp://}"
470         mkdir -p "${SRC%/*}" || cleanup_and_exit 1
471     elif [ "${url:0:7}" == "http://" -o "${url:0:8}" == "https://" -o "${url:0:6}" == "ftp://" -o "${url:0:7}" == "ftps://" ] ; then
472         cachedir="$(getcachedir "$url")"
473         local name="$(basename "$url")"
474         name=${name/%.pkg.tar.?z/.arch}
475         SRC="$cachedir/$name"
476     else
477         echo "Invalid url: $url"
478         cleanup_and_exit 1
479     fi
480
481     if [ ! -e "$SRC" ]; then
482         local destdir="$cachedir/tmp"
483         mkdir -p "$destdir"
484         echo "downloading $url ... ";
485         $BUILD_DIR/download "$destdir" "$url" || cleanup_and_exit 1
486         local destfile="$destdir/${url##*/}"
487         if [ ! -e "$destfile" ]; then
488             echo "expected $destfile after download but it's missing" >&2
489             cleanup_and_exit 1
490         fi
491         case $destfile in
492           *.rpm)
493             rpm -K "$destfile" > $destfile.v || { echo "rpm verify failed" >&2; rm -rf "$destdir"; cleanup_and_exit 1; }
494             if grep "NOT OK" $destfile.v; then
495                 rm -rf "$destdir"
496                 cleanup_and_exit 1
497             fi
498             rm -f "$destfile.v"
499             ;;
500         esac
501         mv "$destfile" "$SRC" || cleanup_and_exit 1
502     fi
503 }
504
505 getcachedir()
506 {
507     url=$1
508     case $url in
509       *.pkg.tar.?z) url="arch@$url" ;;
510     esac
511     for repo in "${repos[@]}" ; do
512         if [ "${url:0:${#repo}}" == "$repo" -o "${url:0:${#repo}}" == "$repo" ] ; then
513             read repoid dummy < <(echo -n "$repo" | md5sum)
514             echo "$CACHE_DIR/$repoid"
515             break
516         fi
517     done
518 }
519
520 get_pkg_filename()
521 {
522     local url="$1"
523     local name=$(basename $url)
524     local cachedir=$(getcachedir $url)
525     local destfile="$cachedir/$name"
526     echo $destfile
527 }
528
529 set_build_arch
530
531 trap fail_exit EXIT
532
533 if [ "$BUILD_ROOT" = / ]; then
534     browner="$(stat -c %U /)"
535 fi
536
537 if [ -n "$CLEAN_BUILD" ]; then
538     clean_build_root
539 fi
540
541 #
542 # now test if there was an incomplete run
543 #
544 if test -e $BUILD_IS_RUNNING ; then
545     echo It seems that there was an incomplete setup of $BUILD_ROOT.
546     echo To be sure, we will build it again completely...
547     umount -n $BUILD_ROOT/proc/sys/fs/binfmt_misc 2> /dev/null || true
548     umount -n $BUILD_ROOT/proc 2> /dev/null
549     umount -n $BUILD_ROOT/dev/pts 2> /dev/null
550     umount -n $BUILD_ROOT/mnt 2> /dev/null
551     echo "Your build system is broken!! Shall I execute"
552     echo
553     echo "    rm -rf -- $BUILD_ROOT/*"
554     echo
555     echo "y - yes, cleanup the build root"
556     echo "N - No, abort build (default on enter)"
557     echo "c - Continue anyway with this build root"
558     echo -n "[y/N/c] "
559     read ANSWER
560     case "$ANSWER" in
561         c|C) rm -f $BUILD_IS_RUNNING ;;
562         y|Y) clean_build_root ;;
563         *) cleanup_and_exit 1 ;;
564     esac
565 fi
566
567 #
568 # store that we start to build system
569 #
570 mkdir -p $BUILD_ROOT
571 mkdir -p $BUILD_ROOT/.build
572 touch $BUILD_IS_RUNNING
573
574 if test -n "$PREPARE_VM" ; then
575     rm -f $BUILD_ROOT/.build/init_buildsystem.data
576 fi
577 if test -e $BUILD_ROOT/.build/init_buildsystem.data ; then
578     # vm continuation
579     . $BUILD_ROOT/.build/init_buildsystem.data
580     if ! test -e $BUILD_ROOT/.init_b_cache/preinstall_finished ; then
581         # finish preinstall
582         run_pkg_scripts
583         init_db
584         touch $BUILD_ROOT/.init_b_cache/preinstall_finished
585     fi
586 else
587     #
588     # now make sure that all the packages are installed.
589     #
590     rm -rf $BUILD_ROOT/.init_b_cache
591     mkdir -p $BUILD_ROOT/.init_b_cache/scripts
592
593     if test -z "$RPMLIST" ; then
594         #
595         # create rpmdeps file
596         #
597         CACHE_FILE=$BUILD_ROOT/.srcfiles.cache
598         validate_cache_file
599
600         #
601         # select and expand packages
602         #
603         RPMLIST=$BUILD_ROOT/.init_b_cache/rpmlist
604         test -z "$LIST_STATE" && echo "expanding package dependencies..."
605         if ! $BUILD_DIR/expanddeps $USEUSEDFORBUILD "${definesnstuff[@]}" --dist "$BUILD_DIST" --depfile "$CACHE_FILE" --archpath "$BUILD_ARCH" --configdir $BUILD_DIR/configs "${PKGS[@]}" > $RPMLIST ; then
606             rm -f $BUILD_IS_RUNNING
607             cleanup_and_exit 1
608         fi
609     fi
610
611     if test -n "$LIST_STATE" ; then
612         rm -f $BUILD_IS_RUNNING
613         while read PKG SRC ; do
614            test "$PKG" = "preinstall:" && continue
615            test "$PKG" = "runscripts:" && continue
616            test "$PKG" = "cbpreinstall:" && continue
617            test "$PKG" = "vminstall:" && continue
618            test "$PKG" = "dist:" && continue
619            test "$PKG" = "rpmid:" && continue
620            echo "${SRC##*/}"
621         done < $BUILD_ROOT/.init_b_cache/rpmlist
622         rm -rf $BUILD_ROOT/.init_b_cache
623         cleanup_and_exit 0
624     fi
625
626     #
627     # copy the QEMU emulator
628     #
629     if check_use_emulator; then
630         copy_qemu
631         if [ -z "$PREPARE_VM" ]; then
632             if ! check_binfmt_registered; then
633                 echo "registering binfmt handlers"
634                 if [ -x "$BUILD_DIR/initvm" -a -e "$BUILD_DIR/qemu-reg" ]; then
635                     $BUILD_DIR/initvm
636                 elif [ -x /usr/sbin/qemu-binfmt-conf.sh ]; then
637                    /usr/sbin/qemu-binfmt-conf.sh
638                 else
639                     echo "Warning: could not register binfmt handlers"
640                 fi
641             fi
642             if [ -n "$PREPARE_VM" ]; then
643                 echo 0 > /proc/sys/vm/mmap_min_addr
644             fi
645             read mmap_min_addr < /proc/sys/vm/mmap_min_addr
646             if [ "$mmap_min_addr" != 0 ]; then
647                 echo "Warning: mmap_min_addr is != 0. If programs fail at mmap this could be the reason"
648             fi
649         fi
650     fi
651
652     PACKAGES_TO_INSTALL=
653     PACKAGES_TO_PREINSTALL=
654     PACKAGES_TO_RUNSCRIPTS=
655     PACKAGES_TO_VMINSTALL=
656     PACKAGES_TO_CBPREINSTALL=
657     PACKAGES_TO_CBINSTALL=
658     RUNSCRIPTS_SEEN=
659     GUESSED_DIST=unknown
660     mkdir -p $BUILD_ROOT/.init_b_cache/rpms
661     while read PKG SRC ; do
662         if test "$PKG" = "preinstall:" ; then
663             PACKAGES_TO_PREINSTALL=$SRC
664             continue
665         fi
666         if test "$PKG" = "vminstall:" ; then
667             PACKAGES_TO_VMINSTALL=$SRC
668             continue
669         fi
670         # these additional preinstall / install values are only set for 
671         # emulated "CrossBuild" setups - thus CB(pre)install
672         if test "$PKG" = "cbpreinstall:" ; then
673             if is_emulator_arch ; then
674                 PACKAGES_TO_CBPREINSTALL=$SRC
675             fi
676             continue
677         fi
678         if test "$PKG" = "cbinstall:" ; then
679             if is_emulator_arch ; then
680                 PACKAGES_TO_CBINSTALL=$SRC
681             fi
682             continue
683         fi
684         if test "$PKG" = "runscripts:" ; then
685             RUNSCRIPTS_SEEN=true
686             PACKAGES_TO_RUNSCRIPTS=$SRC
687             continue
688         fi
689         if test "$PKG" = "dist:" ; then
690             GUESSED_DIST=$SRC
691             continue
692         fi
693         if test "$PKG" = "rpmid:" ; then
694             echo "${SRC#*:}" > $BUILD_ROOT/.init_b_cache/rpms/${SRC%%:*}.id
695             continue
696         fi
697         if test "$PKG" != "${PKG%:}"; then
698             echo "Warning: ignoring unsupported tag '$PKG'" >&2
699             continue
700         fi
701         if [ "${SRC#/}" = "$SRC" ]; then
702             url="$SRC"
703             case "$url" in
704                 zypp://* | http://* | https://* | ftp://* | ftps://*)
705                     downloadpkg "$url"
706                     ;;
707                 *)
708                     echo "unsupported url for '$PKG': $url" >&2
709                     cleanup_and_exit 1
710                     ;;
711             esac
712             # downloadpkg modified $SRC, so it has a right name for use
713         fi
714         SRCSUF=${SRC/%.pkg.tar.?z/.arch}
715         ln -s "$SRC" "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRCSUF##*.}"
716         PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL $PKG"
717     done < $RPMLIST
718
719     test -n "$PACKAGES_TO_CBPREINSTALL" && echo "cbpreinstall: $PACKAGES_TO_CBPREINSTALL"
720     test -n "$PACKAGES_TO_CBINSTALL" && echo "cbinstall   : $PACKAGES_TO_CBINSTALL"
721     # compatibility...
722     test -z "$RUNSCRIPTS_SEEN" && PACKAGES_TO_RUNSCRIPTS="$PACKAGES_TO_PREINSTALL"
723
724     echo "$GUESSED_DIST" > $BUILD_ROOT/.guessed_dist
725     test -n "$BUILD_DIST" || BUILD_DIST="$GUESSED_DIST"
726     DIST_TYPE=`gettype --dist "$BUILD_DIST" --configdir "$BUILD_DIR/configs" --archpath "$BUILD_ARCH"`
727     case "$DIST_TYPE" in
728         spec)  PSUF=rpm ;;
729         dsc)   PSUF=deb ;;
730         arch)  PSUF=arch ;;
731         *)
732             # auto detect from packages
733             PSUF=rpm
734             test -L $BUILD_ROOT/.init_b_cache/rpms/dpkg.deb && PSUF=deb
735             test -L $BUILD_ROOT/.init_b_cache/rpms/pacman.arch && PSUF=arch
736             ;;
737     esac
738 fi
739
740 #
741 # now test if there is already a build dir.
742 #
743 if test ! -f $BUILD_ROOT/var/lib/rpm/packages.rpm -a ! -f $BUILD_ROOT/var/lib/rpm/Packages -a ! -e $BUILD_ROOT/.build/init_buildsystem.data ; then
744     mkdir -p $BUILD_ROOT/var/lib/rpm || cleanup_and_exit 1
745     mkdir -p $BUILD_ROOT/usr/src/packages/SOURCES || cleanup_and_exit 1
746     mkdir -p $BUILD_ROOT/etc || cleanup_and_exit 1
747     mkdir -p $BUILD_ROOT/proc || cleanup_and_exit 1
748     test -f $BUILD_ROOT/etc/HOSTNAME || hostname -f > $BUILD_ROOT/etc/HOSTNAME
749     if test $PSUF = deb ; then
750         mkdir -p $BUILD_ROOT/var/lib/dpkg
751         mkdir -p $BUILD_ROOT/var/log
752         mkdir -p $BUILD_ROOT/etc/default
753         :> $BUILD_ROOT/var/lib/dpkg/status
754         :> $BUILD_ROOT/var/lib/dpkg/available
755         :> $BUILD_ROOT/var/log/dpkg.log
756         :> $BUILD_ROOT/etc/ld.so.conf
757         :> $BUILD_ROOT/etc/default/rcS
758     fi
759     for PKG in $PACKAGES_TO_RUNSCRIPTS ; do
760         : > $BUILD_ROOT/.init_b_cache/scripts/$PKG.run
761     done
762     progress_setup PACKAGES_TO_PREINSTALL
763     for PKG in $PACKAGES_TO_PREINSTALL ; do
764         progress_step PACKAGES_TO_PREINSTALL
765         preinstall ${PKG##*/}
766     done
767     if test -n "$PREPARE_VM" ; then
768         progress_setup PACKAGES_TO_VMINSTALL
769         for PKG in $PACKAGES_TO_VMINSTALL ; do
770             progress_step PACKAGES_TO_VMINSTALL
771             preinstall ${PKG##*/}
772         done
773     fi
774     # add cbpreinstall if cross HOST != TARGET
775     progress_setup PACKAGES_TO_CBPREINSTALL
776     for PKG in $PACKAGES_TO_CBPREINSTALL ; do
777         progress_step PACKAGES_TO_CBPREINSTALL
778         preinstall ${PKG##*/}
779     done
780     if [ -w /root ]; then
781             test -c $BUILD_ROOT/dev/null || create_devs
782     fi
783     test -e $BUILD_ROOT/etc/fstab || touch $BUILD_ROOT/etc/fstab
784     test ! -e $BUILD_ROOT/etc/ld.so.conf -a -e $BUILD_ROOT/etc/ld.so.conf.in && cp $BUILD_ROOT/etc/ld.so.conf.in $BUILD_ROOT/etc/ld.so.conf
785     if test -z "$PREPARE_VM" ; then
786         run_pkg_scripts
787         init_db
788         touch $BUILD_ROOT/.init_b_cache/preinstall_finished
789     fi
790 fi
791
792 if test -n "$PREPARE_VM" ; then
793     echo "copying packages..."
794     for PKG in $PACKAGES_TO_INSTALL ; do
795         rm -f $BUILD_ROOT/.init_b_cache/$PKG.$PSUF
796         cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF $BUILD_ROOT/.init_b_cache/$PKG.$PSUF || cleanup_and_exit 1
797         ln -s -f ../$PKG.$PSUF $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF
798         check_exit
799     done
800     # alreadyinstalled check will not work, but we have to live with
801     # that...
802     echo -n 'reordering...'
803     PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
804     echo 'done'
805     Q="'\''"
806     echo "PACKAGES_TO_INSTALL='${PACKAGES_TO_INSTALL//"'"/$Q}'" > $BUILD_ROOT/.build/init_buildsystem.data
807     echo "PACKAGES_TO_RUNSCRIPTS='${PACKAGES_TO_RUNSCRIPTS//"'"/$Q}'" >> $BUILD_ROOT/.build/init_buildsystem.data
808     # needed for continuation in xen/kvm with rpm-x86
809     echo "PACKAGES_TO_PREINSTALL='${PACKAGES_TO_PREINSTALL//"'"/$Q}'" >> $BUILD_ROOT/.build/init_buildsystem.data
810     echo "PACKAGES_TO_CBPREINSTALL='${PACKAGES_TO_CBPREINSTALL//"'"/$Q}'" >> $BUILD_ROOT/.build/init_buildsystem.data
811     echo "PSUF='$PSUF'" >> $BUILD_ROOT/.build/init_buildsystem.data
812     rm -f $BUILD_IS_RUNNING
813     cleanup_and_exit 0
814 fi
815
816 mkdir -p $BUILD_ROOT/proc
817 mkdir -p $BUILD_ROOT/dev/pts
818 mount -n -tproc none $BUILD_ROOT/proc 2>/dev/null || true
819 mount -n -tdevpts none $BUILD_ROOT/dev/pts 2>/dev/null || true
820
821 #
822 # create .build.binaries directory if requested
823 #
824 rm -rf $BUILD_ROOT/.build.binaries
825 if test -n "$CREATE_BUILD_BINARIES" ; then
826     echo "creating .build.binaries directory..."
827     mkdir -p "$BUILD_ROOT/.build.binaries"
828     for PKG in $PACKAGES_TO_INSTALL ; do
829         test -L "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" || continue
830         LPKG=`readlink -f "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF"`
831         ln "$LPKG" "$BUILD_ROOT/.build.binaries/$PKG.$PSUF" 2>/dev/null
832         test -f "$BUILD_ROOT/.build.binaries/$PKG.$PSUF" && continue
833         cp "$LPKG" "$BUILD_ROOT/.build.binaries/$PKG.$PSUF"
834         check_exit
835     done
836 fi
837
838 #
839 # get list and ids of already installed rpms
840 #
841 mkdir -p $BUILD_ROOT/.init_b_cache/alreadyinstalled
842 if test -f $BUILD_ROOT/var/lib/rpm/packages.rpm -o -f $BUILD_ROOT/var/lib/rpm/Packages ; then
843     chroot $BUILD_ROOT rpm -qa --qf "%{NAME} $RPMIDFMT" | (
844         while read pp ii; do
845             echo "$ii" > "$BUILD_ROOT/.init_b_cache/alreadyinstalled/$pp"
846         done
847     )
848 fi
849
850 #
851 # reorder packages (already done in vm continuation)
852 #
853 if ! test -e $BUILD_ROOT/.build/init_buildsystem.data ; then
854     echo -n 'reordering...'
855     PACKAGES_TO_INSTALL_FIRST=`reorder $PACKAGES_TO_INSTALL_FIRST`
856     PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
857     echo 'done'
858 fi
859
860 rpm_e()
861 {
862     chroot $BUILD_ROOT rpm --nodeps -e $PKG 2>&1 | \
863     while read line; do
864         case "$line" in
865
866             r*failed:\ No\ such\ file\ or\ directory) ;;
867             error:\ failed\ to\ stat\ *:\ No\ such\ file\ or\ directory) ;;
868             error:\ *scriptlet\ failed*)
869                 echo "$line"
870                 echo "re-try deleting $PKG using --noscripts"
871                 chroot $BUILD_ROOT rpm --nodeps --noscripts -e $PKG || true
872             ;;
873             *) echo "$line" ;;
874         esac
875     done
876 }
877
878 #
879 # delete all packages we don't want
880 #
881 mkdir -p $BUILD_ROOT/.init_b_cache/todelete
882 for PKG in $BUILD_ROOT/.init_b_cache/alreadyinstalled/* ; do
883     PKG=${PKG##*/}
884     test "$PKG" = "*" && continue
885     ln $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG $BUILD_ROOT/.init_b_cache/todelete/$PKG
886 done
887 for PKG in $PACKAGES_TO_INSTALL_FIRST $PACKAGES_TO_INSTALL $PACKAGES_TO_CBINSTALL ; do
888     rm -f $BUILD_ROOT/.init_b_cache/todelete/$PKG
889 done
890 for PKG in $BUILD_ROOT/.init_b_cache/todelete/* ; do
891     PKG=${PKG##*/}
892     test "$PKG" = "*" && continue
893     echo "deleting $PKG"
894     rpm_e "$PKG"
895     check_exit
896 done
897 rm -rf $BUILD_ROOT/.init_b_cache/todelete
898
899 rm -rf $BUILD_ROOT/.init_b_cache/preinstalls
900 mkdir -p $BUILD_ROOT/.init_b_cache/preinstalls
901 for PKG in $PACKAGES_TO_PREINSTALL $PACKAGES_TO_CBPREINSTALL; do
902     touch "$BUILD_ROOT/.init_b_cache/preinstalls/$PKG"
903 done
904
905 rm -rf $BUILD_ROOT/installed-pkg
906 mkdir -p $BUILD_ROOT/installed-pkg
907
908 RPMCHECKOPTS=
909 RPMCHECKOPTS_HOST=
910 # on Fedora 10 rpmbuild is in a separate package so we need something else to
911 # detect rpm4
912 test -x $BUILD_ROOT/usr/bin/rpmsign && RPMCHECKOPTS="--nodigest --nosignature"
913 test -x /usr/bin/rpmsign && RPMCHECKOPTS_HOST="--nodigest --nosignature"
914
915 MAIN_LIST="$PACKAGES_TO_INSTALL_FIRST $PACKAGES_TO_INSTALL $PACKAGES_TO_CBINSTALL"
916 progress_setup MAIN_LIST
917 for PKG in $PACKAGES_TO_INSTALL_FIRST RUN_LDCONFIG $PACKAGES_TO_INSTALL $PACKAGES_TO_CBINSTALL; do
918     case $PKG in
919       RUN_LDCONFIG)
920         test -x $BUILD_ROOT/sbin/ldconfig && chroot $BUILD_ROOT /sbin/ldconfig 2>&1
921         continue
922       ;;
923     esac
924     progress_step MAIN_LIST
925
926     test -f $BUILD_ROOT/installed-pkg/$PKG && continue
927
928     if test $PSUF = deb ; then
929         # debian world, install deb files
930         test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb || continue
931         if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.deb" -ef "$BUILD_ROOT/.init_b_cache/$PKG.deb" ; then
932             rm -f $BUILD_ROOT/.init_b_cache/$PKG.deb
933             cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb $BUILD_ROOT/.init_b_cache/$PKG.deb || cleanup_and_exit 1
934         fi
935         PKGID=`readlink $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb`
936         PKGID="${PKGID##*/}"
937         PKGID="${PKGID%.deb}"
938         echo "installing ${PKGID%_*}"
939         ( chroot $BUILD_ROOT dpkg --install --force all .init_b_cache/$PKG.deb 2>&1 || touch $BUILD_ROOT/exit ) | \
940             perl -ne '$|=1;/^(Configuration file|Installing new config file|Selecting previously deselected|\(Reading database|Unpacking |Setting up|Creating config file|Preparing to replace dpkg)/||/^$/||print'
941         check_exit
942         echo "$PKGID debian" > $BUILD_ROOT/installed-pkg/$PKG
943         # ugly workaround for upstart system. some packages (procps) try
944         # to start a service in their configure phase. As we don't have
945         # a running upstart, we just link the start binary to /bin/true
946         if test -e "$BUILD_ROOT/sbin/start"; then
947             if test "$BUILD_ROOT/sbin/start" -ef "$BUILD_ROOT/sbin/initctl" ; then
948                 echo "linking /sbin/start to /bin/true"
949                 mv "$BUILD_ROOT/sbin/start" "$BUILD_ROOT/sbin/start.disabled"
950                 ln -s "/bin/true" "$BUILD_ROOT/sbin/start"
951             fi
952         fi
953         # another workaround, see bug bnc#733699
954         rm -f "$BUILD_ROOT/var/run/init.upgraded"
955         continue
956     fi
957
958     if test $PSUF = arch ; then
959         test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF || continue
960         if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" -ef "$BUILD_ROOT/.init_b_cache/$PKG.$PSUF" ; then 
961             rm -f $BUILD_ROOT/.init_b_cache/$PKG.$PSUF
962             cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF $BUILD_ROOT/.init_b_cache/$PKG.$PSUF || cleanup_and_exit 1
963         fi
964         PKGID=`readlink $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF`
965         PKGID="${PKGID##*/}"
966         PKGID="${PKGID/%.pkg.tar.?z/.arch}"
967         PKGID="${PKGID%.$PSUF}"
968         echo "installing ${PKGID%_*}"
969         # -d -d disables deps checking
970         ( chroot $BUILD_ROOT pacman -Uf -d -d --noconfirm .init_b_cache/$PKG.$PSUF 2>&1 || touch $BUILD_ROOT/exit ) | \
971             perl -ne '$|=1;/^(warning: could not get filesystem information for |loading packages|looking for inter-conflicts|Targets |Total Installed Size: |Net Upgrade Size: |Proceed with installation|checking package integrity|loading package files|checking available disk space|installing |upgrading |warning:.*is up to date -- reinstalling|Optional dependencies for|    )/||/^$/||print'
972         check_exit
973         echo "$PKGID $PSUF" > $BUILD_ROOT/installed-pkg/$PKG
974         continue
975     fi
976
977     test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm || continue
978
979     if test -f $BUILD_ROOT/.init_b_cache/rpms/$PKG.id -a -f $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG ; then
980         read PKGID < $BUILD_ROOT/.init_b_cache/rpms/$PKG.id
981         read OLDPKGID < $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG
982         if test "$PKGID" = "$OLDPKGID" ; then
983             #echo "keeping ${PKGID%% *}"
984             echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
985             continue
986         fi
987     fi
988
989     PKGID=`rpm -qp --qf "$RPMIDFMT" $RPMCHECKOPTS_HOST $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm`
990
991     if test -f $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG ; then
992         read OLDPKGID < $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG
993         if test "$PKGID" != "$OLDPKGID" ; then
994             echo deleting unwanted ${OLDPKGID%% *}
995             rpm_e "$PKG"
996         elif test "$VERIFY_BUILD_SYSTEM" = true ; then
997             chroot $BUILD_ROOT rpm --verify $PKG 2>&1 | tee $TMPFILE
998             if grep ^missing $TMPFILE > /dev/null ; then
999                 echo deleting incomplete ${PKGID%% *}
1000                 rpm_e "$PKG"
1001             else
1002                 #echo "keeping ${PKGID%% *}"
1003                 echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
1004                 continue
1005             fi
1006         else
1007             #echo "keeping ${PKGID%% *}"
1008             echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
1009             continue
1010         fi
1011         if test -e "$BUILD_ROOT/.init_b_cache/preinstalls/$PKG" ; then
1012             preinstall "$PKG"
1013             # call for rpm-4.x and not rpm-devel
1014             test -z "${PKG##rpm-[0-9]*}" && chroot $BUILD_ROOT rpm --rebuilddb
1015             # also exec for exchanged rpm !  naming is rpm-x86-<target>-<ver>
1016             test -z "${PKG##rpm-x86-*[0-9]*}" && chroot $BUILD_ROOT rpm --rebuilddb
1017         fi
1018     fi
1019     export ADDITIONAL_PARAMS=
1020     if test "$USE_FORCE" = true ; then
1021         export ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --force"
1022     fi
1023     # work around for cross-build installs, we must not overwrite the running rpm
1024     if test "$PKG" = rpm ; then
1025         for i in $BUILD_ROOT/.init_b_cache/preinstalls/rpm-x86-* ; do
1026             test -e "$i" && ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --justdb"
1027         done
1028     fi
1029     echo "installing ${PKGID%% *}"
1030     if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" -ef "$BUILD_ROOT/.init_b_cache/$PKG.rpm" ; then
1031         rm -f $BUILD_ROOT/.init_b_cache/$PKG.rpm
1032         cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm $BUILD_ROOT/.init_b_cache/$PKG.rpm || cleanup_and_exit 1
1033     fi
1034     ( chroot $BUILD_ROOT rpm --ignorearch --nodeps -U --oldpackage --ignoresize $RPMCHECKOPTS \
1035                 $ADDITIONAL_PARAMS .init_b_cache/$PKG.rpm 2>&1 || \
1036           touch $BUILD_ROOT/exit ) | \
1037               grep -v "^warning:.*saved as.*rpmorig$"
1038     # delete link so package is only installed once
1039     rm -f $BUILD_ROOT/.init_b_cache/$PKG.rpm
1040     check_exit
1041     echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
1042
1043 done
1044
1045 if test $PSUF = deb ; then
1046     echo "configure all installed packages..."
1047     # configure all packages after complete installation, not for each package like rpm does
1048     # We need to run this twice, because of cyclic dependencies as it does not succeed on most
1049     # debian based distros in the first attempt.
1050     if ! chroot $BUILD_ROOT dpkg --configure --pending  2>&1; then
1051        echo "first configure attempt failed, trying again..."
1052        chroot $BUILD_ROOT dpkg --configure --pending  2>&1 || touch $BUILD_ROOT/exit
1053     fi
1054 fi
1055
1056 # devices can vanish if devs got uninstalled
1057 test -c $BUILD_ROOT/dev/null || create_devs
1058
1059 cd $BUILD_ROOT || cleanup_and_exit 1
1060
1061 #
1062 # setup /etc/mtab
1063 #
1064 rm -f $BUILD_ROOT/etc/mtab
1065 cp /proc/mounts $BUILD_ROOT/etc/mtab
1066 chmod 644 $BUILD_ROOT/etc/mtab
1067
1068 #
1069 # to be sure, path is set correctly, we have to source /etc/profile before
1070 # starting rpm.
1071 #
1072 # XXX
1073 #rm -f $BUILD_ROOT/bin/rpm.sh
1074 #cp $BUILD_LIBDIR/lib/rpm.sh $BUILD_ROOT/bin/rpm.sh
1075 #chmod 755 $BUILD_ROOT/bin/rpm.sh
1076 #test -f $BUILD_ROOT/bin/rpm -a ! -L $BUILD_ROOT/bin/rpm && \
1077 #    mv $BUILD_ROOT/bin/rpm $BUILD_ROOT/bin/rpm.bin
1078 #rm -f $BUILD_ROOT/bin/rpm
1079 #ln -s rpm.sh $BUILD_ROOT/bin/rpm
1080
1081 #
1082 # some packages use uname -r to decide which kernel is used to build for.
1083 # this does not work in autobuild always.  Here is a wrapper script, that
1084 # gets Version from kernel sources.
1085 #
1086 # XXX
1087 #rm -f $BUILD_ROOT/bin/uname.sh
1088 #cp -v $BUILD_LIBDIR/lib/uname.sh $BUILD_ROOT/bin/uname.sh
1089 #chmod 755 $BUILD_ROOT/bin/uname.sh
1090 #test -f $BUILD_ROOT/bin/uname -a ! -L $BUILD_ROOT/bin/uname && \
1091 #    mv $BUILD_ROOT/bin/uname $BUILD_ROOT/bin/uname.bin
1092 #rm -f $BUILD_ROOT/bin/uname
1093 #ln -s uname.sh $BUILD_ROOT/bin/uname
1094
1095 #
1096 # some distributions have a /etc/rpmrc or /etc/rpm/macros and some not.
1097 # make sure, that it is setup correctly.
1098 #
1099 # XXX
1100 #rm -f $BUILD_ROOT/etc/rpmrc
1101 #if test -e $BUILD_LIBDIR/lib/rpmrc.$BUILD_BASENAME ; then
1102 #    cp -v $BUILD_LIBDIR/lib/rpmrc.$BUILD_BASENAME $BUILD_ROOT/etc/rpmrc
1103 #elif test -e $BUILD_LIBDIR/lib/rpmrc ; then
1104 #    cp -v $BUILD_LIBDIR/lib/rpmrc $BUILD_ROOT/etc/rpmrc
1105 #fi
1106
1107 # XXX
1108 #rm -f $BUILD_ROOT/etc/rpm/macros $BUILD_ROOT/etc/rpm/suse_macros
1109 #mkdir -p $BUILD_ROOT/etc/rpm
1110 #if test -e $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME ; then
1111 #    cp -v $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME $BUILD_ROOT/etc/rpm/macros
1112 #    cp -v $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME $BUILD_ROOT/etc/rpm/suse_macros
1113 #elif test -e $BUILD_LIBDIR/lib/macros ; then
1114 #    cp -v $BUILD_LIBDIR/lib/macros $BUILD_ROOT/etc/rpm/macros
1115 #    cp -v $BUILD_LIBDIR/lib/macros $BUILD_ROOT/etc/rpm/suse_macros
1116 #fi
1117
1118 #
1119 # make sure, that our nis is not present in the chroot system
1120 #
1121 test -e $BUILD_ROOT/etc/nsswitch.conf && {
1122     echo removing nis flags from $BUILD_ROOT/etc/nsswitch.conf...
1123     cat $BUILD_ROOT/etc/nsswitch.conf | sed -e"s:nis::g" > \
1124         $BUILD_ROOT/etc/nsswitch.conf.tmp
1125     mv $BUILD_ROOT/etc/nsswitch.conf.tmp $BUILD_ROOT/etc/nsswitch.conf
1126 }
1127
1128 #
1129 # creating some default directories
1130 for DIR in /usr/share/doc/packages \
1131            /usr/X11R6/include/X11/pixmaps \
1132            /usr/X11R6/include/X11/bitmaps ; do
1133     mkdir -p $BUILD_ROOT/$DIR
1134 done
1135
1136 for FILE in /var/run/utmp /var/log/wtmp /etc/fstab ; do
1137     mkdir -p $BUILD_ROOT/${FILE%/*}
1138     touch $BUILD_ROOT/$FILE
1139 done
1140
1141 echo now finalizing build dir...
1142 CHROOT_RETURN="`chroot $BUILD_ROOT /sbin/ldconfig 2>&1`"
1143 case "$CHROOT_RETURN" in
1144     *warning:*)
1145       chroot $BUILD_ROOT /sbin/ldconfig
1146       echo
1147       echo chroot $BUILD_ROOT /sbin/ldconfig
1148       echo
1149       echo "$CHROOT_RETURN"
1150       echo
1151       echo "Problem with ldconfig.  It's better to reinit the build system..."
1152       echo
1153       cleanup_and_exit 1
1154     ;;
1155 esac
1156 test -x $BUILD_ROOT/usr/sbin/Check && chroot $BUILD_ROOT /usr/sbin/Check
1157
1158 mkdir -p $BUILD_ROOT/var/adm/packages
1159 touch $BUILD_ROOT/var/adm/packages
1160 if test -x $BUILD_ROOT/sbin/SuSEconfig ; then
1161     if grep norestarts $BUILD_ROOT/sbin/SuSEconfig > /dev/null ; then
1162         chroot $BUILD_ROOT /sbin/SuSEconfig --norestarts --force
1163     else
1164         chroot $BUILD_ROOT /sbin/SuSEconfig --force
1165     fi
1166 fi
1167
1168 if test -x $BUILD_ROOT/usr/X11R6/bin/switch2mesasoft ; then
1169     chroot $BUILD_ROOT /usr/X11R6/bin/switch2mesasoft
1170 fi
1171
1172 for PROG in /usr/bin/TeX/texhash /usr/bin/texhash ; do
1173     test -x $BUILD_ROOT/$PROG && \
1174         chroot $BUILD_ROOT bash -c ". /etc/profile ; $PROG"
1175 done
1176
1177 if test -e $BUILD_ROOT/usr/share/zoneinfo/UTC ; then
1178     chroot $BUILD_ROOT zic -l UTC
1179 fi
1180
1181 test -e $BUILD_ROOT/.build/init_buildsystem.data || HOST=`hostname`
1182 test -e $BUILD_ROOT/etc/hosts || echo "127.0.0.1 localhost" > $BUILD_ROOT/etc/hosts
1183 if ! grep -F "127.0.0.1 $HOST" $BUILD_ROOT/etc/hosts > /dev/null ; then
1184     # this makes a reverse lookup on 127.0.0.1 return the host name,
1185     # which is bad, but 127.0.0.2 does not work on all unix systems
1186     echo "127.0.0.1 $HOST" > $BUILD_ROOT/etc/hosts.new
1187     test -f $BUILD_ROOT/etc/hosts && cat $BUILD_ROOT/etc/hosts >> $BUILD_ROOT/etc/hosts.new
1188     mv $BUILD_ROOT/etc/hosts.new $BUILD_ROOT/etc/hosts
1189 fi
1190
1191 if test -x $BUILD_ROOT/bin/rpm -a ! -f $BUILD_ROOT/var/lib/rpm/packages.rpm -a ! -f $BUILD_ROOT/var/lib/rpm/Packages ; then
1192     echo "initializing rpm db..."
1193     chroot $BUILD_ROOT rpm --initdb || cleanup_and_exit 1
1194     # create provides index
1195     chroot $BUILD_ROOT rpm -q --whatprovides rpm >/dev/null 2>&1
1196 fi
1197
1198 # create modules.dep in kvm/xen
1199 # This can not work, until we use the native repository kernel
1200 #if [ $BUILD_ROOT = "/" -a -x /sbin/depmod ]; then
1201 #  /sbin/depmod -a
1202 #fi
1203
1204 rm -f $BUILD_ROOT/.rpmmacros $BUILD_ROOT/root/.rpmmacros
1205 rm -rf $BUILD_ROOT/.init_b_cache
1206 rm -f $BUILD_IS_RUNNING
1207 rm -f $TMPFILE
1208
1209 cleanup_and_exit 0