Update uid/gid of build root if uid/gid is not match
[tools/obs-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 USE_SYSTEM_QEMU=
38 KEEP_PACKS=
39 USEUSEDFORBUILD=
40 LIST_STATE=
41 RPMLIST=
42 CLEAN_BUILD=
43 CREATE_BUILD_BINARIES=
44
45 while test -n "$1" ; do
46     case "$1" in
47         --prepare)
48             shift
49             PREPARE_VM=true
50             ;;
51         --use-system-qemu)
52             shift
53             USE_SYSTEM_QEMU=true
54             ;;
55         --keep-packs)
56             shift
57             KEEP_PACKS=true
58             ;;
59         --create-build-binaries)
60             shift
61             CREATE_BUILD_BINARIES=true
62             ;;
63         --useusedforbuild)
64             shift
65             USEUSEDFORBUILD=--useusedforbuild
66             ;;
67         --list-state)
68             shift
69             LIST_STATE=true
70             ;;
71         --rpmlist)
72             shift
73             RPMLIST=$1
74             shift
75             ;;
76         --define|--with|--without)
77             definesnstuff[${#definesnstuff[@]}]="$1";
78             definesnstuff[${#definesnstuff[@]}]="$2";
79             shift 2
80             ;;
81         --repository|--repo)
82             repos[${#repos[@]}]="$2";
83             shift 2;
84             ;;
85         --clean)
86             CLEAN_BUILD="$1"
87             shift
88         ;;
89         --cachedir)
90             CACHE_DIR="$2"
91             shift 2
92             ;;
93         --configdir)
94             CONFIG_DIR=$2
95             shift 2
96             ;;
97         *)
98             break
99             ;;
100     esac
101 done
102 PKGS=("$@")
103
104 #
105 # needed functions
106 #
107
108 cleanup_and_exit()
109 {
110     trap EXIT
111     test "$BUILD_ROOT" = / -a -n "$browner" && chown "$browner" "$BUILD_ROOT"
112     # umount so init_buildsystem can be used standalone
113 # XXX: use stat -f /dev/pts/ -c %T  to check whether it's mounted and not suppress errors then?
114     umount -n "$BUILD_ROOT/proc/sys/fs/binfmt_misc" 2> /dev/null || true
115     umount -n "$BUILD_ROOT/proc" 2> /dev/null || true
116     umount -n "$BUILD_ROOT/dev/pts" 2> /dev/null || true
117     umount -n "$BUILD_ROOT/mnt" 2> /dev/null || true
118     exit ${1:-0}
119 }
120
121 clean_build_root()
122 {
123         test -n "$BUILD_ROOT" && {
124             umount -n "$BUILD_ROOT/proc/sys/fs/binfmt_misc" 2> /dev/null || true
125             umount -n "$BUILD_ROOT/proc" 2> /dev/null || true
126             umount -n "$BUILD_ROOT/dev/pts" 2> /dev/null || true
127             umount -n "$BUILD_ROOT/mnt" 2> /dev/null || true
128             rm -rf -- "$BUILD_ROOT"/*
129             rm -rf -- "$BUILD_ROOT/.build"
130             rm -rf -- "$BUILD_ROOT/.root"
131             rm -rf -- "$BUILD_ROOT/.init_b_cache"
132             rm -rf -- "$BUILD_ROOT/.preinstall_image/*"
133             rm -rf -- "$BUILD_ROOT/.preinstallimage"*
134             mkdir -p "$BUILD_ROOT/proc"
135             mkdir -p "$BUILD_ROOT/dev/pts"
136             if [ "$UID" = '0' ]; then
137                     mount -n -tproc none "$BUILD_ROOT/proc"
138                     mount -n -tdevpts none "$BUILD_ROOT/dev/pts"
139             fi
140         }
141 }
142
143
144 unsafe_preinstall_check()
145 {
146     # cpio isn't safe so we require bsdtar for VMs. chroot is
147     # unsafe anyways so it's ok for that.
148     if test -n "$PREPARE_VM" ; then
149         echo "Error: setting up a VM requires bsdtar for security reasons."
150         echo "Please install bsdtar."
151         cleanup_and_exit 1
152     fi
153 }
154
155 preinstall_image_filter()
156 {
157     for PKG in "$@" ; do
158         test -e "$BUILD_ROOT/.preinstall_image/$PKG" && continue
159         echo $PKG
160     done
161 }
162
163 preinstall_image()
164 {
165     check_exit
166     if test -n "$2" ; then
167         echo "unpacking preinstall image $2"
168     else
169         echo "unpacking preinstall image"
170     fi
171     cd $BUILD_ROOT || cleanup_and_exit 1
172     if test -x /usr/bin/bsdtar ; then
173         TAR="/usr/bin/bsdtar -P --chroot --numeric-owner -x"
174     else
175         unsafe_preinstall_check
176         TAR="tar -x"
177     fi
178     # pipe output through perl to print a dot every 1000 lines
179     if ! $TAR -z --exclude .build --exclude .init_b_cache -f "$BUILD_ROOT/.init_b_cache/rpms/$1" -v > >(tee "$BUILD_ROOT/.preinstallimage.unpack" | perl -e '$|=1; my $done=0; $done++ % 1000 or print "." while <STDIN>; print "\n";')  2>&1 ; then
180         echo "unpack failed."
181         test -e "$BUILD_ROOT/.preinstallimage.unpack" && cat "$BUILD_ROOT/.preinstallimage.unpack"
182         cleanup_and_exit 1
183     fi
184     rm -f "$BUILD_ROOT/.preinstallimage.unpack"
185 }
186
187 preinstall()
188 {
189     check_exit
190     echo "preinstalling $1..."
191     cd $BUILD_ROOT || cleanup_and_exit 1
192     if test -x /usr/bin/bsdtar ; then
193         CPIO="/usr/bin/bsdtar -P --chroot -o --numeric-owner -x -f-"
194         TAR="/usr/bin/bsdtar -P --chroot -o --numeric-owner -x"
195     else
196         unsafe_preinstall_check
197         CPIO="cpio --extract --unconditional --preserve-modification-time --make-directories --no-absolute-filenames --quiet"
198         TAR="tar -x"
199     fi
200     if test -e "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" ; then
201         PAYLOADDECOMPRESS=cat
202         case `rpm -qp --nodigest --nosignature --qf "%{PAYLOADCOMPRESSOR}\n" "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm"` in
203             lzma) rpm --showrc | grep PayloadIsLzma > /dev/null || PAYLOADDECOMPRESS="lzma -d" ;;
204             xz) rpm --showrc | grep PayloadIsXz > /dev/null || PAYLOADDECOMPRESS="xz -d" ;;
205         esac
206         if test "$PAYLOADDECOMPRESS" = "lzma -d" ; then
207             if ! lzma </dev/null >/dev/null 2>&1 ; then
208                 test -f "$BUILD_DIR/lzmadec.sh" && PAYLOADDECOMPRESS="bash $BUILD_DIR/lzmadec.sh"
209             fi
210         fi
211         if test "$PAYLOADDECOMPRESS" = "xz -d" ; then
212             if ! xz </dev/null >/dev/null 2>&1 ; then
213                 test -f "$BUILD_DIR/xzdec.sh" && PAYLOADDECOMPRESS="bash $BUILD_DIR/xzdec.sh"
214             fi
215         fi
216         if test "$PAYLOADDECOMPRESS" = cat ; then
217             rpm2cpio "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" | $CPIO
218         else
219             rpm2cpio "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" | $PAYLOADDECOMPRESS | $CPIO
220         fi
221         if test -e ".init_b_cache/scripts/$1.run" ; then
222             rpm -qp --nodigest --nosignature --qf "%{PREIN}" "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" > ".init_b_cache/scripts/$1.pre"
223             rpm -qp --nodigest --nosignature --qf "%{POSTIN}" "$BUILD_ROOT/.init_b_cache/rpms/$1.rpm" > ".init_b_cache/scripts/$1.post"
224             echo -n '(none)' > .init_b_cache/scripts/.none
225             cmp -s ".init_b_cache/scripts/$1.pre" .init_b_cache/scripts/.none && rm -f ".init_b_cache/scripts/$1.pre"
226             cmp -s ".init_b_cache/scripts/$1.post" .init_b_cache/scripts/.none && rm -f ".init_b_cache/scripts/$1.post"
227             rm -f .init_b_cache/scripts/.none
228         fi
229     elif test -e "$BUILD_ROOT/.init_b_cache/rpms/$1.deb" ; then
230         ar x "$BUILD_ROOT/.init_b_cache/rpms/$1.deb" control.tar.gz data.tar.gz
231         mkdir -p .init_b_cache/scripts/control
232         $TAR -C .init_b_cache/scripts/control -z -f control.tar.gz
233         $TAR -z -f data.tar.gz
234         if test -e ".init_b_cache/scripts/$1.run" ; then
235             test -e .init_b_cache/scripts/control/preinst && mv .init_b_cache/scripts/control/preinst ".init_b_cache/scripts/$1.pre"
236             test -e .init_b_cache/scripts/control/postinst && mv .init_b_cache/scripts/control/postinst ".init_b_cache/scripts/$1.post"
237         fi
238         rm -rf .init_b_cache/scripts/control control.tar.gz data.tar.gz
239     elif test -e "$BUILD_ROOT/.init_b_cache/rpms/$1.arch" ; then
240         $TAR -z -f "$BUILD_ROOT/.init_b_cache/rpms/$1.arch"
241         if test -f .INSTALL ; then
242             cat .INSTALL > ".init_b_cache/scripts/$1.post"
243             echo 'type post_install >/dev/null 2>&1 && post_install' >> ".init_b_cache/scripts/$1.post"
244         fi
245         rm -f .PKGINFO .INSTALL
246     else
247         echo "warning: package $1 does not exist"
248     fi
249 }
250
251 run_pkg_scripts()
252 {
253     chroot $BUILD_ROOT /sbin/ldconfig 2>/dev/null
254     for PKG in $PACKAGES_TO_RUNSCRIPTS ; do
255         if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre" ; then
256             echo "running $PKG preinstall script"
257             if test -e "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" ; then
258                 chroot $BUILD_ROOT sh ".init_b_cache/scripts/$PKG.pre" 0
259             else
260                 chroot $BUILD_ROOT ".init_b_cache/scripts/$PKG.pre" install < /dev/null
261             fi
262             rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.pre"
263         fi
264         if test -e "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post" ; then
265             echo "running $PKG postinstall script"
266             if test -e "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" ; then
267                 chroot $BUILD_ROOT sh ".init_b_cache/scripts/$PKG.post" 1
268             else
269                 chroot $BUILD_ROOT ".init_b_cache/scripts/$PKG.post" configure '' < /dev/null
270             fi
271             rm -f "$BUILD_ROOT/.init_b_cache/scripts/$PKG.post"
272         fi
273         check_exit
274     done
275 }
276
277 init_db()
278 {
279     if test $PSUF = rpm ; then
280         echo initializing rpm db...
281         if ! test -e $BUILD_ROOT/usr/lib/rpm/cpuinfo.yaml; then
282             # rpm v5 does not have initdb
283             chroot $BUILD_ROOT rpm --initdb || cleanup_and_exit 1
284         fi
285         # hack: add nofsync to db config to speed up install
286         mkdir -p $BUILD_ROOT/root
287         echo '%__dbi_perms perms=0644 nofsync' > $BUILD_ROOT/.rpmmacros
288         echo '%__dbi_perms perms=0644 nofsync' > $BUILD_ROOT/root/.rpmmacros
289     elif test $PSUF = deb ; then
290         # force dpkg into database to make epoch test work
291         if ! test "$BUILD_ROOT/.init_b_cache/rpms/dpkg.deb" -ef "$BUILD_ROOT/.init_b_cache/dpkg.deb" ; then
292             rm -f $BUILD_ROOT/.init_b_cache/dpkg.deb
293             cp $BUILD_ROOT/.init_b_cache/rpms/dpkg.deb $BUILD_ROOT/.init_b_cache/dpkg.deb || cleanup_and_exit 1
294         fi
295         chroot $BUILD_ROOT dpkg -i --force all .init_b_cache/dpkg.deb >/dev/null 2>&1
296     elif test $PSUF = arch ; then
297         mkdir -p $BUILD_ROOT/var/lib/pacman/sync
298         touch $BUILD_ROOT/var/lib/pacman/sync/core.db
299         touch $BUILD_ROOT/var/lib/pacman/sync/extra.db
300         touch $BUILD_ROOT/var/lib/pacman/sync/community.db
301     fi
302 }
303
304 reorder()
305 {
306     test -z "$*" && return
307     rm -f $BUILD_ROOT/.init_b_cache/order.manifest
308     for PKG in "$@" ; do
309         echo "$PKG" >> $BUILD_ROOT/.init_b_cache/order.manifest
310     done
311     $BUILD_DIR/order --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir $CONFIG_DIR --manifest $BUILD_ROOT/.init_b_cache/order.manifest $BUILD_ROOT/.init_b_cache/rpms || touch $BUILD_ROOT/exit
312     rm -f $BUILD_ROOT/.init_b_cache/order.manifest
313 }
314
315 create_devs()
316 {
317     local com file mode arg
318
319     mkdir -m 755 -p $BUILD_ROOT/dev/pts
320     test -f $BUILD_ROOT/dev/shm && rm -f $BUILD_ROOT/dev/shm
321     mkdir -m 755 -p $BUILD_ROOT/dev/shm
322     while read com file mode arg ; do
323         rm -f $BUILD_ROOT/dev/$file
324         if test $com = ln ; then
325             ln -s $arg $BUILD_ROOT/dev/$file
326             continue
327         fi
328         $com -m $mode $BUILD_ROOT/dev/$file $arg
329     done << DEVLIST
330         mknod null    666 c 1 3
331         mknod zero    666 c 1 5
332         mknod full    622 c 1 7
333         mknod random  666 c 1 8
334         mknod urandom 644 c 1 9
335         mknod tty     666 c 5 0
336         mknod ptmx    666 c 5 2
337         mknod loop0   640 b 7 0
338         mknod loop1   640 b 7 1
339         mknod loop2   640 b 7 2
340         mknod loop3   640 b 7 3
341         ln    fd      777 /proc/self/fd
342         ln    stdin   777 fd/0
343         ln    stdout  777 fd/1
344         ln    stderr  777 fd/2
345 DEVLIST
346 }
347
348 # check whether the repo list contains a plain "zypp://". Add all
349 # enabled zypp repos in this case
350 maybe_add_all_zypp_repos()
351 {
352     local i j
353     r=()
354     for i in "${repos[@]}"; do
355         if [ "$i" = "zypp://" ]; then
356             while read j; do
357                 j="${j#/etc/zypp/repos.d/}"
358                 r=("${r[@]}" "zypp://${j%.repo}")
359             done < <(grep -l enabled=1 /etc/zypp/repos.d/*.repo)
360         else
361             r=("${r[@]}" "$i")
362         fi
363     done
364     repos=("${r[@]}")
365 }
366
367 validate_cache_file()
368 {
369     local findonly=''
370     maybe_add_all_zypp_repos
371     if ! test -f $CACHE_FILE || ! test -f $CACHE_FILE.id || \
372         test "${repos[*]} ${BUILD_RPMS//:/ /}" != "$(cat $CACHE_FILE.id 2>/dev/null)"; then
373         rm -f $CACHE_FILE.id
374     else
375         for SRC in "${repos[@]}" ${BUILD_RPMS//:/ /}; do
376             test -n "$SRC" || SRC=.
377             if [ "${SRC#zypp://}" != "$SRC" ]; then
378                 SRC="/var/cache/zypp/raw" # XXX can't use name here as we'd need to parse the file
379             fi
380             if test "$SRC" -nt $CACHE_FILE; then
381                 rm -f $CACHE_FILE.id
382                 break
383             fi
384             # always rebuild if we have remote repositories and --clean is given
385             if test -n "$CLEAN_BUILD" -a "$SRC" != "${SRC#*://}" ; then
386                 rm -f $CACHE_FILE.id
387                 break
388             fi
389         done
390     fi
391     if ! test -f $CACHE_FILE.id ; then
392         test -z "$LIST_STATE" && echo initializing $CACHE_FILE ...
393         for SRC in "${repos[@]}" -- ${BUILD_RPMS//:/ /}; do
394             if test "$SRC" = '--' ; then
395                 findonly=1
396                 continue
397             fi
398             test -z "$SRC" && SRC=`pwd`
399             if [ "${SRC#zypp://}" != "$SRC" ]; then
400                 set -- $BUILD_DIR/createrepomddeps "$SRC"
401             elif [ "${SRC#http://}" != "$SRC" -o "${SRC#https://}" != "$SRC" -o "${SRC#ftp://}" != "$SRC" -o "${SRC#ftps://}" != "$SRC" ]; then
402                 mkdir -p "$(getcachedir "$SRC")"
403                 set -- $BUILD_DIR/createrepomddeps --cachedir="$CACHE_DIR" "$SRC"
404             elif [ "${SRC#arch@http://}" != "$SRC" -o "${SRC#arch@https://}" != "$SRC" -o "${SRC#arch@ftp://}" != "$SRC" -o "${SRC#arch@ftps://}" != "$SRC" ]; then
405                 mkdir -p "$(getcachedir "$SRC")"
406                 set -- $BUILD_DIR/createrepomddeps --cachedir="$CACHE_DIR" "$SRC"
407             elif [ ! -e "$SRC" ]; then
408                 echo "*** $SRC does not exist" >&2
409                 cleanup_and_exit 1
410             elif [ -z "$findonly" -a \( -e "$SRC"/suse/setup/descr/packages -o -e "$SRC"/suse/setup/descr/packages.gz \) ]; then
411                 set -- $BUILD_DIR/createyastdeps "$SRC"
412             elif [ -z "$findonly" -a -e "$SRC"/repodata/repomd.xml ]; then
413                 set -- $BUILD_DIR/createrepomddeps "$SRC"
414             else
415                 set -- $BUILD_DIR/createrpmdeps "$SRC"
416             fi
417             echo "$@" >&2
418             "$@" || cleanup_and_exit 1
419             echo D:
420         done > $CACHE_FILE.new
421
422         mv $CACHE_FILE.new $CACHE_FILE
423         echo "${repos[*]} ${BUILD_RPMS//:/ /}" > $CACHE_FILE.id
424     fi
425 }
426
427 #
428 # Copy qemu static binaries for cross-build emulation mode.
429 # Preferentially install from $BUILD_DIR (potentially provided by build
430 # server) in preference to build worker's /usr/bin (fallback).
431 # If initvm is available it will be in $BUILD_DIR; otherwise prepare to
432 # fall back to initscript_qemu_vm shell script with corresponding static
433 # bash and mount programs.
434 #
435
436 check_copy_qemu()
437 {
438     local arch
439
440     for arch in $EMULATOR_DEVS; do
441         if test -e $BUILD_DIR/qemu-$arch; then
442             return 0
443         fi
444     done
445     return 1
446 }
447
448 copy_qemu()
449 {
450     local path dest
451
452     echo "copying qemu"
453
454     mkdir -p $BUILD_ROOT/usr/bin
455
456     if check_copy_qemu; then
457
458         for path in $BUILD_DIR/qemu-*; do
459             if file $path | grep -q static; then
460                 dest="$BUILD_ROOT/usr/bin/${path##*/}"
461                 if [ -f "$path" -a ! -x "$dest" ]; then
462                     echo -n " $path"            # report copy
463                     #echo install -m755 "$path" "$dest"
464                     install -m755 "$path" "$dest"
465                 fi
466             fi
467         done
468
469     else
470
471         for path in /usr/bin/qemu-*; do
472             if file $path | grep -q static; then
473                 dest="$BUILD_ROOT/usr/bin/${path##*/}"
474                 if [ -f "$path" -a ! -x "$dest" ]; then
475                     echo -n " $path"            # report copy
476                     #echo install -m755 "$path" "$dest"
477                     install -m755 "$path" "$dest"
478                 fi
479             fi
480         done
481
482         if [ -e /usr/sbin/qemu-binfmt-conf.sh \
483             -a ! -e $BUILD_ROOT/usr/sbin/qemu-binfmt-conf.sh ]; then
484             echo " /usr/sbin/qemu-binfmt-conf.sh"               # report copy
485             mkdir -p $BUILD_ROOT/usr/sbin
486             install -m755 /usr/sbin/qemu-binfmt-conf.sh $BUILD_ROOT/usr/sbin
487         fi
488
489     fi
490     echo ""
491
492     # Below for backward compatibility when /.build/initvm is not present
493
494     if [ -n "$PREPARE_VM" ]; then
495         if [ -x /bin/bash-static -a -x /bin/mount-static ]; then
496             echo " /bin/bash-static /bin/mount-static"  # report copy
497             mkdir -p $BUILD_ROOT/bin
498             install -m755 /bin/bash-static $BUILD_ROOT/bin
499             install -m755 /bin/mount-static $BUILD_ROOT/bin
500         fi
501     fi
502 }
503
504 check_binfmt_registered()
505 {
506     local arch
507     for arch in $EMULATOR_DEVS; do
508         if test -e /proc/sys/fs/binfmt_misc/$arch; then
509             return 0
510         fi
511     done
512     return 1
513 }
514
515 fail_exit()
516 {
517   cleanup_and_exit 1
518 }
519
520 # modifies $SRC
521 downloadpkg()
522 {
523     local url="$1"
524     local cachedir
525
526     if [ "${url:0:7}" == "zypp://" ] ; then
527         cachedir="/var/cache/zypp/packages/"
528         SRC="$cachedir${url#zypp://}"
529         mkdir -p "${SRC%/*}" || cleanup_and_exit 1
530     elif [ "${url:0:7}" == "http://" -o "${url:0:8}" == "https://" -o "${url:0:6}" == "ftp://" -o "${url:0:7}" == "ftps://" ] ; then
531         cachedir="$(getcachedir "$url")"
532         local name="$(basename "$url")"
533         name=${name/%.pkg.tar.?z/.arch}
534         SRC="$cachedir/$name"
535     else
536         echo "Invalid url: $url"
537         cleanup_and_exit 1
538     fi
539
540     local destdir="$cachedir/tmp"
541     mkdir -p "$destdir"
542     echo "downloading $url ... ";
543     $BUILD_DIR/download "$destdir" "$url" || cleanup_and_exit 1
544     local destfile="$destdir/${url##*/}"
545     if [ ! -e "$destfile" ]; then
546         echo "expected $destfile after download but it's missing" >&2
547         cleanup_and_exit 1
548     fi
549     case $destfile in
550       *.rpm)
551         rpm -K "$destfile" > $destfile.v || { echo "rpm verify failed" >&2; rm -rf "$destdir"; cleanup_and_exit 1; }
552         if grep "NOT OK" $destfile.v; then
553             rm -rf "$destdir"
554             cleanup_and_exit 1
555         fi
556         rm -f "$destfile.v"
557         ;;
558     esac
559     mv "$destfile" "$SRC" || cleanup_and_exit 1
560 }
561
562 getcachedir()
563 {
564     local url=$1
565     case $url in
566       zypp://*)
567         url=${url#zypp:/}
568         echo "/var/cache/zypp/packages/${url%/*}"
569         return 0;
570         ;;
571       *.pkg.tar.?z) url="arch@$url" ;;
572     esac
573     for repo in "${repos[@]}" ; do
574         if [ "${url:0:${#repo}}" == "$repo" -o "${url:0:${#repo}}" == "$repo" ] ; then
575             read repoid dummy < <(echo -n "$repo" | md5sum)
576             echo "$CACHE_DIR/$repoid"
577             break
578         fi
579     done
580 }
581
582 get_pkg_filename()
583 {
584     local url="$1"
585     local name=$(basename $url)
586     local cachedir=$(getcachedir $url)
587     local destfile="$cachedir/$name"
588     echo $destfile
589 }
590
591 set_build_arch
592
593 trap fail_exit EXIT
594
595 if [ "$BUILD_ROOT" = / ]; then
596     browner="$(stat -c %u /)"
597 fi
598
599 if [ -n "$CLEAN_BUILD" ]; then
600     clean_build_root
601 fi
602
603 #
604 # now test if there was an incomplete run
605 #
606 if test -e $BUILD_IS_RUNNING ; then
607     echo It seems that there was an incomplete setup of $BUILD_ROOT.
608     echo To be sure, we will build it again completely...
609     umount -n $BUILD_ROOT/proc/sys/fs/binfmt_misc 2> /dev/null || true
610     umount -n $BUILD_ROOT/proc 2> /dev/null
611     umount -n $BUILD_ROOT/dev/pts 2> /dev/null
612     umount -n $BUILD_ROOT/mnt 2> /dev/null
613     echo "Your build system is broken!! Shall I execute"
614     echo
615     echo "    rm -rf -- $BUILD_ROOT/*"
616     echo
617     echo "y - yes, cleanup the build root"
618     echo "N - No, abort build (default on enter)"
619     echo "c - Continue anyway with this build root"
620     echo -n "[y/N/c] "
621     read ANSWER
622     case "$ANSWER" in
623         c|C) rm -f $BUILD_IS_RUNNING ;;
624         y|Y) clean_build_root ;;
625         *) cleanup_and_exit 1 ;;
626     esac
627 fi
628
629 #
630 # store that we start to build system
631 #
632 mkdir -p $BUILD_ROOT
633 mkdir -p $BUILD_ROOT/.build
634 touch $BUILD_IS_RUNNING
635
636 if test -n "$PREPARE_VM" ; then
637     rm -f $BUILD_ROOT/.build/init_buildsystem.data
638 fi
639 if test -e $BUILD_ROOT/.build/init_buildsystem.data ; then
640     # vm continuation
641     . $BUILD_ROOT/.build/init_buildsystem.data
642     if ! test -e $BUILD_ROOT/.init_b_cache/preinstall_finished ; then
643         # finish preinstall
644         run_pkg_scripts
645         init_db
646         touch $BUILD_ROOT/.init_b_cache/preinstall_finished
647     fi
648 else
649     #
650     # now make sure that all the packages are installed.
651     #
652     rm -rf "$BUILD_ROOT/.init_b_cache"
653     mkdir -p "$BUILD_ROOT/.init_b_cache/scripts"
654
655     if test -z "$RPMLIST" ; then
656         #
657         # create rpmdeps file
658         #
659         CACHE_FILE=$BUILD_ROOT/.srcfiles.cache
660         validate_cache_file
661
662         #
663         # select and expand packages
664         #
665         RPMLIST=$BUILD_ROOT/.init_b_cache/rpmlist
666         test -z "$LIST_STATE" && echo "expanding package dependencies..."
667         if ! $BUILD_DIR/expanddeps $USEUSEDFORBUILD "${definesnstuff[@]}" --dist "$BUILD_DIST" --depfile "$CACHE_FILE" --archpath "$BUILD_ARCH" --configdir $CONFIG_DIR "${PKGS[@]}" > $RPMLIST ; then
668             rm -f $BUILD_IS_RUNNING
669             cleanup_and_exit 1
670         fi
671     fi
672
673     if test -n "$LIST_STATE" ; then
674         rm -f $BUILD_IS_RUNNING
675         while read PKG SRC ; do
676            test "$PKG" = "preinstall:" && continue
677            test "$PKG" = "runscripts:" && continue
678            test "$PKG" = "cbpreinstall:" && continue
679            test "$PKG" = "vminstall:" && continue
680            test "$PKG" = "dist:" && continue
681            test "$PKG" = "rpmid:" && continue
682            echo "${SRC##*/}"
683         done < $BUILD_ROOT/.init_b_cache/rpmlist
684         rm -rf "$BUILD_ROOT/.init_b_cache"
685         cleanup_and_exit 0
686     fi
687
688     #
689     # copy the QEMU emulator
690     #
691     if check_use_emulator; then
692         [ -n "$USE_SYSTEM_QEMU" ] && copy_qemu
693         if [ -z "$PREPARE_VM" ]; then
694             if ! check_binfmt_registered; then
695                 echo "registering binfmt handlers"
696
697                 if [ -x "$BUILD_DIR/initvm" -a -e "$BUILD_DIR/qemu-reg" ]; then
698                     $BUILD_DIR/initvm
699                 elif [ -x /usr/sbin/qemu-binfmt-conf.sh ]; then
700                    [ -e /proc/sys/fs/binfmt_misc ] || mount -n binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
701                    /usr/sbin/qemu-binfmt-conf.sh
702                 else
703                     echo "Warning: could not register binfmt handlers. Neither build-initvm nor /usr/sbin/qemu-binfmt-conf.sh exist"
704                 fi
705             fi
706             if [ -n "$PREPARE_VM" ]; then
707                 echo 0 > /proc/sys/vm/mmap_min_addr
708             fi
709             read mmap_min_addr < /proc/sys/vm/mmap_min_addr
710             if [ "$mmap_min_addr" != 0 ]; then
711                 echo "Warning: mmap_min_addr is != 0. If programs fail at mmap this could be the reason"
712             fi
713         fi
714     fi
715
716     PACKAGES_TO_INSTALL=
717     PACKAGES_TO_PREINSTALL=
718     PACKAGES_TO_RUNSCRIPTS=
719     PACKAGES_TO_VMINSTALL=
720     PACKAGES_TO_CBPREINSTALL=
721     PACKAGES_TO_CBINSTALL=
722     PREINSTALL_IMAGE=
723     PREINSTALL_IMAGE_SOURCE=
724     RUNSCRIPTS_SEEN=
725     PACKAGES_FROM_PREINSTALLIMAGE=
726     GUESSED_DIST=unknown
727     mkdir -p $BUILD_ROOT/.init_b_cache/rpms
728     rm -f $BUILD_ROOT/.init_b_cache/rpmlist.download
729     while read PKG SRC ; do
730         if test "$PKG" = "preinstall:" ; then
731             PACKAGES_TO_PREINSTALL=$SRC
732             continue
733         fi
734         if test "$PKG" = "vminstall:" ; then
735             PACKAGES_TO_VMINSTALL=$SRC
736             continue
737         fi
738         if test "$PKG" = "preinstallimage:" ; then
739             PREINSTALL_IMAGE=${SRC##*/}
740             ln -s "$SRC" "$BUILD_ROOT/.init_b_cache/rpms/${SRC##*/}"
741             continue
742         fi
743         if test "$PKG" = "preinstallimagesource:" ; then
744             PREINSTALL_IMAGE_SOURCE="$SRC"
745             continue
746         fi
747         # these additional preinstall / install values are only set for 
748         # emulated "CrossBuild" setups - thus CB(pre)install
749         if test "$PKG" = "cbpreinstall:" ; then
750             if is_emulator_arch ; then
751                 PACKAGES_TO_CBPREINSTALL=$SRC
752             fi
753             continue
754         fi
755         if test "$PKG" = "cbinstall:" ; then
756             if is_emulator_arch ; then
757                 PACKAGES_TO_CBINSTALL=$SRC
758             fi
759             continue
760         fi
761         if test "$PKG" = "runscripts:" ; then
762             RUNSCRIPTS_SEEN=true
763             PACKAGES_TO_RUNSCRIPTS=$SRC
764             continue
765         fi
766         if test "$PKG" = "dist:" ; then
767             GUESSED_DIST=$SRC
768             continue
769         fi
770         if test "$PKG" = "rpmid:" ; then
771             echo "${SRC#*:}" > $BUILD_ROOT/.init_b_cache/rpms/${SRC%%:*}.id
772             continue
773         fi
774         if test "$PKG" != "${PKG%:}"; then
775             echo "Warning: ignoring unsupported tag '$PKG'" >&2
776             continue
777         fi
778         if test "$SRC" = "preinstallimage" ; then
779             PACKAGES_FROM_PREINSTALLIMAGE="$PACKAGES_FROM_PREINSTALLIMAGE $PKG"
780             PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL $PKG"
781             continue
782         fi
783         PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL $PKG"
784         if [ "${SRC#/}" = "$SRC" ]; then
785             case "$SRC" in
786                 zypp://* | http://* | https://* | ftp://* | ftps://*)
787                     echo "$PKG $SRC" >>$BUILD_ROOT/.init_b_cache/rpmlist.download
788                     continue
789                     ;;
790                 *)
791                     echo "unsupported url for '$PKG': $SRC" >&2
792                     cleanup_and_exit 1
793                     ;;
794             esac
795         fi
796         SRCSUF=${SRC/%.pkg.tar.?z/.arch}
797         ln -s "$SRC" "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRCSUF##*.}"
798     done < $RPMLIST
799
800     # check if we really can use cached versions for packages on the download list
801     if test -s $BUILD_ROOT/.init_b_cache/rpmlist.download ; then
802         rm -f $BUILD_ROOT/.init_b_cache/rpmlist.download2
803         while read PKG SRC ; do
804             cachepkg="${SRC##*/}"
805             cachepkg="${cachepkg/%.pkg.tar.?z/.arch}"
806             cachedir="$(getcachedir "$SRC")"
807             if test -s "$cachedir/$cachepkg" ; then
808                 if test -s "$BUILD_ROOT/.init_b_cache/rpms/$PKG.id" -a "${SRC%.rpm}" != "$SRC" ; then
809                     PKGID=`rpm -qp --qf "$RPMIDFMT" $RPMCHECKOPTS_HOST "$cachedir/$cachepkg"`
810                     read cachepkgid < $BUILD_ROOT/.init_b_cache/rpms/$PKG.id
811                     if test "$cachepkgid" = "$PKGID" ; then
812                         SRC="$cachedir/$cachepkg"
813                     else
814                         rm -f "$cachedir/$cachepkg"
815                     fi
816                 else
817                     $SRC="$cachedir/$cachepkg"
818                 fi
819             fi
820             if test "${SRC#/}" = "$SRC" ; then
821                 echo "$PKG $SRC" >>$BUILD_ROOT/.init_b_cache/rpmlist.download2
822             else
823                 SRCSUF=${SRC/%.pkg.tar.?z/.arch}
824                 ln -s "$SRC" "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRCSUF##*.}"
825             fi
826         done < $BUILD_ROOT/.init_b_cache/rpmlist.download
827         rm -f $BUILD_ROOT/.init_b_cache/rpmlist.download
828         test -s $BUILD_ROOT/.init_b_cache/rpmlist.download2 && mv $BUILD_ROOT/.init_b_cache/rpmlist.download2 $BUILD_ROOT/.init_b_cache/rpmlist.download
829     fi
830
831     # now do the download of missing packages
832     if test -s $BUILD_ROOT/.init_b_cache/rpmlist.download ; then
833         PACKAGES_TO_DOWNLOAD=`cat ${RPMLIST}.download|awk '{print $2}'`
834         progress_setup PACKAGES_TO_DOWNLOAD
835         while read PKG SRC ; do
836             progress_step PACKAGES_TO_DOWNLOAD
837             downloadpkg "$SRC"
838             # downloadpkg modified $SRC, so it has a right name for use
839             SRCSUF=${SRC/%.pkg.tar.?z/.arch}
840             ln -s "$SRC" "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRCSUF##*.}"
841         done < $BUILD_ROOT/.init_b_cache/rpmlist.download
842         rm -f $BUILD_ROOT/.init_b_cache/rpmlist.download
843         printf "\n"
844     fi
845
846     test -n "$PACKAGES_TO_CBPREINSTALL" && echo "cbpreinstall: $PACKAGES_TO_CBPREINSTALL"
847     test -n "$PACKAGES_TO_CBINSTALL" && echo "cbinstall   : $PACKAGES_TO_CBINSTALL"
848     # compatibility...
849     test -z "$RUNSCRIPTS_SEEN" && PACKAGES_TO_RUNSCRIPTS="$PACKAGES_TO_PREINSTALL"
850
851     echo "$GUESSED_DIST" > $BUILD_ROOT/.guessed_dist
852     test -n "$BUILD_DIST" || BUILD_DIST="$GUESSED_DIST"
853     PSUF=`gettype --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH"`
854     case "$PSUF" in
855         UNKNOWN|'')
856             # auto detect from packages
857             if test -n "$PREINSTALL_IMAGE" ; then
858                 echo "cannot autodetect build type when using a preinstall image" >&2
859                 cleanup_and_exit 1
860             fi
861             PSUF=rpm
862             test -e $BUILD_ROOT/.init_b_cache/rpms/dpkg.deb && PSUF=deb
863             test -e $BUILD_ROOT/.init_b_cache/rpms/pacman.arch && PSUF=arch
864             ;;
865     esac
866
867     if test -n "$PREINSTALL_IMAGE" ; then
868         for PKG in $PACKAGES_FROM_PREINSTALLIMAGE ; do
869             # touch the file so that the copying works
870             touch $BUILD_ROOT/.init_b_cache/rpms/"$PKG.$PSUF"
871         done
872     fi
873
874 fi
875
876 #
877 # now test if there is already a build dir.
878 #
879 if test ! -f $BUILD_ROOT/var/lib/rpm/packages.rpm -a ! -f $BUILD_ROOT/var/lib/rpm/Packages -a ! -e $BUILD_ROOT/.build/init_buildsystem.data ; then
880     mkdir -p $BUILD_ROOT/var/lib/rpm || cleanup_and_exit 1
881     mkdir -p $BUILD_ROOT/usr/src/packages/SOURCES || cleanup_and_exit 1
882     mkdir -p $BUILD_ROOT/etc || cleanup_and_exit 1
883     mkdir -p $BUILD_ROOT/proc || cleanup_and_exit 1
884     test -f $BUILD_ROOT/etc/HOSTNAME || hostname -f > $BUILD_ROOT/etc/HOSTNAME
885     if test $PSUF = deb ; then
886         mkdir -p $BUILD_ROOT/var/lib/dpkg
887         mkdir -p $BUILD_ROOT/var/log
888         mkdir -p $BUILD_ROOT/etc/default
889         :> $BUILD_ROOT/var/lib/dpkg/status
890         :> $BUILD_ROOT/var/lib/dpkg/available
891         :> $BUILD_ROOT/var/log/dpkg.log
892         :> $BUILD_ROOT/etc/ld.so.conf
893         :> $BUILD_ROOT/etc/default/rcS
894     fi
895     for PKG in $PACKAGES_TO_RUNSCRIPTS ; do
896         : > $BUILD_ROOT/.init_b_cache/scripts/$PKG.run
897     done
898     PACKAGES_TO_PREINSTALL_FILTERED="$PACKAGES_TO_PREINSTALL"
899     PACKAGES_TO_VMINSTALL_FILTERED="$PACKAGES_TO_VMINSTALL"
900     PACKAGES_TO_CBPREINSTALL_FILTERED="$PACKAGES_TO_CBPREINSTALL"
901     rm -f "$BUILD_ROOT/.preinstall_image"/*
902     if test -n "$PREINSTALL_IMAGE" ; then
903         preinstall_image "$PREINSTALL_IMAGE" "$PREINSTALL_IMAGE_SOURCE"
904         PACKAGES_TO_PREINSTALL_FILTERED=`preinstall_image_filter $PACKAGES_TO_PREINSTALL_FILTERED`
905         PACKAGES_TO_VMINSTALL_FILTERED=`preinstall_image_filter $PACKAGES_TO_VMINSTALL_FILTERED`
906         PACKAGES_TO_CBPREINSTALL_FILTERED=`preinstall_image_filter $PACKAGES_TO_VMINSTALL_FILTERED`
907     fi
908     PACKAGES_TO_PREINSTALL_FILTERED=`reorder $PACKAGES_TO_PREINSTALL_FILTERED`
909     progress_setup PACKAGES_TO_PREINSTALL_FILTERED
910     for PKG in $PACKAGES_TO_PREINSTALL_FILTERED ; do
911         progress_step PACKAGES_TO_PREINSTALL_FILTERED
912         preinstall ${PKG##*/}
913     done
914     printf "\n"
915     if test -n "$PREPARE_VM" ; then
916         PACKAGES_TO_VMINSTALL_FILTERED=`reorder $PACKAGES_TO_VMINSTALL_FILTERED`
917         progress_setup PACKAGES_TO_VMINSTALL_FILTERED
918         for PKG in $PACKAGES_TO_VMINSTALL_FILTERED ; do
919             progress_step PACKAGES_TO_VMINSTALL_FILTERED
920             preinstall ${PKG##*/}
921         done
922     fi
923     # add cbpreinstall if cross HOST != TARGET
924     PACKAGES_TO_CBINSTALL_FILTERED=`reorder $PACKAGES_TO_CBINSTALL_FILTERED`
925     progress_setup PACKAGES_TO_CBPREINSTALL_FILTERED
926     for PKG in $PACKAGES_TO_CBPREINSTALL_FILTERED ; do
927         progress_step PACKAGES_TO_CBPREINSTALL_FILTERED
928         preinstall ${PKG##*/}
929     done
930     # for reorder
931     check_exit
932     if [ -w /root ]; then
933             test -c $BUILD_ROOT/dev/null || create_devs
934     fi
935     test -e $BUILD_ROOT/etc/fstab || touch $BUILD_ROOT/etc/fstab
936     test ! -e $BUILD_ROOT/etc/ld.so.conf -a -e $BUILD_ROOT/etc/ld.so.conf.in && cp $BUILD_ROOT/etc/ld.so.conf.in $BUILD_ROOT/etc/ld.so.conf
937     if test -z "$PREPARE_VM" ; then
938         run_pkg_scripts
939         init_db
940         touch $BUILD_ROOT/.init_b_cache/preinstall_finished
941     fi
942 fi
943
944 if test -n "$PREPARE_VM" ; then
945     echo "copying packages..."
946     for PKG in $PACKAGES_TO_INSTALL ; do
947         rm -f $BUILD_ROOT/.init_b_cache/$PKG.$PSUF
948         cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF $BUILD_ROOT/.init_b_cache/$PKG.$PSUF || cleanup_and_exit 1
949         ln -s -f ../$PKG.$PSUF $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF
950         check_exit
951     done
952     # alreadyinstalled check will not work, but we have to live with that...
953     echo -n 'reordering...'
954     PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
955     check_exit
956     echo 'done'
957     Q="'\''"
958     echo "PACKAGES_TO_INSTALL='${PACKAGES_TO_INSTALL//"'"/$Q}'" > $BUILD_ROOT/.build/init_buildsystem.data
959     echo "PACKAGES_TO_RUNSCRIPTS='${PACKAGES_TO_RUNSCRIPTS//"'"/$Q}'" >> $BUILD_ROOT/.build/init_buildsystem.data
960     # needed for continuation in xen/kvm with rpm-x86
961     echo "PACKAGES_TO_PREINSTALL='${PACKAGES_TO_PREINSTALL//"'"/$Q}'" >> $BUILD_ROOT/.build/init_buildsystem.data
962     echo "PACKAGES_TO_CBPREINSTALL='${PACKAGES_TO_CBPREINSTALL//"'"/$Q}'" >> $BUILD_ROOT/.build/init_buildsystem.data
963     echo "PSUF='$PSUF'" >> $BUILD_ROOT/.build/init_buildsystem.data
964     rm -f $BUILD_IS_RUNNING
965     cleanup_and_exit 0
966 fi
967
968 mkdir -p $BUILD_ROOT/proc
969 mkdir -p $BUILD_ROOT/dev/pts
970 mount -n -tproc none $BUILD_ROOT/proc 2>/dev/null || true
971 mount -n -tdevpts none $BUILD_ROOT/dev/pts 2>/dev/null || true
972
973 #
974 # create .build.binaries directory if requested
975 #
976 rm -rf "$BUILD_ROOT/.build.binaries"
977 if test -n "$CREATE_BUILD_BINARIES" ; then
978     echo "creating .build.binaries directory..."
979     mkdir -p "$BUILD_ROOT/.build.binaries"
980     for PKG in $PACKAGES_TO_INSTALL ; do
981         test -L "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" || continue
982         LPKG=`readlink -f "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF"`
983         ln "$LPKG" "$BUILD_ROOT/.build.binaries/$PKG.$PSUF" 2>/dev/null
984         test -f "$BUILD_ROOT/.build.binaries/$PKG.$PSUF" && continue
985         cp "$LPKG" "$BUILD_ROOT/.build.binaries/$PKG.$PSUF"
986         check_exit
987     done
988 fi
989
990 #
991 # get list and ids of already installed rpms
992 #
993 mkdir -p $BUILD_ROOT/.init_b_cache/alreadyinstalled
994 if test -f $BUILD_ROOT/var/lib/rpm/packages.rpm -o -f $BUILD_ROOT/var/lib/rpm/Packages ; then
995     chroot $BUILD_ROOT rpm -qa --qf "%{NAME} $RPMIDFMT" | (
996         while read pp ii; do
997             echo "$ii" > "$BUILD_ROOT/.init_b_cache/alreadyinstalled/$pp"
998         done
999     )
1000 fi
1001
1002 #
1003 # reorder packages (already done in vm continuation)
1004 #
1005 if ! test -e $BUILD_ROOT/.build/init_buildsystem.data ; then
1006     echo -n 'reordering...'
1007     PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
1008     check_exit
1009     echo 'done'
1010 fi
1011
1012 rpm_e()
1013 {
1014     chroot $BUILD_ROOT rpm --nodeps -e $PKG 2>&1 | \
1015     while read line; do
1016         case "$line" in
1017
1018             r*failed:\ No\ such\ file\ or\ directory) ;;
1019             error:\ failed\ to\ stat\ *:\ No\ such\ file\ or\ directory) ;;
1020             error:\ *scriptlet\ failed*)
1021                 echo "$line"
1022                 echo "re-try deleting $PKG using --noscripts"
1023                 chroot $BUILD_ROOT rpm --nodeps --noscripts -e $PKG || true
1024             ;;
1025             *) echo "$line" ;;
1026         esac
1027     done
1028 }
1029
1030 #
1031 # delete all packages we don't want
1032 #
1033 if [ -z "$KEEP_PACKS" ]; then
1034     mkdir -p $BUILD_ROOT/.init_b_cache/todelete
1035     for PKG in $BUILD_ROOT/.init_b_cache/alreadyinstalled/* ; do
1036         PKG=${PKG##*/}
1037         test "$PKG" = "*" && continue
1038         ln $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG $BUILD_ROOT/.init_b_cache/todelete/$PKG
1039     done
1040     for PKG in $PACKAGES_TO_INSTALL $PACKAGES_TO_CBINSTALL ; do
1041         rm -f $BUILD_ROOT/.init_b_cache/todelete/$PKG
1042     done
1043     for PKG in $BUILD_ROOT/.init_b_cache/todelete/* ; do
1044         PKG=${PKG##*/}
1045         test "$PKG" = "*" && continue
1046         echo "deleting $PKG"
1047         rpm_e "$PKG"
1048         check_exit
1049     done
1050     rm -rf "$BUILD_ROOT/.init_b_cache/todelete"
1051 fi
1052
1053 rm -rf "$BUILD_ROOT/.init_b_cache/preinstalls"
1054 mkdir -p "$BUILD_ROOT/.init_b_cache/preinstalls"
1055 for PKG in $PACKAGES_TO_PREINSTALL $PACKAGES_TO_CBPREINSTALL; do
1056     touch "$BUILD_ROOT/.init_b_cache/preinstalls/$PKG"
1057 done
1058
1059 rm -rf "$BUILD_ROOT/installed-pkg"
1060 mkdir -p "$BUILD_ROOT/installed-pkg"
1061
1062 RPMCHECKOPTS=
1063 RPMCHECKOPTS_HOST=
1064 # on Fedora 10 rpmbuild is in a separate package so we need something else to
1065 # detect rpm4
1066 test -x $BUILD_ROOT/usr/bin/rpmquery && RPMCHECKOPTS="--nodigest --nosignature"
1067 test -x /usr/bin/rpmquery && RPMCHECKOPTS_HOST="--nodigest --nosignature"
1068
1069 test -x $BUILD_ROOT/sbin/ldconfig && chroot $BUILD_ROOT /sbin/ldconfig 2>&1
1070
1071 typeset -ri suse_version=$(chroot $BUILD_ROOT rpm --eval '%{?suse_version}' 2>/dev/null)
1072 typeset -i num cumulate=-1
1073 typeset -a CUMULATED_LIST=()
1074 typeset -a CUMULATED_PIDS=()
1075 typeset -a CUMULATED_HMD5=()
1076
1077 DO_CUMULATE=
1078 if ((suse_version > 1220)) ; then
1079     DO_CUMULATE=true
1080 fi
1081
1082 MAIN_LIST="$PACKAGES_TO_INSTALL $PACKAGES_TO_CBINSTALL"
1083 test -n "$DO_CUMULATE" && MAIN_LIST="$MAIN_LIST CUMULATED"
1084 progress_setup MAIN_LIST
1085 for PKG in $MAIN_LIST; do
1086     test -f $BUILD_ROOT/installed-pkg/$PKG && continue
1087     progress_step MAIN_LIST
1088
1089     case $PKG in
1090     CUMULATED)
1091         #
1092         # Use the features of rpm which are reordering the list of packages to
1093         # satisfy dependencies and the final execution of the %posttrans scriplets
1094         #
1095         echo "now installing cumulated packages"
1096         ((cumulate < 0)) && continue
1097         exec 4>$BUILD_ROOT/.init_b_cache/manifest
1098         for ((num=0; num<=cumulate; num++)) ; do
1099             echo ${CUMULATED_LIST[$num]} 1>&4
1100             PKG=${CUMULATED_LIST[$num]##*/}
1101             test "$BUILD_ROOT/.init_b_cache/rpms/$PKG" -ef "$BUILD_ROOT/${CUMULATED_LIST[$num]}" && continue
1102             rm -f $BUILD_ROOT/${CUMULATED_LIST[$num]}
1103             cp $BUILD_ROOT/.init_b_cache/rpms/$PKG $BUILD_ROOT/${CUMULATED_LIST[$num]} || cleanup_and_exit 1
1104         done
1105         exec 4>&-
1106         chroot $BUILD_ROOT rpm --ignorearch --nodeps -Uh --oldpackage --ignoresize --verbose $RPMCHECKOPTS \
1107                 $ADDITIONAL_PARAMS .init_b_cache/manifest 2>&1 || touch $BUILD_ROOT/exit 
1108         for ((num=0; num<=cumulate; num++)) ; do
1109             rm -f $BUILD_ROOT/${CUMULATED_LIST[$num]}
1110         done
1111         rm -f .init_b_cache/manifest
1112         check_exit
1113         for ((num=0; num<=cumulate; num++)) ; do
1114             PKG=${CUMULATED_LIST[$num]##*/}
1115             echo "${CUMULATED_PIDS[$num]}" > $BUILD_ROOT/installed-pkg/${PKG%.rpm}
1116             test -n "${CUMULATED_HMD5[$num]}" || continue
1117             echo "${CUMULATED_HMD5[$num]} ${CUMULATED_PIDS[$num]}" > $BUILD_ROOT/.preinstall_image/${PKG%.rpm}
1118         done
1119         CUMULATED_LIST=()
1120         CUMULATED_PIDS=()
1121         CUMULATED_HMD5=()
1122         let cumulate=-1
1123         continue
1124         ;;
1125     esac
1126     if test -e "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" -a ! -s "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" ; then
1127         # preinstallimage package, make sure it's in the image
1128         if ! test -e $BUILD_ROOT/.preinstall_image/$PKG ; then
1129             echo "Package $PKG is missing from the preinstall image"
1130             cleanup_and_exit 1
1131         fi
1132         read PKG_HDRMD5 PKGID < $BUILD_ROOT/.preinstall_image/$PKG
1133         echo "preinstalled ${PKGID%% *}"
1134         echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
1135         continue
1136     fi
1137     PKG_HDRMD5=
1138     if test -d $BUILD_ROOT/.preinstall_image ; then
1139         if ! test -e $BUILD_ROOT/.preinstall_image/$PKG ; then
1140             PKG_HDRMD5=`perl -I$BUILD_DIR -MBuild -e 'print Build::queryhdrmd5($ARGV[0])' $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF`
1141             test -n "$PKG_HDRMD5" || cleanup_and_exit 1
1142         fi
1143     fi
1144     test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF || continue
1145
1146     if test $PSUF = deb ; then
1147         # debian world, install deb files
1148         if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.deb" -ef "$BUILD_ROOT/.init_b_cache/$PKG.deb" ; then
1149             rm -f $BUILD_ROOT/.init_b_cache/$PKG.deb
1150             cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb $BUILD_ROOT/.init_b_cache/$PKG.deb || cleanup_and_exit 1
1151         fi
1152         PKGID=`readlink $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb`
1153         PKGID="${PKGID##*/}"
1154         PKGID="${PKGID%.deb} debian"
1155         echo "installing ${PKGID%% *}"
1156         ( chroot $BUILD_ROOT dpkg --install --force all .init_b_cache/$PKG.deb 2>&1 || touch $BUILD_ROOT/exit ) | \
1157             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'
1158         check_exit
1159         echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
1160         test -n "$PKG_HDRMD5" && echo "$PKG_HDRMD5 $PKGID" > $BUILD_ROOT/.preinstall_image/$PKG
1161         # ugly workaround for upstart system. some packages (procps) try
1162         # to start a service in their configure phase. As we don't have
1163         # a running upstart, we just link the start binary to /bin/true
1164         if test -e "$BUILD_ROOT/sbin/start"; then
1165             if test "$BUILD_ROOT/sbin/start" -ef "$BUILD_ROOT/sbin/initctl" ; then
1166                 echo "linking /sbin/start to /bin/true"
1167                 mv "$BUILD_ROOT/sbin/start" "$BUILD_ROOT/sbin/start.disabled"
1168                 ln -s "/bin/true" "$BUILD_ROOT/sbin/start"
1169             fi
1170         fi
1171         # another workaround, see bug bnc#733699
1172         rm -f "$BUILD_ROOT/var/run/init.upgraded"
1173         continue
1174     fi
1175
1176     if test $PSUF = arch ; then
1177         if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" -ef "$BUILD_ROOT/.init_b_cache/$PKG.$PSUF" ; then 
1178             rm -f $BUILD_ROOT/.init_b_cache/$PKG.$PSUF
1179             cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF $BUILD_ROOT/.init_b_cache/$PKG.$PSUF || cleanup_and_exit 1
1180         fi
1181         PKGID=`readlink $BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF`
1182         PKGID="${PKGID##*/}"
1183         PKGID="${PKGID/%.pkg.tar.?z/.arch}"
1184         PKGID="${PKGID%.arch} arch"
1185         echo "installing ${PKGID%% *}"
1186         # -d -d disables deps checking
1187         ( chroot $BUILD_ROOT pacman -Uf -d -d --noconfirm .init_b_cache/$PKG.$PSUF 2>&1 || touch $BUILD_ROOT/exit ) | \
1188             perl -ne '$|=1;/^(warning: could not get filesystem information for |loading packages|looking for inter-conflicts|Targets |Total Installed Size: |Net Upgrade Size: |Proceed with installation|checking package integrity|loading package files|checking available disk space|installing |upgrading |warning:.*is up to date -- reinstalling|Optional dependencies for|    )/||/^$/||print'
1189         check_exit
1190         echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
1191         test -n "$PKG_HDRMD5" && echo "$PKG_HDRMD5 $PKGID" > $BUILD_ROOT/.preinstall_image/$PKG
1192         continue
1193     fi
1194
1195     if test -f $BUILD_ROOT/.init_b_cache/rpms/$PKG.id -a -f $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG ; then
1196         read PKGID < $BUILD_ROOT/.init_b_cache/rpms/$PKG.id
1197         read OLDPKGID < $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG
1198         if test "$PKGID" = "$OLDPKGID" ; then
1199             #echo "keeping ${PKGID%% *}"
1200             echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
1201             test -n "$PKG_HDRMD5" && echo "$PKG_HDRMD5 $PKGID" > $BUILD_ROOT/.preinstall_image/$PKG
1202             continue
1203         fi
1204     fi
1205
1206     PKGID=`rpm -qp --qf "$RPMIDFMT" $RPMCHECKOPTS_HOST $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm`
1207
1208     if test -f $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG ; then
1209         read OLDPKGID < $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG
1210         if test "$PKGID" != "$OLDPKGID" ; then
1211             echo deleting unwanted ${OLDPKGID%% *}
1212             rpm_e "$PKG"
1213         elif test "$VERIFY_BUILD_SYSTEM" = true ; then
1214             chroot $BUILD_ROOT rpm --verify $PKG 2>&1 | tee $TMPFILE
1215             if grep ^missing $TMPFILE > /dev/null ; then
1216                 echo deleting incomplete ${PKGID%% *}
1217                 rpm_e "$PKG"
1218             else
1219                 #echo "keeping ${PKGID%% *}"
1220                 echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
1221                 test -n "$PKG_HDRMD5" && echo "$PKG_HDRMD5 $PKGID" > $BUILD_ROOT/.preinstall_image/$PKG
1222                 continue
1223             fi
1224         else
1225             #echo "keeping ${PKGID%% *}"
1226             echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
1227             test -n "$PKG_HDRMD5" && echo "$PKG_HDRMD5 $PKGID" > $BUILD_ROOT/.preinstall_image/$PKG
1228             continue
1229         fi
1230         if test -e "$BUILD_ROOT/.init_b_cache/preinstalls/$PKG" ; then
1231             preinstall "$PKG"
1232             # call for rpm-4.x and not rpm-devel
1233             test -z "${PKG##rpm-[0-9]*}" && chroot $BUILD_ROOT rpm --rebuilddb
1234             # also exec for exchanged rpm !  naming is rpm-x86-<target>-<ver>
1235             test -z "${PKG##rpm-x86-*[0-9]*}" && chroot $BUILD_ROOT rpm --rebuilddb
1236         fi
1237     fi
1238     export ADDITIONAL_PARAMS=
1239     if test "$USE_FORCE" = true ; then
1240         export ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --force"
1241     fi
1242     # FIXME: work around for cross-build installs, we must not overwrite the running rpm
1243     if test "$PKG" = rpm ; then
1244         for i in $BUILD_ROOT/.init_b_cache/preinstalls/rpm-x86-* ; do
1245             test -e "$i" && ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --justdb"
1246         done
1247     fi
1248     if test -n "$DO_CUMULATE" -a "$ADDITIONAL_PARAMS" = "${ADDITIONAL_PARAMS%--justdb}"; then
1249         echo "cumulate ${PKGID%% *}"
1250         let cumulate++
1251         CUMULATED_LIST[$cumulate]=".init_b_cache/$PKG.rpm"
1252         CUMULATED_PIDS[$cumulate]="$PKGID"
1253         CUMULATED_HMD5[$cumulate]="$PKG_HDRMD5"
1254         continue
1255     fi
1256     echo "installing ${PKGID%% *}"
1257     if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm" -ef "$BUILD_ROOT/.init_b_cache/$PKG.rpm" ; then
1258         rm -f $BUILD_ROOT/.init_b_cache/$PKG.rpm
1259         cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.rpm $BUILD_ROOT/.init_b_cache/$PKG.rpm || cleanup_and_exit 1
1260     fi
1261     ( chroot $BUILD_ROOT rpm --ignorearch --nodeps -U --oldpackage --ignoresize $RPMCHECKOPTS \
1262                 $ADDITIONAL_PARAMS .init_b_cache/$PKG.rpm 2>&1 || \
1263           touch $BUILD_ROOT/exit ) | \
1264               grep -v "^warning:.*saved as.*rpmorig$"
1265     # delete link so package is only installed once
1266     rm -f $BUILD_ROOT/.init_b_cache/$PKG.rpm
1267     check_exit
1268     echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
1269     test -n "$PKG_HDRMD5" && echo "$PKG_HDRMD5 $PKGID" > $BUILD_ROOT/.preinstall_image/$PKG
1270
1271 done
1272
1273 if test $PSUF = deb ; then
1274     echo "configuring all installed packages..."
1275     # configure all packages after complete installation, not for each package like rpm does
1276     # We need to run this twice, because of cyclic dependencies as it does not succeed on most
1277     # debian based distros in the first attempt.
1278     if ! chroot $BUILD_ROOT dpkg --configure --pending  2>&1; then
1279        echo "first configure attempt failed, trying again..."
1280        chroot $BUILD_ROOT dpkg --configure --pending  2>&1 || touch $BUILD_ROOT/exit
1281     fi
1282 fi
1283
1284 # devices can vanish if devs got uninstalled
1285 test -c $BUILD_ROOT/dev/null || create_devs
1286
1287 cd $BUILD_ROOT || cleanup_and_exit 1
1288
1289 #
1290 # setup /etc/mtab
1291 #
1292 rm -f $BUILD_ROOT/etc/mtab
1293 cp /proc/mounts $BUILD_ROOT/etc/mtab
1294 chmod 644 $BUILD_ROOT/etc/mtab
1295
1296 #
1297 # to be sure, path is set correctly, we have to source /etc/profile before
1298 # starting rpm.
1299 #
1300 # XXX
1301 #rm -f $BUILD_ROOT/bin/rpm.sh
1302 #cp $BUILD_LIBDIR/lib/rpm.sh $BUILD_ROOT/bin/rpm.sh
1303 #chmod 755 $BUILD_ROOT/bin/rpm.sh
1304 #test -f $BUILD_ROOT/bin/rpm -a ! -L $BUILD_ROOT/bin/rpm && \
1305 #    mv $BUILD_ROOT/bin/rpm $BUILD_ROOT/bin/rpm.bin
1306 #rm -f $BUILD_ROOT/bin/rpm
1307 #ln -s rpm.sh $BUILD_ROOT/bin/rpm
1308
1309 #
1310 # some packages use uname -r to decide which kernel is used to build for.
1311 # this does not work in autobuild always.  Here is a wrapper script, that
1312 # gets Version from kernel sources.
1313 #
1314 # XXX
1315 #rm -f $BUILD_ROOT/bin/uname.sh
1316 #cp -v $BUILD_LIBDIR/lib/uname.sh $BUILD_ROOT/bin/uname.sh
1317 #chmod 755 $BUILD_ROOT/bin/uname.sh
1318 #test -f $BUILD_ROOT/bin/uname -a ! -L $BUILD_ROOT/bin/uname && \
1319 #    mv $BUILD_ROOT/bin/uname $BUILD_ROOT/bin/uname.bin
1320 #rm -f $BUILD_ROOT/bin/uname
1321 #ln -s uname.sh $BUILD_ROOT/bin/uname
1322
1323 #
1324 # some distributions have a /etc/rpmrc or /etc/rpm/macros and some not.
1325 # make sure, that it is setup correctly.
1326 #
1327 # XXX
1328 #rm -f $BUILD_ROOT/etc/rpmrc
1329 #if test -e $BUILD_LIBDIR/lib/rpmrc.$BUILD_BASENAME ; then
1330 #    cp -v $BUILD_LIBDIR/lib/rpmrc.$BUILD_BASENAME $BUILD_ROOT/etc/rpmrc
1331 #elif test -e $BUILD_LIBDIR/lib/rpmrc ; then
1332 #    cp -v $BUILD_LIBDIR/lib/rpmrc $BUILD_ROOT/etc/rpmrc
1333 #fi
1334
1335 # XXX
1336 #rm -f $BUILD_ROOT/etc/rpm/macros $BUILD_ROOT/etc/rpm/suse_macros
1337 #mkdir -p $BUILD_ROOT/etc/rpm
1338 #if test -e $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME ; then
1339 #    cp -v $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME $BUILD_ROOT/etc/rpm/macros
1340 #    cp -v $BUILD_LIBDIR/lib/macros.$BUILD_BASENAME $BUILD_ROOT/etc/rpm/suse_macros
1341 #elif test -e $BUILD_LIBDIR/lib/macros ; then
1342 #    cp -v $BUILD_LIBDIR/lib/macros $BUILD_ROOT/etc/rpm/macros
1343 #    cp -v $BUILD_LIBDIR/lib/macros $BUILD_ROOT/etc/rpm/suse_macros
1344 #fi
1345
1346 #
1347 # make sure, that our nis is not present in the chroot system
1348 #
1349 test -e $BUILD_ROOT/etc/nsswitch.conf && {
1350     echo removing nis flags from $BUILD_ROOT/etc/nsswitch.conf...
1351     cat $BUILD_ROOT/etc/nsswitch.conf | sed -e"s:nis::g" > \
1352         $BUILD_ROOT/etc/nsswitch.conf.tmp
1353     mv $BUILD_ROOT/etc/nsswitch.conf.tmp $BUILD_ROOT/etc/nsswitch.conf
1354 }
1355
1356 #
1357 # creating some default directories
1358 for DIR in /usr/share/doc/packages \
1359            /usr/X11R6/include/X11/pixmaps \
1360            /usr/X11R6/include/X11/bitmaps ; do
1361     mkdir -p $BUILD_ROOT/$DIR
1362 done
1363
1364 for FILE in /var/run/utmp /var/log/wtmp /etc/fstab ; do
1365     mkdir -p $BUILD_ROOT/${FILE%/*}
1366     touch $BUILD_ROOT/$FILE
1367 done
1368
1369 echo now finalizing build dir...
1370 if test -x $BUILD_ROOT/sbin/ldconfig ; then
1371         CHROOT_RETURN="`chroot $BUILD_ROOT /sbin/ldconfig 2>&1`"
1372         case "$CHROOT_RETURN" in
1373             *warning:*)
1374               chroot $BUILD_ROOT /sbin/ldconfig
1375               echo
1376               echo chroot $BUILD_ROOT /sbin/ldconfig
1377               echo
1378               echo "$CHROOT_RETURN"
1379               echo
1380               echo "Problem with ldconfig.  It's better to reinit the build system..."
1381               echo
1382               cleanup_and_exit 1
1383             ;;
1384         esac
1385 fi
1386 test -x $BUILD_ROOT/usr/sbin/Check && chroot $BUILD_ROOT /usr/sbin/Check
1387
1388 mkdir -p $BUILD_ROOT/var/adm/packages
1389 touch $BUILD_ROOT/var/adm/packages
1390 if test -x $BUILD_ROOT/sbin/SuSEconfig ; then
1391     if grep norestarts $BUILD_ROOT/sbin/SuSEconfig > /dev/null ; then
1392         chroot $BUILD_ROOT /sbin/SuSEconfig --norestarts --force
1393     else
1394         chroot $BUILD_ROOT /sbin/SuSEconfig --force
1395     fi
1396 fi
1397
1398 if test -x $BUILD_ROOT/usr/X11R6/bin/switch2mesasoft ; then
1399     chroot $BUILD_ROOT /usr/X11R6/bin/switch2mesasoft
1400 fi
1401
1402 for PROG in /usr/bin/TeX/texhash /usr/bin/texhash ; do
1403     test -x $BUILD_ROOT/$PROG && \
1404         chroot $BUILD_ROOT bash -c ". /etc/profile ; $PROG"
1405 done
1406
1407 if test -e $BUILD_ROOT/usr/share/zoneinfo/UTC ; then
1408     for PROG in /usr/sbin/zic /usr/bin/zic /bin/zic /sbin/zic ; do
1409         test -x $BUILD_ROOT/$PROG  && chroot $BUILD_ROOT $PROG -l UTC
1410     done
1411 fi
1412
1413 test -e $BUILD_ROOT/.build/init_buildsystem.data || HOST=`hostname`
1414 test -e $BUILD_ROOT/etc/hosts || echo "127.0.0.1 localhost" > $BUILD_ROOT/etc/hosts
1415 if ! grep -F "127.0.0.1 $HOST" $BUILD_ROOT/etc/hosts > /dev/null ; then
1416     # this makes a reverse lookup on 127.0.0.1 return the host name,
1417     # which is bad, but 127.0.0.2 does not work on all unix systems
1418     echo "127.0.0.1 $HOST" > $BUILD_ROOT/etc/hosts.new
1419     test -f $BUILD_ROOT/etc/hosts && cat $BUILD_ROOT/etc/hosts >> $BUILD_ROOT/etc/hosts.new
1420     mv $BUILD_ROOT/etc/hosts.new $BUILD_ROOT/etc/hosts
1421 fi
1422
1423 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
1424     echo "initializing rpm db..."
1425     chroot $BUILD_ROOT rpm --initdb || cleanup_and_exit 1
1426     # create provides index
1427     chroot $BUILD_ROOT rpm -q --whatprovides rpm >/dev/null 2>&1
1428 fi
1429
1430 # create modules.dep in kvm/xen
1431 # This can not work, until we use the native repository kernel
1432 #if [ $BUILD_ROOT = "/" -a -x /sbin/depmod ]; then
1433 #  /sbin/depmod -a
1434 #fi
1435
1436 rm -f $BUILD_ROOT/.rpmmacros $BUILD_ROOT/root/.rpmmacros
1437 rm -rf "$BUILD_ROOT/.init_b_cache"
1438 rm -f $BUILD_IS_RUNNING
1439 rm -f $TMPFILE
1440
1441 cleanup_and_exit 0