set mmap_min_addr to 0 in vm for qemu
[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 #
308 # Detect cross-build (fixed code) and copy qemus for cross-build emulation mode
309 # And copy also a static host binary shell to run the init scripts in the target chroot
310 #
311 copy_qemu()
312 {
313     echo "copying qemu"
314     mkdir -p  $BUILD_ROOT/usr/bin $BUILD_ROOT/usr/sbin
315     install -m755 /usr/bin/qemu-* $BUILD_ROOT/usr/bin
316     install -m755 /usr/sbin/qemu-binfmt-conf.sh $BUILD_ROOT/usr/sbin
317     # To initially run the init script, there needs to be also a
318     # static bash to register qemu in this chroot and to start up
319     # /.build/build
320     mkdir -p  $BUILD_ROOT/bin
321     install -m755 /bin/bash-static $BUILD_ROOT/bin
322     install -m755 /bin/mount-static $BUILD_ROOT/bin
323 }
324
325 check_binfmt_registered()
326 {
327     local arch
328     for arch in arm $EMULATOR_ARCHS; do
329         if test -e /proc/sys/fs/binfmt_misc/$arch; then
330             return 0
331         fi
332     done
333     return 1
334 }
335
336 fail_exit()
337 {
338   cleanup_and_exit 1
339 }
340
341 # modify $SRC
342 download_zypp()
343 {
344     local url="$1"
345     local zd="/var/cache/zypp/packages/"
346     SRC=$zd${url#zypp://}
347     if [ ! -e "$SRC" ]; then
348         local destdir="$zd/tmp"
349         mkdir -p "$destdir"
350         echo "downloading $url ... ";
351         $BUILD_DIR/download "$destdir" "$url" || cleanup_and_exit 1
352         local destfile="$destdir/${url##*/}"
353         if [ ! -e "$destfile" ]; then
354             echo "expected $destfile after download but it's missing" >&2
355             cleanup_and_exit 1
356         fi
357         rpm -K "$destfile" > $destfile.v || { echo "rpm verify failed" >&2; rm -rf "$destdir"; cleanup_and_exit 1; }
358         if grep "NOT OK" $destfile.v; then
359             rm -rf "$destdir"
360             cleanup_and_exit 1
361         fi
362         rm -f "$destfile.v"
363         mv "$destfile" "$SRC" || cleanup_and_exit 1
364     fi
365 }
366
367 set_build_arch
368
369 trap fail_exit EXIT
370
371 if [ "$BUILD_ROOT" = / ]; then
372     read dummy dummy browner dummy < <(ls -ld /)
373 fi
374
375 if [ -n "$CLEAN_BUILD" ]; then
376     clean_build_root
377 fi
378
379 #
380 # now test if there was an incomplete run
381 #
382 if test -e $BUILD_IS_RUNNING ; then
383     echo It seems that there was an incomplete setup of $BUILD_ROOT.
384     echo To be sure, we will build it again completely...
385     umount -n $BUILD_ROOT/proc/sys/fs/binfmt_misc 2> /dev/null || true
386     umount -n $BUILD_ROOT/proc 2> /dev/null
387     umount -n $BUILD_ROOT/dev/pts 2> /dev/null
388     umount -n $BUILD_ROOT/mnt 2> /dev/null
389     echo "Your build system is broken!! Shall I execute"
390     echo
391     echo "    rm -rf -- $BUILD_ROOT/*"
392     echo
393     echo "y - yes, cleanup the build root"
394     echo "N - No, abort build (default on enter)"
395     echo "c - Continue anyway with this build root"
396     echo -n "[y/N/c] "
397     read ANSWER
398     case "$ANSWER" in
399         c|C) rm -f $BUILD_IS_RUNNING ;;
400         y|Y) clean_build_root ;;
401         *) cleanup_and_exit 1 ;;
402     esac
403 fi
404
405 #
406 # store that we start to build system
407 #
408 mkdir -p $BUILD_ROOT
409 touch $BUILD_IS_RUNNING
410
411 if test -n "$PREPARE_VM" ; then
412     rm -f $BUILD_ROOT/.build/init_buildsystem.data
413 fi
414 if test -e $BUILD_ROOT/.build/init_buildsystem.data ; then
415     # vm continuation
416     . $BUILD_ROOT/.build/init_buildsystem.data
417     if ! test -e $BUILD_ROOT/.init_b_cache/preinstall_finished ; then
418         # finish preinstall
419         preinstall '' true
420         init_db
421         touch $BUILD_ROOT/.init_b_cache/preinstall_finished
422     fi
423 else
424     #
425     # now make sure that all the packages are installed.
426     #
427     rm -rf $BUILD_ROOT/.init_b_cache
428     mkdir -p $BUILD_ROOT/.init_b_cache/scripts
429
430     if test -z "$RPMLIST" ; then
431         #
432         # create rpmdeps file
433         #
434         CACHE_FILE=$BUILD_ROOT/.srcfiles.cache
435         validate_cache_file
436
437         #
438         # select and expand packages
439         #
440         RPMLIST=$BUILD_ROOT/.init_b_cache/rpmlist
441         test -z "$LIST_STATE" && echo "expanding package dependencies..."
442         if ! $BUILD_DIR/expanddeps $USEUSEDFORBUILD "${definesnstuff[@]}" --dist "$BUILD_DIST" --depfile "$CACHE_FILE" --archpath "$BUILD_ARCH" --configdir $BUILD_DIR/configs "${PKGS[@]}" > $RPMLIST ; then
443             rm -f $BUILD_IS_RUNNING
444             cleanup_and_exit 1
445         fi
446     fi
447
448     if test -n "$LIST_STATE" ; then
449         rm -f $BUILD_IS_RUNNING
450         while read PKG SRC ; do
451            test "$PKG" = "preinstall:" && continue
452            test "$PKG" = "runscripts:" && continue
453            test "$PKG" = "vminstall:" && continue
454            test "$PKG" = "dist:" && continue
455            test "$PKG" = "rpmid:" && continue
456            echo "${SRC##*/}"
457         done < $BUILD_ROOT/.init_b_cache/rpmlist
458         rm -rf $BUILD_ROOT/.init_b_cache
459         cleanup_and_exit 0
460     fi
461
462     #
463     # copy the QEMU emulator
464     #
465     if check_use_emulator; then
466         copy_qemu
467         if [ -z "$PREPARE_VM" ]; then
468             if ! check_binfmt_registered; then
469                 echo "registering binfmt handlers"
470                 /usr/sbin/qemu-binfmt-conf.sh
471             fi
472             read mmap_min_addr < /proc/sys/vm/mmap_min_addr
473             if [ "$mmap_min_addr" != 0 ]; then
474                 echo "Warning: mmap_min_addr is != 0. If programs fail at mmap this could be the reason"
475             fi
476         fi
477     fi
478
479     PACKAGES_TO_INSTALL=
480     PACKAGES_TO_PREINSTALL=
481     PACKAGES_TO_RUNSCRIPTS=
482     PACKAGES_TO_VMINSTALL=
483     RUNSCRIPTS_SEEN=
484     GUESSED_DIST=unknown
485     mkdir -p $BUILD_ROOT/.init_b_cache/rpms
486     while read PKG SRC ; do
487         if test "$PKG" = "preinstall:" ; then
488             PACKAGES_TO_PREINSTALL=$SRC
489             continue
490         fi
491         if test "$PKG" = "vminstall:" ; then
492             PACKAGES_TO_VMINSTALL=$SRC
493             continue
494         fi
495         if test "$PKG" = "runscripts:" ; then
496             RUNSCRIPTS_SEEN=true
497             PACKAGES_TO_RUNSCRIPTS=$SRC
498             continue
499         fi
500         if test "$PKG" = "dist:" ; then
501             GUESSED_DIST=$SRC
502             continue
503         fi
504         if test "$PKG" = "rpmid:" ; then
505             echo "${SRC#*:}" > $BUILD_ROOT/.init_b_cache/rpms/${SRC%%:*}.id
506             continue
507         fi
508         if [ "${SRC#/}" = "$SRC" ]; then
509             url="$SRC"
510             case "$url" in
511                 zypp://*)
512                     download_zypp "$url"
513                     ;;
514                 *)
515                     echo "unsupported url: $url" >&2
516                     cleanup_and_exit 1
517                     ;;
518             esac
519         fi
520         ln -s "$SRC" "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRC##*.}"
521         PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL $PKG"
522     done < $RPMLIST
523
524     # compatibility...
525     test -z "$RUNSCRIPTS_SEEN" && PACKAGES_TO_RUNSCRIPTS="$PACKAGES_TO_PREINSTALL"
526
527     echo "$GUESSED_DIST" > $BUILD_ROOT/.guessed_dist
528     PSUF=rpm
529     test -L $BUILD_ROOT/.init_b_cache/rpms/rpm.rpm || PSUF=deb
530 fi
531
532 #
533 # now test if there is already a build dir.
534 #
535 if test ! -f $BUILD_ROOT/var/lib/rpm/packages.rpm -a ! -f $BUILD_ROOT/var/lib/rpm/Packages ; then
536     mkdir -p $BUILD_ROOT/var/lib/rpm || cleanup_and_exit 1
537     mkdir -p $BUILD_ROOT/usr/src/packages/SOURCES || cleanup_and_exit 1
538     mkdir -p $BUILD_ROOT/etc || cleanup_and_exit 1
539     mkdir -p $BUILD_ROOT/proc || cleanup_and_exit 1
540     test -f $BUILD_ROOT/etc/HOSTNAME || hostname -f > $BUILD_ROOT/etc/HOSTNAME
541     if test $PSUF = deb ; then
542         mkdir -p $BUILD_ROOT/var/lib/dpkg
543         mkdir -p $BUILD_ROOT/var/log
544         mkdir -p $BUILD_ROOT/etc/default
545         :> $BUILD_ROOT/var/lib/dpkg/status
546         :> $BUILD_ROOT/var/lib/dpkg/available
547         :> $BUILD_ROOT/var/log/dpkg.log
548         :> $BUILD_ROOT/etc/ld.so.conf
549         :> $BUILD_ROOT/etc/default/rcS
550     fi
551     for PKG in $PACKAGES_TO_RUNSCRIPTS ; do
552         : > $BUILD_ROOT/.init_b_cache/scripts/$PKG.run
553     done
554     for PKG in $PACKAGES_TO_PREINSTALL ; do
555         preinstall ${PKG##*/}
556     done
557     if test -n "$PREPARE_VM" ; then
558         for PKG in $PACKAGES_TO_VMINSTALL ; do
559             preinstall ${PKG##*/}
560         done
561     fi
562     if [ -w /root ]; then
563             test -c $BUILD_ROOT/dev/null || create_devs
564     fi
565     test -e $BUILD_ROOT/etc/fstab || touch $BUILD_ROOT/etc/fstab
566     test -e $BUILD_ROOT/etc/ld.so.conf || cp $BUILD_ROOT/etc/ld.so.conf.in $BUILD_ROOT/etc/ld.so.conf
567     if test -z "$PREPARE_VM" ; then
568         preinstall '' true
569         init_db
570         touch $BUILD_ROOT/.init_b_cache/preinstall_finished
571     fi
572 fi
573
574 if test -n "$PREPARE_VM" ; then
575     mkdir -p $BUILD_ROOT/.build
576     echo "copying packages..."
577     for PKG in $PACKAGES_TO_INSTALL ; do
578         rm -f $BUILD_ROOT/.init_b_cache/$PKG.$PSUF
579         cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF $BUILD_ROOT/.init_b_cache/$PKG.$PSUF || cleanup_and_exit 1
580         ln -s -f ../$PKG.$PSUF $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF
581         check_exit
582     done
583     # alreadyinstalled check will not work, but we have to live with
584     # that...
585     echo -n 'reordering...'
586     PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
587     echo 'done'
588     Q="'\''"
589     echo "PACKAGES_TO_INSTALL='${PACKAGES_TO_INSTALL//"'"/$Q}'" > $BUILD_ROOT/.build/init_buildsystem.data
590     echo "PACKAGES_TO_RUNSCRIPTS='${PACKAGES_TO_RUNSCRIPTS//"'"/$Q}'" >> $BUILD_ROOT/.build/init_buildsystem.data
591     echo "PSUF='$PSUF'" >> $BUILD_ROOT/.build/init_buildsystem.data
592     rm -f $BUILD_IS_RUNNING
593     cleanup_and_exit 0
594 fi
595
596 mkdir -p $BUILD_ROOT/proc
597 mkdir -p $BUILD_ROOT/dev/pts
598 mount -n -tproc none $BUILD_ROOT/proc 2>/dev/null || true
599 mount -n -tdevpts none $BUILD_ROOT/dev/pts 2>/dev/null || true
600
601 #
602 # create .build.binaries directory if requested
603 #
604 rm -rf $BUILD_ROOT/.build.binaries
605 if test -n "$CREATE_BUILD_BINARIES" ; then
606     echo "creating .build.binaries directory..."
607     mkdir -p "$BUILD_ROOT/.build.binaries"
608     for PKG in $PACKAGES_TO_INSTALL ; do
609         test -L "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" || continue
610         LPKG=`readlink -f "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF"`
611         ln "$LPKG" "$BUILD_ROOT/.build.binaries/$PKG.$PSUF" 2>/dev/null
612         test -f "$BUILD_ROOT/.build.binaries/$PKG.$PSUF" && continue
613         cp "$LPKG" "$BUILD_ROOT/.build.binaries/$PKG.$PSUF"
614         check_exit
615     done
616 fi
617
618 #
619 # get list and ids of already installed rpms
620 #
621 mkdir -p $BUILD_ROOT/.init_b_cache/alreadyinstalled
622 if test -f $BUILD_ROOT/var/lib/rpm/packages.rpm -o -f $BUILD_ROOT/var/lib/rpm/Packages ; then
623     chroot $BUILD_ROOT rpm -qa --qf "%{NAME} $RPMIDFMT" | (
624         while read pp ii; do
625             echo "$ii" > "$BUILD_ROOT/.init_b_cache/alreadyinstalled/$pp"
626         done
627     )
628 fi
629
630 #
631 # reorder packages (already done in vm continuation)
632 #
633 if ! test -e $BUILD_ROOT/.build/init_buildsystem.data ; then
634     echo -n 'reordering...'
635     PACKAGES_TO_INSTALL_FIRST=`reorder $PACKAGES_TO_INSTALL_FIRST`
636     PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
637     echo 'done'
638 fi
639
640 rpm_e()
641 {
642     chroot $BUILD_ROOT rpm --nodeps -e $PKG 2>&1 | \
643     while read line; do
644         case "$line" in
645
646             r*failed:\ No\ such\ file\ or\ directory) ;;
647             error:\ failed\ to\ stat\ *:\ No\ such\ file\ or\ directory) ;;
648             error:\ *scriptlet\ failed*)
649                 echo "$line"
650                 echo "re-try deleting $PKG using --noscripts"
651                 chroot $BUILD_ROOT rpm --nodeps --noscripts -e $PKG || true
652             ;;
653             *) echo "$line" ;;
654         esac
655     done
656 }
657
658 #
659 # delete all packages we don't want
660 #
661 mkdir -p $BUILD_ROOT/.init_b_cache/todelete
662 for PKG in $BUILD_ROOT/.init_b_cache/alreadyinstalled/* ; do
663     PKG=${PKG##*/}
664     test "$PKG" = "*" && continue
665     ln $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG $BUILD_ROOT/.init_b_cache/todelete/$PKG
666 done
667 for PKG in $PACKAGES_TO_INSTALL_FIRST $PACKAGES_TO_INSTALL ; do
668     rm -f $BUILD_ROOT/.init_b_cache/todelete/$PKG
669 done
670 for PKG in $BUILD_ROOT/.init_b_cache/todelete/* ; do
671     PKG=${PKG##*/}
672     test "$PKG" = "*" && continue
673     echo "deleting $PKG"
674     rpm_e "$PKG"
675     check_exit
676 done
677 rm -rf $BUILD_ROOT/.init_b_cache/todelete
678
679 rm -rf $BUILD_ROOT/.init_b_cache/preinstalls
680 mkdir -p $BUILD_ROOT/.init_b_cache/preinstalls
681 for PKG in $PACKAGES_TO_PREINSTALL ; do
682     touch "$BUILD_ROOT/.init_b_cache/preinstalls/$PKG"
683 done
684
685 rm -rf $BUILD_ROOT/installed-pkg
686 mkdir -p $BUILD_ROOT/installed-pkg
687
688 RPMCHECKOPTS=
689 RPMCHECKOPTS_HOST=
690 # on Fedora 10 rpmbuild is in a separate package so we need something else to
691 # detect rpm4
692 test -x $BUILD_ROOT/usr/bin/rpmsign && RPMCHECKOPTS="--nodigest --nosignature"
693 test -x /usr/bin/rpmsign && RPMCHECKOPTS_HOST="--nodigest --nosignature"
694
695 for PKG in $PACKAGES_TO_INSTALL_FIRST RUN_LDCONFIG $PACKAGES_TO_INSTALL ; do
696
697     case $PKG in
698       RUN_LDCONFIG)
699         test -x $BUILD_ROOT/sbin/ldconfig && chroot $BUILD_ROOT /sbin/ldconfig 2>&1
700         continue
701       ;;
702     esac
703
704     test -f $BUILD_ROOT/installed-pkg/$PKG && continue
705
706     if test $PSUF = deb ; then
707         # debian world, install deb files
708         test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb || continue
709         if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.deb" -ef "$BUILD_ROOT/.init_b_cache/$PKG.deb" ; then
710             rm -f $BUILD_ROOT/.init_b_cache/$PKG.deb
711             cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb $BUILD_ROOT/.init_b_cache/$PKG.deb || cleanup_and_exit 1
712         fi
713         PKGID=`readlink $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb`
714         PKGID="${PKGID##*/}"
715         PKGID="${PKGID%.deb}"
716         echo "installing ${PKGID%_*}"
717         ( chroot $BUILD_ROOT dpkg --install --force all .init_b_cache/$PKG.deb 2>&1 || touch $BUILD_ROOT/exit ) | \
718             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'
719         check_exit
720         echo "$PKGID debian" > $BUILD_ROOT/installed-pkg/$PKG
721         continue
722     fi
723
724     test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm || continue
725
726     if test -f $BUILD_ROOT/.init_b_cache/rpms/$PKG.id -a -f $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG ; then
727         read PKGID < $BUILD_ROOT/.init_b_cache/rpms/$PKG.id
728         read OLDPKGID < $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG
729         if test "$PKGID" = "$OLDPKGID" ; then
730             #echo "keeping ${PKGID%% *}"
731             echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
732             continue
733         fi
734     fi
735
736     PKGID=`rpm -qp --qf "$RPMIDFMT" $RPMCHECKOPTS_HOST $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm`
737
738     if test -f $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG ; then
739         read OLDPKGID < $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG
740         if test "$PKGID" != "$OLDPKGID" ; then
741             echo deleting unwanted ${OLDPKGID%% *}
742             rpm_e "$PKG"
743         elif test "$VERIFY_BUILD_SYSTEM" = true ; then
744             chroot $BUILD_ROOT rpm --verify $PKG 2>&1 | tee $TMPFILE
745             if grep ^missing $TMPFILE > /dev/null ; then
746                 echo deleting incomplete ${PKGID%% *}
747                 rpm_e "$PKG"
748             else
749                 #echo "keeping ${PKGID%% *}"
750                 echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
751                 continue
752             fi
753         else
754             #echo "keeping ${PKGID%% *}"
755             echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
756             continue
757         fi
758         if test -e "$BUILD_ROOT/.init_b_cache/preinstalls/$PKG" ; then
759             preinstall "$PKG"
760             test rpm = "$PKG" && chroot $BUILD_ROOT rpm --rebuilddb
761         fi
762     fi
763     export ADDITIONAL_PARAMS=
764     if test "$USE_FORCE" = true ; then
765         export ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --force"
766     fi
767     echo "installing ${PKGID%% *}"
768     if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" -ef "$BUILD_ROOT/.init_b_cache/$PKG.rpm" ; then
769         rm -f $BUILD_ROOT/.init_b_cache/$PKG.rpm
770         cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm $BUILD_ROOT/.init_b_cache/$PKG.rpm || cleanup_and_exit 1
771     fi
772     ( chroot $BUILD_ROOT rpm --ignorearch --nodeps -U --oldpackage --ignoresize $RPMCHECKOPTS \
773                 $ADDITIONAL_PARAMS .init_b_cache/$PKG.rpm 2>&1 || \
774           touch $BUILD_ROOT/exit ) | \
775               grep -v "^warning:.*saved as.*rpmorig$"
776     # delete link so package is only installed once
777     rm -f $BUILD_ROOT/.init_b_cache/$PKG.rpm
778     check_exit
779     echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
780
781 done
782
783 if test $PSUF = deb ; then
784     echo "configure all installed packages..."
785     # configure all packages after complete installation, not for each package like rpm does
786     # We need to run this twice, because of cyclic dependencies as it does not succeed on most
787     # debian based distros in the first attempt.
788     if ! chroot $BUILD_ROOT dpkg --configure --pending  2>&1; then
789        echo "first configure attempt failed, trying again..."
790        chroot $BUILD_ROOT dpkg --configure --pending  2>&1 || touch $BUILD_ROOT/exit
791     fi
792 fi
793
794 # devices can vanish if devs got uninstalled
795 test -c $BUILD_ROOT/dev/null || create_devs
796
797 cd $BUILD_ROOT || cleanup_and_exit 1
798
799 #
800 # setup /etc/mtab
801 #
802 rm -f $BUILD_ROOT/etc/mtab
803 cp /proc/mounts $BUILD_ROOT/etc/mtab
804 chmod 644 $BUILD_ROOT/etc/mtab
805
806 #
807 # to be sure, path is set correctly, we have to source /etc/profile before
808 # starting rpm.
809 #
810 # XXX
811 #rm -f $BUILD_ROOT/bin/rpm.sh
812 #cp $BUILD_LIBDIR/lib/rpm.sh $BUILD_ROOT/bin/rpm.sh
813 #chmod 755 $BUILD_ROOT/bin/rpm.sh
814 #test -f $BUILD_ROOT/bin/rpm -a ! -L $BUILD_ROOT/bin/rpm && \
815 #    mv $BUILD_ROOT/bin/rpm $BUILD_ROOT/bin/rpm.bin
816 #rm -f $BUILD_ROOT/bin/rpm
817 #ln -s rpm.sh $BUILD_ROOT/bin/rpm
818
819 #
820 # some packages use uname -r to decide which kernel is used to build for.
821 # this does not work in autobuild always.  Here is a wrapper script, that
822 # gets Version from kernel sources.
823 #
824 # XXX
825 #rm -f $BUILD_ROOT/bin/uname.sh
826 #cp -v $BUILD_LIBDIR/lib/uname.sh $BUILD_ROOT/bin/uname.sh
827 #chmod 755 $BUILD_ROOT/bin/uname.sh
828 #test -f $BUILD_ROOT/bin/uname -a ! -L $BUILD_ROOT/bin/uname && \
829 #    mv $BUILD_ROOT/bin/uname $BUILD_ROOT/bin/uname.bin
830 #rm -f $BUILD_ROOT/bin/uname
831 #ln -s uname.sh $BUILD_ROOT/bin/uname
832
833 #
834 # some distributions have a /etc/rpmrc or /etc/rpm/macros and some not.
835 # make sure, that it is setup correctly.
836 #
837 # XXX
838 #rm -f $BUILD_ROOT/etc/rpmrc
839 #if test -e $BUILD_LIBDIR/lib/rpmrc.$BUILD_BASENAME ; then
840 #    cp -v $BUILD_LIBDIR/lib/rpmrc.$BUILD_BASENAME $BUILD_ROOT/etc/rpmrc
841 #elif test -e $BUILD_LIBDIR/lib/rpmrc ; then
842 #    cp -v $BUILD_LIBDIR/lib/rpmrc $BUILD_ROOT/etc/rpmrc
843 #fi
844
845 # XXX
846 #rm -f $BUILD_ROOT/etc/rpm/macros $BUILD_ROOT/etc/rpm/suse_macros
847 #mkdir -p $BUILD_ROOT/etc/rpm
848 #if test -e $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME ; then
849 #    cp -v $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME $BUILD_ROOT/etc/rpm/macros
850 #    cp -v $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME $BUILD_ROOT/etc/rpm/suse_macros
851 #elif test -e $BUILD_LIBDIR/lib/macros ; then
852 #    cp -v $BUILD_LIBDIR/lib/macros $BUILD_ROOT/etc/rpm/macros
853 #    cp -v $BUILD_LIBDIR/lib/macros $BUILD_ROOT/etc/rpm/suse_macros
854 #fi
855
856 #
857 # make sure, that our nis is not present in the chroot system
858 #
859 test -e $BUILD_ROOT/etc/nsswitch.conf && {
860     echo removing nis flags from $BUILD_ROOT/etc/nsswitch.conf...
861     cat $BUILD_ROOT/etc/nsswitch.conf | sed -e"s:nis::g" > \
862         $BUILD_ROOT/etc/nsswitch.conf.tmp
863     mv $BUILD_ROOT/etc/nsswitch.conf.tmp $BUILD_ROOT/etc/nsswitch.conf
864 }
865
866 #
867 # creating some default directories
868 for DIR in /usr/share/doc/packages \
869            /usr/X11R6/include/X11/pixmaps \
870            /usr/X11R6/include/X11/bitmaps ; do
871     mkdir -p $BUILD_ROOT/$DIR
872 done
873
874 for FILE in /var/run/utmp /var/log/wtmp /etc/fstab ; do
875     touch $BUILD_ROOT/$FILE
876 done
877
878 echo now finalizing build dir...
879 CHROOT_RETURN="`chroot $BUILD_ROOT /sbin/ldconfig 2>&1`"
880 case "$CHROOT_RETURN" in
881     *warning:*)
882       chroot $BUILD_ROOT /sbin/ldconfig
883       echo
884       echo chroot $BUILD_ROOT /sbin/ldconfig
885       echo
886       echo "$CHROOT_RETURN"
887       echo
888       echo "Problem with ldconfig.  It's better to reinit the build system..."
889       echo
890       cleanup_and_exit 1
891     ;;
892 esac
893 test -x $BUILD_ROOT/usr/sbin/Check && chroot $BUILD_ROOT /usr/sbin/Check
894
895 mkdir -p $BUILD_ROOT/var/adm/packages
896 touch $BUILD_ROOT/var/adm/packages
897 if test -x $BUILD_ROOT/sbin/SuSEconfig ; then
898     if grep norestarts $BUILD_ROOT/sbin/SuSEconfig > /dev/null ; then
899         chroot $BUILD_ROOT /sbin/SuSEconfig --norestarts --force
900     else
901         chroot $BUILD_ROOT /sbin/SuSEconfig --force
902     fi
903 fi
904
905 if test -x $BUILD_ROOT/usr/X11R6/bin/switch2mesasoft ; then
906     chroot $BUILD_ROOT /usr/X11R6/bin/switch2mesasoft
907 fi
908
909 for PROG in /usr/bin/TeX/texhash /usr/bin/texhash ; do
910     test -x $BUILD_ROOT/$PROG && \
911         chroot $BUILD_ROOT bash -c ". /etc/profile ; $PROG"
912 done
913
914 if test -e $BUILD_ROOT/usr/share/zoneinfo/UTC ; then
915     chroot $BUILD_ROOT zic -l UTC
916 fi
917
918 test -e $BUILD_ROOT/.build/init_buildsystem.data || HOST=`hostname`
919 test -e $BUILD_ROOT/etc/hosts || echo "127.0.0.1 localhost" > $BUILD_ROOT/etc/hosts
920 if ! grep -F "127.0.0.1 $HOST" $BUILD_ROOT/etc/hosts > /dev/null ; then
921     # this makes a reverse lookup on 127.0.0.1 return the host name,
922     # which is bad, but 127.0.0.2 does not work on all unix systems
923     echo "127.0.0.1 $HOST" > $BUILD_ROOT/etc/hosts.new
924     test -f $BUILD_ROOT/etc/hosts && cat $BUILD_ROOT/etc/hosts >> $BUILD_ROOT/etc/hosts.new
925     mv $BUILD_ROOT/etc/hosts.new $BUILD_ROOT/etc/hosts
926 fi
927
928 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
929     echo "initializing rpm db..."
930     chroot $BUILD_ROOT rpm --initdb || cleanup_and_exit 1
931     # create provides index
932     chroot $BUILD_ROOT rpm -q --whatprovides rpm >/dev/null 2>&1
933 fi
934
935 # create modules.dep in kvm/xen
936 # This can not work, until we use the native repository kernel
937 #if [ $BUILD_ROOT = "/" -a -x /sbin/depmod ]; then
938 #  /sbin/depmod -a
939 #fi
940
941 rm -f $BUILD_ROOT/.rpmmacros $BUILD_ROOT/root/.rpmmacros
942 rm -rf $BUILD_ROOT/.init_b_cache
943 rm -f $BUILD_IS_RUNNING
944 rm -f $TMPFILE
945
946 cleanup_and_exit 0