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