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