source: projects/vbootstrap/tags/0.0.58/vbuilder.sh.in @ 6703

Revision 6703, 30.4 KB checked in by munepi, 12 years ago (diff)

updated po files

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 6:
55$(basename $0) --version 6 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|VineSeed)
234                ;;
235            6.?)
236                VERSION="6"
237                ;;
238            *)
239                echo $"E: ${VERSION} is NOT supported"
240                return 1
241                ;;
242        esac
243
244        case "${VARCH}" in
245            arm*)
246                VARCH="arm"
247                ;;
248        esac
249
250        ## check a chroot archtecture
251        if [ -z ${VARCH} ]; then
252            VARCH=${UARCH}
253        else
254            case "${VARCH}" in
255                i386|x86_64)
256                    [ "${UARCH}" = "ppc" -o "${UARCH}" = "arm" ] && \
257                        echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
258                    ;;
259                ppc)
260                    [ "${UARCH}" = "i386" -o "${UARCH}" = "x86_64" -o "${UARCH}" = "arm" ] && \
261                        echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
262                    ;;
263                arm)
264                    [ "${UARCH}" = "i386" -o "${UARCH}" = "x86_64" -o "${UARCH}" = "ppc" ] && \
265                        echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
266                    ;;
267            esac
268        fi
269
270        ##!! 4.2 is NO support on VARCH=x86_64 or VARCH=arch
271        if [ "${VERSION}" = "4.2" ]; then
272            if [ "${VARCH}" = "x86_64" -o "${VARCH}" = "arm" ]; then
273                echo $"E: ${VERSION}_${VARCH} is NOT supported"
274                return 1
275            fi
276        fi
277
278        ## set base profile <version>_<arch>
279        BASE_PROFILE=${VERSION}_${VARCH}
280
281        ## check support ${BASE_PROFILE}
282        if [ -z "$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*// | grep -m 1 ${BASE_PROFILE})" ]; then
283            echo $"E: ${BASE_PROFILE} is NOT supported"
284            return 1
285        fi
286
287        ## check ${VERSION} equals VineSeed*, when with_dist_upgrade=1
288        if [ $with_dist_upgrade -eq 1 ]; then
289            if [ "${VERSION}" != "VineSeed" ]; then
290                echo $"E: version ${VERSION} does not support --dist-upgrade option"
291                return 1
292            fi
293        fi
294
295        ## support i386 chroot on x86_64 below:
296        [ "${UARCH}" = "x86_64" -a "${VARCH}" = "i386" ] && \
297            with_ix86_on_x86_64=1
298
299        ## check apt categories
300        ## "main" category is unconditionally permited
301        with_category_main=1
302        for cat in $(echo ${CATEGORIES} | sed -e "s/,/ /"g); do
303            case $cat in
304                main)
305                    with_category_main=1
306                    ;;
307                plus)
308                    with_category_plus=1
309                    ;;
310                nonfree)
311                    with_category_nonfree=1
312                    ;;
313                test)
314                    ## "test" category only exists in VineSeed
315                    [ "${VERSION}" = "VineSeed" ] || \
316                        echo $"E: No such category exists: $cat" && return 1
317                    with_category_test=1
318                    ;;
319                proposed-updates)
320                    ##!! "proposed-updates" category does not exist in 4.2
321                    [ "${VERSION}" = "4.2" ] && \
322                        echo $"E: No such category exists: $cat" && return 1
323
324                    with_category_proposed_updates=1
325                    ;;
326                security)
327                    ## "security" category does not exist in VineSeed
328                    [ "${VERSION}" = "VineSeed" ] && \
329                        echo $"E: No such category exists: $cat" && return 1
330                    with_category_security=1
331                    ;;
332                *)
333                    echo $"E: No such category exists: $cat" && return 1
334                    ;;
335            esac
336        done
337
338        ## check build target option ${TARGET}
339        if [ ! -z "${TARGET}" ]; then
340            RPM_TARGET_LIST="$(cat /usr/lib/rpm/rpmrc | grep arch_canon: | sed -e "s/arch_canon:[[:blank:]]*\(.*\):.*/\1/")"
341            RPM_TARGET_LIST="${RPM_TARGET_LIST} noarch"
342            if [ -z "$(echo ${RPM_TARGET_LIST} | grep ${TARGET})" ]; then
343                echo $"E: rpm build target ${TARGET} is NOT supported"
344                return 1
345            fi
346        fi
347
348        ## set ${RPM_PKG_ARCH_LIST}
349        RPM_PKG_ARCH_LIST=" \
350            RPMS/i386 RPMS/i486 RPMS/i586 RPMS/i686 RPMS/x86_64 RPMS/ppc \
351            RPMS/noarch \
352            RPMS/armv3l RPMS/armv4l RPMS/armv4tl \
353            RPMS/armv5tejl RPMS/armv5tel RPMS/armv6l RPMS/armv7l \
354            SRPMS"
355        [ -z "${TARGET}" ] || \
356            RPM_PKG_ARCH_LIST="RPMS/${TARGET} ${RPM_PKG_ARCH_LIST}"
357
358    fi
359
360    ## set global variables
361    BUILD_USER=vbuilder
362    BUILD_DIR=/home/${BUILD_USER}/rpm
363    if [ ${with_profile} -eq 1 ]; then
364        BUILD_ROOT=${VBOOTSTRAP_DIR}/${PROFILE}
365        UNIONFS_ROOT=${UNIONFS_DIR}/${PROFILE}
366    else
367        BUILD_ROOT=${VBOOTSTRAP_DIR}/${BASE_PROFILE}
368        UNIONFS_ROOT=${UNIONFS_DIR}/${BASE_PROFILE}
369    fi
370    ARCHIVES_DIR=${BUILD_ROOT}/var/cache/apt/archives
371    EXTERNAL_ARCHIVES_DIR=${CACHE_DIR}/${BASE_PROFILE}/apt/archives
372
373    __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c -l"
374
375    mkdir -p $VBOOTSTRAP_DIR
376
377    return 0
378}
379
380setup-vbootstrap-rpm(){
381    setup-vbootstrap || return 1
382
383    ## check ${BUILD_ROOT}
384    [ -d ${BUILD_ROOT} ] || Build
385    ## setarch ix86 if ix86 chroot on x86_64 host
386    [ $with_ix86_on_x86_64 -eq 1 ] && \
387        __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH} /bin/sh -c -l"
388
389    DIST_RELEASE=$(cat ${BUILD_ROOT}/etc/vine-release | cut -f3 -d" " | cut -f1 -d.)
390
391    if [ -f $RPM_PKG ]; then
392        BASE_RPM_PKG=$(basename $RPM_PKG)
393        cp -f $RPM_PKG $BUILD_ROOT${BUILD_DIR}
394    else
395        BASE_RPM_PKG=$RPM_PKG   
396    fi
397
398    return 0
399}
400
401## recover apt-get data on host/chroot
402apt-get-update(){
403    case $1 in
404        --host)
405            echo -n $"apt-get update on host ... "
406            apt-get -qq update > /dev/null 2>&1
407            echo $"done."
408            ;;
409        --chroot)
410            echo -n $"apt-get update on chroot ... "
411            $__chroot_sh 'apt-get -qq update' > /dev/null 2>&1
412            echo $"done."
413            ;;
414        *)
415            echo apt-get-update: unknown option $1
416            exit 1
417            ;;
418    esac
419}
420
421## mount-chroot {|--umount} [file system|name]
422## support file systems: /home /tmp /sys /proc /dev/shm /dev/pts /dev
423## support names: vfs archives_dir
424## NOTE: /tmp needs for applications which use X
425##       vfs is virtual file systems
426##       archives_dir uses to mount ${EXTERNAL_ARCHIVES_DIR} to ${ARCHIVES_DIR}
427##       unionfs_dir covers ${BUILD_ROOT} with unionfs
428mount-chroot(){
429    if [ "$1" = "--umount" ]; then
430        mount-chroot-umount $2 || return 1
431    else
432        mount-chroot-mount $1 || return 1
433    fi
434    return 0
435}
436
437## mount-chroot-umount [file system|name]
438mount-chroot-umount(){
439    local fs=$1
440    case $fs in
441        /home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
442            [ -d ${BUILD_ROOT}${fs} ] || return 1
443            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] || \
444                umount ${BUILD_ROOT}${fs}
445            if [ ! -z "$(mount | grep ${BUILD_ROOT}${fs})" ]; then
446                echo $"Retry lazy unmount ${BUILD_ROOT}${fs} ... "
447                umount -l ${BUILD_ROOT}${fs}
448                echo $"done."
449            fi
450            ;;
451        vfs)
452            for dir in /sys /proc /dev/shm /dev/pts /dev; do
453                mount-chroot-umount ${dir} || return 1
454            done
455            ;;
456        archives_dir)
457            [ -d ${ARCHIVES_DIR} ] || return 1
458            [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
459                umount ${ARCHIVES_DIR}
460            ;;
461        unionfs_dir)
462            [ -d ${BUILD_ROOT} ] || return 1
463            [ -z "$(mount | grep ${BUILD_ROOT} | egrep '(unionfs|aufs)')" ] || \
464                umount ${BUILD_ROOT}
465            if [ ! -z "$(mount | grep ${BUILD_ROOT} | egrep '(unionfs|aufs)')" ]; then
466                echo $"Retry lazy unmount ${BUILD_ROOT} ... "
467                umount -l ${BUILD_ROOT}
468                echo $"done."
469            fi
470            ;;
471        *)
472            echo mount-chroot-umount: unknown file system $fs
473            exit 1
474            ;;
475    esac
476    return 0
477}
478
479## mount-chroot-mount [file system|name]
480mount-chroot-mount(){
481    local fs=$1
482
483    case $fs in
484        /home)
485            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
486            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
487                mount -o _netdev,rbind ${fs} ${BUILD_ROOT}${fs}
488            ;;
489        /tmp|/dev)
490            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
491            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
492                mount --bind -o _netdev ${fs} ${BUILD_ROOT}${fs}
493            ;;
494        /sys)
495            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
496            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
497                mount -o _netdev -t sysfs vbuildersysfs ${BUILD_ROOT}${fs}
498            ;;
499        /proc)
500            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
501            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
502                mount -o _netdev -t proc vbuilderproc ${BUILD_ROOT}${fs}
503            ;;
504        /dev/shm)
505            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
506            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
507                mount -o _netdev -t tmpfs vbuildertmpfs ${BUILD_ROOT}${fs}
508            ;;
509        /dev/pts)
510            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
511            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
512                mount -o gid=5,mode=620,_netdev -t devpts vbuilderdevpts ${BUILD_ROOT}${fs}
513            ;;
514        vfs)
515            for dir in /dev /dev/pts /dev/shm /proc /sys; do
516                mount-chroot-mount ${dir} || return 1
517            done
518            ;;
519        archives_dir)
520            [ -d ${EXTERNAL_ARCHIVES_DIR} ] || mkdir -p ${EXTERNAL_ARCHIVES_DIR}
521            [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
522            [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
523                mount --bind -o _netdev ${EXTERNAL_ARCHIVES_DIR} ${ARCHIVES_DIR}
524            [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
525            ;;
526        unionfs_dir)
527            if [ $with_unionfs -eq 1 ]; then
528                [ -d ${UNIONFS_ROOT} ] || mkdir -p ${UNIONFS_ROOT}
529                /sbin/modprobe aufs >& /dev/null ||:
530                if ( grep -q aufs /proc/filesystems ) ; then
531                    [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
532                        mount -t aufs -o br=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro aufs ${BUILD_ROOT}
533                else
534                    [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
535                        mount -t unionfs -o dirs=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro unionfs ${BUILD_ROOT}
536                    unionctl ${BUILD_ROOT} --list
537                fi
538            fi
539            ;;
540        *)
541            echo mount-chroot-mount: unknown file system $fs
542            exit 1
543            ;;
544    esac
545    return 0
546}
547
548## TODO: support spec-validator
549## spec-validator [spec file]
550spec-validator(){
551    echo
552    return 0
553}
554
555
556##############################################################################
557
558Clean(){
559    setup-vbootstrap || return 1
560
561    # mount-chroot --umount /home
562    mount-chroot --umount /tmp
563    mount-chroot --umount /dev/shm
564    mount-chroot --umount /dev/pts
565    mount-chroot --umount /proc
566    mount-chroot --umount archives_dir
567    mount-chroot --umount unionfs_dir
568    apt-get-update --host
569
570    ## We remark that we first remove /, and secondly remove /.
571    ## If we directly remove /, we obtained some non-empty directories:
572    ##   /dev/.udev/rules.d.
573    if [ $with_unionfs -eq 1 ]; then
574        if [ -d ${UNIONFS_ROOT} ]; then
575            echo -n $"Cleaning build root ${UNIONFS_ROOT} via unionfs ... "
576            rm -rf ${UNIONFS_ROOT} 2>/dev/null
577            rm -rf ${UNIONFS_ROOT}
578            echo $"done."
579        fi
580    else
581        if [ -d ${BUILD_ROOT} ]; then
582            echo -n $"Cleaning build root ${BUILD_ROOT} ... "
583            rm -rf ${BUILD_ROOT} 2>/dev/null
584            rm -rf ${BUILD_ROOT}
585            echo $"done."
586        fi
587    fi
588
589    echo $"Cleanup a build farm for ${BUILD_ROOT} done."
590    return 0
591}
592
593Build(){
594    setup-vbootstrap || return 1
595
596    if [ $with_dist_upgrade -eq 1 ]; then
597        ## make bootstrap of ${STABLE_VERSION}
598        /usr/sbin/vbootstrap \
599            $(echo ${BASE_PROFILE} | sed -e "s/VineSeed/${STABLE_VERSION}/") \
600            ${VBOOTSTRAP_FETCH_URL} ${BUILD_ROOT} 2>&1 | \
601            egrep -v -e '^W: Duplicate sources.list entry' \
602                     -e '^W: .*apt-get update'
603
604        ## aim apt-line to VineSeed
605        sed -i "s/apt ${STABLE_VERSION}/apt VineSeed/g" \
606            ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
607    else
608        /usr/sbin/vbootstrap \
609            ${BASE_PROFILE} ${VBOOTSTRAP_FETCH_URL} ${BUILD_ROOT} 2>&1 | \
610            egrep -v -e '^W: Duplicate sources.list entry' \
611                     -e '^W: .*apt-get update'
612    fi
613
614    mount-chroot /proc
615    mount-chroot archives_dir
616    mount-chroot /dev/pts
617    mount-chroot /dev/shm
618    # mount-chroot /tmp
619    # mount-chroot /home
620
621    ##!! 4.2 has no apt-sourceslist-{plus,nonfree,proposed-updates} packages
622    case ${VERSION} in
623        4.2)
624            sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list
625            sed -i -e 's/main plus updates nonfree *$/$(echo ${CATEGORIES} | sed -e "s/,/ /"g) updates/g' ${BUILD_ROOT}/etc/apt/sources.list
626            $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
627            # [ $with_category_security -eq 1 ] && \
628            #   echo
629            ;;
630        5.2|@@VBUILDER_STABLE_VERSION@@)
631            sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
632            $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
633            [ $with_category_plus -eq 1 ] && \
634                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-plus' && \
635                sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/plus.list
636            [ $with_category_nonfree -eq 1 ] && \
637                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-nonfree' && \
638                sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/nonfree.list
639            [ $with_category_proposed_updates -eq 1 ] && \
640                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-proposed-updates' && \
641                sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/proposed-updates.list
642            # [ $with_category_security -eq 1 ] && \
643            #   echo
644            ;;
645        VineSeed)
646            sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
647            $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
648            [ $with_category_plus -eq 1 ] && \
649                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-plus' && \
650                sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/plus.list
651            [ $with_category_nonfree -eq 1 ] && \
652                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-nonfree' && \
653                sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/nonfree.list
654            [ $with_category_test -eq 1 ] && \
655                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-test' && \
656                sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/test.list
657            ;;
658    esac
659
660    [ $with_dist_upgrade -eq 1 ] && \
661        $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
662
663    [ $with_build_essential -eq 1 ] && \
664        $__chroot_sh 'apt-get -qq -y install build-essential'
665
666    $__chroot_sh 'apt-get -qq -y install etcskel shadow-utils MAKEDEV'
667
668    $__chroot_sh 'cd /dev && /sbin/MAKEDEV console'
669    $__chroot_sh 'cd /dev && /sbin/MAKEDEV ptmx'
670    $__chroot_sh 'cd /dev && /sbin/MAKEDEV null'
671    $__chroot_sh 'cd /dev && /sbin/MAKEDEV zero'
672    $__chroot_sh 'cd /dev && /sbin/MAKEDEV random'
673    $__chroot_sh 'cd /dev && /sbin/MAKEDEV urandom'
674    $__chroot_sh 'cd /dev && mkdir -p shm && chmod 777 shm'
675    $__chroot_sh 'cd /dev && mkdir -p pts && chmod 755 pts'
676
677    $__chroot_sh '/usr/sbin/pwconv'
678    $__chroot_sh "/usr/sbin/useradd ${BUILD_USER}"
679
680    ##!! for rpm-4.8.0 or higher
681    ##!! (See http://trac.vinelinux.org/wiki/Vine6/AboutUpdateToolchain)
682    if [ "${VERSION}" = "VineSeed" ]; then
683        $__chroot_sh "sed -i -e 's/^%_topdir/#%_topdir/' /home/${BUILD_USER}/.rpmmacros"
684    fi
685
686    ## set local repositories (profile only)
687    if [ ${with_profile} -eq 1 ]; then
688        if [ ! -z "${LOCAL_REPOS}" ]; then
689            cat >${BUILD_ROOT}/etc/apt/sources.list.d/local.list<<EOF
690${LOCAL_REPOS}
691EOF
692        $__chroot_sh "apt-get update"
693        fi
694
695        ## additional base packages
696        [ ! -z "${ADD_BASE_PKGS}" ] && \
697            $__chroot_sh "apt-get -y install ${ADD_BASE_PKGS}" 
698    fi
699
700    # mount-chroot --umount /home
701    # mount-chroot --umount /tmp
702    mount-chroot --umount /dev/shm
703    mount-chroot --umount /dev/pts
704    mount-chroot --umount archives_dir
705    mount-chroot --umount /proc
706    apt-get-update --host
707
708    echo $"Making a build farm for ${BUILD_ROOT} done."
709    return 0
710}
711
712RPM_Remove(){
713    setup-vbootstrap-rpm || return 1
714    mount-chroot unionfs_dir
715    mount-chroot archives_dir
716    mount-chroot /proc
717    mount-chroot /dev/pts
718    mount-chroot /dev/shm
719    # apt-get-update --chroot
720
721    [ -f $RPM_PKG ] && \
722        echo $"E: $RPM_PKG is not a package name" && return 1
723    $__chroot_sh "apt-get -y remove $BASE_RPM_PKG"
724
725    mount-chroot --umount /dev/shm
726    mount-chroot --umount /dev/pts
727    mount-chroot --umount /proc
728    mount-chroot --umount archives_dir
729    mount-chroot --umount unionfs_dir
730    # apt-get-update --host
731
732    return 0
733}
734
735RPM_Install(){
736    setup-vbootstrap-rpm || return 1
737    mount-chroot unionfs_dir
738    mount-chroot archives_dir
739    mount-chroot /proc
740    mount-chroot /dev/pts
741    mount-chroot /dev/shm
742    apt-get-update --chroot
743
744    [ $with_category_nonfree -eq 1 ] && \
745        [ ! -z "$(echo $BASE_RPM_PKG | grep -e 'self-build-' -e 'install-assist-')" ] && \
746        $__chroot_sh 'rpm -q --quiet self-build-setup || apt-get -qq -y install self-build-setup'
747
748    $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $BASE_RPM_PKG"
749
750    mount-chroot --umount /dev/shm
751    mount-chroot --umount /dev/pts
752    mount-chroot --umount /proc
753    mount-chroot --umount archives_dir
754    mount-chroot --umount unionfs_dir
755    apt-get-update --host
756
757    return 0
758}
759
760RPM_Build(){
761    [ ! -f ${RPM_PKG} ] && \
762        echo $"E: No such file found: ${RPM_PKG}" && return 1
763
764    ## check the extension of given $RPM_PKG
765    local RPM_PKG_EXT=${RPM_PKG##*.}
766    case ${RPM_PKG_EXT} in
767        spec)
768            ## In this case,
769            ## the file ${RPM_PKG} is a spec file!
770            # spec-validator ${RPM_PKG} || return 1
771            # return 1
772
773            ## We assign the variable ${RPM_PKG}
774            ## the src.rpm generated by the spec file ${RPM_PKG}.
775            RPM_PKG=$(rpm -E "%{_srcrpmdir}")/$(rpm -q --qf "%{name}-%{version}-%{release}\n" --specfile ${RPM_PKG} | head -n 1).src.rpm
776            ;;
777        rpm)
778            RPM_PKG_EXT=$(echo ${RPM_PKG} | sed -e "s|.*\.\(src\.rpm\)$|\1|")
779            [ "${RPM_PKG_EXT}" != "src.rpm" ] && \
780                echo $"E: $RPM_PKG is NOT a source RPM package" && return 1
781            ;;
782        *)
783            echo $"The action build-rpm cannot work such extension: ${RPM_PKG_EXT}"
784            return 1
785            ;;
786    esac
787
788    setup-vbootstrap-rpm || return 1
789    mount-chroot unionfs_dir
790    mount-chroot archives_dir
791    mount-chroot /proc
792    mount-chroot /dev/pts
793    mount-chroot /dev/shm
794    apt-get-update --chroot
795   
796    [ ! -f ${RPM_PKG} ] && \
797        echo $"E: No such file found: ${RPM_PKG}" && return 1
798
799    RPM_PKG_USER=$(stat -c %U $RPM_PKG)
800    RPM_PKG_GROUP=$(stat -c %G $RPM_PKG)
801    [ ! -z "${SUDO_UID}" ] && RPM_PKG_USER=${SUDO_UID}
802    [ ! -z "${SUDO_GID}" ] && RPM_PKG_GROUP=${SUDO_GID}
803    local __install="install -p -v -o ${RPM_PKG_USER} -g ${RPM_PKG_GROUP}"
804
805    ## make src.rpm for $VERSION
806    $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpm -ivh $BASE_RPM_PKG'"
807    $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpmbuild -bs --nodeps --clean --rmsource --rmspec $RPM_OPTS ${BUILD_DIR}/SPECS/*.spec'"
808
809
810    ## change ${DIST_RELEASE}
811    BASE_RPM_PKG=$(echo $BASE_RPM_PKG | sed -e "s/vl[0-9]*\([A-Za-z]*[0-9A-Za-z]*\)\./vl${DIST_RELEASE}\1\./")
812
813    ## rebuild $BASE_RPM_PKG on ${DIST_RELEASE}
814    $__chroot_sh "cd ${BUILD_DIR}/SRPMS && apt-get -o APT::Install::Virtual=true -y build-dep $BASE_RPM_PKG"
815    $__chroot_sh "cd ${BUILD_DIR}/SRPMS && su ${BUILD_USER} -c 'rpmbuild --rebuild $RPM_OPTS $BASE_RPM_PKG'"
816
817    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')
818
819    [ $with_category_nonfree -eq 1 ] && \
820        [ ! -z "$(echo $BUILT_RPMS_LIST | grep -e 'self-build-' -e 'install-assist-')" ] && \
821        $__chroot_sh 'rpm -q --quiet self-build-setup || apt-get -qq -y install self-build-setup'
822
823    [ $with_no_install -eq 0 ] && \
824        $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $BUILT_RPMS_LIST"
825
826    ## copy built rpms to ${HOME}/rpm/ for each archtectures
827    echo $"Copying built rpms to ${BUILT_RPMS_DIR} for each archtectures ... "
828    for i in $RPM_PKG_ARCH_LIST; do
829        [ -d $BUILD_ROOT${BUILD_DIR}/${i} ] || continue
830        if [ ! -d ${BUILT_RPMS_DIR}/${VERSION}/${i} ]; then
831            $__install -d ${BUILT_RPMS_DIR}/${VERSION}/${i}/
832            chown -R ${RPM_PKG_USER}:${RPM_PKG_GROUP} ${BUILT_RPMS_DIR}
833        fi
834        find $BUILD_ROOT${BUILD_DIR}/${i} -type f -regex '.*\.rpm' \
835            -exec $__install -m0644 {} ${BUILT_RPMS_DIR}/${VERSION}/${i}/ \;
836    done
837
838    mount-chroot --umount /dev/shm
839    mount-chroot --umount /dev/pts
840    mount-chroot --umount /proc
841    mount-chroot --umount archives_dir
842    mount-chroot --umount unionfs_dir
843    apt-get-update --host
844
845    echo $"done."
846    return 0
847}
848
849RPM_Sign(){
850    [ $with_sign -eq 1 ] || return 1
851
852    setup-vbootstrap || return 1
853
854    [ -d ${BUILD_ROOT} ] || Build
855
856    mount-chroot unionfs_dir
857
858    echo $"Signing built rpms using ${RPM_SIGN_USER}'s key: "
859    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')"
860
861    mount-chroot --umount unionfs_dir
862
863    return 0
864}
865
866Login_Chroot(){
867    [ $with_login -eq 1 ] || return 1
868
869    setup-vbootstrap || return 1
870
871    __chroot="/usr/sbin/chroot ${BUILD_ROOT}"
872    ## setarch ix86 if ix86 chroot on x86_64 host
873    [ $with_ix86_on_x86_64 -eq 1 ] && \
874        __chroot="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH}"
875
876
877    mount-chroot unionfs_dir
878    mount-chroot archives_dir
879    mount-chroot /proc
880    mount-chroot /dev/pts
881    mount-chroot /dev/shm
882    mount-chroot /tmp
883    # mount-chroot /home
884    # apt-get-update --chroot
885
886    ## copy host's configurations of /etc
887    #passwd shadow group gshadow
888    for i in resolv.conf hosts; do
889        cp -pf /etc/${i} ${BUILD_ROOT}/etc
890    done
891    cp -Ppf /etc/localtime ${BUILD_ROOT}/etc
892
893    $__chroot /bin/bash
894
895    # mount-chroot  --umount /home
896    mount-chroot  --umount /tmp
897    mount-chroot --umount /dev/shm
898    mount-chroot --umount /dev/pts
899    mount-chroot --umount /proc
900    mount-chroot --umount archives_dir
901    mount-chroot --umount unionfs_dir
902    # apt-get-update --host
903
904    return 0
905}
906
907
908##############################################################################
909
910initialize-variables || exit 1
911
912check-parameter $* || exit 1
913
914setup-vbuilder || exit 1
915
916while [ $# -gt 0 ]; do
917    tmpARG=$1
918    case $tmpARG in
919        --profile)
920            shift
921            ;;
922        --version|--arch|--category|--fetch-url|--target|--rpmbuild-define|--rpmbuild-with|--rpmbuild-without|--bootstrap-dir|--unionfs-dir|--cache-dir|--built-rpms-dir)
923            shift
924            ;;
925        --dist-upgrade|--unionfs|--with-compat32|--sign|--no-install|--login|--no-build-essential|--debug)
926            ;;
927        --build-rpm|build-rpm|--install-rpm|install-rpm|--remove-rpm|remove-rpm)
928            shift
929            ;;
930        --build|build|--clean|clean)
931            ;;
932        *)
933            echo unknown option $1
934            exit 1
935            ;;
936    esac
937
938    case $tmpARG in
939        --profile)
940            ;;
941        --version)
942            VERSION=$1
943            ;;
944        --arch)
945            VARCH=$1
946            ;;
947        --category)
948            CATEGORIES=$1
949            ;;
950        --fetch-url)
951            VBOOTSTRAP_FETCH_URL=$1
952            ;;
953        --dist-upgrade)
954            with_dist_upgrade=1
955            ;;
956        --unionfs)
957            with_unionfs=1
958            ;;
959        --target)
960            TARGET=$1
961            RPM_OPTS="${RPM_OPTS} --target $TARGET"
962            ;;
963        --with-compat32)
964            RPM_OPTS="${RPM_OPTS} --with compat32"
965            ;;
966        --rpmbuild-define)
967            RPM_OPTS="${RPM_OPTS} --define $1"
968            ;;
969        --rpmbuild-with)
970            RPM_OPTS="${RPM_OPTS} --with $1"
971            ;;
972        --rpmbuild-without)
973            RPM_OPTS="${RPM_OPTS} --without $1"
974            ;;
975        --sign)
976            with_sign=1
977            ;;
978        --no-install)
979            with_no_install=1
980            ;;
981        --no-build-essential)
982            with_build_essential=0
983            ;;
984        --login)
985            with_login=1
986            ;;
987        --bootstrap-dir)
988            VBOOTSTRAP_DIR=$1
989            ;;
990        --unionfs-dir)
991            UNIONFS_DIR=$1
992            ;;
993        --cache-dir)
994            CACHE_DIR=$1
995            ;;
996        --built-rpms-dir)
997            BUILT_RPMS_DIR=$1
998            ;;
999        --debug)
1000            with_debug=1
1001            ;;
1002        --build-rpm|build-rpm)
1003            RPM_PKG=$1
1004            RPM_Build || exit 1
1005            ;;
1006        --install-rpm|install-rpm)
1007            RPM_PKG=$1
1008            RPM_Install || exit 1
1009            ;;
1010        --remove-rpm|remove-rpm)
1011            RPM_PKG=$1
1012            RPM_Remove || exit 1
1013            ;;
1014        --build|build)
1015            Build || exit 1
1016            ;;
1017        --clean|clean)
1018            Clean || exit 1
1019            ;;
1020    esac
1021    shift
1022done
1023
1024RPM_Sign ||:
1025
1026Login_Chroot ||:
1027
1028exit
Note: See TracBrowser for help on using the repository browser.