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