source: projects/vbootstrap/tags/0.0.36/vbuilder.sh.in @ 2664

Revision 2664, 23.0 KB checked in by munepi, 13 years ago (diff)

tagging 0.0.36

Line 
1#!/bin/bash
2# -*- coding: utf-8-unix -*-
3
4TEXTDOMAIN=vbootstrap
5TEXTDOMAINDIR=/usr/share/locale
6
7Usage(){
8    set
9    cat<<EOF
10Usage:  $(basename $0) {--version [version]} {--arch [arch]} {--category [categories]} {--dist-upgrade} {--target [target]} {--with-compat32} {--rpmbuild-define [macro_expr]} {--rpmbuild-with [bcond_with]} {--rpmbuild-without [bcond_with]} {--sign} {--no-install} {--bootstrap-dir [directory]} {--cache-dir [directory]} {--built-rpms-dir [directory]} {clean|build|build-rpm [src.rpm]|install-rpm [arch.rpm|package]|remove-rpm [package]|show-info}
11
12Options:
13        --version:              set [version] (default: ${DEFAULT_VERSION})
14        --arch:                 set [arch] (default: $(uname -i))
15        --category:             set [categories] (default: ${CATEGORIES})
16        --dist-upgrade:         make VineSeed bootstrap via ${STABLE_VERSION}
17        --unionfs:              cover a bootstrap with unionfs
18        --target:               build rpms with [target]
19        --with-compat32:        build rpms with compat32 on boostrap
20        --rpmbuild-define:      give a option --define [macro_expr] to rpmbuild
21        --rpmbuild-with:        give a option --with [bcond_with] to rpmbuild
22        --rpmbuild-without:     give a option --without [bcond_with] to rpmbuild
23        --sign:                 sign built rpms
24        --no-install:           build only a source rpm - do NOT install a built rpm
25        --bootstrap-dir:        set a bootstrap directory (default: ${VBOOTSTRAP_DIR})
26        --cache-dir:            set a directory to cache rpms (default: ${CACHE_DIR})
27        --built-rpms-dir:       set a directory to store built rpms in chroot (default: ${BUILT_RPMS_DIR})
28
29Actions:
30        clean:                  clean the boostrap of [version]
31        build:                  build a boostrap of [version]
32        build-rpm:              build [src.rpm] on a boostrap
33        install-rpm:            install [arch.rpm|package] on a boostrap
34        remove-rpm:             remove [package] on a boostrap
35        show-info:              show basic informations and logs in chroot
36
37For example,
38* make a clean/plain build environment on the current archtecture:
39$(basename $0) clean build
40* build rpms from the specified source rpm:
41$(basename $0) build-rpm [src.rpm]
42* make a plain build environment for Vine Linux 4.2:
43$(basename $0) --version 4.2 clean build
44* make a i386 chroot on x86_64:
45$(basename $0) --arch i386 clean build
46* build a kernel package with target i686:
47$(basename $0) --target i686 build-rpm [kernel src.rpm]
48* build a compat32 package:
49$(basename $0) --arch i386 --with-compat32 build-rpm [src.rpm]
50$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*//)
51EOF
52}
53
54##############################################################################
55
56check-parameter(){
57    [ -z "$*" ] && Usage && return 1
58
59    while [ ! -z "$*" ]; do
60        case $1 in
61            --help|help)
62                Usage
63                return 1
64                ;;
65            --version|--arch|--category|--target|--rpmbuild-define|--rpmbuild-with|--rpmbuild-without|--bootstrap-dir|--cache-dir|--built-rpms-dir)
66                [ $with_actions -eq 1 ] && \
67                    echo $"E: You can give no more options after actions" && \
68                    return 1
69                shift
70                check-next-parameter $1 || return 1
71                ;;
72            --dist-upgrade|--unionfs|--with-compat32|--sign|--no-install)
73                [ $with_actions -eq 1 ] && \
74                    echo $"E: You can give no more options after actions" && \
75                    return 1
76                ;;
77            --build-rpm|build-rpm|--install-rpm|install-rpm|--remove-rpm|remove-rpm)
78                with_actions=1
79                shift
80                check-next-parameter $1 || return 1
81                ;;
82            --build|build|--clean|clean|--show-info|show-info)
83                with_actions=1
84                ;;
85            *)
86                echo $"E: Missing some parameters after $1"
87                return 1
88                ;;
89        esac
90        shift
91    done
92
93    [ $with_actions -eq 0 ] && \
94        echo $"E: You must give at least one action" && return 1
95
96    return 0
97}
98
99check-next-parameter(){
100    [ -z "$1" ] && echo $"E: Missing some parameters after $1" && return 1
101
102    [ $(echo $1 | grep '^-') ] && \
103        echo $"E: Missing some parameters after $1" && return 1
104
105    return 0
106}
107
108setup-vbuilder(){
109    ## load default settings
110    VBUILDER_CONF=/etc/vbootstrap/vbuilder.conf
111    if [ -r $VBUILDER_CONF ]; then
112        . $VBUILDER_CONF
113        [ $? -eq 1 ] && return 1
114    fi
115    [ -z "${DEFAULT_VERSION}" ] && \
116        DEFAULT_VERSION=@@VBUILDER_DEFAULT_VERSION@@
117    [ -z "${CATEGORIES}" ] && \
118        CATEGORIES=@@VBUILDER_CATEGORIES@@
119    [ -z "${VBOOTSTRAP_DIR}" ] && \
120        VBOOTSTRAP_DIR=@@VBUILDER_VBOOTSTRAP_DIR@@
121    [ -z "${CACHE_DIR}" ] && \
122        CACHE_DIR=@@VBUILDER_CACHE_DIR@@
123    [ -z "${BUILT_RPMS_DIR}" ] && \
124        BUILT_RPMS_DIR=@@VBUILDER_BUILT_RPMS_DIR@@
125
126    ## set default version for vbootstrap
127    VERSION=$DEFAULT_VERSION
128
129    ## set current stable relase version
130    STABLE_VERSION=@@VBUILDER_STABLE_VERSION@@
131
132    ## set boolian
133    with_setup_vbootstrap=0
134    with_dist_upgrade=0
135    with_unionfs=0
136    with_sign=0
137    with_no_install=0
138    with_actions=0
139    with_ix86_on_x86_64=0
140    with_category_main=0
141    with_category_plus=0
142    with_category_nonfree=0
143    with_category_test=0
144    with_category_proposed_updates=0
145    with_category_security=0
146
147    return 0
148}
149
150setup-vbootstrap(){
151    if [ ${with_setup_vbootstrap} -eq 0 ]; then
152        with_setup_vbootstrap=1
153
154        ## check some directories
155        ## Note: create $BUILT_RPMS_DIR in RPM_Build()
156        [ -d $VBOOTSTRAP_DIR ] || mkdir -p $VBOOTSTRAP_DIR
157        [ -d $CACHE_DIR ] || mkdir -p $CACHE_DIR
158
159        ## check a chroot archtecture
160        if [ -z ${VARCH} ]; then
161            VARCH=$(uname -i)
162        else
163            case "${VARCH}" in
164                i386|i686|x86_64)
165                    [ "$(uname -i)" = "ppc" ] && \
166                        echo $"E: arch ${VARCH} is NOT supported on $(uname -i)" && return 1
167                    ;;
168                ppc)
169                    [ "$(uname -i)" = "i386" -o "$(uname -i)" = "i686" -o "$(uname -i)" = "x86_64" ] && \
170                        echo $"E: arch ${VARCH} is NOT supported on $(uname -i)" && return 1
171                    ;;
172            esac
173        fi
174
175        ##!! 4.2 is NO support on VARCH=x86_64
176        if [ "${VERSION}" = "4.2" -a "${VARCH}" = "x86_64" ]; then
177            echo $"E: ${VERSION} is NOT supported"
178            return 1
179        fi
180
181        ## support i386 chroot on x86_64 below:
182        [ "${VARCH}" != "$(uname -i)" ] && \
183            VERSION=${VERSION}_${VARCH} && \
184            with_ix86_on_x86_64=1
185
186        ## check support ${VERSION}
187        if [ -z "$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*// | grep -m 1 ${VERSION})" ]; then
188            echo $"E: ${VERSION} is NOT supported"
189            return 1
190        fi
191
192        ## check ${VERSION} equals VineSeed*, when with_dist_upgrade=1
193        if [ $with_dist_upgrade -eq 1 ]; then
194            if [ "$(echo ${VERSION} | sed -e "s/\(VineSeed\).*/\1/")" != "VineSeed" ]; then
195                echo $"E: version ${VERSION} does not support --dist-upgrade option"
196                return 1
197            fi
198        fi
199
200        ## set ${MAJOR_VERSION}
201        MAJOR_VERSION=$(echo ${VERSION} | sed -e "s/_i[0-9]86//")
202
203        ## check apt categories
204        ## "main" category is unconditionally permited
205        with_category_main=1
206        for cat in $(echo ${CATEGORIES} | sed -e "s/,/ /"g); do
207            case $cat in
208                main)
209                    with_category_main=1
210                    ;;
211                plus)
212                    with_category_plus=1
213                    ;;
214                nonfree)
215                    with_category_nonfree=1
216                    ;;
217                test)
218                    ## "test" category only exists in VineSeed
219                    [ "${MAJOR_VERSION}" = "VineSeed" ] || \
220                        echo $"E: No such category exists: $cat" && return 1
221                    with_category_test=1
222                    ;;
223                proposed-updates)
224                    ##!! "proposed-updates" category does not exist in 4.2
225                    [ "${MAJOR_VERSION}" = "4.2" ] && \
226                        echo $"E: No such category exists: $cat" && return 1
227
228                    with_category_proposed_updates=1
229                    ;;
230                security)
231                    ## "security" category does not exist in VineSeed
232                    [ "${MAJOR_VERSION}" = "VineSeed" ] && \
233                        echo $"E: No such category exists: $cat" && return 1
234                    with_category_security=1
235                    ;;
236                *)
237                    echo $"E: No such category exists: $cat" && return 1
238                    ;;
239            esac
240        done
241
242        ## check build target option ${TARGET}
243        if [ ! -z "${TARGET}" ]; then
244            RPM_TARGET_LIST="$(cat /usr/lib/rpm/rpmrc | grep arch_canon: | sed -e "s/arch_canon:[[:blank:]]*\(.*\):.*/\1/")"
245            if [ -z "$(echo $RPM_TARGET_LIST | grep $TARGET)" ]; then
246                echo $"E: rpm build target ${TARGET} is NOT supported"
247                return 1
248            fi
249        fi
250
251        ## set ${RPM_PKG_ARCH_LIST}
252        RPM_PKG_ARCH_LIST="RPMS/i386 RPMS/i686 RPMS/x86_64 RPMS/ppc RPMS/noarch SRPMS"
253        [ -z "${TARGET}" ] || \
254            RPM_PKG_ARCH_LIST="RPMS/${TARGET} ${RPM_PKG_ARCH_LIST}"
255
256    fi
257
258    ## set global variables
259    BUILD_ROOT=${VBOOTSTRAP_DIR}/${VERSION}
260    BUILD_USER=vbuilder
261    BUILD_DIR=/home/${BUILD_USER}/rpm
262    UNIONFS_DIR=${VBOOTSTRAP_DIR}/unionfs/${VERSION}
263    ARCHIVES_DIR=${BUILD_ROOT}/var/cache/apt/archives
264    EXTERNAL_ARCHIVES_DIR=${CACHE_DIR}/${VERSION}/apt/archives
265    VBUILDER_LOG=${BUILD_ROOT}/var/log/vbuilder.log
266
267    __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c -l"
268
269    mkdir -p $VBOOTSTRAP_DIR
270
271    return 0
272}
273
274setup-vbootstrap-rpm(){
275    setup-vbootstrap || return 1
276
277    ## check ${BUILD_ROOT}
278    [ -d ${BUILD_ROOT} ] || Build
279    ## setarch ix86 if ix86 chroot on x86_64 host
280    [ $with_ix86_on_x86_64 -eq 1 ] && \
281        __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH} /bin/sh -c -l"
282
283    DIST_RELEASE=$(cat ${BUILD_ROOT}/etc/vine-release | cut -f3 -d" " | cut -f1 -d.)
284
285    if [ -f $RPM_PKG ]; then
286        BASE_RPM_PKG=$(basename $RPM_PKG)
287        cp -f $RPM_PKG $BUILD_ROOT${BUILD_DIR}
288    else
289        BASE_RPM_PKG=$RPM_PKG   
290    fi
291
292    return 0
293}
294
295## recover apt-get data on host/chroot
296apt-get-update(){
297    case $1 in
298        --host)
299            echo -n $"apt-get update on host ... "
300            apt-get -qq update > /dev/null 2>&1
301            echo $"done."
302            ;;
303        --chroot)
304            echo -n $"apt-get update on chroot ... "
305            $__chroot_sh 'apt-get -qq update' > /dev/null 2>&1
306            echo $"done."
307            ;;
308        *)
309            echo apt-get-update: unknown option $1
310            exit 1
311            ;;
312    esac
313}
314
315## mount-chroot {|--umount} [file system|name]
316## support file systems: /home /tmp /sys /proc /dev/shm /dev/pts /dev
317## support names: vfs archives_dir
318## NOTE: /tmp needs for applications which use X
319##       vfs is virtual file systems
320##       archives_dir uses to mount ${EXTERNAL_ARCHIVES_DIR} to ${ARCHIVES_DIR}
321##       unionfs_dir covers ${BUILD_ROOT} with unionfs
322mount-chroot(){
323    if [ "$1" = "--umount" ]; then
324        mount-chroot-umount $2 || return 1
325    else
326        mount-chroot-mount $1 || return 1
327    fi
328    return 0
329}
330
331## mount-chroot-umount [file system|name]
332mount-chroot-umount(){
333    local fs=$1
334    case $fs in
335        /home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
336            [ -d ${BUILD_ROOT}${fs} ] || return 1
337            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] || \
338                umount ${BUILD_ROOT}${fs}
339            if [ ! -z "$(mount | grep ${BUILD_ROOT}${fs})" ]; then
340                echo $"Retry lazy unmount ${BUILD_ROOT}${fs} ... "
341                umount -l ${BUILD_ROOT}${fs}
342                echo $"done."
343            fi
344            ;;
345        vfs)
346            # for dir in /sys /proc /dev/shm /dev/pts /dev; do
347            #   mount-chroot-umount ${dir} || return 1
348            # done
349            [ -d ${BUILD_ROOT}/proc ] || return 1
350            [ -z "$(mount | grep ${BUILD_ROOT}/proc)" ] || \
351                umount ${BUILD_ROOT}/proc
352            ;;
353        archives_dir)
354            [ -d ${ARCHIVES_DIR} ] || return 1
355            [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
356                umount ${ARCHIVES_DIR}
357            ;;
358        unionfs_dir)
359            [ -d ${BUILD_ROOT} ] || return 1
360            [ -z "$(mount | grep ${BUILD_ROOT} | grep unionfs)" ] || \
361                umount ${BUILD_ROOT}
362            if [ ! -z "$(mount | grep ${BUILD_ROOT} | grep unionfs)" ]; then
363                echo $"Retry lazy unmount ${BUILD_ROOT} ... "
364                umount -l ${BUILD_ROOT}
365                echo $"done."
366            fi
367            ;;
368        *)
369            echo mount-chroot-umount: unknown file system $fs
370            exit 1
371            ;;
372    esac
373    return 0
374}
375
376## mount-chroot-mount [file system|name]
377mount-chroot-mount(){
378    local fs=$1
379    local mnt_opts=""
380
381    case $fs in
382        /home)
383            mnt_opts="-o _netdev,rbind"
384            ;;
385        *)
386            mnt_opts="--bind -o _netdev"
387            ;;
388    esac
389
390    case $fs in
391        /home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
392            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
393            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
394                mount ${mnt_opts} ${fs} ${BUILD_ROOT}${fs}
395            ;;
396        vfs)
397            # for dir in /dev /dev/pts /dev/shm /proc /sys; do
398            #   mount-chroot-mount ${dir} || return 1
399            # done
400            mount-chroot-mount /proc || return 1
401            ;;
402        archives_dir)
403            [ -d ${EXTERNAL_ARCHIVES_DIR} ] || mkdir -p ${EXTERNAL_ARCHIVES_DIR}
404            [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
405            [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
406                mount ${mnt_opts} ${EXTERNAL_ARCHIVES_DIR} ${ARCHIVES_DIR}
407            [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
408            ;;
409        unionfs_dir)
410            if [ $with_unionfs -eq 1 ]; then
411                [ -d ${UNIONFS_DIR} ] || mkdir -p ${UNIONFS_DIR}
412                [ -z "$(mount | grep ${BUILD_ROOT})" ] && \
413                    mount -t unionfs -o dirs=${UNIONFS_DIR}=rw:${BUILD_ROOT}=ro unionfs ${BUILD_ROOT}
414                unionctl ${BUILD_ROOT} --list
415            fi
416            ;;
417        *)
418            echo mount-chroot-mount: unknown file system $fs
419            exit 1
420            ;;
421    esac
422    return 0
423}
424
425write-vbuilder-log(){
426    HRULE="======================================================================"
427
428    [ -d ${BUILD_ROOT} ] || return 1
429
430    if [ ! -f $VBUILDER_LOG ]; then
431        cat<<EOF > $VBUILDER_LOG
432${HRULE}
433VBUILDER REPORT
434DATE:           $(LANG=C date)
435HOSTNAME:       $(hostname)
436OS:             $(echo $($__chroot_sh "cat /etc/vine-release"))
437%_arch:         $(echo $($__chroot_sh "rpm --eval %_arch"))
438
439--version: ${VERSION}
440$(echo $([ -z "${VARCH}" ] || echo "--arch: ${VARCH}"))
441$(echo $([ -z "${CATEGORIES}" ] || echo "--category: ${CATEGORIES}"))
442$(echo $([ $with_dist_upgrade -eq 1 ] && echo "--dist-upgrade"))
443$(echo $([ $with_unionfs -eq 1 ] && echo "--unionfs"))
444$(echo $([ -z "${TARGET}" ] || echo "--target: ${TARGET}"))
445--bootstrap-dir: ${VBOOTSTRAP_DIR}
446--cache-dir: ${CACHE_DIR}
447--built-rpms-dir: ${BUILT_RPMS_DIR}
448${HRULE}
449
450[$VBUILDER_CONF]
451$(cat $VBUILDER_CONF)
452
453[History]
454EOF
455    else
456        cat<<EOF >> $VBUILDER_LOG
457$*
458EOF
459    fi
460
461    return 0
462}
463
464## user_from_uid [uid]
465user_from_uid () {
466    ## check whether or not $1 is a number
467    ! echo $1 | egrep -q '[^0-9]' || return 1
468   
469    # look for the corresponding name in /etc/passwd
470    local IFS=":"
471    while read name x uid the_rest; do
472        [ "$1" = "$uid" ] && echo "$name" && return 0
473    done < /etc/passwd
474
475    # if nothing was found, return false
476    return 1
477}
478
479
480##############################################################################
481
482Clean(){
483    setup-vbootstrap || return 1
484
485    # # output end-of-line in $VBUILDER_LOG
486    # [ -f $VBUILDER_LOG ] && write-vbuilder-log ${HRULE}
487    # Show-Info
488
489    # mount-chroot --umount /home
490    # mount-chroot --umount /tmp
491    mount-chroot --umount vfs
492    mount-chroot --umount archives_dir
493    mount-chroot --umount unionfs_dir
494    apt-get-update --host
495
496    if [ $with_unionfs -eq 1 ]; then
497        if [ -d ${UNIONFS_DIR} ]; then
498            echo -n $"Cleaning build root ${UNIONFS_DIR} via unionfs ... "
499            rm -rf ${UNIONFS_DIR}
500            echo $"done."
501        fi
502    else
503        if [ -d ${BUILD_ROOT} ]; then
504            echo -n $"Cleaning build root ${BUILD_ROOT} ... "
505            rm -rf ${BUILD_ROOT}
506            echo $"done."
507        fi
508    fi
509
510    echo $"Cleanup a build farm for ${VERSION} done."
511    return 0
512}
513
514Build(){
515    setup-vbootstrap || return 1
516
517    if [ $with_dist_upgrade -eq 1 ]; then
518        ## make bootstrap of ${STABLE_VERSION}
519        /usr/sbin/vbootstrap \
520            $(echo ${VERSION} | sed -e "s/VineSeed/${STABLE_VERSION}/") \
521            ${BUILD_ROOT}
522
523        ## aim apt-line to VineSeed
524        sed -i "s/apt ${STABLE_VERSION}/apt VineSeed/g" \
525            ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
526    else
527        /usr/sbin/vbootstrap ${VERSION} ${BUILD_ROOT}
528    fi
529
530    mount-chroot archives_dir
531    mount-chroot vfs
532    # mount-chroot /tmp
533    # mount-chroot /home
534
535    $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
536
537    ##!! 4.2 has no apt-sourceslist-{plus,nonfree,proposed-updates} packages
538    case ${MAJOR_VERSION} in
539        4.2)
540            $__chroot_sh "sed -i -e 's/main plus updates nonfree *$/$(echo ${CATEGORIES} | sed -e "s/,/ /"g) updates/g' /etc/apt/sources.list"
541            # [ $with_category_security -eq 1 ] && \
542            #   echo
543            ;;
544        @@VBUILDER_STABLE_VERSION@@)
545            [ $with_category_plus -eq 1 ] && \
546                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-plus'
547            [ $with_category_nonfree -eq 1 ] && \
548                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-nonfree'
549            [ $with_category_proposed_updates -eq 1 ] && \
550                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-proposed-updates'
551            # [ $with_category_security -eq 1 ] && \
552            #   echo
553            ;;
554        VineSeed)
555            [ $with_category_plus -eq 1 ] && \
556                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-plus'
557            [ $with_category_nonfree -eq 1 ] && \
558                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-nonfree'
559            [ $with_category_test -eq 1 ] && \
560                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-test'
561            ;;
562    esac
563
564    [ $with_dist_upgrade -eq 1 ] && \
565        $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
566
567    $__chroot_sh 'apt-get -qq -y install build-essential'
568
569    [ $with_category_nonfree -eq 1 ] && \
570        $__chroot_sh 'apt-get -qq -y install self-build-setup'
571
572    $__chroot_sh 'apt-get -qq -y install etcskel shadow-utils'
573
574    $__chroot_sh 'cd /dev && /sbin/MAKEDEV console'
575    $__chroot_sh 'cd /dev && /sbin/MAKEDEV null'
576    $__chroot_sh 'cd /dev && /sbin/MAKEDEV zero'
577    $__chroot_sh 'cd /dev && /sbin/MAKEDEV random'
578    $__chroot_sh 'cd /dev && /sbin/MAKEDEV urandom'
579
580    $__chroot_sh '/usr/sbin/pwconv'
581    $__chroot_sh "/usr/sbin/useradd ${BUILD_USER}"
582
583    ##!! for rpm-4.8.0 or higher
584    ##!! (See http://trac.vinelinux.org/wiki/Vine6/AboutUpdateToolchain)
585    if [ "$(echo ${VERSION} | sed -e "s/\(VineSeed\).*/\1/")" = "VineSeed" ]; then
586        $__chroot_sh "sed -i -e 's/^%_topdir/#%_topdir/' /home/${BUILD_USER}/.rpmmacros"
587    fi
588
589    ## output basic informations in chroot
590    write-vbuilder-log
591    write-vbuilder-log "build"
592
593    # mount-chroot --umount /home
594    # mount-chroot --umount /tmp
595    mount-chroot --umount vfs
596    mount-chroot --umount archives_dir
597    apt-get-update --host
598
599    echo $"Making a build farm for ${VERSION} done."
600    return 0
601}
602
603Show-Info(){
604    setup-vbootstrap || return 1
605
606    [ -f $VBUILDER_LOG ] && cat $VBUILDER_LOG
607
608    return 0
609}
610
611RPM_Remove(){
612    setup-vbootstrap-rpm || return 1
613    mount-chroot unionfs_dir
614    mount-chroot archives_dir
615    mount-chroot vfs
616    apt-get-update --chroot
617
618    [ -f $RPM_PKG ] && \
619        echo $"E: $RPM_PKG is not a package name" && return 1
620    $__chroot_sh "apt-get -y remove $BASE_RPM_PKG"
621
622    write-vbuilder-log "remove-rpm $RPM_PKG"
623
624    mount-chroot --umount vfs
625    mount-chroot --umount archives_dir
626    mount-chroot --umount unionfs_dir
627    apt-get-update --host
628
629    return 0
630}
631
632RPM_Install(){
633    setup-vbootstrap-rpm || return 1
634    mount-chroot unionfs_dir
635    mount-chroot archives_dir
636    mount-chroot vfs
637    apt-get-update --chroot
638
639    $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $BASE_RPM_PKG"
640
641    write-vbuilder-log "install-rpm $RPM_PKG"
642
643    mount-chroot --umount vfs
644    mount-chroot --umount archives_dir
645    mount-chroot --umount unionfs_dir
646    apt-get-update --host
647
648    return 0
649}
650
651RPM_Build(){
652    setup-vbootstrap-rpm || return 1
653    mount-chroot unionfs_dir
654    mount-chroot archives_dir
655    mount-chroot vfs
656    apt-get-update --chroot
657
658    [ ! -f $RPM_PKG ] && \
659        echo $"E: $RPM_PKG is not a source RPM package" && return 1
660   
661    RPM_PKG_USER=$(stat -c %U $RPM_PKG)
662    RPM_PKG_GROUP=$(stat -c %G $RPM_PKG)
663    [ ! -z "${SUDO_UID}" ] && RPM_PKG_USER=${SUDO_UID}
664    [ ! -z "${SUDO_GID}" ] && RPM_PKG_GROUP=${SUDO_GID}
665    local __install="install -p -v -o ${RPM_PKG_USER} -g ${RPM_PKG_GROUP}"
666
667    ## make src.rpm for $VERSION
668    $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpm -ivh $BASE_RPM_PKG'"
669    $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpmbuild -bs --nodeps --clean --rmsource --rmspec $RPM_OPTS ${BUILD_DIR}/SPECS/*.spec'"
670
671
672    ## change ${DIST_RELEASE}
673    BASE_RPM_PKG=$(echo $BASE_RPM_PKG | sed -e "s/vl[0-9]*\([A-Za-z]*\)\./vl${DIST_RELEASE}\1\./")
674
675    ## rebuild $BASE_RPM_PKG on ${DIST_RELEASE}
676    $__chroot_sh "cd ${BUILD_DIR}/SRPMS && apt-get -o APT::Install::Virtual=true -y build-dep $BASE_RPM_PKG"
677    $__chroot_sh "cd ${BUILD_DIR}/SRPMS && su ${BUILD_USER} -c 'rpmbuild --rebuild $RPM_OPTS $BASE_RPM_PKG'"
678    [ $with_no_install -eq 0 ] && \
679        $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $(find $BUILD_ROOT${BUILD_DIR}/RPMS -type f -regex '.*\.rpm' | sed -e s@${BUILD_ROOT}@@g -e 's|.*\/compat32-.*||g' -e 's|.*\/.*\.src\.rpm||g' -e 's/$/ \\/g')"
680
681    ## copy built rpms to ${HOME}/rpm/ for each archtectures
682    echo $"Copying built rpms to ${BUILT_RPMS_DIR} for each archtectures ... "
683    for i in $RPM_PKG_ARCH_LIST; do \
684        if [ -d $BUILD_ROOT${BUILD_DIR}/${i} ]; then
685            if [ ! -d ${BUILT_RPMS_DIR}/${MAJOR_VERSION}/${i} ]; then
686                $__install -d ${BUILT_RPMS_DIR}/${MAJOR_VERSION}/${i}/
687                chown -R ${RPM_PKG_USER}:${RPM_PKG_GROUP} ${BUILT_RPMS_DIR}
688            fi
689            find $BUILD_ROOT${BUILD_DIR}/${i} -type f -regex '.*\.rpm' \
690                -exec $__install -m0644 {} ${BUILT_RPMS_DIR}/${MAJOR_VERSION}/${i}/ \;
691        fi
692    done
693
694    write-vbuilder-log "build-rpm $RPM_PKG"
695
696    mount-chroot --umount vfs
697    mount-chroot --umount archives_dir
698    mount-chroot --umount unionfs_dir
699    apt-get-update --host
700
701    echo $"done."
702    return 0
703}
704
705RPM_Sign(){
706    [ $with_sign -eq 1 ] || return 1
707
708    local RPM_SIGN_USER=$SUDO_USER
709    if [ -z "${RPM_SIGN_USER}" ]; then
710        RPM_SIGN_USER=$(user_from_uid $USERHELPER_UID)
711        if [ -z "${RPM_SIGN_USER}" ]; then
712            echo $"W: \$SUDO_USER and \$USERHELPER_UID are empty"
713            return 1
714        fi
715    fi
716
717    echo $"Signing built rpms using ${RPM_SIGN_USER}'s key: "
718    su $RPM_SIGN_USER -c "rpm --addsign $(for i in $RPM_PKG_ARCH_LIST; do find $BUILD_ROOT${BUILD_DIR}/${i} -type f -regex '.*\.rpm' 2>/dev/null; done | sed -e s,$BUILD_ROOT${BUILD_DIR},${BUILT_RPMS_DIR}/${MAJOR_VERSION},g -e 's/$/ \\/g')"
719
720    return 0
721}
722
723
724##############################################################################
725
726setup-vbuilder || exit 1
727
728check-parameter $* || exit 1
729
730while [ $# -gt 0 ]; do
731    tmpARG=$1
732    case $tmpARG in
733        --version|--arch|--category|--target|--rpmbuild-define|--rpmbuild-with|--rpmbuild-without|--bootstrap-dir|--cache-dir|--built-rpms-dir)
734            shift
735            ;;
736        --dist-upgrade|--unionfs|--with-compat32|--sign|--no-install)
737            ;;
738        --build-rpm|build-rpm|--install-rpm|install-rpm|--remove-rpm|remove-rpm)
739            shift
740            ;;
741        --build|build|--clean|clean|--show-info|show-info)
742            ;;
743        *)
744            echo unknown option $1
745            exit 1
746            ;;
747    esac
748
749    case $tmpARG in
750        --version)
751            VERSION=$1
752            ;;
753        --arch)
754            VARCH=$1
755            ;;
756        --category)
757            CATEGORIES=$1
758            ;;
759        --dist-upgrade)
760            with_dist_upgrade=1
761            ;;
762        --unionfs)
763            with_unionfs=1
764            ;;
765        --target)
766            TARGET=$1
767            RPM_OPTS="${RPM_OPTS} --target $TARGET"
768            ;;
769        --with-compat32)
770            RPM_OPTS="${RPM_OPTS} --with compat32"
771            ;;
772        --rpmbuild-define)
773            RPM_OPTS="${RPM_OPTS} --define $1"
774            ;;
775        --rpmbuild-with)
776            RPM_OPTS="${RPM_OPTS} --with $1"
777            ;;
778        --rpmbuild-without)
779            RPM_OPTS="${RPM_OPTS} --without $1"
780            ;;
781        --sign)
782            with_sign=1
783            ;;
784        --no-install)
785            with_no_install=1
786            ;;
787        --bootstrap-dir)
788            VBOOTSTRAP_DIR=$1
789            ;;
790        --cache-dir)
791            CACHE_DIR=$1
792            ;;
793        --built-rpms-dir)
794            BUILT_RPMS_DIR=$1
795            ;;
796        --build-rpm|build-rpm)
797            RPM_PKG=$1
798            RPM_Build || exit 1
799            ;;
800        --install-rpm|install-rpm)
801            RPM_PKG=$1
802            RPM_Install || exit 1
803            ;;
804        --remove-rpm|remove-rpm)
805            RPM_PKG=$1
806            RPM_Remove || exit 1
807            ;;
808        --show-info|show-info)
809            Show-Info || exit 1
810            ;;
811        --build|build)
812            Build || exit 1
813            ;;
814        --clean|clean)
815            Clean || exit 1
816            ;;
817    esac
818    shift
819done
820
821RPM_Sign
822
823exit
Note: See TracBrowser for help on using the repository browser.