source: projects/vbootstrap/trunk/libvbuilder.sh.in @ 5513

Revision 5513, 13.0 KB checked in by daisuke, 12 years ago (diff)

fix unmounting aufs

  • Property svn:executable set to *
Line 
1#!/bin/bash
2
3initialize-variables(){
4    ## set boolian variables
5    with_profile=0
6    with_setup_vbootstrap=0
7    with_dist_upgrade=0
8    with_unionfs=0
9    with_sign=0
10    with_no_install=0
11    with_login=0
12    with_debug=0
13    with_actions=0
14    with_ix86_on_x86_64=0
15    with_category_main=0
16    with_category_plus=0
17    with_category_nonfree=0
18    with_category_test=0
19    with_category_proposed_updates=0
20    with_category_security=0
21
22    return 0
23}
24
25check-parameter(){
26    [ -z "$*" ] && Usage && return 1
27
28    while [ ! -z "$*" ]; do
29        case $1 in
30            --help|help)
31                setup-vbuilder ||:
32                Usage
33                return 1
34                ;;
35            --profile)
36                shift
37                with_profile=1
38                PROFILE=$1
39                check-next-parameter $1 || return 1
40                ;;
41            --version|--arch|--category|--fetch-url|--target|--rpmbuild-define|--rpmbuild-with|--rpmbuild-without|--bootstrap-dir|--unionfs-dir|--cache-dir|--built-rpms-dir)
42                [ $with_actions -eq 1 ] && \
43                    echo $"E: You can give no more options after actions" && \
44                    return 1
45                shift
46                check-next-parameter $1 || return 1
47                ;;
48            --dist-upgrade|--unionfs|--with-compat32|--sign|--no-install|--login|--debug)
49                [ $with_actions -eq 1 ] && \
50                    echo $"E: You can give no more options after actions" && \
51                    return 1
52                ;;
53            --build-rpm|build-rpm|--install-rpm|install-rpm|--remove-rpm|remove-rpm)
54                with_actions=1
55                shift
56                check-next-parameter $1 || return 1
57                ;;
58            --build|build|--clean|clean)
59                with_actions=1
60                ;;
61            *)
62                echo $"E: Missing some parameters after $1"
63                return 1
64                ;;
65        esac
66        shift
67    done
68
69    [ $with_actions -eq 0 ] && \
70        echo $"E: You must give at least one action" && return 1
71
72    return 0
73}
74
75check-next-parameter(){
76    [ -z "$1" ] && echo $"E: Missing some parameters after $1" && return 1
77
78    [ $(echo $1 | grep '^-') ] && \
79        echo $"E: Missing some parameters after $1" && return 1
80
81    return 0
82}
83
84## NOTE: setup-vbuilder() loads
85##  - system wide configurations (from /etc/vbootstrap/vbuilder.conf)
86##  - given profile (from /etc/vbootstrap/profile.d/*.conf)
87##  - given command-line parameters
88## where given command-line parameters override the settings given profile,
89## and the settings given profile override system wide settings.
90## If you use a profile of vbuilder, you may not override the settings
91## given profile.
92setup-vbuilder(){
93    ## check $SUDO_USER and $USERHELPER_UID
94    RPM_SIGN_USER=$SUDO_USER
95    if [ -z "${RPM_SIGN_USER}" ]; then
96        [ -z "${USERHELPER_UID}" ] && \
97            echo $"W: \$SUDO_USER and \$USERHELPER_UID are empty" && \
98            return 1
99           
100        RPM_SIGN_USER=$(getent passwd $USERHELPER_UID | cut -d":" -f1)
101    fi
102    ## get $RPM_SIGN_USER's home directory
103    HOME=$(getent passwd $RPM_SIGN_USER |  cut -d":" -f6)
104
105    ## load default settings
106    VBUILDER_CONF=/etc/vbootstrap/vbuilder.conf
107    if [ -r $VBUILDER_CONF ]; then
108        . $VBUILDER_CONF
109        [ $? -eq 1 ] && return 1
110    fi
111    [ -z "${CATEGORIES}" ] && \
112        CATEGORIES=@@VBUILDER_CATEGORIES@@
113    [ -z "${VBOOTSTRAP_FETCH_URL}" ] && \
114        VBOOTSTRAP_FETCH_URL=@@VBUILDER_VBOOTSTRAP_FETCH_URL@@
115    [ -z "${VBOOTSTRAP_DIR}" ] && \
116        VBOOTSTRAP_DIR=@@VBUILDER_VBOOTSTRAP_DIR@@
117    [ -z "${UNIONFS_DIR}" ] && \
118        UNIONFS_DIR=@@VBUILDER_UNIONFS_DIR@@
119    [ -z "${CACHE_DIR}" ] && \
120        CACHE_DIR=@@VBUILDER_CACHE_DIR@@
121    [ -z "${BUILT_RPMS_DIR}" ] && \
122        BUILT_RPMS_DIR=@@VBUILDER_BUILT_RPMS_DIR@@
123
124    ## set default version for vbootstrap
125    VERSION=@@VBUILDER_DEFAULT_VERSION@@
126
127    ## load profile
128    if [ ${with_profile} -eq 1 ]; then
129        [ ! -r /etc/vbootstrap/profile.d/${PROFILE}.conf ] && \
130            echo $"E: No such profile found: ${PROFILE}" && return 1
131
132        . /etc/vbootstrap/profile.d/${PROFILE}.conf
133        [ $? -eq 1 ] && return 1
134    fi
135
136    ## set current stable relase version
137    STABLE_VERSION=@@VBUILDER_STABLE_VERSION@@
138
139    ## set default chroot archtecture
140    UARCH=$(uname -i)
141    case "${UARCH}" in
142        arm*)
143            UARCH="arm"
144            ;;
145    esac
146
147    return 0
148}
149
150setup-vbootstrap(){
151    if [ ${with_setup_vbootstrap} -eq 0 ]; then
152        with_setup_vbootstrap=1
153
154        ## check debug mode
155        [ ${with_debug} -eq 1 ] && \
156            cat $VBUILDER_CONF && \
157            set && set -x
158
159        ## check some directories
160        ## Note: create $BUILT_RPMS_DIR in RPM_Build()
161        [ -d $VBOOTSTRAP_DIR ] || mkdir -p $VBOOTSTRAP_DIR
162        [ -d $CACHE_DIR ] || mkdir -p $CACHE_DIR
163
164        ## check chroot version
165        case ${VERSION} in
166            4.2|5.2|6.0|VineSeed)
167                ;;
168            *)
169                echo $"E: ${VERSION} is NOT supported"
170                return 1
171                ;;
172        esac
173
174        case "${VARCH}" in
175            arm*)
176                VARCH="arm"
177                ;;
178        esac
179
180        ## check a chroot archtecture
181        if [ -z ${VARCH} ]; then
182            VARCH=${UARCH}
183        else
184            case "${VARCH}" in
185                i386|x86_64)
186                    [ "${UARCH}" = "ppc" -o "${UARCH}" = "arm" ] && \
187                        echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
188                    ;;
189                ppc)
190                    [ "${UARCH}" = "i386" -o "${UARCH}" = "x86_64" -o "${UARCH}" = "arm" ] && \
191                        echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
192                    ;;
193                arm)
194                    [ "${UARCH}" = "i386" -o "${UARCH}" = "x86_64" -o "${UARCH}" = "ppc" ] && \
195                        echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
196                    ;;
197            esac
198        fi
199
200        ##!! 4.2 is NO support on VARCH=x86_64 or VARCH=arch
201        if [ "${VERSION}" = "4.2" ]; then
202            if [ "${VARCH}" = "x86_64" -o "${VARCH}" = "arm" ]; then
203                echo $"E: ${VERSION}_${VARCH} is NOT supported"
204                return 1
205            fi
206        fi
207
208        ## set base profile <version>_<arch>
209        BASE_PROFILE=${VERSION}_${VARCH}
210
211        ## check support ${BASE_PROFILE}
212        if [ -z "$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*// | grep -m 1 ${BASE_PROFILE})" ]; then
213            echo $"E: ${BASE_PROFILE} is NOT supported"
214            return 1
215        fi
216
217        ## check ${VERSION} equals VineSeed*, when with_dist_upgrade=1
218        if [ $with_dist_upgrade -eq 1 ]; then
219            if [ "${VERSION}" != "VineSeed" ]; then
220                echo $"E: version ${VERSION} does not support --dist-upgrade option"
221                return 1
222            fi
223        fi
224
225        ## support i386 chroot on x86_64 below:
226        [ "${UARCH}" = "x86_64" -a "${VARCH}" = "i386" ] && \
227            with_ix86_on_x86_64=1
228
229        ## check apt categories
230        ## "main" category is unconditionally permited
231        with_category_main=1
232        for cat in $(echo ${CATEGORIES} | sed -e "s/,/ /"g); do
233            case $cat in
234                main)
235                    with_category_main=1
236                    ;;
237                plus)
238                    with_category_plus=1
239                    ;;
240                nonfree)
241                    with_category_nonfree=1
242                    ;;
243                test)
244                    ## "test" category only exists in VineSeed
245                    [ "${VERSION}" = "VineSeed" ] || \
246                        echo $"E: No such category exists: $cat" && return 1
247                    with_category_test=1
248                    ;;
249                proposed-updates)
250                    ##!! "proposed-updates" category does not exist in 4.2
251                    [ "${VERSION}" = "4.2" ] && \
252                        echo $"E: No such category exists: $cat" && return 1
253
254                    with_category_proposed_updates=1
255                    ;;
256                security)
257                    ## "security" category does not exist in VineSeed
258                    [ "${VERSION}" = "VineSeed" ] && \
259                        echo $"E: No such category exists: $cat" && return 1
260                    with_category_security=1
261                    ;;
262                *)
263                    echo $"E: No such category exists: $cat" && return 1
264                    ;;
265            esac
266        done
267
268        ## check build target option ${TARGET}
269        if [ ! -z "${TARGET}" ]; then
270            RPM_TARGET_LIST="$(cat /usr/lib/rpm/rpmrc | grep arch_canon: | sed -e "s/arch_canon:[[:blank:]]*\(.*\):.*/\1/")"
271            RPM_TARGET_LIST="${RPM_TARGET_LIST} noarch"
272            if [ -z "$(echo ${RPM_TARGET_LIST} | grep ${TARGET})" ]; then
273                echo $"E: rpm build target ${TARGET} is NOT supported"
274                return 1
275            fi
276        fi
277
278        ## set ${RPM_PKG_ARCH_LIST}
279        RPM_PKG_ARCH_LIST=" \
280            RPMS/i386 RPMS/i486 RPMS/i586 RPMS/i686 RPMS/x86_64 RPMS/ppc \
281            RPMS/noarch \
282            RPMS/armv3l RPMS/armv4l RPMS/armv4tl \
283            RPMS/armv5tejl RPMS/armv5tel RPMS/armv6l RPMS/armv7l \
284            SRPMS"
285        [ -z "${TARGET}" ] || \
286            RPM_PKG_ARCH_LIST="RPMS/${TARGET} ${RPM_PKG_ARCH_LIST}"
287
288    fi
289
290    ## set global variables
291    BUILD_USER=vbuilder
292    BUILD_DIR=/home/${BUILD_USER}/rpm
293    if [ ${with_profile} -eq 1 ]; then
294        BUILD_ROOT=${VBOOTSTRAP_DIR}/${PROFILE}
295        UNIONFS_ROOT=${UNIONFS_DIR}/${PROFILE}
296    else
297        BUILD_ROOT=${VBOOTSTRAP_DIR}/${BASE_PROFILE}
298        UNIONFS_ROOT=${UNIONFS_DIR}/${BASE_PROFILE}
299    fi
300    ARCHIVES_DIR=${BUILD_ROOT}/var/cache/apt/archives
301    EXTERNAL_ARCHIVES_DIR=${CACHE_DIR}/${BASE_PROFILE}/apt/archives
302
303    __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c -l"
304
305    mkdir -p $VBOOTSTRAP_DIR
306
307    return 0
308}
309
310setup-vbootstrap-rpm(){
311    setup-vbootstrap || return 1
312
313    ## check ${BUILD_ROOT}
314    [ -d ${BUILD_ROOT} ] || Build
315    ## setarch ix86 if ix86 chroot on x86_64 host
316    [ $with_ix86_on_x86_64 -eq 1 ] && \
317        __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH} /bin/sh -c -l"
318
319    DIST_RELEASE=$(cat ${BUILD_ROOT}/etc/vine-release | cut -f3 -d" " | cut -f1 -d.)
320
321    if [ -f $RPM_PKG ]; then
322        BASE_RPM_PKG=$(basename $RPM_PKG)
323        cp -f $RPM_PKG $BUILD_ROOT${BUILD_DIR}
324    else
325        BASE_RPM_PKG=$RPM_PKG   
326    fi
327
328    return 0
329}
330
331## recover apt-get data on host/chroot
332apt-get-update(){
333    case $1 in
334        --host)
335            echo -n $"apt-get update on host ... "
336            apt-get -qq update > /dev/null 2>&1
337            echo $"done."
338            ;;
339        --chroot)
340            echo -n $"apt-get update on chroot ... "
341            $__chroot_sh 'apt-get -qq update' > /dev/null 2>&1
342            echo $"done."
343            ;;
344        *)
345            echo apt-get-update: unknown option $1
346            exit 1
347            ;;
348    esac
349}
350
351## mount-chroot {|--umount} [file system|name]
352## support file systems: /home /tmp /sys /proc /dev/shm /dev/pts /dev
353## support names: vfs archives_dir
354## NOTE: /tmp needs for applications which use X
355##       vfs is virtual file systems
356##       archives_dir uses to mount ${EXTERNAL_ARCHIVES_DIR} to ${ARCHIVES_DIR}
357##       unionfs_dir covers ${BUILD_ROOT} with unionfs
358mount-chroot(){
359    if [ "$1" = "--umount" ]; then
360        mount-chroot-umount $2 || return 1
361    else
362        mount-chroot-mount $1 || return 1
363    fi
364    return 0
365}
366
367## mount-chroot-umount [file system|name]
368mount-chroot-umount(){
369    local fs=$1
370    case $fs in
371        /home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
372            [ -d ${BUILD_ROOT}${fs} ] || return 1
373            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] || \
374                umount ${BUILD_ROOT}${fs}
375            if [ ! -z "$(mount | grep ${BUILD_ROOT}${fs})" ]; then
376                echo $"Retry lazy unmount ${BUILD_ROOT}${fs} ... "
377                umount -l ${BUILD_ROOT}${fs}
378                echo $"done."
379            fi
380            ;;
381        vfs)
382            for dir in /sys /proc /dev/shm /dev/pts /dev; do
383                mount-chroot-umount ${dir} || return 1
384            done
385            ;;
386        archives_dir)
387            [ -d ${ARCHIVES_DIR} ] || return 1
388            [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
389                umount ${ARCHIVES_DIR}
390            ;;
391        unionfs_dir)
392            [ -d ${BUILD_ROOT} ] || return 1
393            [ -z "$(mount | grep ${BUILD_ROOT} | egrep '(unionfs|aufs)')" ] || \
394                umount ${BUILD_ROOT}
395            if [ ! -z "$(mount | grep ${BUILD_ROOT} | egrep '(unionfs|aufs)')" ]; then
396                echo $"Retry lazy unmount ${BUILD_ROOT} ... "
397                umount -l ${BUILD_ROOT}
398                echo $"done."
399            fi
400            ;;
401        *)
402            echo mount-chroot-umount: unknown file system $fs
403            exit 1
404            ;;
405    esac
406    return 0
407}
408
409## mount-chroot-mount [file system|name]
410mount-chroot-mount(){
411    local fs=$1
412
413    case $fs in
414        /home)
415            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
416            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
417                mount -o _netdev,rbind ${fs} ${BUILD_ROOT}${fs}
418            ;;
419        /tmp|/dev)
420            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
421            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
422                mount --bind -o _netdev ${fs} ${BUILD_ROOT}${fs}
423            ;;
424        /sys)
425            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
426            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
427                mount -o _netdev -t sysfs vbuildersysfs ${BUILD_ROOT}${fs}
428            ;;
429        /proc)
430            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
431            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
432                mount -o _netdev -t proc vbuilderproc ${BUILD_ROOT}${fs}
433            ;;
434        /dev/shm)
435            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
436            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
437                mount -o _netdev -t tmpfs vbuildertmpfs ${BUILD_ROOT}${fs}
438            ;;
439        /dev/pts)
440            [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
441            [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
442                mount -o gid=5,mode=620,_netdev -t devpts vbuilderdevpts ${BUILD_ROOT}${fs}
443            ;;
444        vfs)
445            for dir in /dev /dev/pts /dev/shm /proc /sys; do
446                mount-chroot-mount ${dir} || return 1
447            done
448            ;;
449        archives_dir)
450            [ -d ${EXTERNAL_ARCHIVES_DIR} ] || mkdir -p ${EXTERNAL_ARCHIVES_DIR}
451            [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
452            [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
453                mount --bind -o _netdev ${EXTERNAL_ARCHIVES_DIR} ${ARCHIVES_DIR}
454            [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
455            ;;
456        unionfs_dir)
457            if [ $with_unionfs -eq 1 ]; then
458                [ -d ${UNIONFS_ROOT} ] || mkdir -p ${UNIONFS_ROOT}
459                if ( /sbin/modprobe aufs >& /dev/null ) ; then
460                    [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
461                        mount -t aufs -o br=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro aufs ${BUILD_ROOT}
462                else
463                    [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
464                        mount -t unionfs -o dirs=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro unionfs ${BUILD_ROOT}
465                    unionctl ${BUILD_ROOT} --list
466                fi
467            fi
468            ;;
469        *)
470            echo mount-chroot-mount: unknown file system $fs
471            exit 1
472            ;;
473    esac
474    return 0
475}
476
477### end of file
Note: See TracBrowser for help on using the repository browser.