Browse Source

added vchroot.sh.in, libvbuilder.sh.in: vchroot is experimental

git-svn-id: http://trac.vinelinux.org/repos/projects/vbootstrap/trunk@2629 ec354946-7b23-47d6-9f5a-488ba84defc7
munepi 12 years ago
parent
commit
3fd73aa9c8
3 changed files with 495 additions and 0 deletions
  1. 17 0
      Makefile
  2. 359 0
      libvbuilder.sh.in
  3. 119 0
      vchroot.sh.in

+ 17 - 0
Makefile

@@ -29,6 +29,16 @@ all:
 	    -e "s|@@VBUILDER_BUILT_RPMS_DIR@@|${VBUILDER_BUILT_RPMS_DIR}|g" \
 		vbuilder.conf.in > vbuilder.conf
 
+	##!! experimental: vchroot
+	sed -e "s/@@VBUILDER_STABLE_VERSION@@/${VBUILDER_STABLE_VERSION}/g" \
+	    -e "s/@@VBUILDER_DEFAULT_VERSION@@/${VBUILDER_DEFAULT_VERSION}/g" \
+	    -e "s/@@VBUILDER_CATEGORIES@@/${VBUILDER_CATEGORIES}/g" \
+	    -e "s|@@VBUILDER_VBOOTSTRAP_DIR@@|${VBUILDER_VBOOTSTRAP_DIR}|g" \
+	    -e "s|@@VBUILDER_CACHE_DIR@@|${VBUILDER_CACHE_DIR}|g" \
+	    -e "s|@@VBUILDER_BUILT_RPMS_DIR@@|${VBUILDER_BUILT_RPMS_DIR}|g" \
+		libvbuilder.sh.in > libvbuilder.sh
+	cp -f vchroot.sh.in vchroot.sh
+
 	cd po && make
 
 install:
@@ -52,6 +62,10 @@ install:
 	install -m 644 vbuilder.pamd ${DESTDIR}/etc/pam.d/vbuilder
 	install -m 644 vbuilder-bash-completion.sh ${DESTDIR}/etc/bash_completion.d/vbuilder
 
+	##!! experimental: vchroot
+	install -m 644 libvbuilder.sh ${DESTDIR}/usr/share/vbootstrap/libvbuilder.sh
+	install -m 755 vchroot.sh ${DESTDIR}/usr/sbin/vchroot
+
 	cd po && make install DESTDIR=${DESTDIR}
 
 tarbz2: clean
@@ -72,4 +86,7 @@ clean:
 	rm -fr *~ .*???~ vbootstrap.sh vbuilder.sh vbuilder.conf
 	find . -name *~ -exec rm -f {} \;
 
+	##!! experimental: vchroot
+	rm -f vchroot.sh libvbuilder.sh
+
 	cd po && make clean

+ 359 - 0
libvbuilder.sh.in

@@ -0,0 +1,359 @@
+#!/bin/bash
+
+setup-vbuilder(){
+    ## load default settings
+    VBUILDER_CONF=/etc/vbootstrap/vbuilder.conf
+    if [ -r $VBUILDER_CONF ]; then
+	. $VBUILDER_CONF
+	[ $? -eq 1 ] && return 1
+    fi
+    [ -z "${DEFAULT_VERSION}" ] && \
+	DEFAULT_VERSION=@@VBUILDER_DEFAULT_VERSION@@
+    [ -z "${CATEGORIES}" ] && \
+	CATEGORIES=@@VBUILDER_CATEGORIES@@
+    [ -z "${VBOOTSTRAP_DIR}" ] && \
+	VBOOTSTRAP_DIR=@@VBUILDER_VBOOTSTRAP_DIR@@
+    [ -z "${CACHE_DIR}" ] && \
+	CACHE_DIR=@@VBUILDER_CACHE_DIR@@
+    [ -z "${BUILT_RPMS_DIR}" ] && \
+	BUILT_RPMS_DIR=@@VBUILDER_BUILT_RPMS_DIR@@
+
+    ## set default version for vbootstrap
+    VERSION=$DEFAULT_VERSION
+
+    ## set current stable relase version
+    STABLE_VERSION=@@VBUILDER_STABLE_VERSION@@
+
+    ## set boolian
+    with_setup_vbootstrap=0
+    with_dist_upgrade=0
+    with_unionfs=0
+    with_sign=0
+    with_no_install=0
+    with_actions=0
+    with_ix86_on_x86_64=0
+    with_category_main=0
+    with_category_plus=0
+    with_category_nonfree=0
+    with_category_test=0
+    with_category_proposed_updates=0
+    with_category_security=0
+
+    return 0
+}
+
+setup-vbootstrap(){
+    if [ ${with_setup_vbootstrap} -eq 0 ]; then
+	with_setup_vbootstrap=1
+
+	## check some directories
+	## Note: create $BUILT_RPMS_DIR in RPM_Build()
+	[ -d $VBOOTSTRAP_DIR ] || mkdir -p $VBOOTSTRAP_DIR
+	[ -d $CACHE_DIR ] || mkdir -p $CACHE_DIR
+
+	## check a chroot archtecture
+	if [ -z ${VARCH} ]; then
+	    VARCH=$(uname -i)
+	else
+	    case "${VARCH}" in
+		i386|i686|x86_64)
+		    [ "$(uname -i)" = "ppc" ] && \
+			echo $"E: arch ${VARCH} is NOT supported on $(uname -i)" && return 1
+		    ;;
+		ppc)
+		    [ "$(uname -i)" = "i386" -o "$(uname -i)" = "i686" -o "$(uname -i)" = "x86_64" ] && \
+			echo $"E: arch ${VARCH} is NOT supported on $(uname -i)" && return 1
+		    ;;
+	    esac
+	fi
+
+        ##!! 4.2 is NO support on VARCH=x86_64
+	if [ "${VERSION}" = "4.2" -a "${VARCH}" = "x86_64" ]; then
+	    echo $"E: ${VERSION} is NOT supported"
+	    return 1
+	fi
+
+	## support i386 chroot on x86_64 below: 
+	[ "${VARCH}" != "$(uname -i)" ] && \
+	    VERSION=${VERSION}_${VARCH} && \
+	    with_ix86_on_x86_64=1
+
+        ## check support ${VERSION}
+	if [ -z "$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*// | grep -m 1 ${VERSION})" ]; then
+	    echo $"E: ${VERSION} is NOT supported"
+	    return 1
+	fi
+
+	## check ${VERSION} equals VineSeed*, when with_dist_upgrade=1
+	if [ $with_dist_upgrade -eq 1 ]; then
+	    if [ "$(echo ${VERSION} | sed -e "s/\(VineSeed\).*/\1/")" != "VineSeed" ]; then 
+		echo $"E: version ${VERSION} does not support --dist-upgrade option"
+		return 1
+	    fi
+	fi
+
+	## set ${MAJOR_VERSION}
+	MAJOR_VERSION=$(echo ${VERSION} | sed -e "s/_i[0-9]86//")
+
+	## check apt categories
+	## "main" category is unconditionally permited
+	with_category_main=1
+	for cat in $(echo ${CATEGORIES} | sed -e "s/,/ /"g); do
+	    case $cat in
+		main)
+		    with_category_main=1
+		    ;;
+		plus)
+		    with_category_plus=1
+		    ;;
+		nonfree)
+		    with_category_nonfree=1
+		    ;;
+		test)
+                    ## "test" category only exists in VineSeed
+		    [ "${MAJOR_VERSION}" = "VineSeed" ] || \
+			echo $"E: No such category exists: $cat" && return 1
+		    with_category_test=1
+		    ;;
+		proposed-updates)
+                    ##!! "proposed-updates" category does not exist in 4.2
+		    [ "${MAJOR_VERSION}" = "4.2" ] && \
+			echo $"E: No such category exists: $cat" && return 1
+
+		    with_category_proposed_updates=1
+		    ;;
+		security)
+                    ## "security" category does not exist in VineSeed
+		    [ "${MAJOR_VERSION}" = "VineSeed" ] && \
+			echo $"E: No such category exists: $cat" && return 1
+		    with_category_security=1
+		    ;;
+		*)
+		    echo $"E: No such category exists: $cat" && return 1
+		    ;;
+	    esac
+	done
+
+	## check build target option ${TARGET}
+	if [ ! -z "${TARGET}" ]; then
+	    RPM_TARGET_LIST="$(cat /usr/lib/rpm/rpmrc | grep arch_canon: | sed -e "s/arch_canon:[[:blank:]]*\(.*\):.*/\1/")"
+	    if [ -z "$(echo $RPM_TARGET_LIST | grep $TARGET)" ]; then
+		echo $"E: rpm build target ${TARGET} is NOT supported"
+		return 1
+	    fi
+	fi
+
+	## set ${RPM_PKG_ARCH_LIST}
+	RPM_PKG_ARCH_LIST="RPMS/i386 RPMS/i686 RPMS/x86_64 RPMS/ppc RPMS/noarch SRPMS"
+	[ -z "${TARGET}" ] || \
+	    RPM_PKG_ARCH_LIST="RPMS/${TARGET} ${RPM_PKG_ARCH_LIST}"
+
+    fi
+
+    ## set global variables
+    BUILD_ROOT=${VBOOTSTRAP_DIR}/${VERSION}
+    BUILD_USER=vbuilder
+    BUILD_DIR=/home/${BUILD_USER}/rpm
+    UNIONFS_DIR=${VBOOTSTRAP_DIR}/unionfs/${VERSION}
+    ARCHIVES_DIR=${BUILD_ROOT}/var/cache/apt/archives
+    EXTERNAL_ARCHIVES_DIR=${CACHE_DIR}/${VERSION}/apt/archives
+    VBUILDER_LOG=${BUILD_ROOT}/var/log/vbuilder.log
+
+    __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c -l"
+
+    mkdir -p $VBOOTSTRAP_DIR
+
+    return 0
+}
+
+setup-vbootstrap-rpm(){
+    setup-vbootstrap || return 1
+
+    ## check ${BUILD_ROOT}
+    [ -d ${BUILD_ROOT} ] || Build
+    ## setarch ix86 if ix86 chroot on x86_64 host
+    [ $with_ix86_on_x86_64 -eq 1 ] && \
+	__chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH} /bin/sh -c -l"
+
+    DIST_RELEASE=$(cat ${BUILD_ROOT}/etc/vine-release | cut -f3 -d" " | cut -f1 -d.)
+
+    if [ -f $RPM_PKG ]; then
+	BASE_RPM_PKG=$(basename $RPM_PKG)
+	cp -f $RPM_PKG $BUILD_ROOT${BUILD_DIR}
+    else
+	BASE_RPM_PKG=$RPM_PKG	
+    fi
+
+    return 0
+}
+
+## recover apt-get data on host/chroot
+apt-get-update(){
+    case $1 in
+	--host)
+	    echo -n $"apt-get update on host ... "
+	    apt-get -qq update > /dev/null 2>&1
+	    echo $"done."
+	    ;;
+	--chroot)
+	    echo -n $"apt-get update on chroot ... "
+	    $__chroot_sh 'apt-get -qq update' > /dev/null 2>&1
+	    echo $"done."
+	    ;;
+	*)
+	    echo apt-get-update: unknown option $1
+	    exit 1
+	    ;;
+    esac
+}
+
+## mount-chroot {|--umount} [file system|name]
+## support file systems: /home /tmp /sys /proc /dev/shm /dev/pts /dev
+## support name: vfs archives_dir
+## NOTE: /tmp needs for applications which use X
+##       vfs is virtual file systems
+##       archives_dir uses to mount ${EXTERNAL_ARCHIVES_DIR} to ${ARCHIVES_DIR}
+##       unionfs_dir covers ${BUILD_ROOT} with unionfs
+mount-chroot(){
+    if [ "$1" = "--umount" ]; then
+	mount-chroot-umount $2 || return 1
+    else
+	mount-chroot-mount $1 || return 1
+    fi
+    return 0
+}
+
+## mount-chroot-umount [file system|name]
+mount-chroot-umount(){
+    local fs=$1
+    case $fs in
+	/home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
+	    [ -d ${BUILD_ROOT}${fs} ] || return 1
+	    [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] || \
+    		umount ${BUILD_ROOT}${fs}
+	    if [ ! -z "$(mount | grep ${BUILD_ROOT}${fs})" ]; then
+		echo $"Retry lazy unmount ... "
+		umount -l ${BUILD_ROOT}${fs}
+		echo $"done."
+	    fi
+	    ;;
+	vfs)
+	    for dir in /sys /proc /dev/shm /dev/pts /dev; do
+	    	mount-chroot-umount ${dir} || return 1
+	    done
+	    # [ -d ${BUILD_ROOT}/proc ] || return 1
+	    # [ -z "$(mount | grep ${BUILD_ROOT}/proc)" ] || \
+	    # 	umount ${BUILD_ROOT}/proc
+	    ;;
+	archives_dir)
+	    [ -d ${ARCHIVES_DIR} ] || return 1
+	    [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
+    		umount ${ARCHIVES_DIR}
+	    ;;
+	unionfs_dir)
+	    [ -d ${BUILD_ROOT} ] || return 1
+	    [ -z "$(mount | grep ${BUILD_ROOT} | grep unionfs)" ] || \
+		umount ${BUILD_ROOT}
+	    if [ ! -z "$(mount | grep ${BUILD_ROOT} | grep unionfs)" ]; then
+		echo $"Retry lazy unmount ... "
+		umount -l ${BUILD_ROOT}
+		echo $"done."
+	    fi
+	    ;;
+	*)
+	    echo mount-chroot-umount: unknown file system $fs
+	    exit 1
+	    ;;
+    esac
+    return 0
+}
+
+## mount-chroot-mount [file system|name]
+mount-chroot-mount(){
+    local fs=$1
+    local mnt_opts=""
+
+    case $fs in
+	/home)
+	    mnt_opts="-o rbind"
+	    ;;
+	*)
+	    mnt_opts="--bind"
+	    ;;
+    esac
+
+    case $fs in
+	/home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
+	    [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
+	    [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
+		mount ${mnt_opts} ${fs} ${BUILD_ROOT}${fs}
+	    ;;
+	vfs)
+	    for dir in /dev /dev/pts /dev/shm /proc /sys; do
+	    	mount-chroot-mount ${dir} || return 1
+	    done
+	    # mount-chroot-mount /proc || return 1
+	    ;;
+	archives_dir)
+	    [ -d ${EXTERNAL_ARCHIVES_DIR} ] || mkdir -p ${EXTERNAL_ARCHIVES_DIR}
+	    [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
+	    [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
+		mount ${mnt_opts} ${EXTERNAL_ARCHIVES_DIR} ${ARCHIVES_DIR}
+	    [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
+	    ;;
+	unionfs_dir)
+	    if [ $with_unionfs -eq 1 ]; then
+		[ -d ${UNIONFS_DIR} ] || mkdir -p ${UNIONFS_DIR}
+		[ -z "$(mount | grep ${BUILD_ROOT})" ] && \
+		    mount -t unionfs -o dirs=${UNIONFS_DIR}=rw:${BUILD_ROOT}=ro unionfs ${BUILD_ROOT}
+		unionctl ${BUILD_ROOT} --list
+	    fi
+	    ;;
+	*)
+	    echo mount-chroot-mount: unknown file system $fs
+	    exit 1
+	    ;;
+    esac
+    return 0
+}
+
+write-vbuilder-log(){
+    HRULE="======================================================================"
+
+    [ -d ${BUILD_ROOT} ] || return 1
+
+    if [ ! -f $VBUILDER_LOG ]; then
+	cat<<EOF > $VBUILDER_LOG
+${HRULE}
+VBUILDER REPORT
+DATE:		$(LANG=C date)
+HOSTNAME:	$(hostname)
+OS:		$(echo $($__chroot_sh "cat /etc/vine-release"))
+%_arch:		$(echo $($__chroot_sh "rpm --eval %_arch"))
+
+--version: ${VERSION}
+$(echo $([ -z "${VARCH}" ] || echo "--arch: ${VARCH}"))
+$(echo $([ -z "${CATEGORIES}" ] || echo "--category: ${CATEGORIES}"))
+$(echo $([ $with_dist_upgrade -eq 1 ] && echo "--dist-upgrade"))
+$(echo $([ $with_unionfs -eq 1 ] && echo "--unionfs"))
+$(echo $([ -z "${TARGET}" ] || echo "--target: ${TARGET}"))
+--bootstrap-dir: ${VBOOTSTRAP_DIR}
+--cache-dir: ${CACHE_DIR}
+--built-rpms-dir: ${BUILT_RPMS_DIR}
+${HRULE}
+
+[$VBUILDER_CONF]
+$(cat $VBUILDER_CONF)
+
+[History]
+EOF
+    else
+	cat<<EOF >> $VBUILDER_LOG
+$*
+EOF
+    fi
+
+    return 0
+}
+
+### end of file

+ 119 - 0
vchroot.sh.in

@@ -0,0 +1,119 @@
+#!/bin/bash
+# -*- coding: utf-8-unix -*-
+
+TEXTDOMAIN=vbootstrap
+TEXTDOMAINDIR=/usr/share/locale
+
+trap clean-vchroot EXIT
+
+Usage(){
+    cat<<EOF
+Usage:	$(basename $0) [OPTION...] [COMMAND] - run command or shell in a chroot
+
+Options:
+	--chroot:		use specified chroot [version] (default: ${DEFAULT_VERSION})
+$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*//)
+EOF
+}
+
+##############################################################################
+
+. /usr/share/vbootstrap/libvbuilder.sh
+
+check-parameter(){
+    [ -z "$*" ] && Usage && return 1
+
+    while [ ! -z "$*" ]; do
+	case $1 in
+	    --help|help)
+		Usage
+		return 1
+		;;
+	    --chroot)
+		shift
+		check-next-parameter $1 || return 1
+		;;
+	esac
+	shift
+    done
+
+    return 0
+}
+
+check-next-parameter(){
+    [ -z "$1" ] && echo $"E: Missing some parameters after $1" && return 1
+
+    [ $(echo $1 | grep '^-') ] && \
+	echo $"E: Missing some parameters after $1" && return 1
+
+    return 0
+}
+
+clean-vchroot(){
+    # mount-chroot --umount /home
+    mount-chroot --umount /tmp
+    mount-chroot --umount vfs
+    mount-chroot  --umount archives_dir
+    apt-get-update --host
+
+    return 0
+}
+
+main(){
+    setup-vbootstrap || return 1
+
+    __chroot="/usr/sbin/chroot ${BUILD_ROOT}"
+    ## setarch ix86 if ix86 chroot on x86_64 host
+    [ $with_ix86_on_x86_64 -eq 1 ] && \
+	__chroot="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH}"
+
+    mount-chroot archives_dir
+    mount-chroot vfs
+    mount-chroot /tmp
+    # mount-chroot /home
+    apt-get-update --chroot
+
+    ## copy host's configurations of /etc
+    #passwd shadow group gshadow
+    for i in resolv.conf hosts; do
+	[ ! -f ${BUILD_ROOT}/etc/${i}.orig ] && \
+	    cp -pf ${BUILD_ROOT}/etc/${i} ${BUILD_ROOT}/etc/${i}.orig && \
+	    cp -pf ${i} ${BUILD_ROOT}/etc
+    done
+    cp -Ppf /etc/localtime ${BUILD_ROOT}/etc
+
+    $__chroot $* || return 1
+    
+    return 0
+}
+
+##############################################################################
+
+# setup-vchroot || exit 1
+
+setup-vbuilder || exit 1
+
+check-parameter $* || exit 1
+
+while [ $# -gt 0 ]; do
+    tmpARG=$1
+    case $tmpARG in
+	--chroot|-c)
+	    shift
+	    ;;
+	*)
+	    break
+	    ;;
+    esac
+
+    case $tmpARG in
+	--chroot|-c)
+	    VERSION=$1
+	    ;;
+    esac
+    shift
+done
+
+main $* || exit 1
+
+exit