User Tools

Site Tools


Writing /var/lib/dokuwiki/data/meta/tutorials/debian_inside_a_chroot.meta failed
Unable to save metadata file. Hint: disk full; file permissions; safe_mode setting.
tutorials:debian_inside_a_chroot

This is an old revision of the document!


Debian Unstable inside a Chroot

  • Install the base system
sudo apt-get install dchroot debootstrap
sudo su
mkdir -p /var/sid-amd64-chroot
debootstrap --arch amd64 sid /var/sid-amd64-chroot http://ftp.us.debian.org/debian/
exit
  • Create a script for mounting, entering, exiting and unmounting the necessary system directories (this is instead of using the fstab file which can be dangerous when, for example, you decide to delete the chroot with the system directories still mounted. If you do rm -rf /var/sid-amd64-chroot, you may not only delete the chroot directory but the system directories as well!!): This is the script: (copy it to /usr/local/bin/sid-amd64-chroot.sh)
#!/bin/bash
CHROOT_DIR=/var
CHROOT_NAME=`basename $0 .sh` 

if [ ! -e /var/run/$CHROOT_NAME ]
then
    sudo touch /var/run/$CHROOT_NAME
    sudo bash -c "echo 1 > /var/run/$CHROOT_NAME"
else
    sudo bash -c "echo `expr 1 + \`cat /var/run/$CHROOT_NAME\`` > /var/run/$CHROOT_NAME"
fi

if [ `cat /var/run/$CHROOT_NAME` -eq 1 ]
then
    echo "First chroot invocation. Mounting host system directories"
    sudo mkdir -p $CHROOT_DIR/$CHROOT_NAME/hostfs
    #mount -o bind /home/ $CHROOT_DIR/$CHROOT_NAME/home
    sudo mount proc -t proc $CHROOT_DIR/$CHROOT_NAME/proc
    sudo mount -o bind /dev $CHROOT_DIR/$CHROOT_NAME/dev
    sudo mount sys -t sysfs $CHROOT_DIR/$CHROOT_NAME/sys
    sudo mount none -t devpts $CHROOT_DIR/$CHROOT_NAME/dev/pts
    sudo mount -o bind / $CHROOT_DIR/$CHROOT_NAME/hostfs
    sudo mount -o bind /run/shm $CHROOT_DIR/$CHROOT_NAME/run/shm
fi

echo "Starting chroot."
#dchroot -c $CHROOT_NAME
sudo chroot $CHROOT_DIR/$CHROOT_NAME su - $USER
echo "Chroot closed."

if [ `cat /var/run/$CHROOT_NAME` -eq 1 ]
then
    echo "Closing last invocation. Unmounting host system directories"
    for i in dev/pts hostfs proc dev sys run/shm
    do
  sudo umount $CHROOT_DIR/$CHROOT_NAME/$i
        sleep 0.5
    done
fi

sudo bash -c "echo `expr \`cat /var/run/$CHROOT_NAME\` - 1` > /var/run/$CHROOT_NAME"

if [ `cat /var/run/$CHROOT_NAME` -eq 0 ]
then
    sudo rm /var/run/$CHROOT_NAME
fi
  • Please remember to name the above script with a name exactly as the directory where the chroot is plus “.sh” at the end. The script uses this name to find the chroot and to do other things.
  • Also, don't forget to give execute permissions to this script.
  • Now configure the /etc/schroot/schroot.conf file with: (please use between the [] the exact same name of the subdirectory containing the chroot, otherwise the script will not work)
[sid-amd64-chroot]
description=Debian sid (unstable)
directory=/var/sid-amd64-chroot
users=memeruiz
#groups=sbuild                                                                  
root-groups=root
aliases=unstable,default   
preserve-environment=true

Now you are done. With:

sid-amd64-chroot.sh

You will get your chroot running. (this will not work because you don't have your same user in chroot yet, look down)

Some things to do initially

  • Start the chroot as root user:
sudo sid-amd64-chroot.sh
  • Add a normal user
adduser username
  • Install some basic useful applications
apt-get install emacs joe mc locales sudo bash-completion less python
  • Add more sources to your sources.list file:
deb http://snapshot.debian.org/archive/debian/20130225T093150Z sid main contrib non-free
deb http://snapshot.debian.org/archive/debian/20130225T093150Z testing main contrib non-free
deb http://snapshot.debian.org/archive/debian/20130225T093150Z unstable main contrib non-free
deb http://snapshot.debian.org/archive/debian/20130225T093150Z experimental main contrib non-free
  • Install locales to stop getting anoying warnings.
sudo sid-amd64-chroot.sh
apt-get install locales
dpkg-reconfigure locales
  • Increase bash history. Edit ~/.bashrc and change:
HISTSIZE=1000000
HISTFILESIZE=2000000
  • Follow configuration of xstow

Some notes

Using fstab instead of the script

  • If you don't want to use the script above. You will need to add the following to your /etc/fstab file:
# sid-amd64 chroot
#/home           /var/sid-amd64-chroot/home none   bind            0       0
none           /var/sid-amd64-chroot/proc proc   defaults        0       0
/dev            /var/sid-amd64-chroot/dev  none   bind            0       0
none            /var/sid-amd64-chroot/sys  sysfs   defaults            0       0
none            /var/sid-amd64-chroot/dev/pts  devpts   defaults            0       0
  • Mount this directories:
sudo mount -a

Using the same users and home directory of the host computer

  1. Remember that this may not be what you really need…
  2. If you erase something in chroot home it gets erased in the host computer also.
  3. If you have different versions of the same programs in the chroot and the host computer, the local home configurations may not work properly or could get corrupted.
  • Manually copy the user entries in files /etc/passwd and /etc/shadow from host to the chroot that have 1000 UID or higher. This must be done everytime a new user is added to the host and wants to use the chroot environment.
  • If you are using the script above, add the home directory mount command in the mount and umount section. In the mount section use -o bind mount option. In the umount section simply add the directy to the list of directories to umount.
  • If you are using fstab, just uncomment the home line and rerun mount -a .
  • Install and configure sudo to allow users to install packages

W: Failed to change to directory ....

This happens because the chroot doesn't have any users initially (unless you followed the instructions to use the users from the host computer), only root You can fix this problem by first logging as root in the chroot and then adding a user with the same name and id of your user in the host computer.

sudo sid-amd64-chroot.sh
adduser --uid <user number in host computer> <username of host computer>

Then you can logging with no errors.

If you are using some snapshots.debian.org mirrors

When you do apt-get update you may get an error like this:

E: Release file for http://snapshot.debian.org/archive/debian/20130225T093150Z/dists/sid/InRelease is expired (invalid since 3d 17h 44min 46s). Updates for this repository will not be applied.

You can still update the the mirror if you use this command instead:

apt-get -o Acquire::Check-Valid-Until=false update

chroot and unionfs: Base installation and multiple setups

You can do a base installation and setup to chroot directory: /chroot/base and then unionfs mount this directory to other directories using cow (copy on write), to create specific application chroots. You may save disk space by not replicating the base system several times, and time by not having to install and configure multiple times.

unionfs-fuse -o cow  -o default_permissions -o use_ino -o suid -o noinitgroups -o allow_other -o nonempty /chroot/base=RO:/chroot/specific_chroot.union=RW /chroot/specific_chroot

~~DISCUSSION~~

tutorials/debian_inside_a_chroot.1456509163.txt.gz · Last modified: 2022/09/20 00:08 (external edit)