How to set up a chroot environment for testing

Reading time ~1 minute

Recently I have suffered a total system destruction in one of my computers. It happened while I was testing a software that I am developing, due to a simple bug. It helped me realize my lack of caution while testing unstable things, and I learned that it is always better to do the testing inside a chroot environment. Chroot’s provide isolation from the rest of the operating system, so that in case of severe errors damage can be mitigated.

Here are the steps that I took for installing a chroot environment for testing.

  • Install the packages debootstrap and dchroot:
    sudo apt-get install debootstrap dchroot
  • Add these line to /etc/schroot/schroot.conf:
    description=Ubuntu Precise LTS
  • Bootstrap the chroot with a minimal Ubuntu installation:
    debootstrap --variant=minbase --arch=amd64 precise \
    cp /etc/resolv.conf /var/chroot/btranslator/etc/resolv.conf
    cp /etc/apt/sources.list /var/chroot/btranslator/etc/apt/
    mount -o bind /proc /var/chroot/btranslator/proc
    chroot /var/chroot/btranslator/
    apt-get install ubuntu-minimal
    apt-get update
    apt-get upgrade
    apt-get install language-pack-en-base
    apt-get install vim aptitude
  • Install LAMP:
    apt-get install aptitude tasksel
    tasksel install lamp-server
  • Make a startup script /etc/init.d/chroot-btranslator with a content like this:
    case "$1" in
        service apache2 stop
        service mysql stop
        mount -o bind /proc $CHROOT/proc
        mount -o bind /dev $CHROOT/dev
        mount -o bind /sys $CHROOT/sys
        mount -o bind /dev/pts $CHROOT/dev/pts
        chroot $CHROOT/  service mysql start
        chroot $CHROOT/  service apache2 start
        chroot $CHROOT/  service apache2 stop
        chroot $CHROOT/  service mysql stop
        umount $CHROOT/dev/pts
        umount $CHROOT/sys
        umount $CHROOT/dev
        umount $CHROOT/proc
        service mysql start
        service apache2 start
  • Make it executable and start it at boot:
    chmod +x /etc/init.d/chroot-btranslator
    update-rc.d chroot-btranslator defaults
    service chroot-btranslator start


Easy GnuPG

Easy GnuPGEasy GnuPGTable of Contents1. What is GnuPG2. What is EasyGnuPG3. EasyGnuPG is opinionated4. EGPG integrates seamlessly with GP...… Continue reading

Moodle For Every School

Published on September 15, 2017