X-Git-Url: http://git.grml.org/?a=blobdiff_plain;f=usr_sbin%2Fgrml-chroot;h=2540957ea0bbc5cbee4cd00008e06035f6b1ef2b;hb=refs%2Fheads%2Fmika%2Fgrml_chroot_multi;hp=7e5f50358ec4b0fde35fa80e12a5d7e935402b99;hpb=6f3191ec8e502a461cd26b6deffc9aa4c0451acc;p=grml-scripts.git diff --git a/usr_sbin/grml-chroot b/usr_sbin/grml-chroot index 7e5f503..2540957 100755 --- a/usr_sbin/grml-chroot +++ b/usr_sbin/grml-chroot @@ -10,6 +10,12 @@ PROG_NAME_=$(basename $0) DEST_="" MOUNTED_="" # all mounted destinations +function bailout +{ + umount_all + die "Bailout" +} +trap bailout 1 2 3 3 6 9 14 15 function die { @@ -17,12 +23,22 @@ function die exit 1 } +function isMounted +{ + local dir="$1" + if cut -d\ -f 2 /proc/mounts | grep -q "$dir"; then + return 0 + else + return 1 + fi +} + function printUsage { cat < +Usage: "$PROG_NAME_" NEWROOT [COMMAND....] -$PROG_NAME__ is a chroot wrapper with proc/sys/pts/dev fs handling +$PROG_NAME_ is a chroot wrapper with proc/sys/pts/dev filesystem handling EOT } @@ -45,19 +61,23 @@ function mountit local all_options_="" - if [[ $options_ == "--bind" ]]; then + if ! isMounted "${DEST_}/$dest_"; then + if [[ $options_ == "--bind" ]]; then all_options_="--bind $type_" - else + else all_options_="-t $type_ none" + fi + mount $all_options_ "${DEST_}/$dest_" && storeMounts "$dest_" fi - mount $all_options_ "${DEST_}/$dest_" && storeMounts "$dest_" } function umount_all { - for i in $MOUNTED_; do - umount "${DEST_}/$i" + for i in /proc /sys /dev; do + umount "${DEST_}/${i}" done + + rm -f "$DEST_"/etc/debian_chroot } @@ -74,10 +94,11 @@ done shift $(($OPTIND - 1)) if (( $# < 1 )); then + printUsage die "Wrong number of arguments." fi -DEST_="$1" +DEST_="$1"; shift if [ ! -d "$DEST_" ]; then die "Target chroot does not exist: $DEST_" @@ -87,5 +108,35 @@ fi mountit "proc" "proc" mountit "sysfs" "sys" mountit "/dev" "dev" "--bind" -chroot "$DEST_" /bin/bash -umount_all + +# do not write to /var/run of chroot if it's not present +if [ -d "$DEST_/tmp" ] ; then + STATEDIR="tmp/grml-chroot" + mkdir -p "$DEST_/$STATEDIR" + touch "$DEST_/$STATEDIR/$$" +fi + +if [ ! -e "$DEST_"/etc/debian_chroot ]; then + echo "Writing /etc/debian_chroot ..." + cat "$DEST_"/etc/hostname > "$DEST_"/etc/debian_chroot +fi + +if (( $# < 1 )); then + chroot "$DEST_" + RC=$? +else + chroot "$DEST_" "$@" + RC=$? +fi + +if [ -z "$STATEDIR" ] ; then + umount_all +else + rm "$DEST_/$STATEDIR/$$" + + if rmdir "$DEST_/$STATEDIR" 2>/dev/null; then + umount_all + fi +fi + +exit $RC