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

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

updated vbuilder.sh.in: arranged some procedures to mount /proc and unionfs_dir

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    ## load default settings
120    VBUILDER_CONF=/etc/vbootstrap/vbuilder.conf
121    if [ -r $VBUILDER_CONF ]; then
122        . $VBUILDER_CONF
123        [ $? -eq 1 ] && return 1
124    fi
125    [ -z "${DEFAULT_VERSION}" ] && \
126        DEFAULT_VERSION=@@VBUILDER_DEFAULT_VERSION@@
127    [ -z "${CATEGORIES}" ] && \
128        CATEGORIES=@@VBUILDER_CATEGORIES@@
129    [ -z "${VBOOTSTRAP_DIR}" ] && \
130        VBOOTSTRAP_DIR=@@VBUILDER_VBOOTSTRAP_DIR@@
131    [ -z "${UNIONFS_DIR}" ] && \
132        UNIONFS_DIR=@@VBUILDER_UNIONFS_DIR@@
133    [ -z "${CACHE_DIR}" ] && \
134        CACHE_DIR=@@VBUILDER_CACHE_DIR@@
135    [ -z "${BUILT_RPMS_DIR}" ] && \
136        BUILT_RPMS_DIR=@@VBUILDER_BUILT_RPMS_DIR@@
137
138    ## set default version for vbootstrap
139    VERSION=$DEFAULT_VERSION
140
141    ## set current stable relase version
142    STABLE_VERSION=@@VBUILDER_STABLE_VERSION@@
143
144    ## set boolian variables
145    with_setup_vbootstrap=0
146    with_dist_upgrade=0
147    with_unionfs=0
148    with_sign=0
149    with_no_install=0
150    with_login=0
151    with_debug=0
152    with_actions=0
153    with_ix86_on_x86_64=0
154    with_category_main=0
155    with_category_plus=0
156    with_category_nonfree=0
157    with_category_test=0
158    with_category_proposed_updates=0
159    with_category_security=0
160
161    return 0
162}
163
164setup-vbootstrap(){
165    if [ ${with_setup_vbootstrap} -eq 0 ]; then
166        with_setup_vbootstrap=1
167
168        ## check debug mode
169        [ ${with_debug} -eq 1 ] && \
170            set && set -x
171
172        ## check some directories
173        ## Note: create $BUILT_RPMS_DIR in RPM_Build()
174        [ -d $VBOOTSTRAP_DIR ] || mkdir -p $VBOOTSTRAP_DIR
175        [ -d $CACHE_DIR ] || mkdir -p $CACHE_DIR
176
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
190        ## check a chroot archtecture
191        if [ -z ${VARCH} ]; then
192            VARCH=${UARCH}
193        else
194            case "${VARCH}" in
195                i386|i686|x86_64)
196                    [ "$(uname -i)" = "ppc" -o "${UARCH}" = "arm" ] && \
197                        echo $"E: arch ${VARCH} is NOT supported on $(uname -i)" && return 1
198                    ;;
199                ppc)
200                    [ "$(uname -i)" = "i386" -o "$(uname -i)" = "i686" -o "$(uname -i)" = "x86_64" -o "${UARCH}" = "arm" ] && \
201                        echo $"E: arch ${VARCH} is NOT supported on $(uname -i)" && return 1
202                    ;;
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                    ;;
207            esac
208        fi
209
210        ##!! 4.2 is NO support on VARCH=x86_64
211        if [ "${VERSION}" = "4.2" -a "${VARCH}" = "x86_64" ]; then
212            echo $"E: ${VERSION} is NOT supported"
213            return 1
214        fi
215
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
222        ## support i386 chroot on x86_64 below:
223        [ "${VARCH}" != "${UARCH}" ] && \
224            VERSION=${VERSION}_${VARCH} && \
225            with_ix86_on_x86_64=1
226
227        ## hack arm repository to local
228        [ "${VARCH}" = "arm" ] && \
229            VERSION=${VERSION}_${VARCH}
230
231        ## check support ${VERSION}
232        if [ -z "$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*// | grep -m 1 ${VERSION})" ]; then
233            echo $"E: ${VERSION} is NOT supported"
234            return 1
235        fi
236
237        ## check ${VERSION} equals VineSeed*, when with_dist_upgrade=1
238        if [ $with_dist_upgrade -eq 1 ]; then
239            if [ "$(echo ${VERSION} | sed -e "s/\(VineSeed\).*/\1/")" != "VineSeed" ]; then
240                echo $"E: version ${VERSION} does not support --dist-upgrade option"
241                return 1
242            fi
243        fi
244
245        ## set ${MAJOR_VERSION}
246        MAJOR_VERSION=$(echo ${VERSION} | sed -e "s/_i[0-9]86//")
247
248        ## check apt categories
249        ## "main" category is unconditionally permited
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                    ;;
262                test)
263                    ## "test" category only exists in VineSeed
264                    [ "${MAJOR_VERSION}" = "VineSeed" ] || \
265                        echo $"E: No such category exists: $cat" && return 1
266                    with_category_test=1
267                    ;;
268                proposed-updates)
269                    ##!! "proposed-updates" category does not exist in 4.2
270                    [ "${MAJOR_VERSION}" = "4.2" ] && \
271                        echo $"E: No such category exists: $cat" && return 1
272
273                    with_category_proposed_updates=1
274                    ;;
275                security)
276                    ## "security" category does not exist in VineSeed
277                    [ "${MAJOR_VERSION}" = "VineSeed" ] && \
278                        echo $"E: No such category exists: $cat" && return 1
279                    with_category_security=1
280                    ;;
281                *)
282                    echo $"E: No such category exists: $cat" && return 1
283                    ;;
284            esac
285        done
286
287        ## check build target option ${TARGET}
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
291                echo $"E: rpm build target ${TARGET} is NOT supported"
292                return 1
293            fi
294        fi
295
296        ## set ${RPM_PKG_ARCH_LIST}
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"
298        [ -z "${TARGET}" ] || \
299            RPM_PKG_ARCH_LIST="RPMS/${TARGET} ${RPM_PKG_ARCH_LIST}"
300
301    fi
302
303    ## set global variables
304    BUILD_ROOT=${VBOOTSTRAP_DIR}/${VERSION}
305    BUILD_USER=vbuilder
306    BUILD_DIR=/home/${BUILD_USER}/rpm
307    UNIONFS_ROOT=${UNIONFS_DIR}/${VERSION}
308    ARCHIVES_DIR=${BUILD_ROOT}/var/cache/apt/archives
309    EXTERNAL_ARCHIVES_DIR=${CACHE_DIR}/${VERSION}/apt/archives
310    VBUILDER_LOG=${BUILD_ROOT}/var/log/vbuilder.log
311
312    __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c -l"
313
314    mkdir -p $VBOOTSTRAP_DIR
315
316    return 0
317}
318
319setup-vbootstrap-rpm(){
320    setup-vbootstrap || return 1
321
322    ## check ${BUILD_ROOT}
323    [ -d ${BUILD_ROOT} ] || Build
324    ## setarch ix86 if ix86 chroot on x86_64 host
325    [ $with_ix86_on_x86_64 -eq 1 ] && \
326        __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH} /bin/sh -c -l"
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
336
337    return 0
338}
339
340## recover apt-get data on host/chroot
341apt-get-update(){
342    case $1 in
343        --host)
344            echo -n $"apt-get update on host ... "
345            apt-get -qq update > /dev/null 2>&1
346            echo $"done."
347            ;;
348        --chroot)
349            echo -n $"apt-get update on chroot ... "
350            $__chroot_sh 'apt-get -qq update' > /dev/null 2>&1
351            echo $"done."
352            ;;
353        *)
354            echo apt-get-update: unknown option $1
355            exit 1
356            ;;
357    esac
358}
359
360## mount-chroot {|--umount} [file system|name]
361## support file systems: /home /tmp /sys /proc /dev/shm /dev/pts /dev
362## support names: vfs archives_dir
363## NOTE: /tmp needs for applications which use X
364##       vfs is virtual file systems
365##       archives_dir uses to mount ${EXTERNAL_ARCHIVES_DIR} to ${ARCHIVES_DIR}
366##       unionfs_dir covers ${BUILD_ROOT} with unionfs
367mount-chroot(){
368    if [ "$1" = "--umount" ]; then
369        mount-chroot-umount $2 || return 1
370    else
371        mount-chroot-mount $1 || return 1
372    fi
373    return 0
374}
375
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}
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
389            ;;
390        vfs)
391            for dir in /sys /proc /dev/shm /dev/pts /dev; do
392                mount-chroot-umount ${dir} || return 1
393            done
394            ;;
395        archives_dir)
396            [ -d ${ARCHIVES_DIR} ] || return 1
397            [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
398                umount ${ARCHIVES_DIR}
399            ;;
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
405                echo $"Retry lazy unmount ${BUILD_ROOT} ... "
406                umount -l ${BUILD_ROOT}
407                echo $"done."
408            fi
409            ;;
410        *)
411            echo mount-chroot-umount: unknown file system $fs
412            exit 1
413            ;;
414    esac
415    return 0
416}
417
418## mount-chroot-mount [file system|name]
419mount-chroot-mount(){
420    local fs=$1
421
422    case $fs in
423        /home)
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}
427            ;;
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}
432            ;;
433        /sys)
434            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
435            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
436                mount -o _netdev -t sysfs vbuildersysfs ${BUILD_ROOT}${fs}
437            ;;
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            ;;
453        vfs)
454            for dir in /dev /dev/pts /dev/shm /proc /sys; do
455                mount-chroot-mount ${dir} || return 1
456            done
457            ;;
458        archives_dir)
459            [ -d ${EXTERNAL_ARCHIVES_DIR} ] || mkdir -p ${EXTERNAL_ARCHIVES_DIR}
460            [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
461            [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
462                mount --bind -o _netdev ${EXTERNAL_ARCHIVES_DIR} ${ARCHIVES_DIR}
463            [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
464            ;;
465        unionfs_dir)
466            if [ $with_unionfs -eq 1 ]; then
467                [ -d ${UNIONFS_ROOT} ] || mkdir -p ${UNIONFS_ROOT}
468                [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
469                    mount -t unionfs -o dirs=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro unionfs ${BUILD_ROOT}
470                unionctl ${BUILD_ROOT} --list
471            fi
472            ;;
473        *)
474            echo mount-chroot-mount: unknown file system $fs
475            exit 1
476            ;;
477    esac
478    return 0
479}
480
481write-vbuilder-log(){
482    HRULE="======================================================================"
483
484    [ -d ${BUILD_ROOT} ] || return 1
485
486    if [ ! -f $VBUILDER_LOG ]; then
487        cat<<EOF > $VBUILDER_LOG
488${HRULE}
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
495--version: ${VERSION}
496$(echo $([ -z "${VARCH}" ] || echo "--arch: ${VARCH}"))
497$(echo $([ -z "${CATEGORIES}" ] || echo "--category: ${CATEGORIES}"))
498$(echo $([ $with_dist_upgrade -eq 1 ] && echo "--dist-upgrade"))
499$(echo $([ $with_unionfs -eq 1 ] && echo "--unionfs"))
500$(echo $([ -z "${TARGET}" ] || echo "--target: ${TARGET}"))
501--bootstrap-dir: ${VBOOTSTRAP_DIR}
502--unionfs-dir: ${UNIONFS_DIR}
503--cache-dir: ${CACHE_DIR}
504--built-rpms-dir: ${BUILT_RPMS_DIR}
505${HRULE}
506
507[$VBUILDER_CONF]
508$(cat $VBUILDER_CONF)
509
510[History]
511EOF
512    else
513        cat<<EOF >> $VBUILDER_LOG
514$*
515EOF
516    fi
517
518    return 0
519}
520
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
537##############################################################################
538
539Clean(){
540    setup-vbootstrap || return 1
541
542    # mount-chroot --umount /home
543    mount-chroot --umount /tmp
544    mount-chroot --umount /dev/shm
545    mount-chroot --umount /dev/pts
546    mount-chroot --umount /proc
547    mount-chroot --umount archives_dir
548    mount-chroot --umount unionfs_dir
549    apt-get-update --host
550
551    ## We remark that we first remove /, and secondly remove /.
552    ## If we directly remove /, we obtained some non-empty directories:
553    ##   /dev/.udev/rules.d.
554    if [ $with_unionfs -eq 1 ]; then
555        if [ -d ${UNIONFS_ROOT} ]; then
556            echo -n $"Cleaning build root ${UNIONFS_ROOT} via unionfs ... "
557            rm -rf ${UNIONFS_ROOT} 2>/dev/null
558            rm -rf ${UNIONFS_ROOT}
559            echo $"done."
560        fi
561    else
562        if [ -d ${BUILD_ROOT} ]; then
563            echo -n $"Cleaning build root ${BUILD_ROOT} ... "
564            rm -rf ${BUILD_ROOT} 2>/dev/null
565            rm -rf ${BUILD_ROOT}
566            echo $"done."
567        fi
568    fi
569
570    echo $"Cleanup a build farm for ${VERSION} done."
571    return 0
572}
573
574Build(){
575    setup-vbootstrap || return 1
576
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}
582
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
590    mount-chroot /proc
591    mount-chroot archives_dir
592    mount-chroot /dev/pts
593    mount-chroot /dev/shm
594    # mount-chroot /tmp
595    # mount-chroot /home
596
597    $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
598
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"
603            # [ $with_category_security -eq 1 ] && \
604            #   echo
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'
613            # [ $with_category_security -eq 1 ] && \
614            #   echo
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'
621            [ $with_category_test -eq 1 ] && \
622                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-test'
623            ;;
624    esac
625
626    [ $with_dist_upgrade -eq 1 ] && \
627        $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
628
629    $__chroot_sh 'apt-get -qq -y install build-essential'
630
631    [ $with_category_nonfree -eq 1 ] && \
632        $__chroot_sh 'apt-get -qq -y install self-build-setup'
633
634    $__chroot_sh 'apt-get -qq -y install etcskel shadow-utils'
635
636    $__chroot_sh 'cd /dev && /sbin/MAKEDEV console'
637    $__chroot_sh 'cd /dev && /sbin/MAKEDEV ptmx'
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'
642    $__chroot_sh 'cd /dev && mkdir -p shm && chmod 777 shm'
643    $__chroot_sh 'cd /dev && mkdir -p pts && chmod 755 pts'
644
645    $__chroot_sh '/usr/sbin/pwconv'
646    $__chroot_sh "/usr/sbin/useradd ${BUILD_USER}"
647
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
651        $__chroot_sh "sed -i -e 's/^%_topdir/#%_topdir/' /home/${BUILD_USER}/.rpmmacros"
652    fi
653
654    ## output basic informations in chroot
655    write-vbuilder-log
656    write-vbuilder-log "build"
657
658    # mount-chroot --umount /home
659    # mount-chroot --umount /tmp
660    mount-chroot --umount /dev/shm
661    mount-chroot --umount /dev/pts
662    mount-chroot --umount archives_dir
663    mount-chroot --umount /proc
664    apt-get-update --host
665
666    echo $"Making a build farm for ${VERSION} done."
667    return 0
668}
669
670Show-Info(){
671    setup-vbootstrap || return 1
672
673    [ -f $VBUILDER_LOG ] && cat $VBUILDER_LOG
674
675    return 0
676}
677
678RPM_Remove(){
679    setup-vbootstrap-rpm || return 1
680    mount-chroot unionfs_dir
681    mount-chroot archives_dir
682    mount-chroot /proc
683    mount-chroot /dev/pts
684    mount-chroot /dev/shm
685    apt-get-update --chroot
686
687    [ -f $RPM_PKG ] && \
688        echo $"E: $RPM_PKG is not a package name" && return 1
689    $__chroot_sh "apt-get -y remove $BASE_RPM_PKG"
690
691    write-vbuilder-log "remove-rpm $RPM_PKG"
692
693    mount-chroot --umount /dev/shm
694    mount-chroot --umount /dev/pts
695    mount-chroot --umount /proc
696    mount-chroot --umount archives_dir
697    mount-chroot --umount unionfs_dir
698    apt-get-update --host
699
700    return 0
701}
702
703RPM_Install(){
704    setup-vbootstrap-rpm || return 1
705    mount-chroot unionfs_dir
706    mount-chroot archives_dir
707    mount-chroot /proc
708    mount-chroot /dev/pts
709    mount-chroot /dev/shm
710
711    apt-get-update --chroot
712
713    $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $BASE_RPM_PKG"
714
715    write-vbuilder-log "install-rpm $RPM_PKG"
716
717    mount-chroot --umount /dev/shm
718    mount-chroot --umount /dev/pts
719    mount-chroot --umount /proc
720    mount-chroot --umount archives_dir
721    mount-chroot --umount unionfs_dir
722    apt-get-update --host
723
724    return 0
725}
726
727RPM_Build(){
728    setup-vbootstrap-rpm || return 1
729    mount-chroot unionfs_dir
730    mount-chroot archives_dir
731    mount-chroot /proc
732    mount-chroot /dev/pts
733    mount-chroot /dev/shm
734    apt-get-update --chroot
735
736    [ ! -f $RPM_PKG ] && \
737        echo $"E: $RPM_PKG is not a source RPM package" && return 1
738   
739    RPM_PKG_USER=$(stat -c %U $RPM_PKG)
740    RPM_PKG_GROUP=$(stat -c %G $RPM_PKG)
741    [ ! -z "${SUDO_UID}" ] && RPM_PKG_USER=${SUDO_UID}
742    [ ! -z "${SUDO_GID}" ] && RPM_PKG_GROUP=${SUDO_GID}
743    local __install="install -p -v -o ${RPM_PKG_USER} -g ${RPM_PKG_GROUP}"
744
745    ## make src.rpm for $VERSION
746    $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpm -ivh $BASE_RPM_PKG'"
747    $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpmbuild -bs --nodeps --clean --rmsource --rmspec $RPM_OPTS ${BUILD_DIR}/SPECS/*.spec'"
748
749
750    ## change ${DIST_RELEASE}
751    BASE_RPM_PKG=$(echo $BASE_RPM_PKG | sed -e "s/vl[0-9]*\([A-Za-z]*\)\./vl${DIST_RELEASE}\1\./")
752
753    ## rebuild $BASE_RPM_PKG on ${DIST_RELEASE}
754    $__chroot_sh "cd ${BUILD_DIR}/SRPMS && apt-get -o APT::Install::Virtual=true -y build-dep $BASE_RPM_PKG"
755    $__chroot_sh "cd ${BUILD_DIR}/SRPMS && su ${BUILD_USER} -c 'rpmbuild --rebuild $RPM_OPTS $BASE_RPM_PKG'"
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')"
758
759    ## copy built rpms to ${HOME}/rpm/ for each archtectures
760    echo $"Copying built rpms to ${BUILT_RPMS_DIR} for each archtectures ... "
761    for i in $RPM_PKG_ARCH_LIST; do \
762        if [ -d $BUILD_ROOT${BUILD_DIR}/${i} ]; then
763            if [ ! -d ${BUILT_RPMS_DIR}/${MAJOR_VERSION}/${i} ]; then
764                $__install -d ${BUILT_RPMS_DIR}/${MAJOR_VERSION}/${i}/
765                chown -R ${RPM_PKG_USER}:${RPM_PKG_GROUP} ${BUILT_RPMS_DIR}
766            fi
767            find $BUILD_ROOT${BUILD_DIR}/${i} -type f -regex '.*\.rpm' \
768                -exec $__install -m0644 {} ${BUILT_RPMS_DIR}/${MAJOR_VERSION}/${i}/ \;
769        fi
770    done
771
772    write-vbuilder-log "build-rpm $RPM_PKG"
773
774    mount-chroot --umount /dev/shm
775    mount-chroot --umount /dev/pts
776    mount-chroot --umount /proc
777    mount-chroot --umount archives_dir
778    mount-chroot --umount unionfs_dir
779    apt-get-update --host
780
781    echo $"done."
782    return 0
783}
784
785RPM_Sign(){
786    [ $with_sign -eq 1 ] || return 1
787
788    ## check $SUDO_USER and $USERHELPER_UID
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
797
798    setup-vbootstrap || return 1
799
800    [ -d ${BUILD_ROOT} ] || Build
801
802    mount-chroot unionfs_dir
803
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')"
806
807    mount-chroot --umount unionfs_dir
808
809    return 0
810}
811
812Login_Chroot(){
813    [ $with_login -eq 1 ] || return 1
814
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 unionfs_dir
824    mount-chroot archives_dir
825    mount-chroot /proc
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 /proc
846    mount-chroot --umount archives_dir
847    mount-chroot --umount unionfs_dir
848    apt-get-update --host
849
850    return 0
851}
852
853
854##############################################################################
855
856setup-vbuilder || exit 1
857
858check-parameter $* || exit 1
859
860while [ $# -gt 0 ]; do
861    tmpARG=$1
862    case $tmpARG in
863        --version|--arch|--category|--target|--rpmbuild-define|--rpmbuild-with|--rpmbuild-without|--bootstrap-dir|--unionfs-dir|--cache-dir|--built-rpms-dir)
864            shift
865            ;;
866        --dist-upgrade|--unionfs|--with-compat32|--sign|--no-install|--login|--debug)
867            ;;
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            ;;
873        *)
874            echo unknown option $1
875            exit 1
876            ;;
877    esac
878
879    case $tmpARG in
880        --version)
881            VERSION=$1
882            ;;
883        --arch)
884            VARCH=$1
885            ;;
886        --category)
887            CATEGORIES=$1
888            ;;
889        --dist-upgrade)
890            with_dist_upgrade=1
891            ;;
892        --unionfs)
893            with_unionfs=1
894            ;;
895        --target)
896            TARGET=$1
897            RPM_OPTS="${RPM_OPTS} --target $TARGET"
898            ;;
899        --with-compat32)
900            RPM_OPTS="${RPM_OPTS} --with compat32"
901            ;;
902        --rpmbuild-define)
903            RPM_OPTS="${RPM_OPTS} --define $1"
904            ;;
905        --rpmbuild-with)
906            RPM_OPTS="${RPM_OPTS} --with $1"
907            ;;
908        --rpmbuild-without)
909            RPM_OPTS="${RPM_OPTS} --without $1"
910            ;;
911        --sign)
912            with_sign=1
913            ;;
914        --no-install)
915            with_no_install=1
916            ;;
917        --login)
918            with_login=1
919            ;;
920        --bootstrap-dir)
921            VBOOTSTRAP_DIR=$1
922            ;;
923        --unionfs-dir)
924            UNIONFS_DIR=$1
925            ;;
926        --cache-dir)
927            CACHE_DIR=$1
928            ;;
929        --built-rpms-dir)
930            BUILT_RPMS_DIR=$1
931            ;;
932        --debug)
933            with_debug=1
934            ;;
935        --build-rpm|build-rpm)
936            RPM_PKG=$1
937            RPM_Build || exit 1
938            ;;
939        --install-rpm|install-rpm)
940            RPM_PKG=$1
941            RPM_Install || exit 1
942            ;;
943        --remove-rpm|remove-rpm)
944            RPM_PKG=$1
945            RPM_Remove || exit 1
946            ;;
947        --show-info|show-info)
948            Show-Info || exit 1
949            ;;
950        --build|build)
951            Build || exit 1
952            ;;
953        --clean|clean)
954            Clean || exit 1
955            ;;
956    esac
957    shift
958done
959
960RPM_Sign
961
962Login_Chroot
963
964exit
Note: See TracBrowser for help on using the repository browser.