Mercurial > hg > aboriginal
changeset 1346:ffdc94c67308
Move do_in_chroot in control-images to setup-choot in system-image, and move zapchroot into system-image as well. New setup-chroot is more flexible, copying whatever existing host mounts you've got and with the ability to re-enter an existing chroot (if the target directory exists it'll just setup and tear down the bind mounts again without copying files).
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 13 Apr 2011 03:07:02 -0500 |
parents | f0b13de49034 |
children | 8a0a65d7a072 |
files | more/zapchroot.sh sources/control-images/bootstrap-skeleton/mnt/functions.sh sources/control-images/bootstrap-skeleton/mnt/init sources/root-filesystem/sbin/setup-chroot sources/root-filesystem/sbin/zapchroot |
diffstat | 5 files changed, 109 insertions(+), 64 deletions(-) [+] |
line wrap: on
line diff
--- a/more/zapchroot.sh Sun Apr 03 15:09:19 2011 -0500 +++ b/more/zapchroot.sh Wed Apr 13 03:07:02 2011 -0500 @@ -1,40 +1,1 @@ -#!/bin/bash - -# Copyright 2010 Rob Landley <rob@landley.net> licensed under GPLv2 - -if [ "$1" == "-d" ] -then - DELETE=1 - shift -fi - -# Clean up a chroot directory - -ZAP=$(readlink -f "$1" 2>/dev/null) - -if [ ! -d "$ZAP" ] -then - echo "usage: zapchroot [-d] dirname" - exit 1 -fi - -i="$(readlink -f "$(pwd)")" -if [ "$ZAP" == "${i:0:${#ZAP}}" ] -then - echo "Sanity check failed: cwd is under zapdir" >&2 - exit 1 -fi - -# Iterate through the second entry of /proc/mounts in reverse order - -for i in $(awk '{print $2}' /proc/mounts | tac) -do - # De-escape octal versions of space, tab, backslash, newline... - i=$(echo -e "$i") - - # Skip entries that aren't under our chroot - [ "$ZAP" != "${i:0:${#ZAP}}" ] && continue - - echo "Umounting: $i" - umount "$i" -done +../sources/root-filesystem/sbin/zapchroot \ No newline at end of file
--- a/sources/control-images/bootstrap-skeleton/mnt/functions.sh Sun Apr 03 15:09:19 2011 -0500 +++ b/sources/control-images/bootstrap-skeleton/mnt/functions.sh Wed Apr 13 03:07:02 2011 -0500 @@ -15,26 +15,3 @@ { while read i; do echo -n .; done; echo } - -do_in_chroot() -{ - # Copy root filesystem into a new chroot directory and restart in there. - - CHROOT="$1" - shift - - set_titlebar "Setup chroot" - mkdir "$CHROOT" - cp -a /mnt/files/. "$CHROOT" - find / -xdev | cpio -m -v -p "$CHROOT" | dotprogress - for i in mnt proc sys dev; do mount --bind /$i "$CHROOT"/$i; done - - echo Chroot - chroot "$CHROOT" "$@" - RC=$? - - echo Chroot cleanup - for i in mnt proc sys dev; do umount "$CHROOT"/$i; done - - return $RC -}
--- a/sources/control-images/bootstrap-skeleton/mnt/init Sun Apr 03 15:09:19 2011 -0500 +++ b/sources/control-images/bootstrap-skeleton/mnt/init Wed Apr 13 03:07:02 2011 -0500 @@ -10,7 +10,7 @@ # tell tar to skip /proc and /sys and such without unmounting bits of the # host environment. -do_in_chroot /home/"$NATIVE_BUILD" /mnt/run-build-stages.sh +setup-chroot /home/"$NATIVE_BUILD" /mnt/run-build-stages.sh if [ $? -eq 0 ] then
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sources/root-filesystem/sbin/setup-chroot Wed Apr 13 03:07:02 2011 -0500 @@ -0,0 +1,67 @@ +#!/bin/bash + +if [ $# -lt 1 ] || [ "${1:0:1}" == "-" ] || [ "$(readlink -f "$1")" == '/' ] +then + ( + echo "Usage: setup-chroot NEWDIR [COMMAND...]" + echo -e "\nCopies root filesystem to NEWDIR and executes COMMAND or /bin/sh" + echo "If NEWDIR already exists, bind mounts directories instead." + ) >&2 + exit 1 +fi + +# Copy root filesystem into a new chroot directory and restart in there. + +NEWROOT="$1" +shift + +dotprogress() +{ + while read i; do echo -n .; done; echo +} + +if [ ! -d "$NEWROOT" ] +then + mkdir -p "$NEWROOT" + [ -d /mnt/files ] && cp -a /mnt/files/. "$NEWROOT" + find / -xdev | cpio -m -v -p "$NEWROOT" | dotprogress +fi + +ZAP="$NEWROOT" + +# Iterate through mount points in reverse order, find most recent mount +# that + +ZAP="$NEWROOT" + +for i in $(awk '{print $2}' /proc/mounts | tac) +do + # De-escape octal versions of space, tab, backslash, newline... + x=$(echo -e "$i") + + # Skip the entry our chroot is under (generally /home) + if [ "$x" == "${ZAP:0:${#x}}" ] || [ "$x" == '/' ] + then + [ ! -z "$VERBOSE" ] && echo Zapping "$x" + ZAP="" + continue + fi + + echo "$i" +done | tac | while read i +do + mountpoint -q "$NEWROOT$i" && continue + + [ ! -z "$VERBOSE" ] && echo "Bind $i to $NEWROOT$i" + + mount --bind "$i" "$NEWROOT$i" +done + +[ ! -z "$VERBOSE" ] && echo "Chrooting to \"$NEWROOT\"" +chroot "$NEWROOT" "$@" +RC=$? + +[ ! -z "$VERBOSE" ] && echo "Cleanup chroot" +zapchroot "$NEWROOT" + +exit $RC
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sources/root-filesystem/sbin/zapchroot Wed Apr 13 03:07:02 2011 -0500 @@ -0,0 +1,40 @@ +#!/bin/bash + +# Copyright 2010 Rob Landley <rob@landley.net> licensed under GPLv2 + +if [ "$1" == "-d" ] +then + DELETE=1 + shift +fi + +# Clean up a chroot directory + +ZAP=$(readlink -f "$1" 2>/dev/null) + +if [ ! -d "$ZAP" ] +then + echo "usage: zapchroot [-d] dirname" + exit 1 +fi + +i="$(readlink -f "$(pwd)")" +if [ "$ZAP" == "${i:0:${#ZAP}}" ] +then + echo "Sanity check failed: cwd is under zapdir" >&2 + exit 1 +fi + +# Iterate through the second entry of /proc/mounts in reverse order + +for i in $(awk '{print $2}' /proc/mounts | tac) +do + # De-escape octal versions of space, tab, backslash, newline... + i=$(echo -e "$i") + + # Skip entries that aren't under our chroot + [ "$ZAP" != "${i:0:${#ZAP}}" ] && continue + + echo "Umounting: $i" + umount "$i" +done