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