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

Revision 2770, 27.0 KB checked in by munepi, 13 years ago (diff)

updated vbuilder.sh.in: improved Clean()

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