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