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