-# Move specified directories out of the chroot and into a "data"
-# directory that will be bind mounted on /data inside the chroot.
-move_datadirs() {
- root=$1
- data=$2
- shift 2
-
- mkdir -p $root/data
- for datadir in "$@" ; do
- mkdir -p ${data}$datadir
- if [ -d $root/$datadir -a ! -h $root/$datadir ] ; then
- (cd $root && find ./$datadir | cpio -p -d -u ../$data/)
- fi
- rm -rf $root/$datadir
- mkdir -p $(dirname $root/$datadir)
- ln -nsf /data$datadir $root/$datadir
- done
-}
-
-# Make loopback filesystem from specified location
-make_image() {
- root=$1
- image=$2
-
- # Leave about 100 MB free space and allow for about 20% inode overhead
- bytes=$((($(du -sb $root | cut -f1) + 100000000) * 120 / 100))
- bs=4096
- blocks=$(($bytes / $bs))
- dd bs=$bs count=$blocks if=/dev/zero of=$image
- mkfs.ext3 -b $bs -j -F $image
-
- # Temporarily mount it
- tmp=$(mktemp -d tmp.XXXXXX)
- mount -o loop $image $tmp
- trap "umount $tmp; rmdir $tmp" ERR INT
-
- # Move files to it
- (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
-
- # Unmount it
- umount $tmp
- rmdir $tmp
- trap - ERR INT
-}