Ubuntu Bionic inside a Chroot

  • Install the base system
sudo apt-get install schroot debootstrap
sudo mkdir -p /var/chroot
sudo debootstrap --variant=buildd --arch amd64 bionic /var/chroot http://mirrors.ucr.ac.cr/ubuntu/
exit
  • Make a new file in /usr/local/bin/ named chroot.sh
sudo nano /usr/local/bin/chroot.sh
  • Now modify the chroot.sh you have just created with the following script
#!/bin/bash
CHROOT_DIR=/var
CHROOT_NAME=`basename $0 .sh`

xhost +local:

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 /dev/shm $CHROOT_DIR/$CHROOT_NAME/dev/shm
fi

echo "Starting chroot."
#dchroot -c $CHROOT_NAME
sudo chroot $CHROOT_DIR/$CHROOT_NAME su - $USER
export DISPLAY=:0.0
echo "Chroot closed."

if [ `cat /var/run/$CHROOT_NAME` -eq 1 ]
then
    echo "Closing last invocation. Unmounting host system directories"
    for i in dev/shm dev/pts hostfs proc dev sys 
    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
  • Configure schroot. Edit /etc/schroot/schroot.conf file with. Change YOUR_USER_NAME for your username:
[ubuntu-bionic]
description=Ubuntu Bionic
location=/var/chroot
priority=3
users=YOUR_USER_NAME
groups=sbuild
root-groups=root
  • Now make the file chroot.sh executable:
sudo chmod 755 /usr/local/bin/chroot.sh
  • Done! You have created a ubuntu chroot. To enter it you just need to do the following:
sudo chroot.sh
  • If you want to exit the chroot, you should type exit and the script should automatically do the job!
  • There is this error every time you log in:
mesg: ttyname failed: No such device
  • To fix this error. Enter the Chroot and edit the file /root/.profile and remove the line that starts with “mesg”

Discussion

Enter your comment. Wiki syntax is allowed:
If you can't read the letters on the image, download this .wav file to get them read to you.
 
  • tutorials/ubuntu_bionic_chroot.txt
  • Last modified: 2019/04/04 16:02
  • by admin