Revert "[build] Add cb(pre)ignore capabilities to build. Removes dependencies from...
[opensuse: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     CPIO="cpio --extract --unconditional --preserve-modification-time --make-directories --no-absolute-filenames --quiet"
133     if test -e "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" ; then
134         PAYLOADDECOMPRESS=cat
135         case `rpm -qp --nodigest --nosignature --qf "%{PAYLOADCOMPRESSOR}\n" "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm"` in
136             lzma) rpm --showrc | grep PayloadIsLzma > /dev/null || PAYLOADDECOMPRESS="lzma -d" ;;
137             xz) rpm --showrc | grep PayloadIsXz > /dev/null || PAYLOADDECOMPRESS="xz -d" ;;
138         esac
139         if test "$PAYLOADDECOMPRESS" = "lzma -d" ; then
140             if ! lzma </dev/null >/dev/null 2>&1 ; then
141                 test -f "$BUILD_DIR/lzmadec.sh" && PAYLOADDECOMPRESS="bash $BUILD_DIR/lzmadec.sh"
142             fi
143         fi
144         if test "$PAYLOADDECOMPRESS" = "xz -d" ; then
145             if ! xz </dev/null >/dev/null 2>&1 ; then
146                 test -f "$BUILD_DIR/xzdec.sh" && PAYLOADDECOMPRESS="bash $BUILD_DIR/xzdec.sh"
147             fi
148         fi
149         if test "$PAYLOADDECOMPRESS" = cat ; then
150             rpm2cpio "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" | $CPIO
151         else
152             rpm2cpio "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" | $PAYLOADDECOMPRESS | $CPIO
153         fi
154         if test -e .init_b_cache/scripts/$1.run ; then
155             rpm -qp --nodigest --nosignature --qf "%{PREIN}" "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" > .init_b_cache/scripts/$1.pre
156             rpm -qp --nodigest --nosignature --qf "%{POSTIN}" "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" > .init_b_cache/scripts/$1.post
157             echo -n '(none)' > .init_b_cache/scripts/.none
158             cmp -s .init_b_cache/scripts/$1.pre .init_b_cache/scripts/.none && rm -f .init_b_cache/scripts/$1.pre
159             cmp -s .init_b_cache/scripts/$1.post .init_b_cache/scripts/.none && rm -f .init_b_cache/scripts/$1.post
160             rm -f .init_b_cache/scripts/.none
161         fi
162     elif test -e "$BUILD_ROOT/.init_b_cache/rpms/$1.deb" ; then
163         ar x "$BUILD_ROOT/.init_b_cache/rpms/$1.deb" control.tar.gz data.tar.gz
164         mkdir -p .init_b_cache/scripts/control
165         tar -C .init_b_cache/scripts/control -xzf control.tar.gz
166         tar xzf data.tar.gz
167         if test -e .init_b_cache/scripts/$1.run ; then
168             test -e .init_b_cache/scripts/control/preinst && mv .init_b_cache/scripts/control/preinst .init_b_cache/scripts/$1.pre
169             test -e .init_b_cache/scripts/control/postinst && mv .init_b_cache/scripts/control/postinst .init_b_cache/scripts/$1.post
170         fi
171         rm -rf .init_b_cache/scripts/control control.tar.gz data.tar.gz
172     else
173         echo "warning: package $1 does not exist"
174     fi
175 }
176
177 run_pkg_scripts()
178 {
179     chroot $BUILD_ROOT /sbin/ldconfig 2>/dev/null
180     for PKG in $PACKAGES_TO_RUNSCRIPTS ; do
181         if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre" ; then
182             echo "running $PKG preinstall script"
183             if test -e "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" ; then
184                 chroot $BUILD_ROOT sh ".init_b_cache/scripts/$PKG.pre" 0
185             else
186                 chroot $BUILD_ROOT ".init_b_cache/scripts/$PKG.pre" install < /dev/null
187             fi
188             rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre"
189         fi
190         if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post" ; then
191             echo "running $PKG postinstall script"
192             if test -e "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" ; then
193                 chroot $BUILD_ROOT sh ".init_b_cache/scripts/$PKG.post" 1
194             else
195                 chroot $BUILD_ROOT ".init_b_cache/scripts/$PKG.post" configure '' < /dev/null
196             fi
197             rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post"
198         fi
199         check_exit
200     done
201 }
202
203 init_db()
204 {
205     if test $PSUF = rpm ; then
206         echo initializing rpm db...
207         chroot $BUILD_ROOT rpm --initdb || cleanup_and_exit 1
208         # hack: add nofsync to db config to speed up install
209         mkdir -p $BUILD_ROOT/root
210         echo '%__dbi_perms perms=0644 nofsync' > $BUILD_ROOT/.rpmmacros
211         echo '%__dbi_perms perms=0644 nofsync' > $BUILD_ROOT/root/.rpmmacros
212     else
213         # force dpkg into database to make epoch test work
214         if ! test "$BUILD_ROOT/.init_b_cache/rpms/dpkg.deb" -ef "$BUILD_ROOT/.init_b_cache/dpkg.deb" ; then
215             rm -f $BUILD_ROOT/.init_b_cache/dpkg.deb
216             cp $BUILD_ROOT/.init_b_cache/rpms/dpkg.deb $BUILD_ROOT/.init_b_cache/dpkg.deb || cleanup_and_exit 1
217         fi
218         chroot $BUILD_ROOT dpkg -i --force all .init_b_cache/dpkg.deb >/dev/null 2>&1
219     fi
220 }
221
222 reorder()
223 {
224     test -z "$*" && return
225     rm -f $BUILD_ROOT/.init_b_cache/order.manifest
226     for PKG in "$@" ; do
227         echo "$PKG" >> $BUILD_ROOT/.init_b_cache/order.manifest
228     done
229     $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
230     rm -f $BUILD_ROOT/.init_b_cache/order.manifest
231 }
232
233 create_devs()
234 {
235     local com file mode arg
236
237     mkdir -m 755 -p $BUILD_ROOT/dev/pts
238     test -f $BUILD_ROOT/dev/shm && rm -f $BUILD_ROOT/dev/shm
239     mkdir -m 755 -p $BUILD_ROOT/dev/shm
240     while read com file mode arg ; do
241         rm -f $BUILD_ROOT/dev/$file
242         if test $com = ln ; then
243             ln -s $arg $BUILD_ROOT/dev/$file
244             continue
245         fi
246         $com -m $mode $BUILD_ROOT/dev/$file $arg
247     done << DEVLIST
248         mknod null    666 c 1 3
249         mknod zero    666 c 1 5
250         mknod full    622 c 1 7
251         mknod random  666 c 1 8
252         mknod urandom 644 c 1 9
253         mknod tty     666 c 5 0
254         mknod ptmx    666 c 5 2
255         mknod loop0   640 b 7 0
256         mknod loop1   640 b 7 1
257         mknod loop2   640 b 7 2
258         mknod loop3   640 b 7 3
259         ln    fd      777 /proc/self/fd
260         ln    stdin   777 fd/0
261         ln    stdout  777 fd/1
262         ln    stderr  777 fd/2
263 DEVLIST
264 }
265
266 # check whether the repo list contains a plain "zypp://". Add all
267 # enabled zypp repos in this case
268 maybe_add_all_zypp_repos()
269 {
270     local i j
271     r=()
272     for i in "${repos[@]}"; do
273         if [ "$i" = "zypp://" ]; then
274             while read j; do
275                 j="${j#/etc/zypp/repos.d/}"
276                 r=("${r[@]}" "zypp://${j%.repo}")
277             done < <(grep -l enabled=1 /etc/zypp/repos.d/*.repo)
278         else
279             r=("${r[@]}" "$i")
280         fi
281     done
282     repos=("${r[@]}")
283 }
284
285 validate_cache_file()
286 {
287     local findonly=''
288     maybe_add_all_zypp_repos
289     if ! test -f $CACHE_FILE || ! test -f $CACHE_FILE.id || \
290         test "${repos[*]} ${BUILD_RPMS//:/ /}" != "$(cat $CACHE_FILE.id 2>/dev/null)"; then
291         rm -f $CACHE_FILE.id
292     else
293         for SRC in "${repos[@]}" ${BUILD_RPMS//:/ /}; do
294             test -n "$SRC" || SRC=.
295             if [ "${SRC#zypp://}" != "$SRC" ]; then
296                 SRC="/var/cache/zypp/raw" # XXX can't use name here as we'd need to parse the file
297             fi
298             if test "$SRC" -nt $CACHE_FILE; then
299                 rm -f $CACHE_FILE.id
300                 break
301             fi
302         done
303     fi
304     if ! test -f $CACHE_FILE.id ; then
305         test -z "$LIST_STATE" && echo initializing $CACHE_FILE ...
306         for SRC in "${repos[@]}" -- ${BUILD_RPMS//:/ /}; do
307             if test "$SRC" = '--' ; then
308                 findonly=1
309                 continue
310             fi
311             test -z "$SRC" && SRC=`pwd`
312             if [ "${SRC#zypp://}" != "$SRC" ]; then
313                 set -- $BUILD_DIR/createrepomddeps "$SRC"
314             elif [ "${SRC#http://}" != "$SRC" -o "${SRC#https://}" != "$SRC" -o "${SRC#ftp://}" != "$SRC" -o "${SRC#ftps://}" != "$SRC" ]; then
315                 mkdir -p "$(getcachedir "$SRC")"
316                 set -- $BUILD_DIR/createrepomddeps --cachedir="$CACHE_DIR" "$SRC"
317             elif [ ! -e "$SRC" ]; then
318                 echo "*** $SRC does not exist" >&2
319                 cleanup_and_exit 1
320             elif [ -z "$findonly" -a -e "$SRC"/suse/setup/descr/packages -o -e "$SRC"/suse/setup/descr/packages.gz ]; then
321                 set -- $BUILD_DIR/createyastdeps "$SRC"
322             elif [ -z "$findonly" -a -e "$SRC"/repodata/repomd.xml ]; then
323                 set -- $BUILD_DIR/createrepomddeps "$SRC"
324             else
325                 set -- $BUILD_DIR/createrpmdeps "$SRC"
326             fi
327             echo "$@" >&2
328             "$@" || cleanup_and_exit 1
329         done > $CACHE_FILE.new
330
331         mv $CACHE_FILE.new $CACHE_FILE
332         echo "${repos[*]} ${BUILD_RPMS//:/ /}" > $CACHE_FILE.id
333     fi
334 }
335
336 #
337 # Detect cross-build (fixed code) and copy qemus for cross-build emulation mode
338 # And copy also a static host binary shell to run the init scripts in the target chroot
339 #
340 copy_qemu()
341 {
342     echo "copying qemu"
343     mkdir -p  $BUILD_ROOT/usr/bin $BUILD_ROOT/usr/sbin
344     install -m755 /usr/bin/qemu-* $BUILD_ROOT/usr/bin
345     install -m755 /usr/sbin/qemu-binfmt-conf.sh $BUILD_ROOT/usr/sbin
346     # To initially run the init script, there needs to be also a
347     # static bash to register qemu in this chroot and to start up
348     # /.build/build
349     if [ -n "$PREPARE_VM" ]; then
350         mkdir -p  $BUILD_ROOT/bin
351         install -m755 /bin/bash-static $BUILD_ROOT/bin
352         install -m755 /bin/mount-static $BUILD_ROOT/bin
353     fi
354 }
355
356 check_binfmt_registered()
357 {
358     local arch
359     for arch in arm $EMULATOR_ARCHS; do
360         if test -e /proc/sys/fs/binfmt_misc/$arch; then
361             return 0
362         fi
363     done
364     return 1
365 }
366
367 fail_exit()
368 {
369   cleanup_and_exit 1
370 }
371
372 # modify $SRC
373 downloadpkg()
374 {
375     local url="$1"
376     local cachedir
377
378     if [ "${url:0:7}" == "zypp://" ] ; then
379         cachedir="/var/cache/zypp/packages/"
380         SRC="$cachedir${url#zypp://}"
381         mkdir -p "${SRC%/*}" || cleanup_and_exit 1
382     elif [ "${url:0:7}" == "http://" -o "${url:0:8}" == "https://" -o "${url:0:6}" == "ftp://" -o "${url:0:7}" == "ftps://" ] ; then
383         cachedir="$(getcachedir "$url")"
384         local name="$(basename "$url")"
385         SRC="$cachedir/$name"
386     else
387         echo "Invalid url: $url"
388         cleanup_and_exit 1
389     fi
390
391     if [ ! -e "$SRC" ]; then
392         local destdir="$cachedir/tmp"
393         mkdir -p "$destdir"
394         echo "downloading $url ... ";
395         $BUILD_DIR/download "$destdir" "$url" || cleanup_and_exit 1
396         local destfile="$destdir/${url##*/}"
397         if [ ! -e "$destfile" ]; then
398             echo "expected $destfile after download but it's missing" >&2
399             cleanup_and_exit 1
400         fi
401         rpm -K "$destfile" > $destfile.v || { echo "rpm verify failed" >&2; rm -rf "$destdir"; cleanup_and_exit 1; }
402         if grep "NOT OK" $destfile.v; then
403             rm -rf "$destdir"
404             cleanup_and_exit 1
405         fi
406         rm -f "$destfile.v"
407         mv "$destfile" "$SRC" || cleanup_and_exit 1
408     fi
409 }
410
411 getcachedir()
412 {
413     url=$1
414     for repo in "${repos[@]}" ; do
415         if [ "${url:0:${#repo}}" == "$repo" ] ; then
416             read repoid dummy < <(echo -n "$repo" | md5sum)
417             echo "$CACHE_DIR/$repoid"
418             break
419         fi
420     done
421 }
422
423 get_pkg_filename()
424 {
425     local url="$1"
426     local name=$(basename $url)
427     local cachedir=$(getcachedir $url)
428     local destfile="$cachedir/$name"
429     echo $destfile
430 }
431
432 set_build_arch
433
434 trap fail_exit EXIT
435
436 if [ "$BUILD_ROOT" = / ]; then
437     read dummy dummy browner dummy < <(ls -ld /)
438 fi
439
440 if [ -n "$CLEAN_BUILD" ]; then
441     clean_build_root
442 fi
443
444 #
445 # now test if there was an incomplete run
446 #
447 if test -e $BUILD_IS_RUNNING ; then
448     echo It seems that there was an incomplete setup of $BUILD_ROOT.
449     echo To be sure, we will build it again completely...
450     umount -n $BUILD_ROOT/proc/sys/fs/binfmt_misc 2> /dev/null || true
451     umount -n $BUILD_ROOT/proc 2> /dev/null
452     umount -n $BUILD_ROOT/dev/pts 2> /dev/null
453     umount -n $BUILD_ROOT/mnt 2> /dev/null
454     echo "Your build system is broken!! Shall I execute"
455     echo
456     echo "    rm -rf -- $BUILD_ROOT/*"
457     echo
458     echo "y - yes, cleanup the build root"
459     echo "N - No, abort build (default on enter)"
460     echo "c - Continue anyway with this build root"
461     echo -n "[y/N/c] "
462     read ANSWER
463     case "$ANSWER" in
464         c|C) rm -f $BUILD_IS_RUNNING ;;
465         y|Y) clean_build_root ;;
466         *) cleanup_and_exit 1 ;;
467     esac
468 fi
469
470 #
471 # store that we start to build system
472 #
473 mkdir -p $BUILD_ROOT
474 touch $BUILD_IS_RUNNING
475
476 if test -n "$PREPARE_VM" ; then
477     rm -f $BUILD_ROOT/.build/init_buildsystem.data
478 fi
479 if test -e $BUILD_ROOT/.build/init_buildsystem.data ; then
480     # vm continuation
481     . $BUILD_ROOT/.build/init_buildsystem.data
482     if ! test -e $BUILD_ROOT/.init_b_cache/preinstall_finished ; then
483         # finish preinstall
484         run_pkg_scripts
485         init_db
486         touch $BUILD_ROOT/.init_b_cache/preinstall_finished
487     fi
488 else
489     #
490     # now make sure that all the packages are installed.
491     #
492     rm -rf $BUILD_ROOT/.init_b_cache
493     mkdir -p $BUILD_ROOT/.init_b_cache/scripts
494
495     if test -z "$RPMLIST" ; then
496         #
497         # create rpmdeps file
498         #
499         CACHE_FILE=$BUILD_ROOT/.srcfiles.cache
500         validate_cache_file
501
502         #
503         # select and expand packages
504         #
505         RPMLIST=$BUILD_ROOT/.init_b_cache/rpmlist
506         test -z "$LIST_STATE" && echo "expanding package dependencies..."
507         if ! $BUILD_DIR/expanddeps $USEUSEDFORBUILD "${definesnstuff[@]}" --dist "$BUILD_DIST" --depfile "$CACHE_FILE" --archpath "$BUILD_ARCH" --configdir $BUILD_DIR/configs "${PKGS[@]}" > $RPMLIST ; then
508             rm -f $BUILD_IS_RUNNING
509             cleanup_and_exit 1
510         fi
511     fi
512
513     if test -n "$LIST_STATE" ; then
514         rm -f $BUILD_IS_RUNNING
515         while read PKG SRC ; do
516            test "$PKG" = "preinstall:" && continue
517            test "$PKG" = "runscripts:" && continue
518            test "$PKG" = "cbpreinstall:" && continue
519            test "$PKG" = "vminstall:" && continue
520            test "$PKG" = "dist:" && continue
521            test "$PKG" = "rpmid:" && continue
522            echo "${SRC##*/}"
523         done < $BUILD_ROOT/.init_b_cache/rpmlist
524         rm -rf $BUILD_ROOT/.init_b_cache
525         cleanup_and_exit 0
526     fi
527
528     #
529     # copy the QEMU emulator
530     #
531     if check_use_emulator; then
532         copy_qemu
533         if [ -z "$PREPARE_VM" ]; then
534             if ! check_binfmt_registered; then
535                 echo "registering binfmt handlers"
536                 /usr/sbin/qemu-binfmt-conf.sh
537             fi
538             read mmap_min_addr < /proc/sys/vm/mmap_min_addr
539             if [ "$mmap_min_addr" != 0 ]; then
540                 echo "Warning: mmap_min_addr is != 0. If programs fail at mmap this could be the reason"
541             fi
542         fi
543     fi
544
545     PACKAGES_TO_INSTALL=
546     PACKAGES_TO_PREINSTALL=
547     PACKAGES_TO_RUNSCRIPTS=
548     PACKAGES_TO_VMINSTALL=
549     PACKAGES_TO_CBPREINSTALL=
550     PACKAGES_TO_CBINSTALL=
551     RUNSCRIPTS_SEEN=
552     GUESSED_DIST=unknown
553     mkdir -p $BUILD_ROOT/.init_b_cache/rpms
554     while read PKG SRC ; do
555         if test "$PKG" = "preinstall:" ; then
556             PACKAGES_TO_PREINSTALL=$SRC
557             continue
558         fi
559         if test "$PKG" = "vminstall:" ; then
560             PACKAGES_TO_VMINSTALL=$SRC
561             continue
562         fi
563         # these additional preinstall / install values are only set for 
564         # emulated "CrossBuild" setups - thus CB(pre)install
565         if test "$PKG" = "cbpreinstall:" ; then
566             if is_emulator_arch ; then
567                 PACKAGES_TO_CBPREINSTALL=$SRC
568             fi
569             continue
570         fi
571         if test "$PKG" = "cbinstall:" ; then
572             if is_emulator_arch ; then
573                 PACKAGES_TO_CBINSTALL=$SRC
574             fi
575             continue
576         fi
577         if test "$PKG" = "runscripts:" ; then
578             RUNSCRIPTS_SEEN=true
579             PACKAGES_TO_RUNSCRIPTS=$SRC
580             continue
581         fi
582         if test "$PKG" = "dist:" ; then
583             GUESSED_DIST=$SRC
584             continue
585         fi
586         if test "$PKG" = "rpmid:" ; then
587             echo "${SRC#*:}" > $BUILD_ROOT/.init_b_cache/rpms/${SRC%%:*}.id
588             continue
589         fi
590         if test "$PKG" != "${PKG%:}"; then
591             echo "Warning: ignoring unsupported tag '$PKG'" >&2
592             continue
593         fi
594         if [ "${SRC#/}" = "$SRC" ]; then
595             url="$SRC"
596             case "$url" in
597                 zypp://* | http://* | https://* | ftp://* | ftps://*)
598                     downloadpkg "$url"
599                     ;;
600                 *)
601                     echo "unsupported url for '$PKG': $url" >&2
602                     cleanup_and_exit 1
603                     ;;
604             esac
605         fi
606         # downloadpkg modified $SRC, so it has a right name for use
607         ln -s "$SRC" "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRC##*.}"
608         PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL $PKG"
609     done < $RPMLIST
610
611     test -n "$PACKAGES_TO_CBPREINSTALL" && echo "cbpreinstall: $PACKAGES_TO_CBPREINSTALL"
612     test -n "$PACKAGES_TO_CBINSTALL" && echo "cbinstall   : $PACKAGES_TO_CBINSTALL"
613     # compatibility...
614     test -z "$RUNSCRIPTS_SEEN" && PACKAGES_TO_RUNSCRIPTS="$PACKAGES_TO_PREINSTALL"
615
616     echo "$GUESSED_DIST" > $BUILD_ROOT/.guessed_dist
617     test -n "$BUILD_DIST" || BUILD_DIST="$GUESSED_DIST"
618     PSUF=rpm
619     test -L $BUILD_ROOT/.init_b_cache/rpms/rpm.rpm || PSUF=deb
620 fi
621
622 #
623 # now test if there is already a build dir.
624 #
625 if test ! -f $BUILD_ROOT/var/lib/rpm/packages.rpm -a ! -f $BUILD_ROOT/var/lib/rpm/Packages ; then
626     mkdir -p $BUILD_ROOT/var/lib/rpm || cleanup_and_exit 1
627     mkdir -p $BUILD_ROOT/usr/src/packages/SOURCES || cleanup_and_exit 1
628     mkdir -p $BUILD_ROOT/etc || cleanup_and_exit 1
629     mkdir -p $BUILD_ROOT/proc || cleanup_and_exit 1
630     test -f $BUILD_ROOT/etc/HOSTNAME || hostname -f > $BUILD_ROOT/etc/HOSTNAME
631     if test $PSUF = deb ; then
632         mkdir -p $BUILD_ROOT/var/lib/dpkg
633         mkdir -p $BUILD_ROOT/var/log
634         mkdir -p $BUILD_ROOT/etc/default
635         :> $BUILD_ROOT/var/lib/dpkg/status
636         :> $BUILD_ROOT/var/lib/dpkg/available
637         :> $BUILD_ROOT/var/log/dpkg.log
638         :> $BUILD_ROOT/etc/ld.so.conf
639         :> $BUILD_ROOT/etc/default/rcS
640     fi
641     for PKG in $PACKAGES_TO_RUNSCRIPTS ; do
642         : > $BUILD_ROOT/.init_b_cache/scripts/$PKG.run
643     done
644     for PKG in $PACKAGES_TO_PREINSTALL ; do
645         preinstall ${PKG##*/}
646     done
647     if test -n "$PREPARE_VM" ; then
648         for PKG in $PACKAGES_TO_VMINSTALL ; do
649             preinstall ${PKG##*/}
650         done
651     fi
652     # add cbpreinstall if cross HOST != TARGET
653     for PKG in $PACKAGES_TO_CBPREINSTALL ; do
654         preinstall ${PKG##*/}
655     done
656     if [ -w /root ]; then
657             test -c $BUILD_ROOT/dev/null || create_devs
658     fi
659     test -e $BUILD_ROOT/etc/fstab || touch $BUILD_ROOT/etc/fstab
660     test -e $BUILD_ROOT/etc/ld.so.conf || cp $BUILD_ROOT/etc/ld.so.conf.in $BUILD_ROOT/etc/ld.so.conf
661     if test -z "$PREPARE_VM" ; then
662         run_pkg_scripts
663         init_db
664         touch $BUILD_ROOT/.init_b_cache/preinstall_finished
665     fi
666 fi
667
668 if test -n "$PREPARE_VM" ; then
669     mkdir -p $BUILD_ROOT/.build
670     echo "copying packages..."
671     for PKG in $PACKAGES_TO_INSTALL ; do
672         rm -f $BUILD_ROOT/.init_b_cache/$PKG.$PSUF
673         cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF $BUILD_ROOT/.init_b_cache/$PKG.$PSUF || cleanup_and_exit 1
674         ln -s -f ../$PKG.$PSUF $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF
675         check_exit
676     done
677     # alreadyinstalled check will not work, but we have to live with
678     # that...
679     echo -n 'reordering...'
680     PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
681     echo 'done'
682     Q="'\''"
683     echo "PACKAGES_TO_INSTALL='${PACKAGES_TO_INSTALL//"'"/$Q}'" > $BUILD_ROOT/.build/init_buildsystem.data
684     echo "PACKAGES_TO_RUNSCRIPTS='${PACKAGES_TO_RUNSCRIPTS//"'"/$Q}'" >> $BUILD_ROOT/.build/init_buildsystem.data
685     echo "PSUF='$PSUF'" >> $BUILD_ROOT/.build/init_buildsystem.data
686     rm -f $BUILD_IS_RUNNING
687     cleanup_and_exit 0
688 fi
689
690 mkdir -p $BUILD_ROOT/proc
691 mkdir -p $BUILD_ROOT/dev/pts
692 mount -n -tproc none $BUILD_ROOT/proc 2>/dev/null || true
693 mount -n -tdevpts none $BUILD_ROOT/dev/pts 2>/dev/null || true
694
695 #
696 # create .build.binaries directory if requested
697 #
698 rm -rf $BUILD_ROOT/.build.binaries
699 if test -n "$CREATE_BUILD_BINARIES" ; then
700     echo "creating .build.binaries directory..."
701     mkdir -p "$BUILD_ROOT/.build.binaries"
702     for PKG in $PACKAGES_TO_INSTALL ; do
703         test -L "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" || continue
704         LPKG=`readlink -f "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF"`
705         ln "$LPKG" "$BUILD_ROOT/.build.binaries/$PKG.$PSUF" 2>/dev/null
706         test -f "$BUILD_ROOT/.build.binaries/$PKG.$PSUF" && continue
707         cp "$LPKG" "$BUILD_ROOT/.build.binaries/$PKG.$PSUF"
708         check_exit
709     done
710 fi
711
712 #
713 # get list and ids of already installed rpms
714 #
715 mkdir -p $BUILD_ROOT/.init_b_cache/alreadyinstalled
716 if test -f $BUILD_ROOT/var/lib/rpm/packages.rpm -o -f $BUILD_ROOT/var/lib/rpm/Packages ; then
717     chroot $BUILD_ROOT rpm -qa --qf "%{NAME} $RPMIDFMT" | (
718         while read pp ii; do
719             echo "$ii" > "$BUILD_ROOT/.init_b_cache/alreadyinstalled/$pp"
720         done
721     )
722 fi
723
724 #
725 # reorder packages (already done in vm continuation)
726 #
727 if ! test -e $BUILD_ROOT/.build/init_buildsystem.data ; then
728     echo -n 'reordering...'
729     PACKAGES_TO_INSTALL_FIRST=`reorder $PACKAGES_TO_INSTALL_FIRST`
730     PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
731     echo 'done'
732 fi
733
734 rpm_e()
735 {
736     chroot $BUILD_ROOT rpm --nodeps -e $PKG 2>&1 | \
737     while read line; do
738         case "$line" in
739
740             r*failed:\ No\ such\ file\ or\ directory) ;;
741             error:\ failed\ to\ stat\ *:\ No\ such\ file\ or\ directory) ;;
742             error:\ *scriptlet\ failed*)
743                 echo "$line"
744                 echo "re-try deleting $PKG using --noscripts"
745                 chroot $BUILD_ROOT rpm --nodeps --noscripts -e $PKG || true
746             ;;
747             *) echo "$line" ;;
748         esac
749     done
750 }
751
752 #
753 # delete all packages we don't want
754 #
755 mkdir -p $BUILD_ROOT/.init_b_cache/todelete
756 for PKG in $BUILD_ROOT/.init_b_cache/alreadyinstalled/* ; do
757     PKG=${PKG##*/}
758     test "$PKG" = "*" && continue
759     ln $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG $BUILD_ROOT/.init_b_cache/todelete/$PKG
760 done
761 for PKG in $PACKAGES_TO_INSTALL_FIRST $PACKAGES_TO_INSTALL $PACKAGES_TO_CBINSTALL ; do
762     rm -f $BUILD_ROOT/.init_b_cache/todelete/$PKG
763 done
764 for PKG in $BUILD_ROOT/.init_b_cache/todelete/* ; do
765     PKG=${PKG##*/}
766     test "$PKG" = "*" && continue
767     echo "deleting $PKG"
768     rpm_e "$PKG"
769     check_exit
770 done
771 rm -rf $BUILD_ROOT/.init_b_cache/todelete
772
773 rm -rf $BUILD_ROOT/.init_b_cache/preinstalls
774 mkdir -p $BUILD_ROOT/.init_b_cache/preinstalls
775 for PKG in $PACKAGES_TO_PREINSTALL $PACKAGES_TO_CBPREINSTALL; do
776     touch "$BUILD_ROOT/.init_b_cache/preinstalls/$PKG"
777 done
778
779 rm -rf $BUILD_ROOT/installed-pkg
780 mkdir -p $BUILD_ROOT/installed-pkg
781
782 RPMCHECKOPTS=
783 RPMCHECKOPTS_HOST=
784 # on Fedora 10 rpmbuild is in a separate package so we need something else to
785 # detect rpm4
786 test -x $BUILD_ROOT/usr/bin/rpmsign && RPMCHECKOPTS="--nodigest --nosignature"
787 test -x /usr/bin/rpmsign && RPMCHECKOPTS_HOST="--nodigest --nosignature"
788
789 for PKG in $PACKAGES_TO_INSTALL_FIRST RUN_LDCONFIG $PACKAGES_TO_INSTALL $PACKAGES_TO_CBINSTALL; do
790
791     case $PKG in
792       RUN_LDCONFIG)
793         test -x $BUILD_ROOT/sbin/ldconfig && chroot $BUILD_ROOT /sbin/ldconfig 2>&1
794         continue
795       ;;
796     esac
797
798     test -f $BUILD_ROOT/installed-pkg/$PKG && continue
799
800     if test $PSUF = deb ; then
801         # debian world, install deb files
802         test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb || continue
803         if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.deb" -ef "$BUILD_ROOT/.init_b_cache/$PKG.deb" ; then
804             rm -f $BUILD_ROOT/.init_b_cache/$PKG.deb
805             cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb $BUILD_ROOT/.init_b_cache/$PKG.deb || cleanup_and_exit 1
806         fi
807         PKGID=`readlink $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb`
808         PKGID="${PKGID##*/}"
809         PKGID="${PKGID%.deb}"
810         echo "installing ${PKGID%_*}"
811         ( chroot $BUILD_ROOT dpkg --install --force all .init_b_cache/$PKG.deb 2>&1 || touch $BUILD_ROOT/exit ) | \
812             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'
813         check_exit
814         echo "$PKGID debian" > $BUILD_ROOT/installed-pkg/$PKG
815         continue
816     fi
817
818     test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm || continue
819
820     if test -f $BUILD_ROOT/.init_b_cache/rpms/$PKG.id -a -f $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG ; then
821         read PKGID < $BUILD_ROOT/.init_b_cache/rpms/$PKG.id
822         read OLDPKGID < $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG
823         if test "$PKGID" = "$OLDPKGID" ; then
824             #echo "keeping ${PKGID%% *}"
825             echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
826             continue
827         fi
828     fi
829
830     PKGID=`rpm -qp --qf "$RPMIDFMT" $RPMCHECKOPTS_HOST $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm`
831
832     if test -f $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG ; then
833         read OLDPKGID < $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG
834         if test "$PKGID" != "$OLDPKGID" ; then
835             echo deleting unwanted ${OLDPKGID%% *}
836             rpm_e "$PKG"
837         elif test "$VERIFY_BUILD_SYSTEM" = true ; then
838             chroot $BUILD_ROOT rpm --verify $PKG 2>&1 | tee $TMPFILE
839             if grep ^missing $TMPFILE > /dev/null ; then
840                 echo deleting incomplete ${PKGID%% *}
841                 rpm_e "$PKG"
842             else
843                 #echo "keeping ${PKGID%% *}"
844                 echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
845                 continue
846             fi
847         else
848             #echo "keeping ${PKGID%% *}"
849             echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
850             continue
851         fi
852         if test -e "$BUILD_ROOT/.init_b_cache/preinstalls/$PKG" ; then
853             preinstall "$PKG"
854             # call for rpm-4.x and not rpm-devel
855             test -z "${PKG##rpm-[0-9]*}" && chroot $BUILD_ROOT rpm --rebuilddb
856             # also exec for exchanged rpm !  naming is rpm-x86-<target>-<ver>
857             test -z "${PKG##rpm-x86-*[0-9]*}" && chroot $BUILD_ROOT rpm --rebuilddb
858         fi
859     fi
860     export ADDITIONAL_PARAMS=
861     if test "$USE_FORCE" = true ; then
862         export ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --force"
863     fi
864     echo "installing ${PKGID%% *}"
865     if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" -ef "$BUILD_ROOT/.init_b_cache/$PKG.rpm" ; then
866         rm -f $BUILD_ROOT/.init_b_cache/$PKG.rpm
867         cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm $BUILD_ROOT/.init_b_cache/$PKG.rpm || cleanup_and_exit 1
868     fi
869     ( chroot $BUILD_ROOT rpm --ignorearch --nodeps -U --oldpackage --ignoresize $RPMCHECKOPTS \
870                 $ADDITIONAL_PARAMS .init_b_cache/$PKG.rpm 2>&1 || \
871           touch $BUILD_ROOT/exit ) | \
872               grep -v "^warning:.*saved as.*rpmorig$"
873     # delete link so package is only installed once
874     rm -f $BUILD_ROOT/.init_b_cache/$PKG.rpm
875     check_exit
876     echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
877
878 done
879
880 if test $PSUF = deb ; then
881     echo "configure all installed packages..."
882     # configure all packages after complete installation, not for each package like rpm does
883     # We need to run this twice, because of cyclic dependencies as it does not succeed on most
884     # debian based distros in the first attempt.
885     if ! chroot $BUILD_ROOT dpkg --configure --pending  2>&1; then
886        echo "first configure attempt failed, trying again..."
887        chroot $BUILD_ROOT dpkg --configure --pending  2>&1 || touch $BUILD_ROOT/exit
888     fi
889 fi
890
891 # devices can vanish if devs got uninstalled
892 test -c $BUILD_ROOT/dev/null || create_devs
893
894 cd $BUILD_ROOT || cleanup_and_exit 1
895
896 #
897 # setup /etc/mtab
898 #
899 rm -f $BUILD_ROOT/etc/mtab
900 cp /proc/mounts $BUILD_ROOT/etc/mtab
901 chmod 644 $BUILD_ROOT/etc/mtab
902
903 #
904 # to be sure, path is set correctly, we have to source /etc/profile before
905 # starting rpm.
906 #
907 # XXX
908 #rm -f $BUILD_ROOT/bin/rpm.sh
909 #cp $BUILD_LIBDIR/lib/rpm.sh $BUILD_ROOT/bin/rpm.sh
910 #chmod 755 $BUILD_ROOT/bin/rpm.sh
911 #test -f $BUILD_ROOT/bin/rpm -a ! -L $BUILD_ROOT/bin/rpm && \
912 #    mv $BUILD_ROOT/bin/rpm $BUILD_ROOT/bin/rpm.bin
913 #rm -f $BUILD_ROOT/bin/rpm
914 #ln -s rpm.sh $BUILD_ROOT/bin/rpm
915
916 #
917 # some packages use uname -r to decide which kernel is used to build for.
918 # this does not work in autobuild always.  Here is a wrapper script, that
919 # gets Version from kernel sources.
920 #
921 # XXX
922 #rm -f $BUILD_ROOT/bin/uname.sh
923 #cp -v $BUILD_LIBDIR/lib/uname.sh $BUILD_ROOT/bin/uname.sh
924 #chmod 755 $BUILD_ROOT/bin/uname.sh
925 #test -f $BUILD_ROOT/bin/uname -a ! -L $BUILD_ROOT/bin/uname && \
926 #    mv $BUILD_ROOT/bin/uname $BUILD_ROOT/bin/uname.bin
927 #rm -f $BUILD_ROOT/bin/uname
928 #ln -s uname.sh $BUILD_ROOT/bin/uname
929
930 #
931 # some distributions have a /etc/rpmrc or /etc/rpm/macros and some not.
932 # make sure, that it is setup correctly.
933 #
934 # XXX
935 #rm -f $BUILD_ROOT/etc/rpmrc
936 #if test -e $BUILD_LIBDIR/lib/rpmrc.$BUILD_BASENAME ; then
937 #    cp -v $BUILD_LIBDIR/lib/rpmrc.$BUILD_BASENAME $BUILD_ROOT/etc/rpmrc
938 #elif test -e $BUILD_LIBDIR/lib/rpmrc ; then
939 #    cp -v $BUILD_LIBDIR/lib/rpmrc $BUILD_ROOT/etc/rpmrc
940 #fi
941
942 # XXX
943 #rm -f $BUILD_ROOT/etc/rpm/macros $BUILD_ROOT/etc/rpm/suse_macros
944 #mkdir -p $BUILD_ROOT/etc/rpm
945 #if test -e $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME ; then
946 #    cp -v $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME $BUILD_ROOT/etc/rpm/macros
947 #    cp -v $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME $BUILD_ROOT/etc/rpm/suse_macros
948 #elif test -e $BUILD_LIBDIR/lib/macros ; then
949 #    cp -v $BUILD_LIBDIR/lib/macros $BUILD_ROOT/etc/rpm/macros
950 #    cp -v $BUILD_LIBDIR/lib/macros $BUILD_ROOT/etc/rpm/suse_macros
951 #fi
952
953 #
954 # make sure, that our nis is not present in the chroot system
955 #
956 test -e $BUILD_ROOT/etc/nsswitch.conf && {
957     echo removing nis flags from $BUILD_ROOT/etc/nsswitch.conf...
958     cat $BUILD_ROOT/etc/nsswitch.conf | sed -e"s:nis::g" > \
959         $BUILD_ROOT/etc/nsswitch.conf.tmp
960     mv $BUILD_ROOT/etc/nsswitch.conf.tmp $BUILD_ROOT/etc/nsswitch.conf
961 }
962
963 #
964 # creating some default directories
965 for DIR in /usr/share/doc/packages \
966            /usr/X11R6/include/X11/pixmaps \
967            /usr/X11R6/include/X11/bitmaps ; do
968     mkdir -p $BUILD_ROOT/$DIR
969 done
970
971 for FILE in /var/run/utmp /var/log/wtmp /etc/fstab ; do
972     touch $BUILD_ROOT/$FILE
973 done
974
975 echo now finalizing build dir...
976 CHROOT_RETURN="`chroot $BUILD_ROOT /sbin/ldconfig 2>&1`"
977 case "$CHROOT_RETURN" in
978     *warning:*)
979       chroot $BUILD_ROOT /sbin/ldconfig
980       echo
981       echo chroot $BUILD_ROOT /sbin/ldconfig
982       echo
983       echo "$CHROOT_RETURN"
984       echo
985       echo "Problem with ldconfig.  It's better to reinit the build system..."
986       echo
987       cleanup_and_exit 1
988     ;;
989 esac
990 test -x $BUILD_ROOT/usr/sbin/Check && chroot $BUILD_ROOT /usr/sbin/Check
991
992 mkdir -p $BUILD_ROOT/var/adm/packages
993 touch $BUILD_ROOT/var/adm/packages
994 if test -x $BUILD_ROOT/sbin/SuSEconfig ; then
995     if grep norestarts $BUILD_ROOT/sbin/SuSEconfig > /dev/null ; then
996         chroot $BUILD_ROOT /sbin/SuSEconfig --norestarts --force
997     else
998         chroot $BUILD_ROOT /sbin/SuSEconfig --force
999     fi
1000 fi
1001
1002 if test -x $BUILD_ROOT/usr/X11R6/bin/switch2mesasoft ; then
1003     chroot $BUILD_ROOT /usr/X11R6/bin/switch2mesasoft
1004 fi
1005
1006 for PROG in /usr/bin/TeX/texhash /usr/bin/texhash ; do
1007     test -x $BUILD_ROOT/$PROG && \
1008         chroot $BUILD_ROOT bash -c ". /etc/profile ; $PROG"
1009 done
1010
1011 if test -e $BUILD_ROOT/usr/share/zoneinfo/UTC ; then
1012     chroot $BUILD_ROOT zic -l UTC
1013 fi
1014
1015 test -e $BUILD_ROOT/.build/init_buildsystem.data || HOST=`hostname`
1016 test -e $BUILD_ROOT/etc/hosts || echo "127.0.0.1 localhost" > $BUILD_ROOT/etc/hosts
1017 if ! grep -F "127.0.0.1 $HOST" $BUILD_ROOT/etc/hosts > /dev/null ; then
1018     # this makes a reverse lookup on 127.0.0.1 return the host name,
1019     # which is bad, but 127.0.0.2 does not work on all unix systems
1020     echo "127.0.0.1 $HOST" > $BUILD_ROOT/etc/hosts.new
1021     test -f $BUILD_ROOT/etc/hosts && cat $BUILD_ROOT/etc/hosts >> $BUILD_ROOT/etc/hosts.new
1022     mv $BUILD_ROOT/etc/hosts.new $BUILD_ROOT/etc/hosts
1023 fi
1024
1025 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
1026     echo "initializing rpm db..."
1027     chroot $BUILD_ROOT rpm --initdb || cleanup_and_exit 1
1028     # create provides index
1029     chroot $BUILD_ROOT rpm -q --whatprovides rpm >/dev/null 2>&1
1030 fi
1031
1032 # create modules.dep in kvm/xen
1033 # This can not work, until we use the native repository kernel
1034 #if [ $BUILD_ROOT = "/" -a -x /sbin/depmod ]; then
1035 #  /sbin/depmod -a
1036 #fi
1037
1038 rm -f $BUILD_ROOT/.rpmmacros $BUILD_ROOT/root/.rpmmacros
1039 rm -rf $BUILD_ROOT/.init_b_cache
1040 rm -f $BUILD_IS_RUNNING
1041 rm -f $TMPFILE
1042
1043 cleanup_and_exit 0