Merge "Support BuildFlags: nocumulaterpms" into devel
[tools/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 ################################################################
11 #
12 # Copyright (c) 1997-2014 SUSE Linux Products GmbH
13 #
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License version 2 or 3 as
16 # published by the Free Software Foundation.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program (see the file COPYING); if not, write to the
25 # Free Software Foundation, Inc.,
26 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 #
28 ################################################################
29
30 #
31 # needed globals variables
32 #
33 export SRC
34 export BUILD_DIR=${BUILD_DIR:-/usr/lib/build}
35 PATH="$BUILD_DIR:$PATH"
36
37 export YAST_IS_RUNNING=instsys
38
39 # slurp in package binary support
40 . "$BUILD_DIR/build-pkg"
41
42 # need to restore build root owner for non-root builds
43 browner=0
44 definesnstuff=()
45 repos=()
46
47 . $BUILD_DIR/common_functions || exit 1
48
49 BUILD_IS_RUNNING="$BUILD_ROOT"/not-ready
50 TMPFILE="$BUILD_ROOT"/tmpfile
51 RPMIDFMT="%{NAME}-%{VERSION}-%{RELEASE} %{BUILDTIME}-%{ARCH}\n"
52 RPMCHECKOPTS_HOST=
53 test -x /usr/bin/rpmquery && RPMCHECKOPTS_HOST="--nodigest --nosignature"
54
55 # should RPMs be installed with --force ?
56 USE_FORCE=false
57 PREPARE_VM=
58 KEEP_PACKS=
59 USEUSEDFORBUILD=
60 LIST_STATE=
61 RPMLIST=
62 CLEAN_BUILD=
63 CREATE_BUILD_BINARIES=
64 DLNOSIGNATURE=
65 CACHE_DIR=/var/cache/build
66 USEHIGHERDEPS=
67 test -z "$CONFIG_DIR" && CONFIG_DIR="$BUILD_DIR/configs"
68
69 while test -n "$1" ; do
70     case "$1" in
71         --prepare)
72             shift
73             PREPARE_VM=true
74             ;;
75         --keep-packs)
76         shift
77         KEEP_PACKS=true
78         ;;
79         --create-build-binaries)
80             shift
81             CREATE_BUILD_BINARIES=true
82             ;;
83         --useusedforbuild)
84             shift
85             USEUSEDFORBUILD=--useusedforbuild
86             ;;
87         --list-state)
88             shift
89             LIST_STATE=true
90             ;;
91         --rpmlist)
92             shift
93             RPMLIST=$1
94             shift
95             ;;
96         --define|--with|--without)
97             definesnstuff[${#definesnstuff[@]}]="$1";
98             definesnstuff[${#definesnstuff[@]}]="$2";
99             shift 2
100             ;;
101         --repository|--repo)
102             repos[${#repos[@]}]="$2";
103             shift 2
104             ;;
105         --clean)
106             CLEAN_BUILD="$1"
107             shift
108             ;;
109         --cachedir)
110             CACHE_DIR="$2"
111             shift 2
112             ;;
113         --configdir)
114             CONFIG_DIR="$2"
115             shift 2
116             ;;
117         --nosignature)
118             shift
119             DLNOSIGNATURE="--nosignature"
120             ;;
121     --use-higher-deps)
122         shift
123         USEHIGHERDEPS="--usehigherdeps"
124         ;;
125         --root)
126             BUILD_ROOT="$2"
127             shift 2
128             ;;
129         --arch)
130             BUILD_ARCH="$2"
131             shift 2
132             ;;
133         --dist)
134             BUILD_DIST="$2"
135             shift 2
136             ;;
137         *)
138             break
139             ;;
140     esac
141 done
142 PKGS=("$@")
143
144 if test -z "$BUILD_ROOT" ; then
145     echo "Please specify a build root!"
146     exit 1
147 fi
148
149 BUILD_IS_RUNNING="$BUILD_ROOT"/not-ready
150 TMPFILE="$BUILD_ROOT"/tmpfile
151
152 #
153 # needed functions
154 #
155 mount_stuff() {
156     if test "$UID" = 0 -a -n "$BUILD_ROOT" -a "$BUILD_ROOT" != / ; then
157         mount -n -tproc none "$BUILD_ROOT/proc"
158         test -e "$BUILD_ROOT/proc/self" || cleanup_and_exit 1 "Could not mount /proc, is this a container with missing capabilities?"
159         mount -n -tdevpts -omode=0620,gid=5 none "$BUILD_ROOT/dev/pts"
160     fi
161 }
162
163 umount_stuff() {
164     if test -n "$BUILD_ROOT" -a "$BUILD_ROOT" != / ; then
165         buildroot_umount "/proc/sys/fs/binfmt_misc"
166         buildroot_umount "/proc"
167         buildroot_umount "/sys"
168         buildroot_umount "/dev/pts"
169         buildroot_umount "/dev/shm"
170         buildroot_umount "/mnt"
171     fi
172 }
173
174 cleanup_and_exit() {
175     trap EXIT
176     test -z "$1" && set 0
177     if test -n "$2" ; then
178         if test "$1" -ne 0 ; then
179             echo "$2" >&2
180         else
181             echo "$2"
182         fi
183     fi
184     test "$BUILD_ROOT" = / -a -n "$browner" && chown "$browner" "$BUILD_ROOT"
185     # umount so init_buildsystem can be used standalone
186     umount_stuff
187     # never report a fatal error when the job got killed from outside
188     test "$1" = 4 -a -e "$BUILD_ROOT/exit" && exit 1
189     exit $1
190 }
191
192 clean_build_root() {
193     if test -n "$BUILD_ROOT" -a "$BUILD_ROOT" != / ; then
194         umount_stuff
195         rm -rf -- "$BUILD_ROOT"/* 2> /dev/null || true
196         chattr -a -A -i -R -- "$BUILD_ROOT" 2> /dev/null || true    # does not follow symlinks
197         rm -rf -- "$BUILD_ROOT"/*
198         rm -rf -- "$BUILD_ROOT/.build"
199         rm -rf -- "$BUILD_ROOT"/.build.console.*
200         rm -rf -- "$BUILD_ROOT"/.build.hostarch.*
201         rm -rf -- "$BUILD_ROOT"/.build.kernel.*
202         rm -rf -- "$BUILD_ROOT"/.build.initrd.*
203         rm -rf -- "$BUILD_ROOT"/.build.sysroot
204         rm -rf -- "$BUILD_ROOT/.root"
205         rm -rf -- "$BUILD_ROOT/.init_b_cache"
206         test -L "$BUILD_ROOT/.preinstall_image" && rm -f -- "$BUILD_ROOT/.preinstall_image"
207         rm -rf -- "$BUILD_ROOT"/.preinstall_image/*
208         rm -rf -- "$BUILD_ROOT"/.preinstallimage*
209     fi
210 }
211
212 unsafe_preinstall_check() {
213     # cpio isn't safe so we require bsdtar for VMs. chroot is
214     # unsafe anyways so it's ok for that.
215     if test -n "$PREPARE_VM" ; then
216         echo "Error: setting up a VM requires bsdtar for security reasons."
217         echo "Please install bsdtar."
218         cleanup_and_exit 1
219     fi
220 }
221
222 preinstall_image_filter() {
223     for PKG in "$@" ; do
224         test -e "$BUILD_ROOT/.preinstall_image/$PKG" && continue
225         echo $PKG
226     done
227 }
228
229 preinstall_image() {
230     check_exit
231     echo "unpacking preinstall image${2:+ $2}"
232     cd "$BUILD_ROOT" || cleanup_and_exit 1
233     if test -x /usr/bin/bsdtar ; then
234         TAR="/usr/bin/bsdtar -P --chroot --numeric-owner -x"
235     else
236         unsafe_preinstall_check
237         TAR="tar -x"
238     fi
239     # pipe output through perl to print a dot every 1000 lines
240     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
241         echo "unpack failed."
242         test -e "$BUILD_ROOT/.preinstallimage.unpack" && cat "$BUILD_ROOT/.preinstallimage.unpack"
243         cleanup_and_exit 1
244     fi
245     rm -f "$BUILD_ROOT/.preinstallimage.unpack"
246 }
247
248 preinstall() {
249     local PKG="$1"
250     check_exit
251     echo "preinstalling $PKG..."
252     cd "$BUILD_ROOT" || cleanup_and_exit 1
253     if test -x /usr/bin/bsdtar ; then
254         CPIO="/usr/bin/bsdtar -P --chroot -o --numeric-owner -x -f-"
255         TAR="/usr/bin/bsdtar -P --chroot -o --numeric-owner -x"
256     else
257         unsafe_preinstall_check
258         CPIO="cpio --extract --unconditional --preserve-modification-time --make-directories --no-absolute-filenames --quiet"
259         cpio --help 2>/dev/null | grep -q -e --extract-over-symlinks && CPIO="$CPIO --extract-over-symlinks"
260         TAR="tar -x"
261     fi
262     pkg_preinstall
263 }
264
265 run_pkg_scripts() {
266     chroot "$BUILD_ROOT" /sbin/ldconfig 2>/dev/null
267     for PKG in $PACKAGES_TO_RUNSCRIPTS ; do
268         pkg_runscripts
269         check_exit
270     done
271 }
272
273
274 create_devs() {
275     local com file mode arg
276
277     mkdir -m 755 -p "$BUILD_ROOT"/dev/pts
278     test -d "$BUILD_ROOT"/dev/shm || rm -f "$BUILD_ROOT"/dev/shm
279     mkdir -m 755 -p "$BUILD_ROOT"/dev/shm
280     while read com file mode arg ; do
281         rm -f "$BUILD_ROOT"/dev/$file
282         if test $com = ln ; then
283             ln -s $arg "$BUILD_ROOT"/dev/$file
284             continue
285         fi
286         $com -m $mode "$BUILD_ROOT"/dev/$file $arg
287     done << DEVLIST
288         mknod null    666 c 1 3
289         mknod zero    666 c 1 5
290         mknod full    666 c 1 7
291         mknod random  666 c 1 8
292         mknod urandom 644 c 1 9
293         mknod tty     666 c 5 0
294         mknod ptmx    666 c 5 2
295         mknod loop-control 600 c 10 237
296         mknod loop0   640 b 7 0
297         mknod loop1   640 b 7 1
298         mknod loop2   640 b 7 2
299         mknod loop3   640 b 7 3
300         ln    fd      777 /proc/self/fd
301         ln    stdin   777 fd/0
302         ln    stdout  777 fd/1
303         ln    stderr  777 fd/2
304 DEVLIST
305 }
306
307 # check whether the repo list contains a plain "zypp://". Add all
308 # enabled zypp repos in this case
309 expand_plain_zypp_repo() {
310     local i j
311     r=()
312     for i in "${repos[@]}"; do
313         if test "$i" = "zypp://" ; then
314             for j in $($BUILD_DIR/createzyppdeps --listrepos) ; do
315                 r=("${r[@]}" "zypp://$j")
316             done
317         else
318             r=("${r[@]}" "$i")
319         fi
320     done
321     repos=("${r[@]}")
322 }
323
324 create_cache_file() {
325     local findonly=
326     expand_plain_zypp_repo
327     if ! test -f $CACHE_FILE || ! test -f $CACHE_FILE.id || \
328         test "${repos[*]} ${BUILD_RPMS//:/ /}" != "$(cat $CACHE_FILE.id 2>/dev/null)"; then
329         rm -f $CACHE_FILE.id
330     else
331         for SRC in "${repos[@]}" ${BUILD_RPMS//:/ /}; do
332             test -n "$SRC" || SRC=.
333             if test "${SRC#zypp://}" != "$SRC" ; then
334                 SRC="/var/cache/zypp/raw/${SRC#zypp://}"
335             fi
336             if test "$SRC" -nt $CACHE_FILE; then
337                 rm -f $CACHE_FILE.id
338                 break
339             fi
340             # always rebuild if we have remote repositories and --clean is given
341             if test -n "$CLEAN_BUILD" -a "$SRC" != "${SRC#*://}" -a "${SRC#zypp://}" = "$SRC" ; then
342                 rm -f $CACHE_FILE.id
343                 break
344             fi
345         done
346     fi
347     if ! test -f $CACHE_FILE.id ; then
348         test -z "$LIST_STATE" && echo initializing $CACHE_FILE ...
349         REPOTYPE=
350         if test -n "$BUILD_DIST" ; then
351             for i in `queryconfig repotype --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH"` ; do
352                 case $i in
353                     arch|debian|hdlist2|rpm-md) REPOTYPE=$i
354                 esac
355                 test -n "$REPOTYPE" && break
356             done
357             if test -z "$REPOTYPE"; then
358                 BINTYPE=`queryconfig binarytype --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH"`
359                 case "$BINTYPE" in
360                     arch) REPOTYPE=arch ;;
361                     deb)  REPOTYPE=debian ;;
362                 esac
363             fi
364         fi
365         if test -z "$REPOTYPE" ; then
366             # check the first init_buildsystem arg, maybe it is a recipe
367             case ${PKGS[0]} in
368                 *.spec)
369                     REPOTYPE=rpm-md ;;
370                 *.dsc)
371                     REPOTYPE=debian ;;
372                 */PKGBUILD|PKGBUILD)
373                     REPOTYPE=arch ;;
374             esac
375         fi
376         for SRC in "${repos[@]}" -- ${BUILD_RPMS//:/ /}; do
377             if test "$SRC" = '--' ; then
378                 findonly=1
379                 continue
380             fi
381             test -z "$SRC" && SRC=`pwd`
382             if test "${SRC#http://}" != "$SRC" -o "${SRC#https://}" != "$SRC" -o "${SRC#ftp://}" != "$SRC" -o "${SRC#ftps://}" != "$SRC" ; then
383                 # remote repo, cache binary packages
384                 mkdir -p "$(getcachedir "$SRC")"
385                 if test "$REPOTYPE" = arch ; then
386                     set -- $BUILD_DIR/createarchdeps --cachedir="$CACHE_DIR" "$SRC"
387                 elif test "$REPOTYPE" = debian ; then
388                     set -- $BUILD_DIR/createdebdeps --cachedir="$CACHE_DIR" --archpath "$BUILD_ARCH" "$SRC"
389                 elif test "$REPOTYPE" = hdlist2 ; then
390                     set -- $BUILD_DIR/createmdkdeps --cachedir="$CACHE_DIR" "$SRC"
391                 else
392                     set -- $BUILD_DIR/createrepomddeps --cachedir="$CACHE_DIR" "$SRC"
393                 fi
394             elif test "${SRC#zypp://}" != "$SRC" ; then
395                 # special zypp repo
396                 set -- $BUILD_DIR/createzyppdeps --cachedir="$CACHE_DIR" "$SRC"
397             elif test ! -e "$SRC" ; then
398                 echo "*** $SRC does not exist" >&2
399                 cleanup_and_exit 1
400             elif test -z "$findonly" -a \( -e "$SRC"/suse/setup/descr/packages -o -e "$SRC"/suse/setup/descr/packages.gz \) ; then
401                 set -- $BUILD_DIR/createyastdeps "$SRC"
402             elif test -z "$findonly" -a -e "$SRC"/repodata/repomd.xml ; then
403                 set -- $BUILD_DIR/createrepomddeps "$SRC"
404             else
405                 set -- $BUILD_DIR/createdirdeps --oldfile "$CACHE_FILE" "$SRC"
406             fi
407             echo "$@" >&2
408             "$@" || cleanup_and_exit 1
409             echo D:
410         done > $CACHE_FILE.new
411
412         mv $CACHE_FILE.new $CACHE_FILE
413         echo "${repos[*]} ${BUILD_RPMS//:/ /}" > $CACHE_FILE.id
414     fi
415 }
416
417 fail_exit() {
418     cleanup_and_exit 1
419 }
420
421 # modifies $SRC
422 downloadpkg() {
423     local url="$1"
424     local cachedir
425
426     if test "${url:0:7}" == "zypp://" -o "${url:0:7}" == "http://" -o "${url:0:8}" == "https://" -o "${url:0:6}" == "ftp://" -o "${url:0:7}" == "ftps://" ; then
427         cachedir="$(getcachedir "$url")"
428         local name="$(basename "$url")"
429         name=${name/%.pkg.tar.zst/.arch}
430         name=${name/%.pkg.tar.?z/.arch}
431         SRC="$cachedir/$name"
432     else
433         echo "Invalid url: $url"
434         cleanup_and_exit 1
435     fi
436
437     local destdir="$cachedir/tmp_$$"
438     mkdir -p "$destdir"
439     if [ ! -f "$BUILD_ROOT"/exit ] ; then
440         echo "downloading $url ... ";
441     fi
442     $BUILD_DIR/download "$destdir" "$url" || cleanup_and_exit 1
443     local destfile="$destdir/${url##*/}"
444     if test ! -e "$destfile" ; then
445         echo "expected $destfile after download but it's missing" >&2
446         cleanup_and_exit 1
447     fi
448     # for rpm check integrity and the signature
449     case $destfile in
450       *.rpm)
451         rpm -K $DLNOSIGNATURE "$destfile" > $destfile.v || { echo "rpm verify failed" >&2; rm -rf "$destdir"; cleanup_and_exit 1; }
452         if grep "NOT OK" $destfile.v; then
453             rm -rf "$destdir"
454             cleanup_and_exit 1
455         fi
456         rm -f "$destfile.v"
457         ;;
458     esac
459     # if there is such package, didn't mv.
460     mv -n "$destfile" "$SRC" || cleanup_and_exit 1
461     rm -rf $destdir
462 }
463
464 reorder() {
465     test -z "$*" && return
466     rm -f "$BUILD_ROOT"/.init_b_cache/order.manifest
467     for PKG in "$@" ; do
468         echo "$PKG" >> "$BUILD_ROOT"/.init_b_cache/order.manifest
469     done
470
471     #NOTICE: in reorder, you can't print any logs, or it will treate the logs as pacakges outside
472     #Change: If reorder failed because of missing packages, add logic to re-download the missed packages.
473     touch "$BUILD_ROOT"/missedPKG
474     echo "" > "$BUILD_ROOT"/missedPKG
475     while true
476     do
477         #remove exit file, it will create exit file if reorder fail.
478         rm -f "$BUILD_ROOT"/exit
479         #call order operation
480         $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 "$BUILD_ROOT"/missedPKG || touch "$BUILD_ROOT"/exit
481
482         #order operation OK, exit reorder funciton
483         if [ ! -f "$BUILD_ROOT"/exit ]; then
484             #after reorder again, if reorder is normal, should delete tmp file.
485             rm -f "$BUILD_ROOT"/missedPKG
486             rm -f "$BUILD_ROOT"/.init_b_cache/rpmlist_reorder_check.download
487             break
488         fi
489         #order operation failed!
490         if [ -f "$BUILD_ROOT"/exit ]; then
491             #fail reason: binary not found. To try to download packages again.
492             if test -s "$BUILD_ROOT"/missedPKG; then
493                 for line in `cat "$BUILD_ROOT"/missedPKG`; do
494                     while read PKG SRC; do
495                         if [ "$PKG" = "$line" ]; then
496                             downloadpkg "$SRC"
497                             # downloadpkg missed packages
498                             SRCSUF=${SRC/%.pkg.tar.?z/.arch}
499                             #remove the soft link that already existed.
500                             rm -f "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRCSUF##*.}"
501                             ln -s "$SRC" "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRCSUF##*.}"
502                             break
503                         fi
504                     done < "$BUILD_ROOT"/.init_b_cache/rpmlist_reorder_check.download
505                 done
506                 #clear file. use it next loop if needed.
507                 echo "" > "$BUILD_ROOT"/missedPKG
508             #Other fail reason. Exit!
509             else
510                 rm -f "$BUILD_ROOT"/missedPKG
511                 rm -f "$BUILD_ROOT"/.init_b_cache/rpmlist_reorder_check.download
512                 break
513             fi
514         fi
515     done
516     rm -f "$BUILD_ROOT"/.init_b_cache/order.manifest
517 }
518
519 getcachedir() {
520     local url=$1
521     for repo in "${repos[@]}" ; do
522         if test "${url:0:${#repo}}" == "$repo" ; then
523             read repoid dummy < <(echo -n "$repo" | md5sum)
524             echo "$CACHE_DIR/$repoid"
525             break
526         fi
527     done
528 }
529
530 can_reuse_cached_package() {
531     local cachepkgid pkgid xpkgid
532     test -s "$1" || return 1
533     if test -s "$BUILD_ROOT/.init_b_cache/rpms/$PKG.id" ; then
534         local ep=$(rpm -qp --qf "%{EPOCH}" "'${1}'")
535         if test "$ep" != "(none)"; then
536                 RPMIDFMT_EP="%{NAME}-%{EPOCH}:%{VERSION}-%{RELEASE} %{BUILDTIME}-%{ARCH}\n"
537                 pkgid=`rpm -qp --qf "${RPMIDFMT_EP}" $RPMCHECKOPTS_HOST "'${1}'"`
538         else
539                 pkgid=`rpm -qp --qf "${RPMIDFMT}" $RPMCHECKOPTS_HOST "'${1}'"`
540         fi
541         read cachepkgid < "$BUILD_ROOT"/.init_b_cache/rpms/$PKG.id
542         # check if the build time is zero
543         case $cachepkgid in
544             *" 0-"*) xpkgid="${pkgid##* }"; pkgid="${pkgid% *} 0-${xpkgid#*-}" ;;
545         esac
546         test "$cachepkgid" = "$pkgid" || return 1
547     fi
548     return 0
549 }
550
551 check_copy_qemu()
552 {
553     local arch
554
555     for arch in $EMULATOR_DEVS; do
556        if test -e $BUILD_DIR/qemu-$arch; then
557            return 0
558        fi
559     done
560     return 1
561 }
562
563 copy_qemu()
564 {
565     local path dest
566
567     echo "copying qemu"
568
569     mkdir -p "$BUILD_ROOT"/usr/bin
570
571     if check_copy_qemu; then
572
573        for path in $BUILD_DIR/qemu-*; do
574            if file $path | grep -q static; then
575                dest="$BUILD_ROOT/usr/bin/${path##*/}"
576                if [ -f "$path" -a ! -x "$dest" ]; then
577                    echo -n " $path"            # report copy
578                    #echo install -m755 "$path" "$dest"
579                    install -m755 "$path" "$dest"
580                fi
581            fi
582        done
583
584     else
585
586        for path in /usr/bin/qemu-*; do
587            if file $path | grep -q static; then
588                dest="$BUILD_ROOT/usr/bin/${path##*/}"
589                if [ -f "$path" -a ! -x "$dest" ]; then
590                    echo -n " $path"            # report copy
591                    #echo install -m755 "$path" "$dest"
592                    install -m755 "$path" "$dest"
593                fi
594            fi
595        done
596
597        if [ -e /usr/sbin/qemu-binfmt-conf.sh \
598            -a ! -e "$BUILD_ROOT"/usr/sbin/qemu-binfmt-conf.sh ]; then
599            echo " /usr/sbin/qemu-binfmt-conf.sh"               # report copy
600            mkdir -p "$BUILD_ROOT"/usr/sbin
601            install -m755 /usr/sbin/qemu-binfmt-conf.sh "$BUILD_ROOT"/usr/sbin
602        fi
603
604     fi
605     echo ""
606
607     # Below for backward compatibility when /.build/initvm is not present
608
609     if [ -n "$PREPARE_VM" ]; then
610        if [ -x /bin/bash-static -a -x /bin/mount-static ]; then
611            echo " /bin/bash-static /bin/mount-static"  # report copy
612            mkdir -p "$BUILD_ROOT"/bin
613            install -m755 /bin/bash-static "$BUILD_ROOT"/bin
614            install -m755 /bin/mount-static "$BUILD_ROOT"/bin
615        fi
616     fi
617 }
618
619
620 set_build_arch
621
622 trap fail_exit EXIT
623
624 if test "$BUILD_ROOT" = / ; then
625     browner="$(stat -c %u /)"
626 fi
627
628 if test -n "$CLEAN_BUILD" ; then
629     clean_build_root
630 fi
631
632 #
633 # now test if there was an incomplete run
634 #
635 if test -e "$BUILD_IS_RUNNING" ; then
636     echo "It seems that there was an incomplete setup of $BUILD_ROOT."
637     echo "To be sure, we will build it again completely..."
638     umount_stuff
639     echo "Your build system is broken!! Shall I execute"
640     echo
641     echo "    rm -rf -- $BUILD_ROOT/*"
642     echo
643     echo "y - yes, cleanup the build root"
644     echo "N - No, abort build (default on enter)"
645     echo "c - Continue anyway with this build root"
646     echo -n "[y/N/c] "
647     read ANSWER
648     case "$ANSWER" in
649         c|C)
650             rm -f $BUILD_IS_RUNNING "$BUILD_ROOT"/exit ;;
651         y|Y)
652             clean_build_root ;;
653         *)
654             cleanup_and_exit 1 ;;
655     esac
656 fi
657
658 # check for surprises
659 if test -z "$CLEAN_BUILD" -a -n "$BUILD_ROOT" -a "$BUILD_ROOT" != / ; then
660     assert_dirs
661 fi
662
663 # umount stuff if mounted
664 if test -n "$BUILD_ROOT" -a "$BUILD_ROOT" != / -a -e "$BUILD_ROOT/proc/self" ; then
665     umount_stuff
666 fi
667
668 #
669 # store that we start to build the system
670 #
671 mkdir -p -- "$BUILD_ROOT" "$BUILD_ROOT/.build" "$BUILD_ROOT/proc" "$BUILD_ROOT/sys" "$BUILD_ROOT/dev/pts" || cleanup_and_exit 4
672
673 touch $BUILD_IS_RUNNING
674
675 if test -n "$PREPARE_VM" ; then
676     rm -f "$BUILD_ROOT"/.build/init_buildsystem.data
677 fi
678
679 if test -e "$BUILD_ROOT"/.build/init_buildsystem.data ; then
680     # vm continuation, read old config
681     . "$BUILD_ROOT"/.build/init_buildsystem.data
682 else
683     #
684     # now make sure that all the packages are installed.
685     #
686     rm -rf "$BUILD_ROOT/.init_b_cache"
687     mkdir -p "$BUILD_ROOT/.init_b_cache/scripts"
688
689     if test -z "$RPMLIST" ; then
690         #
691         # create rpmdeps file
692         #
693         CACHE_FILE="$BUILD_ROOT"/.srcfiles.cache
694         create_cache_file
695
696         #
697         # select and expand packages
698         #
699         RPMLIST="$BUILD_ROOT"/.init_b_cache/rpmlist
700         test -z "$LIST_STATE" && echo "expanding package dependencies..."
701         VMOPT=
702         test -z "$PREPARE_VM" || VMOPT=--vm
703         if ! $BUILD_DIR/expanddeps $USEUSEDFORBUILD $VMOPT $USEHIGHERDEPS "${definesnstuff[@]}" --dist "$BUILD_DIST" --depfile "$CACHE_FILE" --archpath "$BUILD_ARCH" --configdir $CONFIG_DIR "${PKGS[@]}" > $RPMLIST ; then
704             rm -f $BUILD_IS_RUNNING
705             cleanup_and_exit 1
706         fi
707     fi
708
709     if test -n "$LIST_STATE" ; then
710         rm -f $BUILD_IS_RUNNING
711         while read PKG SRC ; do
712            test "$PKG" = "preinstall:" && continue
713            test "$PKG" = "runscripts:" && continue
714            test "$PKG" = "cbpreinstall:" && continue
715            test "$PKG" = "vminstall:" && continue
716            test "$PKG" = "dist:" && continue
717            test "$PKG" = "rpmid:" && continue
718            echo "${SRC##*/}"
719         done < "$BUILD_ROOT"/.init_b_cache/rpmlist
720         rm -rf "$BUILD_ROOT/.init_b_cache"
721         cleanup_and_exit 0
722     fi
723
724     #
725     # register the QEMU emulator if needed
726     # (we do not need this for the prepare step, as we do not run scripts in this case)
727     #
728     copy_qemu
729     if test -z "$PREPARE_VM" ; then
730         if check_use_emulator ; then
731             echo "registering binfmt handlers for cross build"
732             "$BUILD_DIR/$INITVM_NAME"
733             echo 0 > /proc/sys/vm/mmap_min_addr
734             read mmap_min_addr < /proc/sys/vm/mmap_min_addr
735             if test "$mmap_min_addr" != 0 ; then
736                 echo "Warning: mmap_min_addr is != 0. If programs fail at mmap this could be the reason."
737             fi
738         fi
739     fi
740
741     #
742     # extract the data from the (generated) rpm list
743     #
744     PACKAGES_TO_INSTALL=
745     PACKAGES_TO_PREINSTALL=
746     PACKAGES_TO_RUNSCRIPTS=
747     PACKAGES_TO_VMINSTALL=
748     PACKAGES_TO_INSTALLONLY=
749     PACKAGES_TO_NOINSTALL=
750     PREINSTALL_IMAGE=
751     PREINSTALL_IMAGE_SOURCE=
752     RUNSCRIPTS_SEEN=
753     PACKAGES_FROM_PREINSTALLIMAGE=
754     GUESSED_DIST=unknown
755     mkdir -p "$BUILD_ROOT"/.init_b_cache/rpms
756     rm -f "$BUILD_ROOT"/.init_b_cache/rpmlist.download
757     while read PKG SRC ; do
758         if test "$PKG" = "preinstall:" ; then
759             PACKAGES_TO_PREINSTALL=$SRC
760             continue
761         fi
762         if test "$PKG" = "vminstall:" ; then
763             PACKAGES_TO_VMINSTALL=$SRC
764             continue
765         fi
766         if test "$PKG" = "installonly:" ; then
767             PACKAGES_TO_INSTALLONLY=$SRC
768             continue
769         fi
770         if test "$PKG" = "noinstall:" ; then
771             PACKAGES_TO_NOINSTALL=$SRC
772             continue
773         fi
774         if test "$PKG" = "preinstallimage:" ; then
775             PREINSTALL_IMAGE=${SRC##*/}
776             ln -s "$SRC" "$BUILD_ROOT/.init_b_cache/rpms/${SRC##*/}"
777             continue
778         fi
779         if test "$PKG" = "preinstallimagesource:" ; then
780             PREINSTALL_IMAGE_SOURCE="$SRC"
781             continue
782         fi
783         if test "$PKG" = "runscripts:" ; then
784             RUNSCRIPTS_SEEN=true
785             PACKAGES_TO_RUNSCRIPTS=$SRC
786             continue
787         fi
788         if test "$PKG" = "dist:" ; then
789             GUESSED_DIST=$SRC
790             continue
791         fi
792         if test "$PKG" = "rpmid:" ; then
793             echo "${SRC#*:}" > "$BUILD_ROOT"/.init_b_cache/rpms/${SRC%%:*}.id
794             continue
795         fi
796         if test "$PKG" != "${PKG%:}"; then
797             echo "Warning: ignoring unsupported tag '$PKG'" >&2
798             continue
799         fi
800         if test "$SRC" = "preinstallimage" ; then
801             PACKAGES_FROM_PREINSTALLIMAGE="$PACKAGES_FROM_PREINSTALLIMAGE $PKG"
802             PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL $PKG"
803             continue
804         fi
805         PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL $PKG"
806         if test "${SRC#/}" = "$SRC" ; then
807             case "$SRC" in
808                 zypp://* | http://* | https://* | ftp://* | ftps://*)
809                     echo "$PKG $SRC" >>"$BUILD_ROOT"/.init_b_cache/rpmlist.download
810                     continue
811                     ;;
812                 *)
813                     echo "unsupported url for '$PKG': $SRC" >&2
814                     cleanup_and_exit 1
815                     ;;
816             esac
817         fi
818         SRCSUF=${SRC/%.pkg.tar.zst/.arch}
819         SRCSUF=${SRCSUF/%.pkg.tar.?z/.arch}
820         ln -s "$SRC" "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRCSUF##*.}"
821     done < $RPMLIST
822
823     PACKAGES_TO_ALL="$PACKAGES_TO_INSTALL"
824     PACKAGES_TO_AVAILABLE="$PACKAGES_TO_INSTALL"
825
826     # subtract noinstall packages from PACKAGES_TO_INSTALL
827     if test -n "$PACKAGES_TO_NOINSTALL" ; then
828         settest=" $PACKAGES_TO_NOINSTALL "
829         PACKAGES_TO_INSTALL=
830         for PKG in $PACKAGES_TO_ALL ; do
831             test "$settest" = "${settest/ $PKG /}" && PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL $PKG"
832         done
833     fi
834     # subtract installonly packages from PACKAGES_TO_AVAILABLE
835     if test -n "$PACKAGES_TO_INSTALLONLY" ; then
836         settest=" $PACKAGES_TO_INSTALLONLY "
837         PACKAGES_TO_AVAILABLE=
838         for PKG in $PACKAGES_TO_ALL ; do
839             test "$settest" = "${settest/ $PKG /}" && PACKAGES_TO_AVAILABLE="$PACKAGES_TO_AVAILABLE $PKG"
840         done
841     fi
842
843     # check if we really can use cached versions for packages on the download list
844     if test -s "$BUILD_ROOT"/.init_b_cache/rpmlist.download ; then
845         echo "calculating packages to download..."
846         rm -f "$BUILD_ROOT"/.init_b_cache/rpmlist.download2
847         while read PKG SRC ; do
848             cachepkg="${SRC##*/}"
849             cachepkg="${cachepkg/%.pkg.tar.zst/.arch}"
850             cachepkg="${cachepkg/%.pkg.tar.?z/.arch}"
851             if test "$SRC" != "${SRC#zypp://}" ; then
852                 # for zypp packages also look in the zypp cache
853                 cachedir="/var/cache/zypp/packages/${SRC#zypp://}"
854                 cachedir="${cachedir%/*}"
855                 if can_reuse_cached_package "$cachedir/$cachepkg" ; then
856                     SRCSUF=${SRC/%.pkg.tar.zst/.arch}
857                     SRCSUF=${SRCSUF/%.pkg.tar.?z/.arch}
858                     ln -s "$cachedir/$cachepkg" "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRCSUF##*.}"
859                     continue
860                 fi
861             fi
862             cachedir="$(getcachedir "$SRC")"
863             if can_reuse_cached_package "$cachedir/$cachepkg" ; then
864                 SRCSUF=${SRC/%.pkg.tar.zst/.arch}
865                 SRCSUF=${SRCSUF/%.pkg.tar.?z/.arch}
866         ln -s "$cachedir/$cachepkg" "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRCSUF##*.}"
867                 continue
868             fi
869             # not found in cache or cache has different package
870             rm -f "$cachedir/$cachepkg"
871             echo "$PKG $SRC" >>"$BUILD_ROOT"/.init_b_cache/rpmlist.download2
872         done < "$BUILD_ROOT"/.init_b_cache/rpmlist.download
873         rm -f "$BUILD_ROOT"/.init_b_cache/rpmlist.download
874         test -s "$BUILD_ROOT"/.init_b_cache/rpmlist.download2 && mv "$BUILD_ROOT"/.init_b_cache/rpmlist.download2 "$BUILD_ROOT"/.init_b_cache/rpmlist.download
875     fi
876
877     # now do the download of missing packages
878     if test -s "$BUILD_ROOT"/.init_b_cache/rpmlist.download ; then
879         PACKAGES_TO_DOWNLOAD=`cat ${RPMLIST}.download|awk '{print $2}'`
880         progress_setup PACKAGES_TO_DOWNLOAD
881         while read PKG SRC ; do
882             progress_step PACKAGES_TO_DOWNLOAD
883             downloadpkg "$SRC"
884             # downloadpkg modified $SRC, so it has a right name for use
885             SRCSUF=${SRC/%.pkg.tar.zst/.arch}
886             SRCSUF=${SRCSUF/%.pkg.tar.?z/.arch}
887             ln -s "$SRC" "$BUILD_ROOT/.init_b_cache/rpms/$PKG.${SRCSUF##*.}"
888         done < "$BUILD_ROOT"/.init_b_cache/rpmlist.download
889         cp "$BUILD_ROOT"/.init_b_cache/rpmlist.download "$BUILD_ROOT"/.init_b_cache/rpmlist_reorder_check.download
890         rm -f "$BUILD_ROOT"/.init_b_cache/rpmlist.download
891         echo
892     fi
893
894     # compatibility...
895     test -z "$RUNSCRIPTS_SEEN" && PACKAGES_TO_RUNSCRIPTS="$PACKAGES_TO_PREINSTALL"
896
897     echo "$GUESSED_DIST" > "$BUILD_ROOT"/.guessed_dist
898     test -n "$BUILD_DIST" || BUILD_DIST="$GUESSED_DIST"
899
900     pkg_set_type
901
902     if test -n "$PREINSTALL_IMAGE" ; then
903         for PKG in $PACKAGES_FROM_PREINSTALLIMAGE ; do
904             # touch the file so that the copying works
905             touch "$BUILD_ROOT"/.init_b_cache/rpms/"$PKG.$PSUF"
906         done
907     fi
908
909 fi
910
911 #
912 # test if we need to preinstall
913 #
914 if test ! -e "$BUILD_ROOT"/installed-pkg -a ! -e "$BUILD_ROOT"/.build/init_buildsystem.data ; then
915     mkdir -p "$BUILD_ROOT"/etc || cleanup_and_exit 1
916     mkdir -p "$BUILD_ROOT"/proc || cleanup_and_exit 1
917     test -f "$BUILD_ROOT"/etc/HOSTNAME || hostname -f > "$BUILD_ROOT"/etc/HOSTNAME
918     for PKG in $PACKAGES_TO_RUNSCRIPTS ; do
919         : > "$BUILD_ROOT"/.init_b_cache/scripts/$PKG.run
920     done
921     PACKAGES_TO_PREINSTALL_FILTERED="$PACKAGES_TO_PREINSTALL"
922     PACKAGES_TO_VMINSTALL_FILTERED="$PACKAGES_TO_VMINSTALL"
923     rm -f "$BUILD_ROOT/.preinstall_image"/*
924     if test -n "$PREINSTALL_IMAGE" ; then
925         preinstall_image "$PREINSTALL_IMAGE" "$PREINSTALL_IMAGE_SOURCE"
926         PACKAGES_TO_PREINSTALL_FILTERED=`preinstall_image_filter $PACKAGES_TO_PREINSTALL_FILTERED`
927         PACKAGES_TO_VMINSTALL_FILTERED=`preinstall_image_filter $PACKAGES_TO_VMINSTALL_FILTERED`
928     fi
929     PACKAGES_TO_PREINSTALL_FILTERED=`reorder $PACKAGES_TO_PREINSTALL_FILTERED`
930     progress_setup PACKAGES_TO_PREINSTALL_FILTERED
931     for PKG in $PACKAGES_TO_PREINSTALL_FILTERED ; do
932         progress_step PACKAGES_TO_PREINSTALL_FILTERED
933         preinstall ${PKG##*/}
934     done
935     echo
936     if test -n "$PREPARE_VM" ; then
937         PACKAGES_TO_VMINSTALL_FILTERED=`reorder $PACKAGES_TO_VMINSTALL_FILTERED`
938         progress_setup PACKAGES_TO_VMINSTALL_FILTERED
939         for PKG in $PACKAGES_TO_VMINSTALL_FILTERED ; do
940             progress_step PACKAGES_TO_VMINSTALL_FILTERED
941             preinstall ${PKG##*/}
942         done
943     fi
944     # for reorder
945     check_exit
946     if test -w /root ; then
947         test -c "$BUILD_ROOT"/dev/null || create_devs
948     fi
949     test -e "$BUILD_ROOT"/etc/fstab || touch "$BUILD_ROOT"/etc/fstab
950     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
951     if test -z "$PREPARE_VM" ; then
952         mount_stuff
953         run_pkg_scripts
954         pkg_initdb
955         touch "$BUILD_ROOT"/.init_b_cache/preinstall_finished
956     fi
957     # mark as preinstalled no longer needed
958     rm -rf "$BUILD_ROOT/installed-pkg"
959     mkdir -p "$BUILD_ROOT/installed-pkg"
960 else
961     # preinstall was already done
962     if test -z "$PREPARE_VM" ; then
963         mount_stuff
964     fi
965 fi
966
967 if test -n "$PREPARE_VM" ; then
968     echo "copying packages..."
969     for PKG in $PACKAGES_TO_ALL ; do
970         rm -f "$BUILD_ROOT"/.init_b_cache/$PKG.$PSUF
971         cp "$BUILD_ROOT"/.init_b_cache/rpms/$PKG.$PSUF "$BUILD_ROOT"/.init_b_cache/$PKG.$PSUF || cleanup_and_exit 1
972         ln -s -f ../$PKG.$PSUF "$BUILD_ROOT"/.init_b_cache/rpms/$PKG.$PSUF
973         check_exit
974     done
975     # alreadyinstalled check will not work, but we have to live with that...
976     echo -n 'reordering...'
977     PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
978     check_exit
979     echo 'done'
980     Q="'\''"
981     echo "PACKAGES_TO_INSTALL='${PACKAGES_TO_INSTALL//"'"/$Q}'" > "$BUILD_ROOT"/.build/init_buildsystem.data
982     echo "PACKAGES_TO_RUNSCRIPTS='${PACKAGES_TO_RUNSCRIPTS//"'"/$Q}'" >> "$BUILD_ROOT"/.build/init_buildsystem.data
983     # needed for continuation in xen/kvm with rpm-x86
984     echo "PACKAGES_TO_PREINSTALL='${PACKAGES_TO_PREINSTALL//"'"/$Q}'" >> "$BUILD_ROOT"/.build/init_buildsystem.data
985     echo "PACKAGES_TO_AVAILABLE='${PACKAGES_TO_AVAILABLE//"'"/$Q}'" >> "$BUILD_ROOT"/.build/init_buildsystem.data
986     echo "PSUF='$PSUF'" >> "$BUILD_ROOT"/.build/init_buildsystem.data
987     rm -f $BUILD_IS_RUNNING
988     cleanup_and_exit 0
989 fi
990
991 #
992 # finish preinstall if this is a vm continuation
993 #
994 if test -e "$BUILD_ROOT/.build/init_buildsystem.data" -a ! -e "$BUILD_ROOT/.init_b_cache/preinstall_finished" ; then
995     run_pkg_scripts
996     pkg_initdb
997     touch "$BUILD_ROOT/.init_b_cache/preinstall_finished"
998 fi
999
1000
1001 #
1002 # create .build.binaries directory if requested
1003 #
1004 rm -rf "$BUILD_ROOT/.build.binaries"
1005 if test -n "$CREATE_BUILD_BINARIES" ; then
1006     echo "creating .build.binaries directory..."
1007     mkdir -p "$BUILD_ROOT/.build.binaries"
1008     for PKG in $PACKAGES_TO_AVAILABLE ; do
1009         test -L "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" || continue
1010         LPKG=`readlink -f "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF"`
1011         ln "$LPKG" "$BUILD_ROOT/.build.binaries/$PKG.$PSUF" 2>/dev/null
1012         test -f "$BUILD_ROOT/.build.binaries/$PKG.$PSUF" && continue
1013         cp "$LPKG" "$BUILD_ROOT/.build.binaries/$PKG.$PSUF"
1014         check_exit
1015     done
1016 fi
1017
1018 #
1019 # reorder packages (already done in vm continuation)
1020 #
1021 if ! test -e "$BUILD_ROOT"/.build/init_buildsystem.data ; then
1022     echo -n 'reordering...'
1023     PACKAGES_TO_INSTALL=`reorder $PACKAGES_TO_INSTALL`
1024     check_exit
1025     echo 'reordering done'
1026 fi
1027
1028 #
1029 # get list and ids of already installed packages
1030 #
1031 mkdir -p "$BUILD_ROOT"/.init_b_cache/alreadyinstalled
1032 listinstalled --root "$BUILD_ROOT" --type "$PSUF" --extraname | (
1033     while read id name buildid; do
1034         echo "$buildid" > "$BUILD_ROOT/.init_b_cache/alreadyinstalled/$name"
1035     done
1036 )
1037
1038 # do pre-installation work
1039 pkg_prepare
1040
1041 #
1042 # delete all packages we don't want
1043 #
1044 if [ -z "$KEEP_PACKS" ]; then
1045     mkdir -p "$BUILD_ROOT"/.init_b_cache/todelete
1046     for PKG in "$BUILD_ROOT"/.init_b_cache/alreadyinstalled/* ; do
1047         PKG=${PKG##*/}
1048         test "$PKG" = "*" && continue
1049         ln "$BUILD_ROOT"/.init_b_cache/alreadyinstalled/$PKG "$BUILD_ROOT"/.init_b_cache/todelete/$PKG
1050     done
1051     for PKG in $PACKAGES_TO_INSTALL; do
1052         rm -f "$BUILD_ROOT"/.init_b_cache/todelete/$PKG
1053     done
1054     for PKG in "$BUILD_ROOT"/.init_b_cache/todelete/* ; do
1055         PKG=${PKG##*/}
1056         test "$PKG" = "*" && continue
1057         echo "deleting $PKG"
1058                 pkg_erase
1059         check_exit
1060     done
1061     rm -rf "$BUILD_ROOT/.init_b_cache/todelete"
1062 fi
1063 rm -rf "$BUILD_ROOT/.init_b_cache/preinstalls"
1064 mkdir -p "$BUILD_ROOT/.init_b_cache/preinstalls"
1065 for PKG in $PACKAGES_TO_PREINSTALL; do
1066     touch "$BUILD_ROOT/.init_b_cache/preinstalls/$PKG"
1067 done
1068
1069 rm -rf "$BUILD_ROOT/installed-pkg"
1070 mkdir -p "$BUILD_ROOT/installed-pkg"
1071
1072 test -x "$BUILD_ROOT"/sbin/ldconfig && chroot "$BUILD_ROOT" /sbin/ldconfig 2>&1
1073
1074 MAIN_LIST="$PACKAGES_TO_INSTALL"
1075 progress_setup MAIN_LIST
1076 for PKG in $MAIN_LIST ; do
1077     test -f "$BUILD_ROOT"/installed-pkg/$PKG && continue
1078     progress_step MAIN_LIST
1079
1080     if test -e "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" -a ! -s "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" ; then
1081         # preinstallimage package, make sure it's in the image
1082         if ! test -e "$BUILD_ROOT"/.preinstall_image/$PKG ; then
1083             echo "Package $PKG is missing from the preinstall image"
1084             cleanup_and_exit 1
1085         fi
1086         read PKG_HDRMD5 PKGID < "$BUILD_ROOT"/.preinstall_image/$PKG
1087         echo "preinstalled ${PKGID%% *}"
1088         echo "$PKGID" > "$BUILD_ROOT"/installed-pkg/$PKG
1089         continue
1090     fi
1091
1092     # get the hdrmd5 if we want to create a preinstall image
1093     PKG_HDRMD5=
1094     if test -d "$BUILD_ROOT"/.preinstall_image -a ! -e "$BUILD_ROOT"/.preinstall_image/$PKG ; then
1095         PKG_HDRMD5=`perl -I$BUILD_DIR -MBuild -e 'print Build::queryhdrmd5($ARGV[0])' "$BUILD_ROOT"/.init_b_cache/rpms/$PKG.$PSUF`
1096         test -n "$PKG_HDRMD5" || cleanup_and_exit 1
1097     fi
1098
1099     test -L "$BUILD_ROOT"/.init_b_cache/rpms/$PKG.$PSUF || continue
1100
1101     # check if we can keep an already installed package
1102     if test "$VERIFY_BUILD_SYSTEM" != true -a -f "$BUILD_ROOT"/.init_b_cache/rpms/$PKG.id -a -f "$BUILD_ROOT"/.init_b_cache/alreadyinstalled/$PKG ; then
1103         read PKGID < "$BUILD_ROOT"/.init_b_cache/rpms/$PKG.id
1104         read OLDPKGID < "$BUILD_ROOT"/.init_b_cache/alreadyinstalled/$PKG
1105         if test "$PKGID" = "$OLDPKGID" ; then
1106             echo "keeping ${PKGID%% *}"
1107             rm -f "$BUILD_ROOT"/.init_b_cache/$PKG.$PSUF
1108             echo "$PKGID" > "$BUILD_ROOT"/installed-pkg/$PKG
1109             test -n "$PKG_HDRMD5" && echo "$PKG_HDRMD5 $PKGID" > "$BUILD_ROOT"/.preinstall_image/$PKG
1110             continue
1111         fi
1112     fi
1113
1114    # revert use rpm to query pkgid if building rpm package
1115    if test "$PSUF" != "rpm" ; then
1116        PKGID=$(perl -I$BUILD_DIR -MBuild -e Build::showquery "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" buildid)
1117    else
1118        ep=$(rpm -qp --qf "%{EPOCH}" "'$BUILD_ROOT'/.init_b_cache/rpms/$PKG.$PSUF")
1119        if test "$ep" != "(none)"; then
1120            RPMIDFMT_EP="%{NAME}-%{EPOCH}:%{VERSION}-%{RELEASE} %{BUILDTIME}-%{ARCH}\n"
1121            PKGID=`rpm -qp --qf "${RPMIDFMT_EP}" $RPMCHECKOPTS_HOST "'$BUILD_ROOT'/.init_b_cache/rpms/$PKG.$PSUF"`
1122        else
1123            PKGID=`rpm -qp --qf "${RPMIDFMT}" $RPMCHECKOPTS_HOST "'$BUILD_ROOT'/.init_b_cache/rpms/$PKG.$PSUF"`
1124        fi
1125    fi
1126
1127     if test -f "$BUILD_ROOT"/.init_b_cache/alreadyinstalled/$PKG ; then
1128         read OLDPKGID < "$BUILD_ROOT"/.init_b_cache/alreadyinstalled/$PKG
1129         if test "$PKGID" != "$OLDPKGID" ; then
1130             echo "deleting unwanted ${OLDPKGID%% *}"
1131             pkg_erase
1132         else
1133             if test "$VERIFY_BUILD_SYSTEM" != true || pkg_verify_installed ; then
1134                 echo "keeping ${PKGID%% *}"
1135                 echo "$PKGID" > "$BUILD_ROOT"/installed-pkg/$PKG
1136                 test -n "$PKG_HDRMD5" && echo "$PKG_HDRMD5 $PKGID" > "$BUILD_ROOT"/.preinstall_image/$PKG
1137                 continue
1138             fi
1139             echo "deleting incomplete ${OLDPKGID%% *}"
1140             pkg_erase
1141         fi
1142         if test -e "$BUILD_ROOT/.init_b_cache/preinstalls/$PKG" ; then
1143             preinstall "$PKG"
1144         fi
1145         check_exit
1146     fi
1147
1148     export ADDITIONAL_PARAMS=
1149     if test "$USE_FORCE" = true ; then
1150        export ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --force"
1151     fi
1152     # work around for cross-build installs, we must not overwrite the running rpm
1153     if test "$PKG" = rpm ; then
1154        for i in "$BUILD_ROOT"/.init_b_cache/preinstalls/rpm-x86-* ; do
1155            test -e "$i" && ADDITIONAL_PARAMS="$ADDITIONAL_PARAMS --justdb"
1156        done
1157     fi
1158
1159     if pkg_cumulate ; then
1160         echo "cumulate ${PKGID%% *}"
1161         continue
1162     fi
1163
1164     # install the package
1165     echo "installing ${PKGID%% *}"
1166     if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.$PSUF" -ef "$BUILD_ROOT/.init_b_cache/$PKG.$PSUF" ; then
1167         rm -f "$BUILD_ROOT"/.init_b_cache/$PKG.$PSUF
1168         cp "$BUILD_ROOT"/.init_b_cache/rpms/$PKG.$PSUF "$BUILD_ROOT"/.init_b_cache/$PKG.$PSUF || cleanup_and_exit 1
1169     fi
1170     pkg_install
1171     check_exit
1172     rm -f "$BUILD_ROOT"/.init_b_cache/$PKG.$PSUF
1173     echo "$PKGID" > "$BUILD_ROOT"/installed-pkg/$PKG
1174     test -n "$PKG_HDRMD5" && echo "$PKG_HDRMD5 $PKGID" > "$BUILD_ROOT"/.preinstall_image/$PKG
1175
1176 done
1177
1178 # do post-installation work
1179 pkg_finalize
1180
1181 # devices can vanish if devs got uninstalled
1182 test -c "$BUILD_ROOT"/dev/null || create_devs
1183
1184 cd "$BUILD_ROOT" || cleanup_and_exit 1
1185
1186 #
1187 # setup /etc/mtab
1188 #
1189 #init_buildsystem: make /etc/mtab a symlink
1190 #from patch:https://github.com/openSUSE/obs-build/commit/f0e7d37c811dbde49fcd5b9c8779063f4e87cb74
1191 ln -sf ../proc/self/mounts $BUILD_ROOT/etc/mtab
1192
1193 #
1194 # make sure, that our nis is not present in the chroot system
1195 #
1196 test -e "$BUILD_ROOT"/etc/nsswitch.conf && {
1197     echo "removing nis flags from $BUILD_ROOT/etc/nsswitch.conf..."
1198     cat "$BUILD_ROOT"/etc/nsswitch.conf | sed -e"s:nis::g" > "$BUILD_ROOT"/etc/nsswitch.conf.tmp
1199     mv "$BUILD_ROOT"/etc/nsswitch.conf.tmp "$BUILD_ROOT"/etc/nsswitch.conf
1200 }
1201
1202 #
1203 # create some default directories and files
1204 #
1205 for DIR in /usr/share/doc/packages \
1206            /usr/X11R6/include/X11/pixmaps \
1207            /usr/X11R6/include/X11/bitmaps ; do
1208     mkdir -p "$BUILD_ROOT"/$DIR
1209 done
1210 for FILE in /var/run/utmp /var/log/wtmp /etc/fstab ; do
1211     mkdir -p "$BUILD_ROOT"/${FILE%/*}
1212     touch "$BUILD_ROOT"/$FILE
1213 done
1214
1215 echo now finalizing build dir...
1216 if test -x "$BUILD_ROOT"/sbin/ldconfig ; then
1217         CHROOT_RETURN="`chroot "$BUILD_ROOT" /sbin/ldconfig 2>&1`"
1218         case "$CHROOT_RETURN" in
1219             *warning:*)
1220                 chroot "$BUILD_ROOT" /sbin/ldconfig
1221                 echo
1222                 echo chroot "$BUILD_ROOT" /sbin/ldconfig
1223                 echo
1224                 echo "$CHROOT_RETURN"
1225                 echo
1226                 echo "Problem with ldconfig.  It's better to reinit the build system..."
1227                 echo
1228                 cleanup_and_exit 1
1229                 ;;
1230         esac
1231 fi
1232
1233 if test -x "$BUILD_ROOT"/usr/sbin/Check && ! grep -q "/usr/sbin/Check is obsolete" "$BUILD_ROOT"/usr/sbin/Check ; then
1234     chroot "$BUILD_ROOT" /usr/sbin/Check
1235 fi
1236
1237 mkdir -p "$BUILD_ROOT"/var/adm/packages
1238 touch "$BUILD_ROOT"/var/adm/packages
1239
1240 if test -x "$BUILD_ROOT"/sbin/SuSEconfig ; then
1241     if grep norestarts "$BUILD_ROOT"/sbin/SuSEconfig > /dev/null ; then
1242         chroot "$BUILD_ROOT" /sbin/SuSEconfig --norestarts --force
1243     else
1244         chroot "$BUILD_ROOT" /sbin/SuSEconfig --force
1245     fi
1246 fi
1247
1248 if test -x "$BUILD_ROOT"/usr/X11R6/bin/switch2mesasoft ; then
1249     chroot "$BUILD_ROOT" /usr/X11R6/bin/switch2mesasoft
1250 fi
1251
1252 for PROG in /usr/bin/TeX/texhash /usr/bin/texhash ; do
1253     test -x "$BUILD_ROOT"/$PROG && \
1254         chroot "$BUILD_ROOT" bash -c ". /etc/profile ; $PROG"
1255 done
1256
1257 if test -e "$BUILD_ROOT"/usr/share/zoneinfo/UTC ; then
1258     for PROG in /usr/sbin/zic /usr/bin/zic /bin/zic /sbin/zic ; do
1259         test -x "$BUILD_ROOT"/$PROG  && chroot "$BUILD_ROOT" bash -c "$PROG -l \$(readlink -f /usr/share/zoneinfo/UTC)"
1260     done
1261 fi
1262
1263 test -e "$BUILD_ROOT"/.build/init_buildsystem.data || HOST=`hostname`
1264 test -e "$BUILD_ROOT"/etc/hosts || echo "127.0.0.1 localhost" > "$BUILD_ROOT"/etc/hosts
1265 if ! grep -F "127.0.0.1 $HOST" "$BUILD_ROOT"/etc/hosts > /dev/null ; then
1266     # this makes a reverse lookup on 127.0.0.1 return the host name,
1267     # which is bad, but 127.0.0.2 does not work on all unix systems
1268     echo "127.0.0.1 $HOST" > "$BUILD_ROOT"/etc/hosts.new
1269     test -f "$BUILD_ROOT"/etc/hosts && cat "$BUILD_ROOT"/etc/hosts >> "$BUILD_ROOT"/etc/hosts.new
1270     mv "$BUILD_ROOT"/etc/hosts.new "$BUILD_ROOT"/etc/hosts
1271 fi
1272
1273 # XXX: still needed?
1274 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
1275     pkg_initdb_rpm
1276     chroot "$BUILD_ROOT" rpm -q --whatprovides rpm >/dev/null 2>&1      # create provides index
1277 fi
1278 rm -f "$BUILD_ROOT"/.rpmmacros "$BUILD_ROOT"/root/.rpmmacros
1279 rm -rf "$BUILD_ROOT/.init_b_cache"
1280 rm -f $BUILD_IS_RUNNING
1281 rm -f $TMPFILE
1282
1283 cleanup_and_exit 0
1284
1285 # vim: set tabstop=8 softtabstop=4 shiftwidth=4 noexpandtab: