I’ve posted a chroot script in the past which dealt with upstart. However, if you also have an OS which does not use upstart you need to change the sources of the chroot script. I decided to make a script which does the general things, and have a config file per chroot enviroment. I have need two chrooted enviroments for my dual boot laptop. One for Debian and one for Ubuntu.
Let’s start with creating some directories for our chroot script and the configuration files. Btw, I will use vi in my examples for editing files, use any editor you like, eg nano, gedit, kate, emacs, etc, etc.
# Create /home/youruser/chroot/etc mkdir -p $HOME/chroot/etc cd $HOME/chroot
Create the Debian chroot configuration.
vi etc/debian.conf
# This file is read as a shell script ## Debian root_disk=/dev/sda3 # What is the mount point of the OS? mnt_point=$HOME/chroot/debian # Mount various things # Have seperate mount points for /var/log, /home and /opt, # include them in the chroot. mnt_devices_start="proc dev dev/pts sys var/log home opt" # 1 if your OS uses upstart or 0 if it doesn't. upstart=0 # Command to start chroot with # I use my own account to enter the chroot chroot="su - youruser"
And the Ubuntu config.
vi etc/ubuntu.conf
# This file is read as a shell script ## Ubuntu root_disk=/dev/sda2 mnt=$HOME/chroot/ubuntu mnt_devices_start="proc dev dev/pts sys var/log home opt" upstart=0 chroot="su - youruser"
Then we will create/our chroot script, we define what we want to mount at start/stop (there should be some kind of shell thingy to reverse the list, but I haven’t found it – or just use an array )
vi chroot.sh
#!/bin/bash mounted=$(mount) mnt_devices_start="proc dev dev/pts sys var/log home opt" upstart=0 chroot=bash SELF=$(basename $0) SELF_DIR=$(dirname $0) usage() { echo "$SELF [chrootname]" exit 0 } if [ -n "$1" ] ; then if [ ! -f "$SELF_DIR/etc/$1.conf" ] ; then echo "chroot '$1' does not exist!" >&2 exit 1 fi source $SELF_DIR/etc/$1.conf shift else usage fi if [ -z "$mnt_devices_stop" ] ; then mnt_devices_stop=$(echo $mnt_devices_start | tac -s ' ') fi resolv="$mnt"/etc/resolv.conf resolv_o="$resolv.orig" start() { [ ! -d "$mnt" ] && sudo mkdir -p "$mnt" echo -e "$mounted" | grep -q "$mnt" if [ $? -ne 0 ] ; then sudo mount $root_disk "$mnt" if [ $? -ne 0 ] ; then echo "Unable to mount '$root_disk' on '$mnt'" >&2 exit 1 fi fi local i for i in $mnt_devices_start ; do echo -e "$mounted" | grep -q "$mnt/$i" [ $? -eq 0 ] && continue sudo mount -o bind /$i "$mnt"/$i done if [ ! -e "$resolv_o" ] ; then sudo cat "/etc/resolv.conf" | sudo tee "$resolv_o" >/dev/null fi cat /etc/resolv.conf | sudo tee "$resolv" >/dev/null if [ $upstart -ne 0 ] && [ ! -e "$mnt/sbin/initctl.distrib" ] ; then sudo chroot "$mnt" dpkg-divert --local --rename --add /sbin/initctl sudo chroot "$mnt" ln -s /bin/true /sbin/initctl fi eval sudo chroot "$mnt" "$chroot" } stop() { [ ! -e "$mnt" ] && return if [ "$upstart" -ne 0 ] && [ -e "$mnt/sbin/initctl.distrib" ] ; then sudo chroot "$mnt" rm /sbin/initctl sudo chroot "$mnt" dpkg-divert --local --rename --remove /sbin/initctl fi if [ -e "$resolv_o" ] ; then cat $resolv_o | tee "$resolv" >/dev/null sudo rm "$resolv_o" fi local i for i in $mnt_devices_stop; do echo -e "$mounted" | grep -q "$mnt/$i" [ $? -ne 0 ] && continue sudo umount "$mnt/$i" done echo -e "$mounted" | grep -q "$mnt" if [ $? -eq 0 ] ; then sudo umount "$mnt" [ $? -ne 0 ] && return sudo rmdir "$mnt" fi } case $1 in start|stop) $1;; *) usage;; esac
To be able to call chroot.sh from anywhere, just add it to your path.
vi $HOME/.bashrc # or .zshrc, or whatever your shell requires
export PATH=$PATH:$HOME/chroot
# Load new .bashrc source $HOME/.bashrc # or.. bash # or export PATH=$PATH:$HOME/chroot
Now you can start/stop chroot by:
chroot.sh ubuntu start chroot.sh ubuntu stop # or Debian chroot.sh debian start chroot.sh debian stop
I hope you find this useful .