How to set up a chroot environment for testing

Reading time ~1 minute

How to set up a chroot environment for testing

How to set up a chroot environment for testing

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:

Date: 2013-02-14

Author: Dashamir Hoxha

Created: 2019-01-24 Thu 05:13

Emacs 25.1.1 (Org mode 8.2.10)

Validate

OpenPGP Web Key Directory

OpenPGP Web Key DirectoryOpenPGP Web Key DirectoryTable of Contents1. Introduction2. How WKD works3. Building a WKD3.1. Create the direct...… Continue reading

SMTP Server with LDAP Authentication

Published on April 17, 2021

Using WireGuard VPN

Published on November 09, 2020