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