source: projects/vbootstrap/tags/0.0.55/vbuilder.sh.in @ 6010

Revision 6010, 30.3 KB checked in by munepi, 12 years ago (diff)

reduce the frequency of running apt-get-chroot

Line 
1#!/bin/bash
2# -*- coding: utf-8-unix -*-
3
4TEXTDOMAIN=vbootstrap
5TEXTDOMAINDIR=/usr/share/locale
6
7Usage(){
8    cat<<EOF
9$(basename $0) @@VBUILDER_VERSION@@ $(echo $([ -z "@@VBUILDER_REVISION@@" ] || echo "(r@@VBUILDER_REVISION@@)"))
10Usage:  $(basename $0) {--profile [profile]} {--version [version]} {--arch [arch]} {--category [categories]} {--fetch-url [fetch_url]} {--dist-upgrade} {--target [target]} {--no-build-essential} {--with-compat32} {--rpmbuild-define [macro_expr]} {--rpmbuild-with [bcond_with]} {--rpmbuild-without [bcond_with]} {--sign} {--no-install} {--debug} {--help} {--bootstrap-dir [directory]} {--unionfs-dir [directory]} {--cache-dir [directory]} {--built-rpms-dir [directory]} {clean|build|build-rpm [src.rpm]|install-rpm [arch.rpm|package]|remove-rpm [package]}
11EOF
12
13    echo $"
14Options:
15        --profile:              set a profile
16        --version:              set [version] (default: ${VERSION})
17        --arch:                 set [arch] (default: ${UARCH})
18        --category:             set [categories] (default: ${CATEGORIES})
19        --fetch-url:            set [fetch_url] to fetch packages (default: ${VBOOTSTRAP_FETCH_URL})
20        --dist-upgrade:         make VineSeed bootstrap via ${STABLE_VERSION}
21        --unionfs:              cover a bootstrap with unionfs
22        --target:               build rpms with [target]
23        --no-build-essential:   do not install build-essential
24        --with-compat32:        build rpms with compat32 on bootstrap
25        --rpmbuild-define:      give a option --define [macro_expr] to rpmbuild
26        --rpmbuild-with:        give a option --with [bcond_with] to rpmbuild
27        --rpmbuild-without:     give a option --without [bcond_with] to rpmbuild
28        --sign:                 sign built rpms
29        --no-install:           build only a source rpm - do NOT install a built rpm
30        --login:                login in chroot as root user
31        --bootstrap-dir:        set a bootstrap directory (default: ${VBOOTSTRAP_DIR})
32        --unionfs-dir:          set a directory to store unionfs images of vbootstrap (default: ${UNIONFS_DIR})
33        --cache-dir:            set a directory to cache rpms (default: ${CACHE_DIR})
34        --built-rpms-dir:       set a directory to store built rpms in chroot (default: ${BUILT_RPMS_DIR})
35        --debug:                enable debug mode
36        --help:                 show this help
37"
38
39    echo $"
40Actions:
41        clean:                  clean the bootstrap of [version]
42        build:                  build a bootstrap of [version]
43        build-rpm:              build [src.rpm] on a bootstrap
44        install-rpm:            install [arch.rpm|package] on a bootstrap
45        remove-rpm:             remove [package] on a bootstrap
46"
47
48    echo $"
49Examples:
50* make a clean/plain build environment on the current archtecture:
51$(basename $0) clean build
52* build rpms from the specified source rpm:
53$(basename $0) build-rpm [src.rpm]
54* make a plain build environment for Vine Linux 5.2:
55$(basename $0) --version 5.2 clean build
56* make a i386 chroot on x86_64:
57$(basename $0) --arch i386 clean build
58* build a kernel package with target i686:
59$(basename $0) --target i686 build-rpm [kernel src.rpm]
60* build a compat32 package:
61$(basename $0) --arch i386 --with-compat32 build-rpm [src.rpm]
62"
63    /usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*//
64## TODO: output supported profiles and their names/comments
65}
66
67##############################################################################
68
69initialize-variables(){
70    ## set boolian variables
71    with_profile=0
72    with_setup_vbootstrap=0
73    with_dist_upgrade=0
74    with_unionfs=0
75    with_sign=0
76    with_no_install=0
77    with_login=0
78    with_debug=0
79    with_actions=0
80    with_ix86_on_x86_64=0
81    with_category_main=0
82    with_category_plus=0
83    with_category_nonfree=0
84    with_category_test=0
85    with_category_proposed_updates=0
86    with_category_security=0
87    with_build_essential=1
88
89    return 0
90}
91
92check-parameter(){
93    [ -z "$*" ] && Usage && return 1
94
95    while [ ! -z "$*" ]; do
96        case $1 in
97            --help|help)
98                setup-vbuilder ||:
99                Usage
100                return 1
101                ;;
102            --profile)
103                shift
104                with_profile=1
105                PROFILE=$1
106                check-next-parameter $1 || return 1
107                ;;
108            --version|--arch|--category|--fetch-url|--target|--rpmbuild-define|--rpmbuild-with|--rpmbuild-without|--bootstrap-dir|--unionfs-dir|--cache-dir|--built-rpms-dir)
109                [ $with_actions -eq 1 ] && \
110                    echo $"E: You can give no more options after actions" && \
111                    return 1
112                shift
113                check-next-parameter $1 || return 1
114                ;;
115            --dist-upgrade|--unionfs|--with-compat32|--sign|--no-install|--login|--no-build-essential|--debug)
116                [ $with_actions -eq 1 ] && \
117                    echo $"E: You can give no more options after actions" && \
118                    return 1
119                ;;
120            --build-rpm|build-rpm|--install-rpm|install-rpm|--remove-rpm|remove-rpm)
121                with_actions=1
122                shift
123                check-next-parameter $1 || return 1
124                ;;
125            --build|build|--clean|clean)
126                with_actions=1
127                ;;
128            *)
129                echo $"E: Missing some parameters after $1"
130                return 1
131                ;;
132        esac
133        shift
134    done
135
136    [ $with_actions -eq 0 ] && \
137        echo $"E: You must give at least one action" && return 1
138
139    return 0
140}
141
142check-next-parameter(){
143    [ -z "$1" ] && echo $"E: Missing some parameters after $1" && return 1
144
145    [ $(echo $1 | grep '^-') ] && \
146        echo $"E: Missing some parameters after $1" && return 1
147
148    return 0
149}
150
151## NOTE: setup-vbuilder() loads
152##  - system wide configurations (from /etc/vbootstrap/vbuilder.conf)
153##  - given profile (from /etc/vbootstrap/profile.d/*.conf)
154##  - given command-line parameters
155## where given command-line parameters override the settings given profile,
156## and the settings given profile override system wide settings.
157## If you use a profile of vbuilder, you may not override the settings
158## given profile.
159setup-vbuilder(){
160    ## check $SUDO_USER and $USERHELPER_UID
161    RPM_SIGN_USER=$SUDO_USER
162    if [ -z "${RPM_SIGN_USER}" ]; then
163        [ -z "${USERHELPER_UID}" ] && \
164            echo $"W: \$SUDO_USER and \$USERHELPER_UID are empty" && \
165            return 1
166           
167        RPM_SIGN_USER=$(getent passwd $USERHELPER_UID | cut -d":" -f1)
168    fi
169    ## get $RPM_SIGN_USER's home directory
170    HOME=$(getent passwd $RPM_SIGN_USER |  cut -d":" -f6)
171
172    ## load default settings
173    VBUILDER_CONF=/etc/vbootstrap/vbuilder.conf
174    if [ -r $VBUILDER_CONF ]; then
175        . $VBUILDER_CONF
176        [ $? -eq 1 ] && return 1
177    fi
178    [ -z "${CATEGORIES}" ] && \
179        CATEGORIES=@@VBUILDER_CATEGORIES@@
180    [ -z "${VBOOTSTRAP_FETCH_URL}" ] && \
181        VBOOTSTRAP_FETCH_URL=@@VBUILDER_VBOOTSTRAP_FETCH_URL@@
182    [ -z "${VBOOTSTRAP_DIR}" ] && \
183        VBOOTSTRAP_DIR=@@VBUILDER_VBOOTSTRAP_DIR@@
184    [ -z "${UNIONFS_DIR}" ] && \
185        UNIONFS_DIR=@@VBUILDER_UNIONFS_DIR@@
186    [ -z "${CACHE_DIR}" ] && \
187        CACHE_DIR=@@VBUILDER_CACHE_DIR@@
188    [ -z "${BUILT_RPMS_DIR}" ] && \
189        BUILT_RPMS_DIR=@@VBUILDER_BUILT_RPMS_DIR@@
190
191    ## set default version for vbootstrap
192    VERSION=@@VBUILDER_DEFAULT_VERSION@@
193
194    ## load profile
195    if [ ${with_profile} -eq 1 ]; then
196        [ ! -r /etc/vbootstrap/profile.d/${PROFILE}.conf ] && \
197            echo $"E: No such profile found: ${PROFILE}" && return 1
198
199        . /etc/vbootstrap/profile.d/${PROFILE}.conf
200        [ $? -eq 1 ] && return 1
201    fi
202
203    ## set current stable relase version
204    STABLE_VERSION=@@VBUILDER_STABLE_VERSION@@
205
206    ## set default chroot archtecture
207    UARCH=$(uname -i)
208    case "${UARCH}" in
209        arm*)
210            UARCH="arm"
211            ;;
212    esac
213
214    return 0
215}
216
217setup-vbootstrap(){
218    if [ ${with_setup_vbootstrap} -eq 0 ]; then
219        with_setup_vbootstrap=1
220
221        ## check debug mode
222        [ ${with_debug} -eq 1 ] && \
223            cat $VBUILDER_CONF && \
224            set && set -x
225
226        ## check some directories
227        ## Note: create $BUILT_RPMS_DIR in RPM_Build()
228        [ -d $VBOOTSTRAP_DIR ] || mkdir -p $VBOOTSTRAP_DIR
229        [ -d $CACHE_DIR ] || mkdir -p $CACHE_DIR
230
231        ## check chroot version
232        case ${VERSION} in
233            4.2|5.2|6.0|VineSeed)
234                ;;
235            *)
236                echo $"E: ${VERSION} is NOT supported"
237                return 1
238                ;;
239        esac
240
241        case "${VARCH}" in
242            arm*)
243                VARCH="arm"
244                ;;
245        esac
246
247        ## check a chroot archtecture
248        if [ -z ${VARCH} ]; then
249            VARCH=${UARCH}
250        else
251            case "${VARCH}" in
252                i386|x86_64)
253                    [ "${UARCH}" = "ppc" -o "${UARCH}" = "arm" ] && \
254                        echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
255                    ;;
256                ppc)
257                    [ "${UARCH}" = "i386" -o "${UARCH}" = "x86_64" -o "${UARCH}" = "arm" ] && \
258                        echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
259                    ;;
260                arm)
261                    [ "${UARCH}" = "i386" -o "${UARCH}" = "x86_64" -o "${UARCH}" = "ppc" ] && \
262                        echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
263                    ;;
264            esac
265        fi
266
267        ##!! 4.2 is NO support on VARCH=x86_64 or VARCH=arch
268        if [ "${VERSION}" = "4.2" ]; then
269            if [ "${VARCH}" = "x86_64" -o "${VARCH}" = "arm" ]; then
270                echo $"E: ${VERSION}_${VARCH} is NOT supported"
271                return 1
272            fi
273        fi
274
275        ## set base profile <version>_<arch>
276        BASE_PROFILE=${VERSION}_${VARCH}
277
278        ## check support ${BASE_PROFILE}
279        if [ -z "$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*// | grep -m 1 ${BASE_PROFILE})" ]; then
280            echo $"E: ${BASE_PROFILE} is NOT supported"
281            return 1
282        fi
283
284        ## check ${VERSION} equals VineSeed*, when with_dist_upgrade=1
285        if [ $with_dist_upgrade -eq 1 ]; then
286            if [ "${VERSION}" != "VineSeed" ]; then
287                echo $"E: version ${VERSION} does not support --dist-upgrade option"
288                return 1
289            fi
290        fi
291
292        ## support i386 chroot on x86_64 below:
293        [ "${UARCH}" = "x86_64" -a "${VARCH}" = "i386" ] && \
294            with_ix86_on_x86_64=1
295
296        ## check apt categories
297        ## "main" category is unconditionally permited
298        with_category_main=1
299        for cat in $(echo ${CATEGORIES} | sed -e "s/,/ /"g); do
300            case $cat in
301                main)
302                    with_category_main=1
303                    ;;
304                plus)
305                    with_category_plus=1
306                    ;;
307                nonfree)
308                    with_category_nonfree=1
309                    ;;
310                test)
311                    ## "test" category only exists in VineSeed
312                    [ "${VERSION}" = "VineSeed" ] || \
313                        echo $"E: No such category exists: $cat" && return 1
314                    with_category_test=1
315                    ;;
316                proposed-updates)
317                    ##!! "proposed-updates" category does not exist in 4.2
318                    [ "${VERSION}" = "4.2" ] && \
319                        echo $"E: No such category exists: $cat" && return 1
320
321                    with_category_proposed_updates=1
322                    ;;
323                security)
324                    ## "security" category does not exist in VineSeed
325                    [ "${VERSION}" = "VineSeed" ] && \
326                        echo $"E: No such category exists: $cat" && return 1
327                    with_category_security=1
328                    ;;
329                *)
330                    echo $"E: No such category exists: $cat" && return 1
331                    ;;
332            esac
333        done
334
335        ## check build target option ${TARGET}
336        if [ ! -z "${TARGET}" ]; then
337            RPM_TARGET_LIST="$(cat /usr/lib/rpm/rpmrc | grep arch_canon: | sed -e "s/arch_canon:[[:blank:]]*\(.*\):.*/\1/")"
338            RPM_TARGET_LIST="${RPM_TARGET_LIST} noarch"
339            if [ -z "$(echo ${RPM_TARGET_LIST} | grep ${TARGET})" ]; then
340                echo $"E: rpm build target ${TARGET} is NOT supported"
341                return 1
342            fi
343        fi
344
345        ## set ${RPM_PKG_ARCH_LIST}
346        RPM_PKG_ARCH_LIST=" \
347            RPMS/i386 RPMS/i486 RPMS/i586 RPMS/i686 RPMS/x86_64 RPMS/ppc \
348            RPMS/noarch \
349            RPMS/armv3l RPMS/armv4l RPMS/armv4tl \
350            RPMS/armv5tejl RPMS/armv5tel RPMS/armv6l RPMS/armv7l \
351            SRPMS"
352        [ -z "${TARGET}" ] || \
353            RPM_PKG_ARCH_LIST="RPMS/${TARGET} ${RPM_PKG_ARCH_LIST}"
354
355    fi
356
357    ## set global variables
358    BUILD_USER=vbuilder
359    BUILD_DIR=/home/${BUILD_USER}/rpm
360    if [ ${with_profile} -eq 1 ]; then
361        BUILD_ROOT=${VBOOTSTRAP_DIR}/${PROFILE}
362        UNIONFS_ROOT=${UNIONFS_DIR}/${PROFILE}
363    else
364        BUILD_ROOT=${VBOOTSTRAP_DIR}/${BASE_PROFILE}
365        UNIONFS_ROOT=${UNIONFS_DIR}/${BASE_PROFILE}
366    fi
367    ARCHIVES_DIR=${BUILD_ROOT}/var/cache/apt/archives
368    EXTERNAL_ARCHIVES_DIR=${CACHE_DIR}/${BASE_PROFILE}/apt/archives
369
370    __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c -l"
371
372    mkdir -p $VBOOTSTRAP_DIR
373
374    return 0
375}
376
377setup-vbootstrap-rpm(){
378    setup-vbootstrap || return 1
379
380    ## check ${BUILD_ROOT}
381    [ -d ${BUILD_ROOT} ] || Build
382    ## setarch ix86 if ix86 chroot on x86_64 host
383    [ $with_ix86_on_x86_64 -eq 1 ] && \
384        __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH} /bin/sh -c -l"
385
386    DIST_RELEASE=$(cat ${BUILD_ROOT}/etc/vine-release | cut -f3 -d" " | cut -f1 -d.)
387
388    if [ -f $RPM_PKG ]; then
389        BASE_RPM_PKG=$(basename $RPM_PKG)
390        cp -f $RPM_PKG $BUILD_ROOT${BUILD_DIR}
391    else
392        BASE_RPM_PKG=$RPM_PKG   
393    fi
394
395    return 0
396}
397
398## recover apt-get data on host/chroot
399apt-get-update(){
400    case $1 in
401        --host)
402            echo -n $"apt-get update on host ... "
403            apt-get -qq update > /dev/null 2>&1
404            echo $"done."
405            ;;
406        --chroot)
407            echo -n $"apt-get update on chroot ... "
408            $__chroot_sh 'apt-get -qq update' > /dev/null 2>&1
409            echo $"done."
410            ;;
411        *)
412            echo apt-get-update: unknown option $1
413            exit 1
414            ;;
415    esac
416}
417
418## mount-chroot {|--umount} [file system|name]
419## support file systems: /home /tmp /sys /proc /dev/shm /dev/pts /dev
420## support names: vfs archives_dir
421## NOTE: /tmp needs for applications which use X
422##       vfs is virtual file systems
423##       archives_dir uses to mount ${EXTERNAL_ARCHIVES_DIR} to ${ARCHIVES_DIR}
424##       unionfs_dir covers ${BUILD_ROOT} with unionfs
425mount-chroot(){
426    if [ "$1" = "--umount" ]; then
427        mount-chroot-umount $2 || return 1
428    else
429        mount-chroot-mount $1 || return 1
430    fi
431    return 0
432}
433
434## mount-chroot-umount [file system|name]
435mount-chroot-umount(){
436    local fs=$1
437    case $fs in
438        /home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
439            [ -d ${BUILD_ROOT}${fs} ] || return 1
440            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] || \
441                umount ${BUILD_ROOT}${fs}
442            if [ ! -z "$(mount | grep ${BUILD_ROOT}${fs})" ]; then
443                echo $"Retry lazy unmount ${BUILD_ROOT}${fs} ... "
444                umount -l ${BUILD_ROOT}${fs}
445                echo $"done."
446            fi
447            ;;
448        vfs)
449            for dir in /sys /proc /dev/shm /dev/pts /dev; do
450                mount-chroot-umount ${dir} || return 1
451            done
452            ;;
453        archives_dir)
454            [ -d ${ARCHIVES_DIR} ] || return 1
455            [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
456                umount ${ARCHIVES_DIR}
457            ;;
458        unionfs_dir)
459            [ -d ${BUILD_ROOT} ] || return 1
460            [ -z "$(mount | grep ${BUILD_ROOT} | egrep '(unionfs|aufs)')" ] || \
461                umount ${BUILD_ROOT}
462            if [ ! -z "$(mount | grep ${BUILD_ROOT} | egrep '(unionfs|aufs)')" ]; then
463                echo $"Retry lazy unmount ${BUILD_ROOT} ... "
464                umount -l ${BUILD_ROOT}
465                echo $"done."
466            fi
467            ;;
468        *)
469            echo mount-chroot-umount: unknown file system $fs
470            exit 1
471            ;;
472    esac
473    return 0
474}
475
476## mount-chroot-mount [file system|name]
477mount-chroot-mount(){
478    local fs=$1
479
480    case $fs in
481        /home)
482            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
483            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
484                mount -o _netdev,rbind ${fs} ${BUILD_ROOT}${fs}
485            ;;
486        /tmp|/dev)
487            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
488            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
489                mount --bind -o _netdev ${fs} ${BUILD_ROOT}${fs}
490            ;;
491        /sys)
492            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
493            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
494                mount -o _netdev -t sysfs vbuildersysfs ${BUILD_ROOT}${fs}
495            ;;
496        /proc)
497            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
498            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
499                mount -o _netdev -t proc vbuilderproc ${BUILD_ROOT}${fs}
500            ;;
501        /dev/shm)
502            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
503            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
504                mount -o _netdev -t tmpfs vbuildertmpfs ${BUILD_ROOT}${fs}
505            ;;
506        /dev/pts)
507            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
508            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
509                mount -o gid=5,mode=620,_netdev -t devpts vbuilderdevpts ${BUILD_ROOT}${fs}
510            ;;
511        vfs)
512            for dir in /dev /dev/pts /dev/shm /proc /sys; do
513                mount-chroot-mount ${dir} || return 1
514            done
515            ;;
516        archives_dir)
517            [ -d ${EXTERNAL_ARCHIVES_DIR} ] || mkdir -p ${EXTERNAL_ARCHIVES_DIR}
518            [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
519            [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
520                mount --bind -o _netdev ${EXTERNAL_ARCHIVES_DIR} ${ARCHIVES_DIR}
521            [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
522            ;;
523        unionfs_dir)
524            if [ $with_unionfs -eq 1 ]; then
525                [ -d ${UNIONFS_ROOT} ] || mkdir -p ${UNIONFS_ROOT}
526                if ( /sbin/modprobe aufs >& /dev/null ) ; then
527                    [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
528                        mount -t aufs -o br=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro aufs ${BUILD_ROOT}
529                else
530                    [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
531                        mount -t unionfs -o dirs=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro unionfs ${BUILD_ROOT}
532                    unionctl ${BUILD_ROOT} --list
533                fi
534            fi
535            ;;
536        *)
537            echo mount-chroot-mount: unknown file system $fs
538            exit 1
539            ;;
540    esac
541    return 0
542}
543
544## TODO: support spec-validator
545## spec-validator [spec file]
546spec-validator(){
547    echo
548    return 0
549}
550
551
552##############################################################################
553
554Clean(){
555    setup-vbootstrap || return 1
556
557    # mount-chroot --umount /home
558    mount-chroot --umount /tmp
559    mount-chroot --umount /dev/shm
560    mount-chroot --umount /dev/pts
561    mount-chroot --umount /proc
562    mount-chroot --umount archives_dir
563    mount-chroot --umount unionfs_dir
564    apt-get-update --host
565
566    ## We remark that we first remove /, and secondly remove /.
567    ## If we directly remove /, we obtained some non-empty directories:
568    ##   /dev/.udev/rules.d.
569    if [ $with_unionfs -eq 1 ]; then
570        if [ -d ${UNIONFS_ROOT} ]; then
571            echo -n $"Cleaning build root ${UNIONFS_ROOT} via unionfs ... "
572            rm -rf ${UNIONFS_ROOT} 2>/dev/null
573            rm -rf ${UNIONFS_ROOT}
574            echo $"done."
575        fi
576    else
577        if [ -d ${BUILD_ROOT} ]; then
578            echo -n $"Cleaning build root ${BUILD_ROOT} ... "
579            rm -rf ${BUILD_ROOT} 2>/dev/null
580            rm -rf ${BUILD_ROOT}
581            echo $"done."
582        fi
583    fi
584
585    echo $"Cleanup a build farm for ${BUILD_ROOT} done."
586    return 0
587}
588
589Build(){
590    setup-vbootstrap || return 1
591
592    if [ $with_dist_upgrade -eq 1 ]; then
593        ## make bootstrap of ${STABLE_VERSION}
594        /usr/sbin/vbootstrap \
595            $(echo ${BASE_PROFILE} | sed -e "s/VineSeed/${STABLE_VERSION}/") \
596            ${VBOOTSTRAP_FETCH_URL} ${BUILD_ROOT} 2>&1 | \
597            egrep -v -e '^W: Duplicate sources.list entry' \
598                     -e '^W: .*apt-get update'
599
600        ## aim apt-line to VineSeed
601        sed -i "s/apt ${STABLE_VERSION}/apt VineSeed/g" \
602            ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
603    else
604        /usr/sbin/vbootstrap \
605            ${BASE_PROFILE} ${VBOOTSTRAP_FETCH_URL} ${BUILD_ROOT} 2>&1 | \
606            egrep -v -e '^W: Duplicate sources.list entry' \
607                     -e '^W: .*apt-get update'
608    fi
609
610    mount-chroot /proc
611    mount-chroot archives_dir
612    mount-chroot /dev/pts
613    mount-chroot /dev/shm
614    # mount-chroot /tmp
615    # mount-chroot /home
616
617    ##!! 4.2 has no apt-sourceslist-{plus,nonfree,proposed-updates} packages
618    case ${VERSION} in
619        4.2)
620            sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list
621            sed -i -e 's/main plus updates nonfree *$/$(echo ${CATEGORIES} | sed -e "s/,/ /"g) updates/g' ${BUILD_ROOT}/etc/apt/sources.list
622            $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
623            # [ $with_category_security -eq 1 ] && \
624            #   echo
625            ;;
626        5.2|@@VBUILDER_STABLE_VERSION@@)
627            sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
628            $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
629            [ $with_category_plus -eq 1 ] && \
630                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-plus' && \
631                sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/plus.list
632            [ $with_category_nonfree -eq 1 ] && \
633                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-nonfree' && \
634                sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/nonfree.list
635            [ $with_category_proposed_updates -eq 1 ] && \
636                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-proposed-updates' && \
637                sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/proposed-updates.list
638            # [ $with_category_security -eq 1 ] && \
639            #   echo
640            ;;
641        VineSeed)
642            sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
643            $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
644            [ $with_category_plus -eq 1 ] && \
645                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-plus' && \
646                sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/plus.list
647            [ $with_category_nonfree -eq 1 ] && \
648                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-nonfree' && \
649                sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/nonfree.list
650            [ $with_category_test -eq 1 ] && \
651                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-test' && \
652                sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/test.list
653            ;;
654    esac
655
656    [ $with_dist_upgrade -eq 1 ] && \
657        $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
658
659    [ $with_build_essential -eq 1 ] && \
660        $__chroot_sh 'apt-get -qq -y install build-essential'
661
662    $__chroot_sh 'apt-get -qq -y install etcskel shadow-utils'
663
664    $__chroot_sh 'cd /dev && /sbin/MAKEDEV console'
665    $__chroot_sh 'cd /dev && /sbin/MAKEDEV ptmx'
666    $__chroot_sh 'cd /dev && /sbin/MAKEDEV null'
667    $__chroot_sh 'cd /dev && /sbin/MAKEDEV zero'
668    $__chroot_sh 'cd /dev && /sbin/MAKEDEV random'
669    $__chroot_sh 'cd /dev && /sbin/MAKEDEV urandom'
670    $__chroot_sh 'cd /dev && mkdir -p shm && chmod 777 shm'
671    $__chroot_sh 'cd /dev && mkdir -p pts && chmod 755 pts'
672
673    $__chroot_sh '/usr/sbin/pwconv'
674    $__chroot_sh "/usr/sbin/useradd ${BUILD_USER}"
675
676    ##!! for rpm-4.8.0 or higher
677    ##!! (See http://trac.vinelinux.org/wiki/Vine6/AboutUpdateToolchain)
678    if [ "${VERSION}" = "VineSeed" ]; then
679        $__chroot_sh "sed -i -e 's/^%_topdir/#%_topdir/' /home/${BUILD_USER}/.rpmmacros"
680    fi
681
682    ## set local repositories (profile only)
683    if [ ${with_profile} -eq 1 ]; then
684        if [ ! -z "${LOCAL_REPOS}" ]; then
685            cat >${BUILD_ROOT}/etc/apt/sources.list.d/local.list<<EOF
686${LOCAL_REPOS}
687EOF
688        $__chroot_sh "apt-get update"
689        fi
690
691        ## additional base packages
692        [ ! -z "${ADD_BASE_PKGS}" ] && \
693            $__chroot_sh "apt-get -y install ${ADD_BASE_PKGS}" 
694    fi
695
696    # mount-chroot --umount /home
697    # mount-chroot --umount /tmp
698    mount-chroot --umount /dev/shm
699    mount-chroot --umount /dev/pts
700    mount-chroot --umount archives_dir
701    mount-chroot --umount /proc
702    apt-get-update --host
703
704    echo $"Making a build farm for ${BUILD_ROOT} done."
705    return 0
706}
707
708RPM_Remove(){
709    setup-vbootstrap-rpm || return 1
710    mount-chroot unionfs_dir
711    mount-chroot archives_dir
712    mount-chroot /proc
713    mount-chroot /dev/pts
714    mount-chroot /dev/shm
715    # apt-get-update --chroot
716
717    [ -f $RPM_PKG ] && \
718        echo $"E: $RPM_PKG is not a package name" && return 1
719    $__chroot_sh "apt-get -y remove $BASE_RPM_PKG"
720
721    mount-chroot --umount /dev/shm
722    mount-chroot --umount /dev/pts
723    mount-chroot --umount /proc
724    mount-chroot --umount archives_dir
725    mount-chroot --umount unionfs_dir
726    # apt-get-update --host
727
728    return 0
729}
730
731RPM_Install(){
732    setup-vbootstrap-rpm || return 1
733    mount-chroot unionfs_dir
734    mount-chroot archives_dir
735    mount-chroot /proc
736    mount-chroot /dev/pts
737    mount-chroot /dev/shm
738    apt-get-update --chroot
739
740    [ $with_category_nonfree -eq 1 ] && \
741        [ ! -z "$(echo $BASE_RPM_PKG | grep -e 'self-build-' -e 'install-assist-')" ] && \
742        $__chroot_sh 'rpm -q --quiet self-build-setup || apt-get -qq -y install self-build-setup'
743
744    $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $BASE_RPM_PKG"
745
746    mount-chroot --umount /dev/shm
747    mount-chroot --umount /dev/pts
748    mount-chroot --umount /proc
749    mount-chroot --umount archives_dir
750    mount-chroot --umount unionfs_dir
751    apt-get-update --host
752
753    return 0
754}
755
756RPM_Build(){
757    [ ! -f ${RPM_PKG} ] && \
758        echo $"E: No such file found: ${RPM_PKG}" && return 1
759
760    ## check the extension of given $RPM_PKG
761    local RPM_PKG_EXT=${RPM_PKG##*.}
762    case ${RPM_PKG_EXT} in
763        spec)
764            ## In this case,
765            ## the file ${RPM_PKG} is a spec file!
766            # spec-validator ${RPM_PKG} || return 1
767            # return 1
768
769            ## We assign the variable ${RPM_PKG}
770            ## the src.rpm generated by the spec file ${RPM_PKG}.
771            RPM_PKG=$(rpm -E "%{_srcrpmdir}")/$(rpm -q --qf "%{name}-%{version}-%{release}\n" --specfile ${RPM_PKG} | head -n 1).src.rpm
772            ;;
773        rpm)
774            RPM_PKG_EXT=$(echo ${RPM_PKG} | sed -e "s|.*\.\(src\.rpm\)$|\1|")
775            [ "${RPM_PKG_EXT}" != "src.rpm" ] && \
776                echo $"E: $RPM_PKG is NOT a source RPM package" && return 1
777            ;;
778        *)
779            echo $"The action build-rpm cannot work such extension: ${RPM_PKG_EXT}"
780            return 1
781            ;;
782    esac
783
784    setup-vbootstrap-rpm || return 1
785    mount-chroot unionfs_dir
786    mount-chroot archives_dir
787    mount-chroot /proc
788    mount-chroot /dev/pts
789    mount-chroot /dev/shm
790    apt-get-update --chroot
791   
792    [ ! -f ${RPM_PKG} ] && \
793        echo $"E: No such file found: ${RPM_PKG}" && return 1
794
795    RPM_PKG_USER=$(stat -c %U $RPM_PKG)
796    RPM_PKG_GROUP=$(stat -c %G $RPM_PKG)
797    [ ! -z "${SUDO_UID}" ] && RPM_PKG_USER=${SUDO_UID}
798    [ ! -z "${SUDO_GID}" ] && RPM_PKG_GROUP=${SUDO_GID}
799    local __install="install -p -v -o ${RPM_PKG_USER} -g ${RPM_PKG_GROUP}"
800
801    ## make src.rpm for $VERSION
802    $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpm -ivh $BASE_RPM_PKG'"
803    $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpmbuild -bs --nodeps --clean --rmsource --rmspec $RPM_OPTS ${BUILD_DIR}/SPECS/*.spec'"
804
805
806    ## change ${DIST_RELEASE}
807    BASE_RPM_PKG=$(echo $BASE_RPM_PKG | sed -e "s/vl[0-9]*\([A-Za-z]*[0-9A-Za-z]*\)\./vl${DIST_RELEASE}\1\./")
808
809    ## rebuild $BASE_RPM_PKG on ${DIST_RELEASE}
810    $__chroot_sh "cd ${BUILD_DIR}/SRPMS && apt-get -o APT::Install::Virtual=true -y build-dep $BASE_RPM_PKG"
811    $__chroot_sh "cd ${BUILD_DIR}/SRPMS && su ${BUILD_USER} -c 'rpmbuild --rebuild $RPM_OPTS $BASE_RPM_PKG'"
812
813    BUILT_RPMS_LIST=$(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')
814
815    [ $with_category_nonfree -eq 1 ] && \
816        [ ! -z "$(echo $BUILT_RPMS_LIST | grep -e 'self-build-' -e 'install-assist-')" ] && \
817        $__chroot_sh 'rpm -q --quiet self-build-setup || apt-get -qq -y install self-build-setup'
818
819    [ $with_no_install -eq 0 ] && \
820        $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $BUILT_RPMS_LIST"
821
822    ## copy built rpms to ${HOME}/rpm/ for each archtectures
823    echo $"Copying built rpms to ${BUILT_RPMS_DIR} for each archtectures ... "
824    for i in $RPM_PKG_ARCH_LIST; do
825        [ -d $BUILD_ROOT${BUILD_DIR}/${i} ] || continue
826        if [ ! -d ${BUILT_RPMS_DIR}/${VERSION}/${i} ]; then
827            $__install -d ${BUILT_RPMS_DIR}/${VERSION}/${i}/
828            chown -R ${RPM_PKG_USER}:${RPM_PKG_GROUP} ${BUILT_RPMS_DIR}
829        fi
830        find $BUILD_ROOT${BUILD_DIR}/${i} -type f -regex '.*\.rpm' \
831            -exec $__install -m0644 {} ${BUILT_RPMS_DIR}/${VERSION}/${i}/ \;
832    done
833
834    mount-chroot --umount /dev/shm
835    mount-chroot --umount /dev/pts
836    mount-chroot --umount /proc
837    mount-chroot --umount archives_dir
838    mount-chroot --umount unionfs_dir
839    apt-get-update --host
840
841    echo $"done."
842    return 0
843}
844
845RPM_Sign(){
846    [ $with_sign -eq 1 ] || return 1
847
848    setup-vbootstrap || return 1
849
850    [ -d ${BUILD_ROOT} ] || Build
851
852    mount-chroot unionfs_dir
853
854    echo $"Signing built rpms using ${RPM_SIGN_USER}'s key: "
855    su $RPM_SIGN_USER -c "rpmsign --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}/${VERSION},g -e 's/$/ \\/g')"
856
857    mount-chroot --umount unionfs_dir
858
859    return 0
860}
861
862Login_Chroot(){
863    [ $with_login -eq 1 ] || return 1
864
865    setup-vbootstrap || return 1
866
867    __chroot="/usr/sbin/chroot ${BUILD_ROOT}"
868    ## setarch ix86 if ix86 chroot on x86_64 host
869    [ $with_ix86_on_x86_64 -eq 1 ] && \
870        __chroot="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH}"
871
872
873    mount-chroot unionfs_dir
874    mount-chroot archives_dir
875    mount-chroot /proc
876    mount-chroot /dev/pts
877    mount-chroot /dev/shm
878    mount-chroot /tmp
879    # mount-chroot /home
880    # apt-get-update --chroot
881
882    ## copy host's configurations of /etc
883    #passwd shadow group gshadow
884    for i in resolv.conf hosts; do
885        cp -pf /etc/${i} ${BUILD_ROOT}/etc
886    done
887    cp -Ppf /etc/localtime ${BUILD_ROOT}/etc
888
889    $__chroot /bin/bash
890
891    # mount-chroot  --umount /home
892    mount-chroot  --umount /tmp
893    mount-chroot --umount /dev/shm
894    mount-chroot --umount /dev/pts
895    mount-chroot --umount /proc
896    mount-chroot --umount archives_dir
897    mount-chroot --umount unionfs_dir
898    # apt-get-update --host
899
900    return 0
901}
902
903
904##############################################################################
905
906initialize-variables || exit 1
907
908check-parameter $* || exit 1
909
910setup-vbuilder || exit 1
911
912while [ $# -gt 0 ]; do
913    tmpARG=$1
914    case $tmpARG in
915        --profile)
916            shift
917            ;;
918        --version|--arch|--category|--fetch-url|--target|--rpmbuild-define|--rpmbuild-with|--rpmbuild-without|--bootstrap-dir|--unionfs-dir|--cache-dir|--built-rpms-dir)
919            shift
920            ;;
921        --dist-upgrade|--unionfs|--with-compat32|--sign|--no-install|--login|--no-build-essential|--debug)
922            ;;
923        --build-rpm|build-rpm|--install-rpm|install-rpm|--remove-rpm|remove-rpm)
924            shift
925            ;;
926        --build|build|--clean|clean)
927            ;;
928        *)
929            echo unknown option $1
930            exit 1
931            ;;
932    esac
933
934    case $tmpARG in
935        --profile)
936            ;;
937        --version)
938            VERSION=$1
939            ;;
940        --arch)
941            VARCH=$1
942            ;;
943        --category)
944            CATEGORIES=$1
945            ;;
946        --fetch-url)
947            VBOOTSTRAP_FETCH_URL=$1
948            ;;
949        --dist-upgrade)
950            with_dist_upgrade=1
951            ;;
952        --unionfs)
953            with_unionfs=1
954            ;;
955        --target)
956            TARGET=$1
957            RPM_OPTS="${RPM_OPTS} --target $TARGET"
958            ;;
959        --with-compat32)
960            RPM_OPTS="${RPM_OPTS} --with compat32"
961            ;;
962        --rpmbuild-define)
963            RPM_OPTS="${RPM_OPTS} --define $1"
964            ;;
965        --rpmbuild-with)
966            RPM_OPTS="${RPM_OPTS} --with $1"
967            ;;
968        --rpmbuild-without)
969            RPM_OPTS="${RPM_OPTS} --without $1"
970            ;;
971        --sign)
972            with_sign=1
973            ;;
974        --no-install)
975            with_no_install=1
976            ;;
977        --no-build-essential)
978            with_build_essential=0
979            ;;
980        --login)
981            with_login=1
982            ;;
983        --bootstrap-dir)
984            VBOOTSTRAP_DIR=$1
985            ;;
986        --unionfs-dir)
987            UNIONFS_DIR=$1
988            ;;
989        --cache-dir)
990            CACHE_DIR=$1
991            ;;
992        --built-rpms-dir)
993            BUILT_RPMS_DIR=$1
994            ;;
995        --debug)
996            with_debug=1
997            ;;
998        --build-rpm|build-rpm)
999            RPM_PKG=$1
1000            RPM_Build || exit 1
1001            ;;
1002        --install-rpm|install-rpm)
1003            RPM_PKG=$1
1004            RPM_Install || exit 1
1005            ;;
1006        --remove-rpm|remove-rpm)
1007            RPM_PKG=$1
1008            RPM_Remove || exit 1
1009            ;;
1010        --build|build)
1011            Build || exit 1
1012            ;;
1013        --clean|clean)
1014            Clean || exit 1
1015            ;;
1016    esac
1017    shift
1018done
1019
1020RPM_Sign ||:
1021
1022Login_Chroot ||:
1023
1024exit
Note: See TracBrowser for help on using the repository browser.