#!/bin/bash
#
# 32bit chroot script for Ubuntu
# Copyright (C) 2010 Brendan Le Foll
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# get the number of command-line arguments given
ARGC=$#
# default ubuntu release
DISTRO_VERSION="hardy"
CHROOTS_DIR="/var/chroot/"
ARCH="i386"
MOUNT_HOME=false
show_usage() {
echo "Usage: chroot-32bit [distro]"
echo "distro is meant to be a ubuntu codename. Default is hardy"
echo "edit the CHROOTS_DIR to set the chroot path. Default is $CHROOTS_DIR"
echo "edit the DISTRO_VERSION to set the default linux ditro. Default is $DISTRO_VERSION"
echo "-h shows this help"
exit
}
start_chroot() {
echo "Starting $DISTRO_VERSION chroot"
cd $CHROOTS_DIR$DISTRO_VERSION-$ARCH
sudo mount -o bind /dev dev/
sudo mount -o bind /proc proc/
sudo mount -o bind /tmp tmp/
if $MOUNT_HOME; then
sudo mount -o bind /home home/
fi
#for networking
sudo cp -L /etc/resolv.conf etc/resolv.conf
sudo chroot $CHROOTS_DIR$DISTRO_VERSION-$ARCH/ setarch $ARCH /bin/bash
}
clean_chroot() {
# once bash session is closed - clean up !
sudo umount $CHROOTS_DIR$DISTRO_VERSION-$ARCH/dev
sudo umount $CHROOTS_DIR$DISTRO_VERSION-$ARCH/proc
sudo umount $CHROOTS_DIR$DISTRO_VERSION-$ARCH/tmp
if $MOUNT_HOME; then
sudo umount $CHROOTS_DIR$DISTRO_VERSION-$ARCH/home
fi
}
case $1 in
"-h"|"--help")
show_usage
;;
esac
# if we have chosen a custom distro or not mounted root
if [[ $ARGC -eq 1 ]]; then
DISTRO_VERSION=$1
if [[ -d $CHROOTS_DIR$DISTRO_VERSION-$ARCH ]]; then
start_chroot
clean_chroot
else
echo $CHROOTS_DIR$DISTRO_VERSION-$ARCH
show_usage
fi
else
if [[ -d $CHROOTS_DIR$DISTRO_VERSION-$ARCH ]]; then
start_chroot
clean_chroot
else
echo "Please check DISTRO_VERSION and CHROOTS_DIR in vars or see usage with -h!"
fi
fi