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

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

updated vbuilder: added --debug, --login options; applied vbootstrap-0.0.37-armhack.patch (Thanks to parrot-san)

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 ${BUILD_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 archives_dir
547    mount-chroot --umount unionfs_dir
548    mount-chroot --umount /proc
549    apt-get-update --host
550
551    if [ $with_unionfs -eq 1 ]; then
552        if [ -d ${UNIONFS_ROOT} ]; then
553            echo -n $"Cleaning build root ${UNIONFS_ROOT} via unionfs ... "
554            rm -rf ${UNIONFS_ROOT}
555            echo $"done."
556        fi
557    else
558        if [ -d ${BUILD_ROOT} ]; then
559            echo -n $"Cleaning build root ${BUILD_ROOT} ... "
560            rm -rf ${BUILD_ROOT}
561            echo $"done."
562        fi
563    fi
564
565    echo $"Cleanup a build farm for ${VERSION} done."
566    return 0
567}
568
569Build(){
570    setup-vbootstrap || return 1
571
572    if [ $with_dist_upgrade -eq 1 ]; then
573        ## make bootstrap of ${STABLE_VERSION}
574        /usr/sbin/vbootstrap \
575            $(echo ${VERSION} | sed -e "s/VineSeed/${STABLE_VERSION}/") \
576            ${BUILD_ROOT}
577
578        ## aim apt-line to VineSeed
579        sed -i "s/apt ${STABLE_VERSION}/apt VineSeed/g" \
580            ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
581    else
582        /usr/sbin/vbootstrap ${VERSION} ${BUILD_ROOT}
583    fi
584
585    mount-chroot /proc
586    mount-chroot archives_dir
587    mount-chroot /dev/pts
588    mount-chroot /dev/shm
589    # mount-chroot /tmp
590    # mount-chroot /home
591
592    $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
593
594    ##!! 4.2 has no apt-sourceslist-{plus,nonfree,proposed-updates} packages
595    case ${MAJOR_VERSION} in
596        4.2)
597            $__chroot_sh "sed -i -e 's/main plus updates nonfree *$/$(echo ${CATEGORIES} | sed -e "s/,/ /"g) updates/g' /etc/apt/sources.list"
598            # [ $with_category_security -eq 1 ] && \
599            #   echo
600            ;;
601        @@VBUILDER_STABLE_VERSION@@)
602            [ $with_category_plus -eq 1 ] && \
603                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-plus'
604            [ $with_category_nonfree -eq 1 ] && \
605                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-nonfree'
606            [ $with_category_proposed_updates -eq 1 ] && \
607                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-proposed-updates'
608            # [ $with_category_security -eq 1 ] && \
609            #   echo
610            ;;
611        VineSeed)
612            [ $with_category_plus -eq 1 ] && \
613                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-plus'
614            [ $with_category_nonfree -eq 1 ] && \
615                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-nonfree'
616            [ $with_category_test -eq 1 ] && \
617                $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-test'
618            ;;
619    esac
620
621    [ $with_dist_upgrade -eq 1 ] && \
622        $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
623
624    $__chroot_sh 'apt-get -qq -y install build-essential'
625
626    [ $with_category_nonfree -eq 1 ] && \
627        $__chroot_sh 'apt-get -qq -y install self-build-setup'
628
629    $__chroot_sh 'apt-get -qq -y install etcskel shadow-utils'
630
631    $__chroot_sh 'cd /dev && /sbin/MAKEDEV console'
632    $__chroot_sh 'cd /dev && /sbin/MAKEDEV ptmx'
633    $__chroot_sh 'cd /dev && /sbin/MAKEDEV null'
634    $__chroot_sh 'cd /dev && /sbin/MAKEDEV zero'
635    $__chroot_sh 'cd /dev && /sbin/MAKEDEV random'
636    $__chroot_sh 'cd /dev && /sbin/MAKEDEV urandom'
637    $__chroot_sh 'cd /dev && mkdir -p shm && chmod 777 shm'
638    $__chroot_sh 'cd /dev && mkdir -p pts && chmod 755 pts'
639
640    $__chroot_sh '/usr/sbin/pwconv'
641    $__chroot_sh "/usr/sbin/useradd ${BUILD_USER}"
642
643    ##!! for rpm-4.8.0 or higher
644    ##!! (See http://trac.vinelinux.org/wiki/Vine6/AboutUpdateToolchain)
645    if [ "$(echo ${VERSION} | sed -e "s/\(VineSeed\).*/\1/")" = "VineSeed" ]; then
646        $__chroot_sh "sed -i -e 's/^%_topdir/#%_topdir/' /home/${BUILD_USER}/.rpmmacros"
647    fi
648
649    ## output basic informations in chroot
650    write-vbuilder-log
651    write-vbuilder-log "build"
652
653    # mount-chroot --umount /home
654    # mount-chroot --umount /tmp
655    mount-chroot --umount /dev/shm
656    mount-chroot --umount /dev/pts
657    mount-chroot --umount archives_dir
658    mount-chroot --umount /proc
659    apt-get-update --host
660
661    echo $"Making a build farm for ${VERSION} done."
662    return 0
663}
664
665Show-Info(){
666    setup-vbootstrap || return 1
667
668    [ -f $VBUILDER_LOG ] && cat $VBUILDER_LOG
669
670    return 0
671}
672
673RPM_Remove(){
674    setup-vbootstrap-rpm || return 1
675    mount-chroot /proc
676    mount-chroot unionfs_dir
677    mount-chroot archives_dir
678    mount-chroot /dev/pts
679    mount-chroot /dev/shm
680    apt-get-update --chroot
681
682    [ -f $RPM_PKG ] && \
683        echo $"E: $RPM_PKG is not a package name" && return 1
684    $__chroot_sh "apt-get -y remove $BASE_RPM_PKG"
685
686    write-vbuilder-log "remove-rpm $RPM_PKG"
687
688    mount-chroot --umount /dev/shm
689    mount-chroot --umount /dev/pts
690    mount-chroot --umount archives_dir
691    mount-chroot --umount unionfs_dir
692    mount-chroot --umount /proc
693    apt-get-update --host
694
695    return 0
696}
697
698RPM_Install(){
699    setup-vbootstrap-rpm || return 1
700    mount-chroot /proc
701    mount-chroot unionfs_dir
702    mount-chroot archives_dir
703    mount-chroot /dev/pts
704    mount-chroot /dev/shm
705
706    apt-get-update --chroot
707
708    $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $BASE_RPM_PKG"
709
710    write-vbuilder-log "install-rpm $RPM_PKG"
711
712    mount-chroot --umount /dev/shm
713    mount-chroot --umount /dev/pts
714    mount-chroot --umount archives_dir
715    mount-chroot --umount unionfs_dir
716    mount-chroot --umount /proc
717    apt-get-update --host
718
719    return 0
720}
721
722RPM_Build(){
723    setup-vbootstrap-rpm || return 1
724    mount-chroot /proc
725    mount-chroot unionfs_dir
726    mount-chroot archives_dir
727    mount-chroot /dev/pts
728    mount-chroot /dev/shm
729    apt-get-update --chroot
730
731    [ ! -f $RPM_PKG ] && \
732        echo $"E: $RPM_PKG is not a source RPM package" && return 1
733   
734    RPM_PKG_USER=$(stat -c %U $RPM_PKG)
735    RPM_PKG_GROUP=$(stat -c %G $RPM_PKG)
736    [ ! -z "${SUDO_UID}" ] && RPM_PKG_USER=${SUDO_UID}
737    [ ! -z "${SUDO_GID}" ] && RPM_PKG_GROUP=${SUDO_GID}
738    local __install="install -p -v -o ${RPM_PKG_USER} -g ${RPM_PKG_GROUP}"
739
740    ## make src.rpm for $VERSION
741    $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpm -ivh $BASE_RPM_PKG'"
742    $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpmbuild -bs --nodeps --clean --rmsource --rmspec $RPM_OPTS ${BUILD_DIR}/SPECS/*.spec'"
743
744
745    ## change ${DIST_RELEASE}
746    BASE_RPM_PKG=$(echo $BASE_RPM_PKG | sed -e "s/vl[0-9]*\([A-Za-z]*\)\./vl${DIST_RELEASE}\1\./")
747
748    ## rebuild $BASE_RPM_PKG on ${DIST_RELEASE}
749    $__chroot_sh "cd ${BUILD_DIR}/SRPMS && apt-get -o APT::Install::Virtual=true -y build-dep $BASE_RPM_PKG"
750    $__chroot_sh "cd ${BUILD_DIR}/SRPMS && su ${BUILD_USER} -c 'rpmbuild --rebuild $RPM_OPTS $BASE_RPM_PKG'"
751    [ $with_no_install -eq 0 ] && \
752        $__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')"
753
754    ## copy built rpms to ${HOME}/rpm/ for each archtectures
755    echo $"Copying built rpms to ${BUILT_RPMS_DIR} for each archtectures ... "
756    for i in $RPM_PKG_ARCH_LIST; do \
757        if [ -d $BUILD_ROOT${BUILD_DIR}/${i} ]; then
758            if [ ! -d ${BUILT_RPMS_DIR}/${MAJOR_VERSION}/${i} ]; then
759                $__install -d ${BUILT_RPMS_DIR}/${MAJOR_VERSION}/${i}/
760                chown -R ${RPM_PKG_USER}:${RPM_PKG_GROUP} ${BUILT_RPMS_DIR}
761            fi
762            find $BUILD_ROOT${BUILD_DIR}/${i} -type f -regex '.*\.rpm' \
763                -exec $__install -m0644 {} ${BUILT_RPMS_DIR}/${MAJOR_VERSION}/${i}/ \;
764        fi
765    done
766
767    write-vbuilder-log "build-rpm $RPM_PKG"
768
769    mount-chroot --umount /dev/shm
770    mount-chroot --umount /dev/pts
771    mount-chroot --umount archives_dir
772    mount-chroot --umount unionfs_dir
773    mount-chroot --umount /proc
774    apt-get-update --host
775
776    echo $"done."
777    return 0
778}
779
780RPM_Sign(){
781    [ $with_sign -eq 1 ] || return 1
782
783    ## check $SUDO_USER and $USERHELPER_UID
784    local RPM_SIGN_USER=$SUDO_USER
785    if [ -z "${RPM_SIGN_USER}" ]; then
786        RPM_SIGN_USER=$(user_from_uid $USERHELPER_UID)
787        if [ -z "${RPM_SIGN_USER}" ]; then
788            echo $"W: \$SUDO_USER and \$USERHELPER_UID are empty"
789            return 1
790        fi
791    fi
792
793    setup-vbootstrap || return 1
794
795    [ -d ${BUILD_ROOT} ] || Build
796
797    mount-chroot unionfs_dir
798
799    echo $"Signing built rpms using ${RPM_SIGN_USER}'s key: "
800    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')"
801
802    mount-chroot --umount unionfs_dir
803
804    return 0
805}
806
807Login_Chroot(){
808    [ $with_login -eq 1 ] || return 1
809
810    setup-vbootstrap || return 1
811
812    __chroot="/usr/sbin/chroot ${BUILD_ROOT}"
813    ## setarch ix86 if ix86 chroot on x86_64 host
814    [ $with_ix86_on_x86_64 -eq 1 ] && \
815        __chroot="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH}"
816
817
818    mount-chroot /proc
819    mount-chroot unionfs_dir
820    mount-chroot archives_dir
821    mount-chroot /dev/pts
822    mount-chroot /dev/shm
823    mount-chroot /tmp
824    # mount-chroot /home
825    apt-get-update --chroot
826
827    ## copy host's configurations of /etc
828    #passwd shadow group gshadow
829    for i in resolv.conf hosts; do
830        cp -pf /etc/${i} ${BUILD_ROOT}/etc
831    done
832    cp -Ppf /etc/localtime ${BUILD_ROOT}/etc
833
834    $__chroot /bin/bash || return 1
835
836    # mount-chroot  --umount /home
837    mount-chroot  --umount /tmp
838    mount-chroot --umount /dev/shm
839    mount-chroot --umount /dev/pts
840    mount-chroot --umount archives_dir
841    mount-chroot --umount unionfs_dir
842    mount-chroot --umount /proc
843    apt-get-update --host
844
845    return 0
846}
847
848
849##############################################################################
850
851setup-vbuilder || exit 1
852
853check-parameter $* || exit 1
854
855while [ $# -gt 0 ]; do
856    tmpARG=$1
857    case $tmpARG in
858        --version|--arch|--category|--target|--rpmbuild-define|--rpmbuild-with|--rpmbuild-without|--bootstrap-dir|--unionfs-dir|--cache-dir|--built-rpms-dir)
859            shift
860            ;;
861        --dist-upgrade|--unionfs|--with-compat32|--sign|--no-install|--login|--debug)
862            ;;
863        --build-rpm|build-rpm|--install-rpm|install-rpm|--remove-rpm|remove-rpm)
864            shift
865            ;;
866        --build|build|--clean|clean|--show-info|show-info)
867            ;;
868        *)
869            echo unknown option $1
870            exit 1
871            ;;
872    esac
873
874    case $tmpARG in
875        --version)
876            VERSION=$1
877            ;;
878        --arch)
879            VARCH=$1
880            ;;
881        --category)
882            CATEGORIES=$1
883            ;;
884        --dist-upgrade)
885            with_dist_upgrade=1
886            ;;
887        --unionfs)
888            with_unionfs=1
889            ;;
890        --target)
891            TARGET=$1
892            RPM_OPTS="${RPM_OPTS} --target $TARGET"
893            ;;
894        --with-compat32)
895            RPM_OPTS="${RPM_OPTS} --with compat32"
896            ;;
897        --rpmbuild-define)
898            RPM_OPTS="${RPM_OPTS} --define $1"
899            ;;
900        --rpmbuild-with)
901            RPM_OPTS="${RPM_OPTS} --with $1"
902            ;;
903        --rpmbuild-without)
904            RPM_OPTS="${RPM_OPTS} --without $1"
905            ;;
906        --sign)
907            with_sign=1
908            ;;
909        --no-install)
910            with_no_install=1
911            ;;
912        --login)
913            with_login=1
914            ;;
915        --bootstrap-dir)
916            VBOOTSTRAP_DIR=$1
917            ;;
918        --unionfs-dir)
919            UNIONFS_DIR=$1
920            ;;
921        --cache-dir)
922            CACHE_DIR=$1
923            ;;
924        --built-rpms-dir)
925            BUILT_RPMS_DIR=$1
926            ;;
927        --debug)
928            with_debug=1
929            ;;
930        --build-rpm|build-rpm)
931            RPM_PKG=$1
932            RPM_Build || exit 1
933            ;;
934        --install-rpm|install-rpm)
935            RPM_PKG=$1
936            RPM_Install || exit 1
937            ;;
938        --remove-rpm|remove-rpm)
939            RPM_PKG=$1
940            RPM_Remove || exit 1
941            ;;
942        --show-info|show-info)
943            Show-Info || exit 1
944            ;;
945        --build|build)
946            Build || exit 1
947            ;;
948        --clean|clean)
949            Clean || exit 1
950            ;;
951    esac
952    shift
953done
954
955RPM_Sign
956
957Login_Chroot
958
959exit
Note: See TracBrowser for help on using the repository browser.