source: projects/vbootstrap/trunk/vbuilder.sh.in @ 3343

Revision 3343, 26.8 KB checked in by munepi, 13 years ago (diff)

updated vbuilder.sh.in: dropped user_from_uid()

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