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