create /repos symlink for kiwi, to fix image build with additional packages in -...
[platform/upstream/build.git] / init_buildsystem
1 #!/bin/bash
2 # Script to create a complete system to build packages in a chroot
3 # environment.  This script ensures, that all packages given as
4 # parameter are installed. All other packges will be deleted.
5 #
6 # BUILD_ROOT  here the packages will be installed/deleted
7 # BUILD_RPMS  here we get our packages to install
8 # BUILD_ARCH  path of the architectures we try
9 #
10 # (c) 1997-2005 SuSE GmbH Nuernberg, Germany
11
12 #
13 # needed globals variables
14 #
15 export SRC
16 export YAST_IS_RUNNING="instsys"
17 export DEBIAN_FRONTEND=noninteractive
18 export DEBIAN_PRIORITY=critical
19 export BUILD_DIR=${BUILD_DIR:-/usr/lib/build}
20 # need to restore build root owner for non-root builds
21 browner=0
22 definesnstuff=()
23 repos=()
24
25 # should RPMs be installed with --force ?
26 USE_FORCE=false
27
28 BUILD_IS_RUNNING=$BUILD_ROOT/not-ready
29 TMPFILE=$BUILD_ROOT/tmpfile
30 #buildhost removed so that id can be generated from repo files
31 #RPMIDFMT="%{NAME}-%{VERSION}-%{RELEASE} %{BUILDHOST}-%{BUILDTIME}\n"
32 RPMIDFMT="%{NAME}-%{VERSION}-%{RELEASE} %{BUILDTIME}\n"
33
34 PREPARE_XEN=
35 USEUSEDFORBUILD=
36 LIST_STATE=
37 RPMLIST=
38 CLEAN_BUILD=
39 CREATE_BUILD_BINARIES=
40
41 while test -n "$1" ; do
42     case "$1" in
43         --prepare)
44             shift
45             PREPARE_XEN=true
46             ;;
47         --create-build-binaries)
48             shift
49             CREATE_BUILD_BINARIES=true
50             ;;
51         --useusedforbuild)
52             shift
53             USEUSEDFORBUILD=--useusedforbuild
54             ;;
55         --list-state)
56             shift
57             LIST_STATE=true
58             ;;
59         --rpmlist)
60             shift
61             RPMLIST=$1
62             shift
63             ;;
64         --define|--with|--without)
65             definesnstuff[${#definesnstuff[@]}]="$1";
66             definesnstuff[${#definesnstuff[@]}]="$2";
67             shift 2
68             ;;
69         --repository|--repo)
70             repos[${#repos[@]}]="$2";
71             shift 2;
72             ;;
73         --clean)
74             CLEAN_BUILD="$1"
75             shift
76         ;;
77         *)
78             break
79             ;;
80     esac
81 done
82 PKGS=("$@")
83
84 #
85 # needed functions
86 #
87
88 function cleanup_and_exit {
89     trap EXIT
90     [ "$BUILD_ROOT" != / ] || chown $browner $BUILD_ROOT
91     # umount so init_buildsystem can be used standalone
92 # XXX: use stat -f /dev/pts/ -c %T  to check whether it's mounted and not suppress errors then?
93     umount -n $BUILD_ROOT/proc 2> /dev/null || true
94     umount -n $BUILD_ROOT/dev/pts 2> /dev/null || true
95     umount -n $BUILD_ROOT/mnt 2> /dev/null || true
96     exit ${1:-0}
97 }
98
99 function clean_build_root () {
100         test -n "$BUILD_ROOT" && {
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             rm -rf -- $BUILD_ROOT/*
105             rm -rf -- $BUILD_ROOT/.build
106             rm -rf -- $BUILD_ROOT/.init_b_cache
107             mkdir -p $BUILD_ROOT/proc
108             mkdir -p $BUILD_ROOT/dev/pts
109             if [ "$UID" = '0' ]; then
110                     mount -n -tproc none $BUILD_ROOT/proc
111                     mount -n -tdevpts none $BUILD_ROOT/dev/pts
112             fi
113         }
114 }
115
116 function preinstall {
117     if test -n "$1" ; then
118         echo "preinstalling $1..."
119         cd $BUILD_ROOT || cleanup_and_exit 1
120         CPIO="cpio --extract --unconditional --preserve-modification-time --make-directories --no-absolute-filenames --quiet"
121         if test -e "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" ; then
122             PAYLOADDECOMPRESS=cat
123             case `rpm -qp --nodigest --nosignature --qf "%{PAYLOADCOMPRESSOR}\n" "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm"` in
124                 lzma) rpm --showrc | grep PayloadIsLzma > /dev/null || PAYLOADDECOMPRESS="lzma -d" ;;
125             esac
126             if test "$PAYLOADDECOMPRESS" = "lzma -d" ; then
127                 if ! lzma </dev/null >/dev/null 2>&1 ; then
128                     test -f "$BUILD_DIR/lzmadec.sh" && PAYLOADDECOMPRESS="bash $BUILD_DIR/lzmadec.sh"
129                 fi
130             fi
131             if test "$PAYLOADDECOMPRESS" = cat ; then
132                 rpm2cpio "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" | $CPIO
133             else
134                 rpm2cpio "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" | $PAYLOADDECOMPRESS | $CPIO
135             fi
136             if test -e .init_b_cache/scripts/$1.run ; then
137                 rpm -qp --nodigest --nosignature --qf "%{PREIN}" "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" > .init_b_cache/scripts/$1.pre
138                 rpm -qp --nodigest --nosignature --qf "%{POSTIN}" "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" > .init_b_cache/scripts/$1.post
139                 echo -n '(none)' > .init_b_cache/scripts/.none
140                 cmp -s .init_b_cache/scripts/$1.pre .init_b_cache/scripts/.none && rm -f .init_b_cache/scripts/$1.pre
141                 cmp -s .init_b_cache/scripts/$1.post .init_b_cache/scripts/.none && rm -f .init_b_cache/scripts/$1.post
142                 rm -f .init_b_cache/scripts/.none
143             fi
144         elif test -e "$BUILD_ROOT/.init_b_cache/rpms/$1.deb" ; then
145             ar x "$BUILD_ROOT/.init_b_cache/rpms/$1.deb" control.tar.gz data.tar.gz
146             mkdir -p .init_b_cache/scripts/control
147             tar -C .init_b_cache/scripts/control -xzf control.tar.gz
148             tar xzf data.tar.gz
149             if test -e .init_b_cache/scripts/$1.run ; then
150                 test -e .init_b_cache/scripts/control/preinst && mv .init_b_cache/scripts/control/preinst .init_b_cache/scripts/$1.pre
151                 test -e .init_b_cache/scripts/control/postinst && mv .init_b_cache/scripts/control/postinst .init_b_cache/scripts/$1.post
152             fi
153             rm -rf .init_b_cache/scripts/control control.tar.gz data.tar.gz
154         else
155             echo "warning: package $1 does not exist"
156         fi
157     fi
158     if test -n "$2" ; then
159         chroot $BUILD_ROOT /sbin/ldconfig 2>/dev/null
160         for PKG in $PACKAGES_TO_RUNSCRIPTS ; do
161             if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre" ; then
162                 echo "running $PKG preinstall script"
163                 if test -e "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" ; then
164                     chroot $BUILD_ROOT sh ".init_b_cache/scripts/$PKG.pre" 0
165                 else
166                     chroot $BUILD_ROOT ".init_b_cache/scripts/$PKG.pre" install < /dev/null
167                 fi
168                 rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre"
169             fi
170             if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post" ; then
171                 echo "running $PKG postinstall script"
172                 if test -e "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" ; then
173                     chroot $BUILD_ROOT sh ".init_b_cache/scripts/$PKG.post" 1
174                 else
175                     chroot $BUILD_ROOT ".init_b_cache/scripts/$PKG.post" configure '' < /dev/null
176                 fi
177                 rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post"
178             fi
179         done
180     fi
181 }
182
183 function init_db {
184     if test $PSUF = rpm ; then
185         echo initializing rpm db...
186         chroot $BUILD_ROOT rpm --initdb || cleanup_and_exit 1
187     else
188         # force dpkg into database to make epoch test work
189         if ! test "$BUILD_ROOT/.init_b_cache/rpms/dpkg.deb" -ef "$BUILD_ROOT/.init_b_cache/dpkg.deb" ; then
190             rm -f $BUILD_ROOT/.init_b_cache/dpkg.deb
191             cp $BUILD_ROOT/.init_b_cache/rpms/dpkg.deb $BUILD_ROOT/.init_b_cache/dpkg.deb || cleanup_and_exit 1
192         fi
193         chroot $BUILD_ROOT dpkg -i --force all .init_b_cache/dpkg.deb >/dev/null 2>&1
194     fi
195 }
196
197 function reorder {
198     test -z "$*" && return
199     rm -f $BUILD_ROOT/.init_b_cache/order.manifest
200     for PKG in "$@" ; do
201         echo "$PKG" >> $BUILD_ROOT/.init_b_cache/order.manifest
202     done
203     $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
204     rm -f $BUILD_ROOT/.init_b_cache/order.manifest
205 }
206
207 function create_devs {
208     local com file mode arg
209
210     mkdir -m 755 -p $BUILD_ROOT/dev/pts
211     test -f $BUILD_ROOT/dev/shm && rm -f $BUILD_ROOT/dev/shm
212     mkdir -m 755 -p $BUILD_ROOT/dev/shm
213     while read com file mode arg ; do
214         rm -f $BUILD_ROOT/dev/$file
215         if test $com = ln ; then
216             ln -s $arg $BUILD_ROOT/dev/$file
217             continue
218         fi
219         $com -m $mode $BUILD_ROOT/dev/$file $arg
220     done << DEVLIST
221         mknod null    666 c 1 3
222         mknod zero    666 c 1 5
223         mknod full    622 c 1 7
224         mknod random  666 c 1 8
225         mknod urandom 644 c 1 9
226         mknod tty     666 c 5 0
227         mknod ptmx    666 c 5 2
228         mknod loop0   640 b 7 0
229         mknod loop1   640 b 7 1
230         mknod loop2   640 b 7 2
231         mknod loop3   640 b 7 3
232         ln    fd      777 /proc/self/fd
233         ln    stdin   777 fd/0
234         ln    stdout  777 fd/1
235         ln    stderr  777 fd/2
236 DEVLIST
237 }
238
239 function validate_cache_file {
240     local findonly=''
241     test "${repos[*]} ${BUILD_RPMS//:/ /}" != "$(cat $CACHE_FILE.id 2>/dev/null)" && rm -f $CACHE_FILE.id
242     test -f $CACHE_FILE || rm -f $CACHE_FILE.id
243     for SRC in "${repos[@]}" ${BUILD_RPMS//:/ /}; do
244         test -z "$SRC" && SRC=.
245         test "$SRC" -nt $CACHE_FILE && rm -f $CACHE_FILE.id
246     done
247     if ! test -f $CACHE_FILE.id ; then
248         test -z "$LIST_STATE" && echo initializing $CACHE_FILE ...
249         for SRC in "${repos[@]}" -- ${BUILD_RPMS//:/ /}; do
250             if test "$SRC" = '--' ; then
251                 findonly=1
252                 continue
253             fi
254             test -z "$SRC" && SRC=`pwd`
255             if [ ! -e "$SRC" ]; then
256                 echo "*** $SRC does not exist" >&2
257                 cleanup_and_exit 1
258             elif [ -z "$findonly" -a -e "$SRC"/suse/setup/descr/packages -o -e "$SRC"/suse/setup/descr/packages.gz ]; then
259                 set -- $BUILD_DIR/createyastdeps "$SRC"
260             elif [ -z "$findonly" -a -e "$SRC"/repodata/repomd.xml ]; then
261                 set -- $BUILD_DIR/createrepomddeps "$SRC"
262             else
263                 set -- $BUILD_DIR/createrpmdeps "$SRC"
264             fi
265             echo "$@" >&2
266             "$@" || cleanup_and_exit 1
267         done > $CACHE_FILE.new
268
269         mv $CACHE_FILE.new $CACHE_FILE
270         echo "${repos[*]} ${BUILD_RPMS//:/ /}" > $CACHE_FILE.id
271     fi
272 }
273
274 fail_exit()
275 {
276   cleanup_and_exit 1
277 }
278
279 trap fail_exit EXIT
280
281 if [ "$BUILD_ROOT" = / ]; then
282     read dummy dummy browner dummy < <(ls -ld /)
283 fi
284
285 if [ -n "$CLEAN_BUILD" ]; then
286     clean_build_root
287 fi
288
289 #
290 # now test if there was an incomplete run
291 #
292 if test -e $BUILD_IS_RUNNING ; then
293     echo It seems that there was an incomplete setup of $BUILD_ROOT.
294     echo To be sure, we will build it again completely...
295     umount -n $BUILD_ROOT/proc 2> /dev/null
296     umount -n $BUILD_ROOT/dev/pts 2> /dev/null
297     umount -n $BUILD_ROOT/mnt 2> /dev/null
298     echo "Your build system is broken!! Shall I execute"
299     echo
300     echo "    rm -rf -- $BUILD_ROOT/*"
301     echo
302     echo -n "[y/N/c] "
303     read ANSWER
304     case "$ANSWER" in
305         c|C) rm -f $BUILD_IS_RUNNING ;;
306         y|Y) clean_build_root ;;
307         *) cleanup_and_exit 1 ;;
308     esac
309 fi
310
311 #
312 # store that we start to build system
313 #
314 mkdir -p $BUILD_ROOT
315 touch $BUILD_IS_RUNNING
316
317 if test -n "$PREPARE_XEN" ; then
318     rm -f $BUILD_ROOT/.build/init_buildsystem.data
319 fi
320 if test -e $BUILD_ROOT/.build/init_buildsystem.data ; then
321     # xen continuation
322     . $BUILD_ROOT/.build/init_buildsystem.data
323     if ! test -e $BUILD_ROOT/.init_b_cache/preinstall_finished ; then
324         # finish preinstall
325         preinstall '' true
326         init_db
327         touch $BUILD_ROOT/.init_b_cache/preinstall_finished
328     fi
329 else
330     #
331     # Hack to detect cross-build (fixed code) and copy qemus for cross-build emulation mode
332     #
333     if [ x"$BUILD_ARCH" == xarmv4l -o x"$BUILD_ARCH" == xarmv5el -o x"$BUILD_ARCH" == xarmv7el -o x"$BUILD_ARCH" == xsh4 ]; then
334        if [ x"`uname -i`" == xx86_64 -o  x"`uname -i`" == xi386 ]; then
335           if [ -e /proc/sys/fs/binfmt_misc/arm -o -e /proc/sys/fs/binfmt_misc/armeb -o -e /proc/sys/fs/binfmt_misc/sh4 ]; then
336              echo -n "qemu-* already registered "
337           else
338              echo -n "qemu will be registered " && test -e /usr/sbin/qemu-binfmt-conf.sh && sh /usr/sbin/qemu-binfmt-conf.sh
339           fi
340           if [ -e /usr/sbin/qemu-binfmt-conf.sh ]; then 
341              (mkdir -p  $BUILD_ROOT/usr/bin && set -x && cp /usr/bin/qemu-* $BUILD_ROOT/usr/bin)
342              # Hack to circumvent qemu mmap addr bug
343              test -e /proc/sys/vm/mmap_min_addr && echo 0 > /proc/sys/vm/mmap_min_addr
344           fi
345        fi
346     fi
347     #
348     # now make sure that all the packages are installed.
349     #
350     rm -rf $BUILD_ROOT/.init_b_cache
351     mkdir -p $BUILD_ROOT/.init_b_cache/scripts
352
353     if test -z "$RPMLIST" ; then
354         #
355         # create rpmdeps file
356         #
357         CACHE_FILE=$BUILD_ROOT/.srcfiles.cache
358         validate_cache_file
359
360         #
361         # select and expand packages
362         #
363         RPMLIST=$BUILD_ROOT/.init_b_cache/rpmlist
364         test -z "$LIST_STATE" && echo "expanding package dependencies..."
365         if ! $BUILD_DIR/expanddeps $USEUSEDFORBUILD "${definesnstuff[@]}" --dist "$BUILD_DIST" --depfile "$CACHE_FILE" --archpath "$BUILD_ARCH" --configdir $BUILD_DIR/configs "${PKGS[@]}" > $RPMLIST ; then
366             rm -f $BUILD_IS_RUNNING
367             cleanup_and_exit 1
368         fi
369     fi
370
371     if test -n "$LIST_STATE" ; then
372         rm -f $BUILD_IS_RUNNING
373         while read PKG SRC ; do
374            test "$PKG" = "preinstall:" && continue
375            test "$PKG" = "runscripts:" && continue
376            test "$PKG" = "vminstall:" && continue
377            test "$PKG" = "dist:" && continue
378            test "$PKG" = "rpmid:" && continue
379            echo "${SRC##*/}"
380         done < $BUILD_ROOT/.init_b_cache/rpmlist
381         rm -rf $BUILD_ROOT/.init_b_cache
382         cleanup_and_exit 0
383     fi
384
385     PACKAGES_TO_INSTALL=
386     PACKAGES_TO_PREINSTALL=
387     PACKAGES_TO_RUNSCRIPTS=
388     PACKAGES_TO_VMINSTALL=
389     RUNSCRIPTS_SEEN=
390     GUESSED_DIST=unknown
391     mkdir -p $BUILD_ROOT/.init_b_cache/rpms
392     while read PKG SRC ; do
393         if test "$PKG" = "preinstall:" ; then
394             PACKAGES_TO_PREINSTALL=$SRC
395             continue
396         fi
397         if test "$PKG" = "vminstall:" ; then
398             PACKAGES_TO_VMINSTALL=$SRC
399             continue
400         fi
401         if test "$PKG" = "runscripts:" ; then
402             RUNSCRIPTS_SEEN=true
403             PACKAGES_TO_RUNSCRIPTS=$SRC
404             continue
405         fi
406         if test "$PKG" = "dist:" ; then
407             GUESSED_DIST=$SRC
408             continue
409         fi
410         if test "$PKG" = "rpmid:" ; then
411             echo "${SRC#*:}" > $BUILD_ROOT/.init_b_cache/rpms/${SRC%%:*}.id
412             continue
413         fi
414         ln -s "$SRC" "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRC##*.}"
415         PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL $PKG"
416     done < $RPMLIST
417
418     # compatibility...
419     test -z "$RUNSCRIPTS_SEEN" && PACKAGES_TO_RUNSCRIPTS="$PACKAGES_TO_PREINSTALL"
420
421     echo "$GUESSED_DIST" > $BUILD_ROOT/.guessed_dist
422     PSUF=rpm
423     test -L $BUILD_ROOT/.init_b_cache/rpms/rpm.rpm || PSUF=deb
424 fi
425
426 #
427 # now test if there is already a build dir.
428 #
429 if test ! -f $BUILD_ROOT/var/lib/rpm/packages.rpm -a ! -f $BUILD_ROOT/var/lib/rpm/Packages ; then
430     mkdir -p $BUILD_ROOT/var/lib/rpm || cleanup_and_exit 1
431     mkdir -p $BUILD_ROOT/usr/src/packages/SOURCES || cleanup_and_exit 1
432     mkdir -p $BUILD_ROOT/etc || cleanup_and_exit 1
433     mkdir -p $BUILD_ROOT/proc || cleanup_and_exit 1
434     test -f $BUILD_ROOT/etc/HOSTNAME || hostname -f > $BUILD_ROOT/etc/HOSTNAME
435     if test $PSUF = deb ; then
436         mkdir -p $BUILD_ROOT/var/lib/dpkg
437         mkdir -p $BUILD_ROOT/var/log
438         mkdir -p $BUILD_ROOT/etc/default
439         :> $BUILD_ROOT/var/lib/dpkg/status
440         :> $BUILD_ROOT/var/lib/dpkg/available
441         :> $BUILD_ROOT/var/log/dpkg.log
442         :> $BUILD_ROOT/etc/ld.so.conf
443         :> $BUILD_ROOT/etc/default/rcS
444     fi
445     for PKG in $PACKAGES_TO_RUNSCRIPTS ; do
446         : > $BUILD_ROOT/.init_b_cache/scripts/$PKG.run
447     done
448     for PKG in $PACKAGES_TO_PREINSTALL ; do
449         preinstall ${PKG##*/}
450     done
451     if test -n "$PREPARE_XEN" ; then
452         for PKG in $PACKAGES_TO_VMINSTALL ; do
453             preinstall ${PKG##*/}
454         done
455     fi
456     if [ -w /root ]; then
457             test -c $BUILD_ROOT/dev/null || create_devs
458     fi
459     test -e $BUILD_ROOT/etc/fstab || touch $BUILD_ROOT/etc/fstab
460     test -e $BUILD_ROOT/etc/ld.so.conf || cp $BUILD_ROOT/etc/ld.so.conf.in $BUILD_ROOT/etc/ld.so.conf
461     if test -z "$PREPARE_XEN" ; then
462         preinstall '' true
463         init_db
464         touch $BUILD_ROOT/.init_b_cache/preinstall_finished
465     fi
466 fi
467
468 if test -n "$PREPARE_XEN" ; then
469     mkdir -p $BUILD_ROOT/.build
470     echo "copying packages..."
471     for PKG in $PACKAGES_TO_INSTALL ; do
472         rm -f $BUILD_ROOT/.init_b_cache/$PKG.$PSUF
473         cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF $BUILD_ROOT/.init_b_cache/$PKG.$PSUF || cleanup_and_exit 1
474         ln -s -f ../$PKG.$PSUF $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF
475     done
476     # alreadyinstalled check will not work, but we have to live with
477     # that...
478     echo -n 'reordering...'
479     PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
480     echo 'done'
481     Q="'\''"
482     echo "PACKAGES_TO_INSTALL='${PACKAGES_TO_INSTALL//"'"/$Q}'" > $BUILD_ROOT/.build/init_buildsystem.data
483     echo "PACKAGES_TO_RUNSCRIPTS='${PACKAGES_TO_RUNSCRIPTS//"'"/$Q}'" >> $BUILD_ROOT/.build/init_buildsystem.data
484     echo "PSUF='$PSUF'" >> $BUILD_ROOT/.build/init_buildsystem.data
485     rm -f $BUILD_IS_RUNNING
486     cleanup_and_exit 0
487 fi
488
489 mkdir -p $BUILD_ROOT/proc
490 mkdir -p $BUILD_ROOT/dev/pts
491 mount -n -tproc none $BUILD_ROOT/proc 2>/dev/null || true
492 mount -n -tdevpts none $BUILD_ROOT/dev/pts 2>/dev/null || true
493
494 #
495 # create .build.binaries directory if requested
496 #
497 rm -rf $BUILD_ROOT/.build.binaries
498 if test -n "$CREATE_BUILD_BINARIES" ; then
499     echo "creating .build.binaries directory..."
500     mkdir -p "$BUILD_ROOT/.build.binaries"
501     for PKG in $PACKAGES_TO_INSTALL ; do
502         test -L "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" || continue
503         LPKG=`readlink -f "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF"`
504         ln "$LPKG" "$BUILD_ROOT/.build.binaries/$PKG.$PSUF" 2>/dev/null
505         test -f "$BUILD_ROOT/.build.binaries/$PKG.$PSUF" && continue
506         cp "$LPKG" "$BUILD_ROOT/.build.binaries/$PKG.$PSUF"
507     done
508 fi
509
510 #
511 # get list and ids of already installed rpms
512 #
513 mkdir -p $BUILD_ROOT/.init_b_cache/alreadyinstalled
514 if test -f $BUILD_ROOT/var/lib/rpm/packages.rpm -o -f $BUILD_ROOT/var/lib/rpm/Packages ; then
515     chroot $BUILD_ROOT rpm -qa --qf "%{NAME} $RPMIDFMT" | (
516         while read pp ii; do
517             echo "$ii" > "$BUILD_ROOT/.init_b_cache/alreadyinstalled/$pp"
518         done
519     )
520 fi
521
522 #
523 # reorder packages (already done in XEN continuation)
524 #
525 if ! test -e $BUILD_ROOT/.build/init_buildsystem.data ; then
526     echo -n 'reordering...'
527     PACKAGES_TO_INSTALL_FIRST=`reorder $PACKAGES_TO_INSTALL_FIRST`
528     PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
529     echo 'done'
530 fi
531
532 #
533 # delete all packages we don't want
534 #
535 mkdir -p $BUILD_ROOT/.init_b_cache/todelete
536 for PKG in $BUILD_ROOT/.init_b_cache/alreadyinstalled/* ; do
537     PKG=${PKG##*/}
538     test "$PKG" = "*" && continue
539     ln $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG $BUILD_ROOT/.init_b_cache/todelete/$PKG
540 done
541 for PKG in $PACKAGES_TO_INSTALL_FIRST $PACKAGES_TO_INSTALL ; do
542     rm -f $BUILD_ROOT/.init_b_cache/todelete/$PKG
543 done
544 for PKG in $BUILD_ROOT/.init_b_cache/todelete/* ; do
545     PKG=${PKG##*/}
546     test "$PKG" = "*" && continue
547     echo deleting `sed -e 's/ .*//' < $BUILD_ROOT/.init_b_cache/todelete/$PKG`
548     chroot $BUILD_ROOT rpm --nodeps -e $PKG 2>&1 | \
549         grep -v -e "^r.*failed: No such file or directory" -e "^error: failed to stat .*: No such file or directory"
550 done
551 rm -rf $BUILD_ROOT/.init_b_cache/todelete
552
553 rm -rf $BUILD_ROOT/.init_b_cache/preinstalls
554 mkdir -p $BUILD_ROOT/.init_b_cache/preinstalls
555 for PKG in $PACKAGES_TO_PREINSTALL ; do
556     touch "$BUILD_ROOT/.init_b_cache/preinstalls/$PKG"
557 done
558
559 rm -rf $BUILD_ROOT/installed-pkg
560 mkdir -p $BUILD_ROOT/installed-pkg
561
562 RPMCHECKOPTS=
563 RPMCHECKOPTS_HOST=
564 # on Fedoar 10 rpmbuild is in a separate package so we need something else to
565 # detect rpm4
566 test -x $BUILD_ROOT/usr/bin/rpmsign && RPMCHECKOPTS="--nodigest --nosignature"
567 test -x /usr/bin/rpmsign && RPMCHECKOPTS_HOST="--nodigest --nosignature"
568
569 for PKG in $PACKAGES_TO_INSTALL_FIRST RUN_LDCONFIG $PACKAGES_TO_INSTALL ; do
570     case $PKG in
571       RUN_LDCONFIG)
572         test -x $BUILD_ROOT/sbin/ldconfig && chroot $BUILD_ROOT /sbin/ldconfig 2>&1
573         continue
574       ;;
575     esac
576     test -f $BUILD_ROOT/installed-pkg/$PKG && continue
577
578     if test $PSUF = deb ; then
579         # debian world, install deb files
580         test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb || continue
581         if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.deb" -ef "$BUILD_ROOT/.init_b_cache/$PKG.deb" ; then
582             rm -f $BUILD_ROOT/.init_b_cache/$PKG.deb
583             cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb $BUILD_ROOT/.init_b_cache/$PKG.deb || cleanup_and_exit 1
584         fi
585         PKGID=`readlink $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb`
586         PKGID="${PKGID##*/}"
587         PKGID="${PKGID%.deb}"
588         echo "installing ${PKGID%_*}"
589         ( chroot $BUILD_ROOT dpkg -i --force all .init_b_cache/$PKG.deb 2>&1 || touch $BUILD_ROOT/exit ) | \
590             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'
591         test -e $BUILD_ROOT/exit && cleanup_and_exit 1
592         echo "$PKGID debian" > $BUILD_ROOT/installed-pkg/$PKG
593         continue
594     fi
595
596     test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm || continue
597
598     if test -f $BUILD_ROOT/.init_b_cache/rpms/$PKG.id -a -f $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG ; then
599         read PKGID < $BUILD_ROOT/.init_b_cache/rpms/$PKG.id
600         read OLDPKGID < $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG
601         if test "$PKGID" = "$OLDPKGID" ; then
602             echo "keeping ${PKGID%% *}"
603             echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
604             continue
605         fi
606     fi
607
608     PKGID=`rpm -qp --qf "$RPMIDFMT" $RPMCHECKOPTS_HOST $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm`
609
610     if test -f $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG ; then
611         read OLDPKGID < $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG
612         if test "$PKGID" != "$OLDPKGID" ; then
613             echo deleting unwanted ${OLDPKGID%% *}
614             chroot $BUILD_ROOT rpm --nodeps -e $PKG 2>&1 | \
615                 grep -v -e "^r.*failed: No such file or directory" -e "^error: failed to stat .*: No such file or directory"
616         elif test "$VERIFY_BUILD_SYSTEM" = true ; then
617             chroot $BUILD_ROOT rpm --verify $PKG 2>&1 | tee $TMPFILE
618             if grep ^missing $TMPFILE > /dev/null ; then
619                 echo deleting incomplete ${PKGID%% *}
620                 chroot $BUILD_ROOT rpm --nodeps -e $PKG 2>&1 | \
621                     grep -v -e "^r.*failed: No such file or directory" -e "^error: failed to stat .*: No such file or directory"
622             else
623                 echo "keeping ${PKGID%% *}"
624                 echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
625                 continue
626             fi
627         else
628             echo "keeping ${PKGID%% *}"
629             echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
630             continue
631         fi
632         if test -e "$BUILD_ROOT/.init_b_cache/preinstalls/$PKG" ; then
633             preinstall "$PKG"
634             test rpm = "$PKG" && chroot $BUILD_ROOT rpm --rebuilddb
635         fi
636     fi
637     export ADDITIONAL_PARAMS=
638     if test "$USE_FORCE" = true ; then
639         export ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --force"
640     fi
641     echo "installing ${PKGID%% *}"
642     if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" -ef "$BUILD_ROOT/.init_b_cache/$PKG.rpm" ; then
643         rm -f $BUILD_ROOT/.init_b_cache/$PKG.rpm
644         cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm $BUILD_ROOT/.init_b_cache/$PKG.rpm || cleanup_and_exit 1
645     fi
646     ( chroot $BUILD_ROOT rpm --ignorearch --nodeps -U --oldpackage --ignoresize $RPMCHECKOPTS \
647                 $ADDITIONAL_PARAMS .init_b_cache/$PKG.rpm 2>&1 || \
648           touch $BUILD_ROOT/exit ) | \
649               grep -v "^warning:.*saved as.*rpmorig$"
650     # delete link so package is only installed once
651     rm -f $BUILD_ROOT/.init_b_cache/$PKG.rpm
652     test -e $BUILD_ROOT/exit && cleanup_and_exit 1
653     echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
654 done
655
656 # devices can vanish if devs got uninstalled
657 test -c $BUILD_ROOT/dev/null || create_devs
658
659 cd $BUILD_ROOT || cleanup_and_exit 1
660
661 #
662 # setup /etc/mtab
663 #
664 rm -f $BUILD_ROOT/etc/mtab
665 cp /proc/mounts $BUILD_ROOT/etc/mtab
666 chmod 644 $BUILD_ROOT/etc/mtab
667
668 #
669 # to be sure, path is set correctly, we have to source /etc/profile before
670 # starting rpm.
671 #
672 # XXX
673 #rm -f $BUILD_ROOT/bin/rpm.sh
674 #cp $BUILD_LIBDIR/lib/rpm.sh $BUILD_ROOT/bin/rpm.sh
675 #chmod 755 $BUILD_ROOT/bin/rpm.sh
676 #test -f $BUILD_ROOT/bin/rpm -a ! -L $BUILD_ROOT/bin/rpm && \
677 #    mv $BUILD_ROOT/bin/rpm $BUILD_ROOT/bin/rpm.bin
678 #rm -f $BUILD_ROOT/bin/rpm
679 #ln -s rpm.sh $BUILD_ROOT/bin/rpm
680
681 #
682 # some packages use uname -r to decide which kernel is used to build for.
683 # this does not work in autobuild always.  Here is a wrapper script, that
684 # gets Version from kernel sources.
685 #
686 # XXX
687 #rm -f $BUILD_ROOT/bin/uname.sh
688 #cp -v $BUILD_LIBDIR/lib/uname.sh $BUILD_ROOT/bin/uname.sh
689 #chmod 755 $BUILD_ROOT/bin/uname.sh
690 #test -f $BUILD_ROOT/bin/uname -a ! -L $BUILD_ROOT/bin/uname && \
691 #    mv $BUILD_ROOT/bin/uname $BUILD_ROOT/bin/uname.bin
692 #rm -f $BUILD_ROOT/bin/uname
693 #ln -s uname.sh $BUILD_ROOT/bin/uname
694
695 #
696 # some distributions have a /etc/rpmrc or /etc/rpm/macros and some not.
697 # make sure, that it is setup correctly.
698 #
699 # XXX
700 #rm -f $BUILD_ROOT/etc/rpmrc
701 #if test -e $BUILD_LIBDIR/lib/rpmrc.$BUILD_BASENAME ; then
702 #    cp -v $BUILD_LIBDIR/lib/rpmrc.$BUILD_BASENAME $BUILD_ROOT/etc/rpmrc
703 #elif test -e $BUILD_LIBDIR/lib/rpmrc ; then
704 #    cp -v $BUILD_LIBDIR/lib/rpmrc $BUILD_ROOT/etc/rpmrc
705 #fi
706
707 # XXX
708 #rm -f $BUILD_ROOT/etc/rpm/macros $BUILD_ROOT/etc/rpm/suse_macros
709 #mkdir -p $BUILD_ROOT/etc/rpm
710 #if test -e $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME ; then
711 #    cp -v $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME $BUILD_ROOT/etc/rpm/macros
712 #    cp -v $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME $BUILD_ROOT/etc/rpm/suse_macros
713 #elif test -e $BUILD_LIBDIR/lib/macros ; then
714 #    cp -v $BUILD_LIBDIR/lib/macros $BUILD_ROOT/etc/rpm/macros
715 #    cp -v $BUILD_LIBDIR/lib/macros $BUILD_ROOT/etc/rpm/suse_macros
716 #fi
717
718 #
719 # make sure, that our nis is not present in the chroot system
720 #
721 test -e $BUILD_ROOT/etc/nsswitch.conf && {
722     echo removing nis flags from $BUILD_ROOT/etc/nsswitch.conf...
723     cat $BUILD_ROOT/etc/nsswitch.conf | sed -e"s:nis::g" > \
724         $BUILD_ROOT/etc/nsswitch.conf.tmp
725     mv $BUILD_ROOT/etc/nsswitch.conf.tmp $BUILD_ROOT/etc/nsswitch.conf
726 }
727
728 #
729 # creating some default directories
730 for DIR in /usr/share/doc/packages \
731            /usr/X11R6/include/X11/pixmaps \
732            /usr/X11R6/include/X11/bitmaps ; do
733     mkdir -p $BUILD_ROOT/$DIR
734 done
735
736 for FILE in /var/run/utmp /var/log/wtmp /etc/fstab ; do
737     touch $BUILD_ROOT/$FILE
738 done
739
740 echo now finalizing build dir...
741 CHROOT_RETURN="`chroot $BUILD_ROOT /sbin/ldconfig 2>&1`"
742 case "$CHROOT_RETURN" in
743     *warning:*)
744       chroot $BUILD_ROOT /sbin/ldconfig
745       echo
746       echo chroot $BUILD_ROOT /sbin/ldconfig
747       echo
748       echo "$CHROOT_RETURN"
749       echo
750       echo "Problem with ldconfig.  It's better to reinit the build system..."
751       echo
752       cleanup_and_exit 1
753     ;;
754 esac
755 test -x $BUILD_ROOT/usr/sbin/Check && chroot $BUILD_ROOT /usr/sbin/Check
756
757 mkdir -p $BUILD_ROOT/var/adm/packages
758 touch $BUILD_ROOT/var/adm/packages
759 if test -x $BUILD_ROOT/sbin/SuSEconfig ; then
760     if grep norestarts $BUILD_ROOT/sbin/SuSEconfig > /dev/null ; then
761         chroot $BUILD_ROOT /sbin/SuSEconfig --norestarts --force
762     else
763         chroot $BUILD_ROOT /sbin/SuSEconfig --force
764     fi
765 fi
766
767 if test -x $BUILD_ROOT/usr/X11R6/bin/switch2mesasoft ; then
768     chroot $BUILD_ROOT /usr/X11R6/bin/switch2mesasoft
769 fi
770
771 for PROG in /usr/bin/TeX/texhash /usr/bin/texhash ; do
772     test -x $BUILD_ROOT/$PROG && \
773         chroot $BUILD_ROOT bash -c ". /etc/profile ; $PROG"
774 done
775
776 if test -e $BUILD_ROOT/usr/share/zoneinfo/UTC ; then
777     chroot $BUILD_ROOT zic -l UTC
778 fi
779
780 test -e $BUILD_ROOT/.build/init_buildsystem.data || HOST=`hostname`
781 test -e $BUILD_ROOT/etc/hosts || echo "127.0.0.1 localhost" > $BUILD_ROOT/etc/hosts
782 if ! grep -F "127.0.0.1 $HOST" $BUILD_ROOT/etc/hosts > /dev/null ; then
783     # this makes a reverse lookup on 127.0.0.1 return the host name,
784     # which is bad, but 127.0.0.2 does not work on all unix systems
785     echo "127.0.0.1 $HOST" > $BUILD_ROOT/etc/hosts.new
786     test -f $BUILD_ROOT/etc/hosts && cat $BUILD_ROOT/etc/hosts >> $BUILD_ROOT/etc/hosts.new
787     mv $BUILD_ROOT/etc/hosts.new $BUILD_ROOT/etc/hosts
788 fi
789
790 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
791     echo "initializing rpm db..."
792     chroot $BUILD_ROOT rpm --initdb || cleanup_and_exit 1
793     # create provides index
794     chroot $BUILD_ROOT rpm -q --whatprovides rpm >/dev/null 2>&1
795 fi
796
797 rm -rf $BUILD_ROOT/.init_b_cache
798
799 rm -f $BUILD_IS_RUNNING
800
801 rm -f $TMPFILE
802
803 if df $BUILD_ROOT 2>/dev/null | grep -q "100%"; then
804     df -h $BUILD_ROOT
805     echo "build does not work on a completely full filesystem"
806     cleanup_and_exit 1
807 fi
808
809 cleanup_and_exit 0