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:
    [precise]
    description=Ubuntu Precise LTS
    location=/var/chroot/btranslator
    priority=3
    users=ubuntu
    groups=sbuild
    root-groups=root
    
  • Bootstrap the chroot with a minimal Ubuntu installation:
    debootstrap --variant=minbase --arch=amd64 precise \
        /var/chroot/btranslator http://mirror.rackspace.com/ubuntu/
    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:
    CHROOT=/var/chroot/btranslator
    
    case "$1" in
      start)
        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
        ;;
    
      stop)
        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
        ;;
    esac
    
  • 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
    

Referencies:

How to Build a Twitter-Bot

How to Build a Twitter-BotHow to Build a Twitter-BotTable of Contents1. Accessing twitter from the terminal2. Getting random proverbs wit...… Continue reading

Linux Guest Account

Published on January 14, 2017

LTSP Server with Ubuntu 16.04

Published on January 07, 2017