From ef7e29326b258de2dbe14d95b7029d3d9c9c2740 Mon Sep 17 00:00:00 2001 From: James Meyer Date: Wed, 15 Oct 2008 17:58:39 -0700 Subject: Major changes to the enter_dev_chroot.sh script. --- setup_env.sh | 7 +- templates/enter_dev_chroot.sh | 313 +++++++++++++++++++----------------------- 2 files changed, 143 insertions(+), 177 deletions(-) mode change 100644 => 100755 templates/enter_dev_chroot.sh diff --git a/setup_env.sh b/setup_env.sh index 85a5ad8..1a1972a 100755 --- a/setup_env.sh +++ b/setup_env.sh @@ -101,12 +101,15 @@ then fi #create custom enter_dev_chroot.sh script -sed -e "s/REPLACEME/$ARCH/g" templates/enter_dev_chroot.sh > ../enter_dev_chroot.$ARCH.sh +#sed -e "s/REPLACEME/$ARCH/g" templates/enter_dev_chroot.sh > ../enter_dev_chroot.$ARCH.sh +DIR=$(pwd) ; DIR=${DIR##*/} +ln -s $DIR/templates/enter_dev_chroot.sh ../enter_dev_chroot.$ARCH + chmod 755 ../enter_dev_chroot.$ARCH.sh echo "********************************************************" echo "** To enter the chroot (build_root.$ARCH) run: **" echo "** cd .. **" -echo "** ./enter_dev_chroot.$ARCH.sh **" +echo "** ./enter_dev_chroot.$ARCH **" echo "********************************************************" diff --git a/templates/enter_dev_chroot.sh b/templates/enter_dev_chroot.sh old mode 100644 new mode 100755 index 1bf957b..1e3160c --- a/templates/enter_dev_chroot.sh +++ b/templates/enter_dev_chroot.sh @@ -1,179 +1,142 @@ #!/bin/bash -MYDIR=`pwd` -BROOT=$MYDIR/build_root.REPLACEME -MIRROR_DIR=$MYDIR/pkg_repo -TOOLS_DIR='LinHES-dev' -ARCH=REPLACEME - - - -# Make sure only root can run our script -if [[ $EUID -ne 0 ]]; then - echo "This script must be run as root" 1>&2 - exit 1 -fi - - - -if [ ! -d $BROOT ] -then - echo "build_root directory not found" - exit 1 -fi - - - -if [ -d LinHES-PKGBUILD ] - then - echo "Found LinHES-PKGBUILD" - else - echo "***********************************************************************************************" - echo "* Couldn't find LinHES-PKGBUILD *" - echo "* Please checkout the repository into the current directory *" - echo "* The LinHES-PKGBUILD git repoistory is at knoppmyth.net/mount/repository/LinHES-PKGBUILD.git *" - echo "***********************************************************************************************" - exit 1 - fi - -mounted=`mount` -umountlist=" " - - - -echo $mounted | grep -q $BROOT/proc -status=$? -if [ ! status = 0 ] -then - mount -t proc proc $BROOT/proc - umountlist="$umountlist proc" -else - echo "proc alredy mounted" -fi - - - - -echo $mounted | grep -q $BROOT/sys -status=$? -if [ ! $status = 0 ] -then - mount -t sysfs sysfs $BROOT/sys - umountlist="$umountlist sys" -else - echo "sys alredy mounted" -fi - -[ -e $BROOT/dev/random ] || mknod $BROOT/dev/random c 1 8 -[ -e $BROOT/dev/urandom ] || mknod $BROOT/dev/urandom c 1 9 -[ -e $BROOT/dev/tty ] || mknod $BROOT/dev/tty c 5 0 -[ -e $BROOT/dev/ptmx ] || mknod $BROOT/dev/ptmx c 5 2 - - - - -echo $mounted | grep -q $BROOT/dev/pts -status=$? -if [ ! $status = 0 ] -then - [ -e $BROOT/dev/pts ] || mkdir -p $BROOT/dev/pts - mount -t devpts devpts $BROOT/dev/pts - umountlist="$umountlist dev/pts" -else - echo "/dev/pts alredy mounted" -fi - - - -#create dir for build_tools -if [ ! -e $BROOT/build_tools ] -then - mkdir $BROOT/build_tools -fi -#mount build_tools -echo $mounted | grep -q $BROOT/build_tools -status=$? -if [ ! $status = 0 ] -then - mount --bind $MYDIR/$TOOLS_DIR/build_tools $BROOT/build_tools - umountlist="$umountlist build_tools" -else - echo "/build_tools alredy mounted" -fi - - -#create dir for the PKGBUILDS -if [ ! -e $BROOT/data/LinHES-PKGBUILD ] -then - mkdir -p $BROOT/data/LinHES-PKGBUILD -fi - -#mount pkg_repo -echo $mounted | grep -q $BROOT/data/LinHES-PKGBUILD -status=$? -if [ ! $status = 0 ] -then - mount --bind $MYDIR/LinHES-PKGBUILD $BROOT/data/LinHES-PKGBUILD - umountlist="$umountlist data/LinHES-PKGBUILD" +# A LinHES development script to enter a chroot tree. +######################################################################### +# Configuration: # +#-----------------------------------------------------------------------# +chrootBaseDir='build_root' # basename of chroot tree. # +repoBaseDir='pkg_repo' # basename of package repository tree. # +toolsDir='LinHES-dev' # directory name of development tools. # +packageDir='LinHES-PKGBUILD' # package development directory. # +C_B='\e[1m' # set bold : comment out for no bold. # +######################################################################### +C_N='\e[0m' # back to normal text output. # +myDir=$(pwd) # initial working directory. # +ARCH=${0##*.} # pick off the architecture part. # +BROOT="$myDir/$chrootBaseDir.$ARCH" # chroot subdir for $ARCH # +######################################################################### +# Proceedures / Functions: # +#-----------------------------------------------------------------------# +# Acquire the lock and modify the value, use: V=$(Modify +|-) +Modify() { # $1 is + for up and - for down. + local LOCKFILE=.lock + local VALUFILE=.inst + ( # Acquire the lock: + flock -e -w 2 200 + [ -e $VALUFILE ] || echo 0 > $VALUFILE + VAL=$(cat $VALUFILE) + NEW=$(expr $VAL $1 1) + echo "$NEW" > $VALUFILE + echo "$NEW" + ) 200>$LOCKFILE +} +#-----------------------------------------------------------------------# +# check if a filesystem/directory is already mounted & if not, mount it. +# ckMount -t +# ckMount --bind +ckMount() { # $1= -t|--bind, $2= fstype|srcDir $3= mntDir|dstDir + grep -q /$BROOT/$3 /proc/mounts || { + [ -d "$3" ] || mkdir -p $3 + echo -e "mounting ${C_B}$3${C_N}" + case $1 in + -t) mount $1 $2 $2 $3 ;; + --bind) mount $1 $myDir/$2 $3 ;; + esac + } +} +#-----------------------------------------------------------------------# +# if this is the last incident, unmount all. +cleanUp() { # no arguments + # mark the exit to normal operation: + echo -en "\e]0;Normal\a" # for xterm, + echo -en "\e]30;Normal\a" # for Konsole. + + V=$(Modify -) + [ "$V" -ne "0" ] && exit 0 + + # unmount all: + local mnts1="data/$repoBaseDir data/$packageDir build_tools" + local mnts2="dev/pts sys proc" + for i in $mnts1 $mnts2 ; do + grep -q $BROOT/$i /proc/mounts && { + echo -e "unMounting ${C_B}$i${C_N}" + umount $BROOT/$i # >& /dev/null + } + done +} +#=======================================================================# +# make certain that the chroot contains some files: +copyIn() { # no arguments + local F=etc/resolv.conf + [ -e "$BROOT/$F" ] || cp -f /$F $F + [ -f etc/bashrc ] || { + local D="$myDir/$toolsDir/templates/etc" + cp -f $D/bashrc etc/ + cp -f $D/kmdev.sh etc/profile.d/ + cp -f $D/.bashrc root/ + cp -f $D/.bash_profile root/ + chmod 0755 root/.bashrc root/.bash_profile etc/profile.d/kmdev.sh + } +} +######################################################################### +# Prerequisites: # +#-----------------------------------------------------------------------# +# must be root: +[ $EUID -ne 0 ] && { + echo -e "${C_B}chroot needs root priv's.${C_N}" 1>&2 + exit 1 +} + +# must have the chroot subdirectory: +[ -d $BROOT ] || { + echo -e "${C_B}$BROOT${C_N} directory not found!" + exit 2 +} + +# if building packages, needs $packageDir +if [ -d "$packageDir" ]; then + echo -e "Found ${C_B}$packageDir${C_N}" else - echo "pkg_repo alredy mounted" -fi - - - -#create dir for pkg_repo / local mirror -if [ ! -e $BROOT/data/pkg_repo ] -then - mkdir -p $BROOT/data/pkg_repo -fi - -#mount pkg_repo -echo $mounted | grep -q $BROOT/data/pkg_repo -status=$? -if [ ! $status = 0 ] -then - mount --bind $MYDIR/pkg_repo $BROOT/data/pkg_repo - umountlist="$umountlist data/pkg_repo" -else - echo "pkg_repo alredy mounted" -fi - - - - - - - - -#copy in some files -[ -e $BROOT/etc/resolv.conf ] || cp -f /etc/resolv.conf $BROOT/etc/resolv.conf - -if [ ! -f $BROOT/etc/bashrc ] -then - cp -f $MYDIR/$TOOLS_DIR/templates/etc/bashrc $BROOT/etc/ - cp -f $MYDIR/$TOOLS_DIR/templates/etc/kmdev.sh $BROOT/etc/profile.d/kmdev.sh - cp -f $MYDIR/$TOOLS_DIR/templates/etc/.bashrc $BROOT/root/ - cp -f $MYDIR/$TOOLS_DIR/templates/etc/.bash_profile $BROOT/root/ - chmod 755 $BROOT/root/.bashrc - chmod 755 $BROOT/root/.bash_profile - chmod 755 $BROOT/etc/profile.d/kmdev.sh + printf "%s\n" "**********************************************************************" + printf "* %-66s *\n" "Could Not find $packageDir" + printf "* %-66s *\n" "If you wish to be able to build packages within the chroot, then" + printf "* %-66s *\n" "Please checkout the package repository into the current directory." + printf "* %-66s *\n" "Its located @ knoppmyth.net/mount/repository/${packageDir}.git" + printf "%s\n" "**********************************************************************" fi - -#change the "xterm" title so we know it's a chroot -echo -e "\e]0;chroot-pkgbuild\a" -#used for Konsole -echo -e "\e]30;chroot-pkgbuild\a" -env -i TERM=$TERM /usr/sbin/chroot $BROOT /bin/bash --login - - - -for i in $umountlist -do - #echo "unmounting $BROOT/$i" - umount $BROOT/$i -done - - -echo -e "\e]30;normal\a" -echo -e "\e]0;normal\a" - +# change to the chroot subdirectory: +cd $BROOT || { + echo -e "Could NOT cd to ${C_B}$BROOT${C_N} !" + exit 3 +} +######################################################################### +# Main: # +#-----------------------------------------------------------------------# +V=$(Modify +) # bump the instance count. +trap cleanUp 1 2 15 EXIT + +ckMount -t proc proc +ckMount -t sysfs sys +ckMount -t devpts dev/pts + +[ -e dev/random ] || mknod dev/random c 1 8 +[ -e dev/urandom ] || mknod dev/urandom c 1 9 +[ -e dev/tty ] || mknod dev/tty c 5 0 +[ -e dev/ptmx ] || mknod dev/ptmx c 5 2 + +ckMount --bind $toolsDir/build_tools build_tools +ckMount --bind $packageDir data/$packageDir +ckMount --bind $repoBaseDir data/$repoBaseDir + +copyIn + +# change the titlebar to indicate chroot: +echo -e "\e]0;CHROOT : $ARCH\a" # for xterm, +echo -e "\e]30;CHROOT : $ARCH\a" # for Konsole. + +# The actual chroot: +env -i TERM="$TERM" DISPLAY="$DISPLAY" /usr/sbin/chroot . /bin/bash --login + +# Note: the unMounts take place within the 'cleanUp' proceedure. +######################################################################### +# End -- cgit v0.12